blob: c40f3eb3ccb9370e2e736bb8b7cbd1ae97d559c6 [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 Paalanen31f7d782014-09-23 22:08:43 -04004 * Copyright © 2012-2014 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberga9410222011-01-14 17:22:35 -050025#include "config.h"
26
Daniel Stoneb7452fe2012-06-01 12:14:06 +010027#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020039#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020040#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040047#include <sys/time.h>
48#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030049#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050050
Marcin Slusarz554a0da2013-02-18 13:27:22 -050051#ifdef HAVE_LIBUNWIND
52#define UNW_LOCAL_ONLY
53#include <libunwind.h>
54#endif
55
Pekka Paalanenb5026542014-11-12 15:09:24 +020056#include "timeline.h"
57
Kristian Høgsberg82863022010-06-04 21:52:02 -040058#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010059#include "scaler-server-protocol.h"
Pekka Paalanen31f7d782014-09-23 22:08:43 -040060#include "presentation_timing-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030061#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040062#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050063#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050064
Kristian Høgsberg27da5382011-06-21 17:32:25 -040065static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040066static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040067
68static int
69sigchld_handler(int signal_number, void *data)
70{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050071 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040072 int status;
73 pid_t pid;
74
Pekka Paalanen23ade622014-08-27 13:31:26 +030075 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
76 wl_list_for_each(p, &child_process_list, link) {
77 if (p->pid == pid)
78 break;
79 }
Bill Spitzak027b9622012-03-17 15:22:03 -070080
Pekka Paalanen23ade622014-08-27 13:31:26 +030081 if (&p->link == &child_process_list) {
82 weston_log("unknown child process exited\n");
83 continue;
84 }
85
86 wl_list_remove(&p->link);
87 p->cleanup(p, status);
Kristian Høgsberg27da5382011-06-21 17:32:25 -040088 }
89
Pekka Paalanen23ade622014-08-27 13:31:26 +030090 if (pid < 0 && errno != ECHILD)
91 weston_log("waitpid error %m\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040092
93 return 1;
94}
95
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020096static void
Alexander Larsson0b135062013-05-28 16:23:36 +020097weston_output_transform_scale_init(struct weston_output *output,
98 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020099
Rob Bradford27b17932013-06-26 18:08:46 +0100100static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500101weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +0100102
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600103static void weston_mode_switch_finish(struct weston_output *output,
104 int mode_changed,
105 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800106{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200107 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200108 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200109 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -0600110 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +0200111
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200112 pixman_region32_init(&old_output_region);
113 pixman_region32_copy(&old_output_region, &output->region);
114
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200115 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200116 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200117
118 pixman_region32_init(&output->previous_damage);
119 pixman_region32_init_rect(&output->region, output->x, output->y,
120 output->width, output->height);
121
122 weston_output_update_matrix(output);
123
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200124 /* If a pointer falls outside the outputs new geometry, move it to its
125 * lower-right corner */
126 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400127 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200128 int32_t x, y;
129
130 if (!pointer)
131 continue;
132
133 x = wl_fixed_to_int(pointer->x);
134 y = wl_fixed_to_int(pointer->y);
135
136 if (!pixman_region32_contains_point(&old_output_region,
137 x, y, NULL) ||
138 pixman_region32_contains_point(&output->region,
139 x, y, NULL))
140 continue;
141
142 if (x >= output->x + output->width)
143 x = output->x + output->width - 1;
144 if (y >= output->y + output->height)
145 y = output->y + output->height - 1;
146
147 pointer->x = wl_fixed_from_int(x);
148 pointer->y = wl_fixed_from_int(y);
149 }
150
151 pixman_region32_fini(&old_output_region);
152
Derek Foremandd4cd332014-11-10 10:29:59 -0600153 if (!mode_changed && !scale_changed)
154 return;
155
Hardening57388e42013-09-18 23:56:36 +0200156 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600157 wl_resource_for_each(resource, &output->resource_list) {
158 if (mode_changed) {
159 wl_output_send_mode(resource,
160 output->current_mode->flags,
161 output->current_mode->width,
162 output->current_mode->height,
163 output->current_mode->refresh);
164 }
Hardening57388e42013-09-18 23:56:36 +0200165
Derek Foreman41bdc272014-11-05 13:26:57 -0600166 version = wl_resource_get_version(resource);
167 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600168 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200169
Derek Foreman41bdc272014-11-05 13:26:57 -0600170 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
171 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600172 }
173}
174
175WL_EXPORT int
176weston_output_mode_set_native(struct weston_output *output,
177 struct weston_mode *mode,
178 int32_t scale)
179{
180 int ret;
181 int mode_changed = 0, scale_changed = 0;
182
183 if (!output->switch_mode)
184 return -1;
185
186 if (!output->original_mode) {
187 mode_changed = 1;
188 ret = output->switch_mode(output, mode);
189 if (ret < 0)
190 return ret;
191 if (output->current_scale != scale) {
192 scale_changed = 1;
193 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200194 }
195 }
196
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600197 output->native_mode = mode;
198 output->native_scale = scale;
199
200 weston_mode_switch_finish(output, mode_changed, scale_changed);
201
202 return 0;
203}
204
205WL_EXPORT int
206weston_output_mode_switch_to_native(struct weston_output *output)
207{
208 int ret;
209 int mode_changed = 0, scale_changed = 0;
210
211 if (!output->switch_mode)
212 return -1;
213
214 if (!output->original_mode) {
215 weston_log("already in the native mode\n");
216 return -1;
217 }
218 /* the non fullscreen clients haven't seen a mode set since we
219 * switched into a temporary, so we need to notify them if the
220 * mode at that time is different from the native mode now.
221 */
222 mode_changed = (output->original_mode != output->native_mode);
223 scale_changed = (output->original_scale != output->native_scale);
224
225 ret = output->switch_mode(output, output->native_mode);
226 if (ret < 0)
227 return ret;
228
229 output->current_scale = output->native_scale;
230
231 output->original_mode = NULL;
232 output->original_scale = 0;
233
234 weston_mode_switch_finish(output, mode_changed, scale_changed);
235
236 return 0;
237}
238
239WL_EXPORT int
240weston_output_mode_switch_to_temporary(struct weston_output *output,
241 struct weston_mode *mode,
242 int32_t scale)
243{
244 int ret;
245
246 if (!output->switch_mode)
247 return -1;
248
249 /* original_mode is the last mode non full screen clients have seen,
250 * so we shouldn't change it if we already have one set.
251 */
252 if (!output->original_mode) {
253 output->original_mode = output->native_mode;
254 output->original_scale = output->native_scale;
255 }
256 ret = output->switch_mode(output, mode);
257 if (ret < 0)
258 return ret;
259
260 output->current_scale = scale;
261
262 weston_mode_switch_finish(output, 0, 0);
263
264 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800265}
266
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400267WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500268weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400269{
270 wl_list_insert(&child_process_list, &process->link);
271}
272
Benjamin Franzke06286262011-05-06 19:12:33 +0200273static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200274child_client_exec(int sockfd, const char *path)
275{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500276 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200277 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200278 sigset_t allsigs;
279
280 /* do not give our signal mask to the new process */
281 sigfillset(&allsigs);
282 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200283
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100284 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
285 if (seteuid(getuid()) == -1) {
286 weston_log("compositor: failed seteuid\n");
287 return;
288 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500289
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500290 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
291 * non-CLOEXEC fd to pass through exec. */
292 clientfd = dup(sockfd);
293 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500295 return;
296 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200297
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500298 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200299 setenv("WAYLAND_SOCKET", s, 1);
300
301 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200302 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200303 path);
304}
305
306WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500307weston_client_launch(struct weston_compositor *compositor,
308 struct weston_process *proc,
309 const char *path,
310 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200311{
312 int sv[2];
313 pid_t pid;
314 struct wl_client *client;
315
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300316 weston_log("launching '%s'\n", path);
317
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300318 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200319 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200320 "socketpair failed while launching '%s': %m\n",
321 path);
322 return NULL;
323 }
324
325 pid = fork();
326 if (pid == -1) {
327 close(sv[0]);
328 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200329 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200330 "fork failed while launching '%s': %m\n", path);
331 return NULL;
332 }
333
334 if (pid == 0) {
335 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700336 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200337 }
338
339 close(sv[1]);
340
341 client = wl_client_create(compositor->wl_display, sv[0]);
342 if (!client) {
343 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200344 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200345 "wl_client_create failed while launching '%s'.\n",
346 path);
347 return NULL;
348 }
349
350 proc->pid = pid;
351 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500352 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200353
354 return client;
355}
356
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300357struct process_info {
358 struct weston_process proc;
359 char *path;
360};
361
362static void
363process_handle_sigchld(struct weston_process *process, int status)
364{
365 struct process_info *pinfo =
366 container_of(process, struct process_info, proc);
367
368 /*
369 * There are no guarantees whether this runs before or after
370 * the wl_client destructor.
371 */
372
373 if (WIFEXITED(status)) {
374 weston_log("%s exited with status %d\n", pinfo->path,
375 WEXITSTATUS(status));
376 } else if (WIFSIGNALED(status)) {
377 weston_log("%s died on signal %d\n", pinfo->path,
378 WTERMSIG(status));
379 } else {
380 weston_log("%s disappeared\n", pinfo->path);
381 }
382
383 free(pinfo->path);
384 free(pinfo);
385}
386
387WL_EXPORT struct wl_client *
388weston_client_start(struct weston_compositor *compositor, const char *path)
389{
390 struct process_info *pinfo;
391 struct wl_client *client;
392
393 pinfo = zalloc(sizeof *pinfo);
394 if (!pinfo)
395 return NULL;
396
397 pinfo->path = strdup(path);
398 if (!pinfo->path)
399 goto out_free;
400
401 client = weston_client_launch(compositor, &pinfo->proc, path,
402 process_handle_sigchld);
403 if (!client)
404 goto out_str;
405
406 return client;
407
408out_str:
409 free(pinfo->path);
410
411out_free:
412 free(pinfo);
413
414 return NULL;
415}
416
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200417static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300418region_init_infinite(pixman_region32_t *region)
419{
420 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
421 UINT32_MAX, UINT32_MAX);
422}
423
Pekka Paalanene67858b2013-04-25 13:57:42 +0300424static struct weston_subsurface *
425weston_surface_to_subsurface(struct weston_surface *surface);
426
Jason Ekstranda7af7042013-10-12 22:38:11 -0500427WL_EXPORT struct weston_view *
428weston_view_create(struct weston_surface *surface)
429{
430 struct weston_view *view;
431
Bryce Harringtonde16d892014-11-20 22:21:57 -0800432 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500433 if (view == NULL)
434 return NULL;
435
436 view->surface = surface;
437
Jason Ekstranda7af7042013-10-12 22:38:11 -0500438 /* Assign to surface */
439 wl_list_insert(&surface->views, &view->surface_link);
440
441 wl_signal_init(&view->destroy_signal);
442 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300443 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500444
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445 pixman_region32_init(&view->clip);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300446 pixman_region32_init(&view->transform.masked_boundingbox);
447 pixman_region32_init(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500448
449 view->alpha = 1.0;
450 pixman_region32_init(&view->transform.opaque);
451
452 wl_list_init(&view->geometry.transformation_list);
453 wl_list_insert(&view->geometry.transformation_list,
454 &view->transform.position.link);
455 weston_matrix_init(&view->transform.position.matrix);
456 wl_list_init(&view->geometry.child_list);
457 pixman_region32_init(&view->transform.boundingbox);
458 view->transform.dirty = 1;
459
Jason Ekstranda7af7042013-10-12 22:38:11 -0500460 return view;
461}
462
Jason Ekstrand108865d2014-06-26 10:04:49 -0700463struct weston_frame_callback {
464 struct wl_resource *resource;
465 struct wl_list link;
466};
467
Pekka Paalanen133e4392014-09-23 22:08:46 -0400468struct weston_presentation_feedback {
469 struct wl_resource *resource;
470
471 /* XXX: could use just wl_resource_get_link() instead */
472 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200473
474 /* The per-surface feedback flags */
475 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400476};
477
478static void
479weston_presentation_feedback_discard(
480 struct weston_presentation_feedback *feedback)
481{
482 presentation_feedback_send_discarded(feedback->resource);
483 wl_resource_destroy(feedback->resource);
484}
485
486static void
487weston_presentation_feedback_discard_list(struct wl_list *list)
488{
489 struct weston_presentation_feedback *feedback, *tmp;
490
491 wl_list_for_each_safe(feedback, tmp, list, link)
492 weston_presentation_feedback_discard(feedback);
493}
494
495static void
496weston_presentation_feedback_present(
497 struct weston_presentation_feedback *feedback,
498 struct weston_output *output,
499 uint32_t refresh_nsec,
500 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200501 uint64_t seq,
502 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400503{
504 struct wl_client *client = wl_resource_get_client(feedback->resource);
505 struct wl_resource *o;
506 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400507
508 wl_resource_for_each(o, &output->resource_list) {
509 if (wl_resource_get_client(o) != client)
510 continue;
511
512 presentation_feedback_send_sync_output(feedback->resource, o);
513 }
514
515 secs = ts->tv_sec;
516 presentation_feedback_send_presented(feedback->resource,
517 secs >> 32, secs & 0xffffffff,
518 ts->tv_nsec,
519 refresh_nsec,
520 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200521 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400522 wl_resource_destroy(feedback->resource);
523}
524
525static void
526weston_presentation_feedback_present_list(struct wl_list *list,
527 struct weston_output *output,
528 uint32_t refresh_nsec,
529 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200530 uint64_t seq,
531 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400532{
533 struct weston_presentation_feedback *feedback, *tmp;
534
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200535 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
536 wl_list_empty(list));
537
Pekka Paalanen133e4392014-09-23 22:08:46 -0400538 wl_list_for_each_safe(feedback, tmp, list, link)
539 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200540 refresh_nsec, ts, seq,
541 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400542}
543
Jason Ekstrand7b982072014-05-20 14:33:03 -0500544static void
545surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
546{
547 struct weston_surface_state *state =
548 container_of(listener, struct weston_surface_state,
549 buffer_destroy_listener);
550
551 state->buffer = NULL;
552}
553
554static void
555weston_surface_state_init(struct weston_surface_state *state)
556{
557 state->newly_attached = 0;
558 state->buffer = NULL;
559 state->buffer_destroy_listener.notify =
560 surface_state_handle_buffer_destroy;
561 state->sx = 0;
562 state->sy = 0;
563
564 pixman_region32_init(&state->damage);
565 pixman_region32_init(&state->opaque);
566 region_init_infinite(&state->input);
567
568 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400569 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500570
571 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
572 state->buffer_viewport.buffer.scale = 1;
573 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
574 state->buffer_viewport.surface.width = -1;
575 state->buffer_viewport.changed = 0;
576}
577
578static void
579weston_surface_state_fini(struct weston_surface_state *state)
580{
581 struct weston_frame_callback *cb, *next;
582
583 wl_list_for_each_safe(cb, next,
584 &state->frame_callback_list, link)
585 wl_resource_destroy(cb->resource);
586
Pekka Paalanen133e4392014-09-23 22:08:46 -0400587 weston_presentation_feedback_discard_list(&state->feedback_list);
588
Jason Ekstrand7b982072014-05-20 14:33:03 -0500589 pixman_region32_fini(&state->input);
590 pixman_region32_fini(&state->opaque);
591 pixman_region32_fini(&state->damage);
592
593 if (state->buffer)
594 wl_list_remove(&state->buffer_destroy_listener.link);
595 state->buffer = NULL;
596}
597
598static void
599weston_surface_state_set_buffer(struct weston_surface_state *state,
600 struct weston_buffer *buffer)
601{
602 if (state->buffer == buffer)
603 return;
604
605 if (state->buffer)
606 wl_list_remove(&state->buffer_destroy_listener.link);
607 state->buffer = buffer;
608 if (state->buffer)
609 wl_signal_add(&state->buffer->destroy_signal,
610 &state->buffer_destroy_listener);
611}
612
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500613WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500614weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500615{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500616 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400617
Bryce Harringtonde16d892014-11-20 22:21:57 -0800618 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400619 if (surface == NULL)
620 return NULL;
621
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500622 wl_signal_init(&surface->destroy_signal);
623
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500624 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200625 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400626
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200627 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
628 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200629 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
630 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500631
632 weston_surface_state_init(&surface->pending);
633
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400634 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500635 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300636 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400637
Jason Ekstranda7af7042013-10-12 22:38:11 -0500638 wl_list_init(&surface->views);
639
640 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400641 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500642
Pekka Paalanene67858b2013-04-25 13:57:42 +0300643 wl_list_init(&surface->subsurface_list);
644 wl_list_init(&surface->subsurface_list_pending);
645
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400646 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500647}
648
Alex Wu8811bf92012-02-28 18:07:54 +0800649WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500650weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200651 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500652{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100653 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500654}
655
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400656WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500657weston_view_to_global_float(struct weston_view *view,
658 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200659{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500660 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200661 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
662
Jason Ekstranda7af7042013-10-12 22:38:11 -0500663 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200664
665 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200666 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700667 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200668 v.f[3]);
669 *x = 0;
670 *y = 0;
671 return;
672 }
673
674 *x = v.f[0] / v.f[3];
675 *y = v.f[1] / v.f[3];
676 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500677 *x = sx + view->geometry.x;
678 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200679 }
680}
681
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500682WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200683weston_transformed_coord(int width, int height,
684 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200685 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200686 float sx, float sy, float *bx, float *by)
687{
688 switch (transform) {
689 case WL_OUTPUT_TRANSFORM_NORMAL:
690 default:
691 *bx = sx;
692 *by = sy;
693 break;
694 case WL_OUTPUT_TRANSFORM_FLIPPED:
695 *bx = width - sx;
696 *by = sy;
697 break;
698 case WL_OUTPUT_TRANSFORM_90:
699 *bx = height - sy;
700 *by = sx;
701 break;
702 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
703 *bx = height - sy;
704 *by = width - sx;
705 break;
706 case WL_OUTPUT_TRANSFORM_180:
707 *bx = width - sx;
708 *by = height - sy;
709 break;
710 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
711 *bx = sx;
712 *by = height - sy;
713 break;
714 case WL_OUTPUT_TRANSFORM_270:
715 *bx = sy;
716 *by = width - sx;
717 break;
718 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
719 *bx = sy;
720 *by = sx;
721 break;
722 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200723
724 *bx *= scale;
725 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200726}
727
728WL_EXPORT pixman_box32_t
729weston_transformed_rect(int width, int height,
730 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200731 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200732 pixman_box32_t rect)
733{
734 float x1, x2, y1, y2;
735
736 pixman_box32_t ret;
737
Alexander Larsson4ea95522013-05-22 14:41:37 +0200738 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200739 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200740 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200741 rect.x2, rect.y2, &x2, &y2);
742
743 if (x1 <= x2) {
744 ret.x1 = x1;
745 ret.x2 = x2;
746 } else {
747 ret.x1 = x2;
748 ret.x2 = x1;
749 }
750
751 if (y1 <= y2) {
752 ret.y1 = y1;
753 ret.y2 = y2;
754 } else {
755 ret.y1 = y2;
756 ret.y2 = y1;
757 }
758
759 return ret;
760}
761
762WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500763weston_transformed_region(int width, int height,
764 enum wl_output_transform transform,
765 int32_t scale,
766 pixman_region32_t *src, pixman_region32_t *dest)
767{
768 pixman_box32_t *src_rects, *dest_rects;
769 int nrects, i;
770
771 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
772 if (src != dest)
773 pixman_region32_copy(dest, src);
774 return;
775 }
776
777 src_rects = pixman_region32_rectangles(src, &nrects);
778 dest_rects = malloc(nrects * sizeof(*dest_rects));
779 if (!dest_rects)
780 return;
781
782 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
783 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
784 } else {
785 for (i = 0; i < nrects; i++) {
786 switch (transform) {
787 default:
788 case WL_OUTPUT_TRANSFORM_NORMAL:
789 dest_rects[i].x1 = src_rects[i].x1;
790 dest_rects[i].y1 = src_rects[i].y1;
791 dest_rects[i].x2 = src_rects[i].x2;
792 dest_rects[i].y2 = src_rects[i].y2;
793 break;
794 case WL_OUTPUT_TRANSFORM_90:
795 dest_rects[i].x1 = height - src_rects[i].y2;
796 dest_rects[i].y1 = src_rects[i].x1;
797 dest_rects[i].x2 = height - src_rects[i].y1;
798 dest_rects[i].y2 = src_rects[i].x2;
799 break;
800 case WL_OUTPUT_TRANSFORM_180:
801 dest_rects[i].x1 = width - src_rects[i].x2;
802 dest_rects[i].y1 = height - src_rects[i].y2;
803 dest_rects[i].x2 = width - src_rects[i].x1;
804 dest_rects[i].y2 = height - src_rects[i].y1;
805 break;
806 case WL_OUTPUT_TRANSFORM_270:
807 dest_rects[i].x1 = src_rects[i].y1;
808 dest_rects[i].y1 = width - src_rects[i].x2;
809 dest_rects[i].x2 = src_rects[i].y2;
810 dest_rects[i].y2 = width - src_rects[i].x1;
811 break;
812 case WL_OUTPUT_TRANSFORM_FLIPPED:
813 dest_rects[i].x1 = width - src_rects[i].x2;
814 dest_rects[i].y1 = src_rects[i].y1;
815 dest_rects[i].x2 = width - src_rects[i].x1;
816 dest_rects[i].y2 = src_rects[i].y2;
817 break;
818 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
819 dest_rects[i].x1 = height - src_rects[i].y2;
820 dest_rects[i].y1 = width - src_rects[i].x2;
821 dest_rects[i].x2 = height - src_rects[i].y1;
822 dest_rects[i].y2 = width - src_rects[i].x1;
823 break;
824 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
825 dest_rects[i].x1 = src_rects[i].x1;
826 dest_rects[i].y1 = height - src_rects[i].y2;
827 dest_rects[i].x2 = src_rects[i].x2;
828 dest_rects[i].y2 = height - src_rects[i].y1;
829 break;
830 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
831 dest_rects[i].x1 = src_rects[i].y1;
832 dest_rects[i].y1 = src_rects[i].x1;
833 dest_rects[i].x2 = src_rects[i].y2;
834 dest_rects[i].y2 = src_rects[i].x2;
835 break;
836 }
837 }
838 }
839
840 if (scale != 1) {
841 for (i = 0; i < nrects; i++) {
842 dest_rects[i].x1 *= scale;
843 dest_rects[i].x2 *= scale;
844 dest_rects[i].y1 *= scale;
845 dest_rects[i].y2 *= scale;
846 }
847 }
848
849 pixman_region32_clear(dest);
850 pixman_region32_init_rects(dest, dest_rects, nrects);
851 free(dest_rects);
852}
853
Jonny Lamb74130762013-11-26 18:19:46 +0100854static void
855scaler_surface_to_buffer(struct weston_surface *surface,
856 float sx, float sy, float *bx, float *by)
857{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200858 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200859 double src_width, src_height;
860 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200861
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200862 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
863 if (vp->surface.width == -1) {
864 *bx = sx;
865 *by = sy;
866 return;
867 }
Jonny Lamb74130762013-11-26 18:19:46 +0100868
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200869 src_x = 0.0;
870 src_y = 0.0;
871 src_width = surface->width_from_buffer;
872 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100873 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200874 src_x = wl_fixed_to_double(vp->buffer.src_x);
875 src_y = wl_fixed_to_double(vp->buffer.src_y);
876 src_width = wl_fixed_to_double(vp->buffer.src_width);
877 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100878 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200879
880 *bx = sx * src_width / surface->width + src_x;
881 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100882}
883
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500884WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200885weston_surface_to_buffer_float(struct weston_surface *surface,
886 float sx, float sy, float *bx, float *by)
887{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200888 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
889
Jonny Lamb74130762013-11-26 18:19:46 +0100890 /* first transform coordinates if the scaler is set */
891 scaler_surface_to_buffer(surface, sx, sy, bx, by);
892
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500893 weston_transformed_coord(surface->width_from_buffer,
894 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200895 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100896 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200897}
898
Alexander Larsson4ea95522013-05-22 14:41:37 +0200899WL_EXPORT void
900weston_surface_to_buffer(struct weston_surface *surface,
901 int sx, int sy, int *bx, int *by)
902{
903 float bxf, byf;
904
Jonny Lamb74130762013-11-26 18:19:46 +0100905 weston_surface_to_buffer_float(surface,
906 sx, sy, &bxf, &byf);
907
Alexander Larsson4ea95522013-05-22 14:41:37 +0200908 *bx = floorf(bxf);
909 *by = floorf(byf);
910}
911
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200912WL_EXPORT pixman_box32_t
913weston_surface_to_buffer_rect(struct weston_surface *surface,
914 pixman_box32_t rect)
915{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200916 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100917 float xf, yf;
918
919 /* first transform box coordinates if the scaler is set */
920 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
921 rect.x1 = floorf(xf);
922 rect.y1 = floorf(yf);
923
924 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
925 rect.x2 = floorf(xf);
926 rect.y2 = floorf(yf);
927
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500928 return weston_transformed_rect(surface->width_from_buffer,
929 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200930 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200931 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200932}
933
934WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500935weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400936 struct weston_plane *plane)
937{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400939 return;
940
Jason Ekstranda7af7042013-10-12 22:38:11 -0500941 weston_view_damage_below(view);
942 view->plane = plane;
943 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400944}
945
Pekka Paalanen51723d52015-02-17 13:10:01 +0200946/** Inflict damage on the plane where the view is visible.
947 *
948 * \param view The view that causes the damage.
949 *
950 * If the view is currently on a plane (including the primary plane),
951 * take the view's boundingbox, subtract all the opaque views that cover it,
952 * and add the remaining region as damage to the plane. This corresponds
953 * to the damage inflicted to the plane if this view disappeared.
954 *
955 * A repaint is scheduled for this view.
956 *
957 * The region of all opaque views covering this view is stored in
958 * weston_view::clip and updated by view_accumulate_damage() during
959 * weston_output_repaint(). Specifically, that region matches the
960 * scenegraph as it was last painted.
961 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400962WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500963weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200964{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500965 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200966
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500967 pixman_region32_init(&damage);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300968 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500969 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800970 if (view->plane)
971 pixman_region32_union(&view->plane->damage,
972 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500973 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800974 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200975}
976
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400977static void
978weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
979{
980 uint32_t different = es->output_mask ^ mask;
981 uint32_t entered = mask & different;
982 uint32_t left = es->output_mask & different;
983 struct weston_output *output;
984 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500985 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400986
987 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500988 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400989 return;
990 if (different == 0)
991 return;
992
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500993 client = wl_resource_get_client(es->resource);
994
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400995 wl_list_for_each(output, &es->compositor->output_list, link) {
996 if (1 << output->id & different)
997 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500998 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400999 client);
1000 if (resource == NULL)
1001 continue;
1002 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001003 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001004 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001005 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001006 }
1007}
1008
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001009
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001010static void
1011weston_surface_assign_output(struct weston_surface *es)
1012{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013 struct weston_output *new_output;
1014 struct weston_view *view;
1015 pixman_region32_t region;
1016 uint32_t max, area, mask;
1017 pixman_box32_t *e;
1018
1019 new_output = NULL;
1020 max = 0;
1021 mask = 0;
1022 pixman_region32_init(&region);
1023 wl_list_for_each(view, &es->views, surface_link) {
1024 if (!view->output)
1025 continue;
1026
1027 pixman_region32_intersect(&region, &view->transform.boundingbox,
1028 &view->output->region);
1029
1030 e = pixman_region32_extents(&region);
1031 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1032
1033 mask |= view->output_mask;
1034
1035 if (area >= max) {
1036 new_output = view->output;
1037 max = area;
1038 }
1039 }
1040 pixman_region32_fini(&region);
1041
1042 es->output = new_output;
1043 weston_surface_update_output_mask(es, mask);
1044}
1045
1046static void
1047weston_view_assign_output(struct weston_view *ev)
1048{
1049 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001050 struct weston_output *output, *new_output;
1051 pixman_region32_t region;
1052 uint32_t max, area, mask;
1053 pixman_box32_t *e;
1054
1055 new_output = NULL;
1056 max = 0;
1057 mask = 0;
1058 pixman_region32_init(&region);
1059 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001060 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001061 &output->region);
1062
1063 e = pixman_region32_extents(&region);
1064 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1065
1066 if (area > 0)
1067 mask |= 1 << output->id;
1068
1069 if (area >= max) {
1070 new_output = output;
1071 max = area;
1072 }
1073 }
1074 pixman_region32_fini(&region);
1075
Jason Ekstranda7af7042013-10-12 22:38:11 -05001076 ev->output = new_output;
1077 ev->output_mask = mask;
1078
1079 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001080}
1081
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001082static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001083view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
1084 int32_t width, int32_t height,
1085 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001086{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001087 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1088 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001089 int32_t s[4][2] = {
1090 { sx, sy },
1091 { sx, sy + height },
1092 { sx + width, sy },
1093 { sx + width, sy + height }
1094 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001095 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001096 int i;
1097
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001098 if (width == 0 || height == 0) {
1099 /* avoid rounding empty bbox to 1x1 */
1100 pixman_region32_init(bbox);
1101 return;
1102 }
1103
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001104 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001105 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001106 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001107 if (x < min_x)
1108 min_x = x;
1109 if (x > max_x)
1110 max_x = x;
1111 if (y < min_y)
1112 min_y = y;
1113 if (y > max_y)
1114 max_y = y;
1115 }
1116
Pekka Paalanen219b9822012-02-08 15:38:37 +02001117 int_x = floorf(min_x);
1118 int_y = floorf(min_y);
1119 pixman_region32_init_rect(bbox, int_x, int_y,
1120 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001121}
1122
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001123static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001125{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001126 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001127
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001128 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001129 view->geometry.x = roundf(view->geometry.x);
1130 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001131
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001132 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1134 view->transform.position.matrix.d[12] = view->geometry.x;
1135 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001136
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001138
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 view->transform.inverse = view->transform.position.matrix;
1140 view->transform.inverse.d[12] = -view->geometry.x;
1141 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001142
Jason Ekstranda7af7042013-10-12 22:38:11 -05001143 pixman_region32_init_rect(&view->transform.boundingbox,
1144 view->geometry.x,
1145 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001146 view->surface->width,
1147 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001148
Jason Ekstranda7af7042013-10-12 22:38:11 -05001149 if (view->alpha == 1.0) {
1150 pixman_region32_copy(&view->transform.opaque,
1151 &view->surface->opaque);
1152 pixman_region32_translate(&view->transform.opaque,
1153 view->geometry.x,
1154 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001155 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001156}
1157
1158static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001160{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 struct weston_view *parent = view->geometry.parent;
1162 struct weston_matrix *matrix = &view->transform.matrix;
1163 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001164 struct weston_transform *tform;
1165
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001167
1168 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1170 view->transform.position.matrix.d[12] = view->geometry.x;
1171 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001172
1173 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001175 weston_matrix_multiply(matrix, &tform->matrix);
1176
Pekka Paalanen483243f2013-03-08 14:56:50 +02001177 if (parent)
1178 weston_matrix_multiply(matrix, &parent->transform.matrix);
1179
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001180 if (weston_matrix_invert(inverse, matrix) < 0) {
1181 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182 weston_log("error: weston_view %p"
1183 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001184 return -1;
1185 }
1186
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001187 view_compute_bbox(view, 0, 0,
1188 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001190
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001191 return 0;
1192}
1193
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001194static struct weston_layer *
1195get_view_layer(struct weston_view *view)
1196{
1197 if (view->parent_view)
1198 return get_view_layer(view->parent_view);
1199 return view->layer_link.layer;
1200}
1201
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001202WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001204{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001206 struct weston_layer *layer;
1207 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001208
Jason Ekstranda7af7042013-10-12 22:38:11 -05001209 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001210 return;
1211
Pekka Paalanen483243f2013-03-08 14:56:50 +02001212 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001214
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001216
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001218
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 pixman_region32_fini(&view->transform.boundingbox);
1220 pixman_region32_fini(&view->transform.opaque);
1221 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001222
Pekka Paalanencd403622012-01-25 13:37:39 +02001223 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 if (view->geometry.transformation_list.next ==
1225 &view->transform.position.link &&
1226 view->geometry.transformation_list.prev ==
1227 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001228 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001230 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 if (weston_view_update_transform_enable(view) < 0)
1232 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001233 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001234
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001235 layer = get_view_layer(view);
1236 if (layer) {
1237 pixman_region32_init_with_extents(&mask, &layer->mask);
1238 pixman_region32_intersect(&view->transform.masked_boundingbox,
1239 &view->transform.boundingbox, &mask);
1240 pixman_region32_intersect(&view->transform.masked_opaque,
1241 &view->transform.opaque, &mask);
1242 pixman_region32_fini(&mask);
1243 }
1244
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001246
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001248
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 wl_signal_emit(&view->surface->compositor->transform_signal,
1250 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001251}
1252
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001253WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001254weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001255{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001257
1258 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 * The invariant: if view->geometry.dirty, then all views
1260 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001261 * Corollary: if not parent->geometry.dirty, then all ancestors
1262 * are not dirty.
1263 */
1264
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001266 return;
1267
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001269
Jason Ekstranda7af7042013-10-12 22:38:11 -05001270 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001271 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001273}
1274
1275WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276weston_view_to_global_fixed(struct weston_view *view,
1277 wl_fixed_t vx, wl_fixed_t vy,
1278 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001279{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001280 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001281
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 weston_view_to_global_float(view,
1283 wl_fixed_to_double(vx),
1284 wl_fixed_to_double(vy),
1285 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001286 *x = wl_fixed_from_double(xf);
1287 *y = wl_fixed_from_double(yf);
1288}
1289
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001290WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291weston_view_from_global_float(struct weston_view *view,
1292 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001293{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001295 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1296
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001298
1299 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001300 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001302 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 *vx = 0;
1304 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001305 return;
1306 }
1307
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 *vx = v.f[0] / v.f[3];
1309 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001310 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 *vx = x - view->geometry.x;
1312 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001313 }
1314}
1315
1316WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317weston_view_from_global_fixed(struct weston_view *view,
1318 wl_fixed_t x, wl_fixed_t y,
1319 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001320{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001322
Jason Ekstranda7af7042013-10-12 22:38:11 -05001323 weston_view_from_global_float(view,
1324 wl_fixed_to_double(x),
1325 wl_fixed_to_double(y),
1326 &vxf, &vyf);
1327 *vx = wl_fixed_from_double(vxf);
1328 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001329}
1330
1331WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332weston_view_from_global(struct weston_view *view,
1333 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001334{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001336
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1338 *vx = floorf(vxf);
1339 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001340}
1341
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001342WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001343weston_surface_schedule_repaint(struct weston_surface *surface)
1344{
1345 struct weston_output *output;
1346
1347 wl_list_for_each(output, &surface->compositor->output_list, link)
1348 if (surface->output_mask & (1 << output->id))
1349 weston_output_schedule_repaint(output);
1350}
1351
1352WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353weston_view_schedule_repaint(struct weston_view *view)
1354{
1355 struct weston_output *output;
1356
1357 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1358 if (view->output_mask & (1 << output->id))
1359 weston_output_schedule_repaint(output);
1360}
1361
1362WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001363weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001364{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001365 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 0, 0, surface->width,
1367 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001368
Kristian Høgsberg98238702012-08-03 16:29:12 -04001369 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001370}
1371
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001372WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001374{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001375 if (view->geometry.x == x && view->geometry.y == y)
1376 return;
1377
Jason Ekstranda7af7042013-10-12 22:38:11 -05001378 view->geometry.x = x;
1379 view->geometry.y = y;
1380 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001381}
1382
Pekka Paalanen483243f2013-03-08 14:56:50 +02001383static void
1384transform_parent_handle_parent_destroy(struct wl_listener *listener,
1385 void *data)
1386{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387 struct weston_view *view =
1388 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001389 geometry.parent_destroy_listener);
1390
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001392}
1393
1394WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395weston_view_set_transform_parent(struct weston_view *view,
1396 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001397{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 if (view->geometry.parent) {
1399 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1400 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001401 }
1402
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001404
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001406 transform_parent_handle_parent_destroy;
1407 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001408 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001410 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001412 }
1413
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 weston_view_geometry_dirty(view);
1415}
1416
Derek Foreman280e7dd2014-10-03 13:13:42 -05001417WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418weston_view_is_mapped(struct weston_view *view)
1419{
1420 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001421 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001423 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001424}
1425
Derek Foreman280e7dd2014-10-03 13:13:42 -05001426WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001427weston_surface_is_mapped(struct weston_surface *surface)
1428{
1429 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001430 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001431 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001432 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001433}
1434
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001435static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001436surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001437{
1438 struct weston_view *view;
1439
1440 if (surface->width == width && surface->height == height)
1441 return;
1442
1443 surface->width = width;
1444 surface->height = height;
1445
1446 wl_list_for_each(view, &surface->views, surface_link)
1447 weston_view_geometry_dirty(view);
1448}
1449
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001450WL_EXPORT void
1451weston_surface_set_size(struct weston_surface *surface,
1452 int32_t width, int32_t height)
1453{
1454 assert(!surface->resource);
1455 surface_set_size(surface, width, height);
1456}
1457
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001458static int
1459fixed_round_up_to_int(wl_fixed_t f)
1460{
1461 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1462}
1463
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001464static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001465weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001466{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001467 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001468 int32_t width, height;
1469
1470 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001471 surface->width_from_buffer = 0;
1472 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001473 return;
1474 }
1475
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001476 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001477 case WL_OUTPUT_TRANSFORM_90:
1478 case WL_OUTPUT_TRANSFORM_270:
1479 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1480 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001481 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1482 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001483 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001484 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001485 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1486 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001487 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001488 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001489
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001490 surface->width_from_buffer = width;
1491 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001492}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001493
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001494static void
1495weston_surface_update_size(struct weston_surface *surface)
1496{
1497 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1498 int32_t width, height;
1499
1500 width = surface->width_from_buffer;
1501 height = surface->height_from_buffer;
1502
1503 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001504 surface_set_size(surface,
1505 vp->surface.width, vp->surface.height);
1506 return;
1507 }
1508
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001509 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001510 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1511 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1512
1513 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001514 return;
1515 }
1516
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001517 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001518}
1519
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001520WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001521weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001522{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001523 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001524
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001525 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001526
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001527 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001528}
1529
Jason Ekstranda7af7042013-10-12 22:38:11 -05001530WL_EXPORT struct weston_view *
1531weston_compositor_pick_view(struct weston_compositor *compositor,
1532 wl_fixed_t x, wl_fixed_t y,
1533 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001534{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001536 int ix = wl_fixed_to_int(x);
1537 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001538
Jason Ekstranda7af7042013-10-12 22:38:11 -05001539 wl_list_for_each(view, &compositor->view_list, link) {
1540 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001541 if (pixman_region32_contains_point(
1542 &view->transform.masked_boundingbox,
1543 ix, iy, NULL) &&
1544 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001545 wl_fixed_to_int(*vx),
1546 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001547 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001549 }
1550
1551 return NULL;
1552}
1553
1554static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001555weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001556{
Daniel Stone37816df2012-05-16 18:45:18 +01001557 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001558
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001559 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001560 return;
1561
Daniel Stone37816df2012-05-16 18:45:18 +01001562 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001563 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001564}
1565
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001566WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001567weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001568{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001569 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001570
Jason Ekstranda7af7042013-10-12 22:38:11 -05001571 if (!weston_view_is_mapped(view))
1572 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001573
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574 weston_view_damage_below(view);
1575 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001576 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001577 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 wl_list_remove(&view->link);
1579 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001580 view->output_mask = 0;
1581 weston_surface_assign_output(view->surface);
1582
1583 if (weston_surface_is_mapped(view->surface))
1584 return;
1585
1586 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1587 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001588 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001590 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001591 NULL,
1592 wl_fixed_from_int(0),
1593 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001594 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001595 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001596 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001598
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599WL_EXPORT void
1600weston_surface_unmap(struct weston_surface *surface)
1601{
1602 struct weston_view *view;
1603
1604 wl_list_for_each(view, &surface->views, surface_link)
1605 weston_view_unmap(view);
1606 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001607}
1608
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001609static void
1610weston_surface_reset_pending_buffer(struct weston_surface *surface)
1611{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001612 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001613 surface->pending.sx = 0;
1614 surface->pending.sy = 0;
1615 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001616 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001617}
1618
Jason Ekstranda7af7042013-10-12 22:38:11 -05001619WL_EXPORT void
1620weston_view_destroy(struct weston_view *view)
1621{
1622 wl_signal_emit(&view->destroy_signal, view);
1623
1624 assert(wl_list_empty(&view->geometry.child_list));
1625
1626 if (weston_view_is_mapped(view)) {
1627 weston_view_unmap(view);
1628 weston_compositor_build_view_list(view->surface->compositor);
1629 }
1630
1631 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001632 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001633
1634 pixman_region32_fini(&view->clip);
1635 pixman_region32_fini(&view->transform.boundingbox);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001636 pixman_region32_fini(&view->transform.masked_boundingbox);
1637 pixman_region32_fini(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638
1639 weston_view_set_transform_parent(view, NULL);
1640
Jason Ekstranda7af7042013-10-12 22:38:11 -05001641 wl_list_remove(&view->surface_link);
1642
1643 free(view);
1644}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001645
1646WL_EXPORT void
1647weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001648{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001649 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001651
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001652 if (--surface->ref_count > 0)
1653 return;
1654
1655 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1656
Pekka Paalanene67858b2013-04-25 13:57:42 +03001657 assert(wl_list_empty(&surface->subsurface_list_pending));
1658 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001659
Jason Ekstranda7af7042013-10-12 22:38:11 -05001660 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1661 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001662
Jason Ekstrand7b982072014-05-20 14:33:03 -05001663 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001664
Pekka Paalanende685b82012-12-04 15:58:12 +02001665 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001666
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001667 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001668 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001669 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001670
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001671 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001672 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001673
Pekka Paalanen133e4392014-09-23 22:08:46 -04001674 weston_presentation_feedback_discard_list(&surface->feedback_list);
1675
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001676 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001677}
1678
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001679static void
1680destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001681{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001682 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001683
Giulio Camuffo0d379742013-11-15 22:06:15 +01001684 /* Set the resource to NULL, since we don't want to leave a
1685 * dangling pointer if the surface was refcounted and survives
1686 * the weston_surface_destroy() call. */
1687 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001688 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001689}
1690
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001691static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001692weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1693{
1694 struct weston_buffer *buffer =
1695 container_of(listener, struct weston_buffer, destroy_listener);
1696
1697 wl_signal_emit(&buffer->destroy_signal, buffer);
1698 free(buffer);
1699}
1700
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001701WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001702weston_buffer_from_resource(struct wl_resource *resource)
1703{
1704 struct weston_buffer *buffer;
1705 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001706
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001707 listener = wl_resource_get_destroy_listener(resource,
1708 weston_buffer_destroy_handler);
1709
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001710 if (listener)
1711 return container_of(listener, struct weston_buffer,
1712 destroy_listener);
1713
1714 buffer = zalloc(sizeof *buffer);
1715 if (buffer == NULL)
1716 return NULL;
1717
1718 buffer->resource = resource;
1719 wl_signal_init(&buffer->destroy_signal);
1720 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001721 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001722 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001723
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001724 return buffer;
1725}
1726
1727static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001728weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1729 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001730{
Pekka Paalanende685b82012-12-04 15:58:12 +02001731 struct weston_buffer_reference *ref =
1732 container_of(listener, struct weston_buffer_reference,
1733 destroy_listener);
1734
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001735 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001736 ref->buffer = NULL;
1737}
1738
1739WL_EXPORT void
1740weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001741 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001742{
1743 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001744 ref->buffer->busy_count--;
1745 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001746 assert(wl_resource_get_client(ref->buffer->resource));
1747 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001748 WL_BUFFER_RELEASE);
1749 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001750 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001751 }
1752
Pekka Paalanende685b82012-12-04 15:58:12 +02001753 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001754 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001755 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001756 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001757 }
1758
Pekka Paalanende685b82012-12-04 15:58:12 +02001759 ref->buffer = buffer;
1760 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1761}
1762
1763static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001764weston_surface_attach(struct weston_surface *surface,
1765 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001766{
1767 weston_buffer_reference(&surface->buffer_ref, buffer);
1768
Pekka Paalanena6421c42012-12-04 15:58:10 +02001769 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001770 if (weston_surface_is_mapped(surface))
1771 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001772 }
1773
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001774 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001775
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001776 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001777 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001778}
1779
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001780WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001781weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001782{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001783 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001784
1785 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001786 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001787}
1788
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001789WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001790weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001791{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001792 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001793
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001794 pixman_region32_union(&compositor->primary_plane.damage,
1795 &compositor->primary_plane.damage,
1796 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001797 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001798}
1799
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001800static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001802{
Pekka Paalanende685b82012-12-04 15:58:12 +02001803 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001804 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001805 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001806
Pekka Paalanenb5026542014-11-12 15:09:24 +02001807 if (weston_timeline_enabled_ &&
1808 pixman_region32_not_empty(&surface->damage))
1809 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1810 TLP_OUTPUT(surface->output), TLP_END);
1811
Jason Ekstrandef540082014-06-26 10:37:36 -07001812 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813}
1814
1815static void
1816view_accumulate_damage(struct weston_view *view,
1817 pixman_region32_t *opaque)
1818{
1819 pixman_region32_t damage;
1820
1821 pixman_region32_init(&damage);
1822 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001823 pixman_box32_t *extents;
1824
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825 extents = pixman_region32_extents(&view->surface->damage);
1826 view_compute_bbox(view, extents->x1, extents->y1,
1827 extents->x2 - extents->x1,
1828 extents->y2 - extents->y1,
1829 &damage);
1830 pixman_region32_translate(&damage,
1831 -view->plane->x,
1832 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001833 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834 pixman_region32_copy(&damage, &view->surface->damage);
1835 pixman_region32_translate(&damage,
1836 view->geometry.x - view->plane->x,
1837 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001838 }
1839
Jason Ekstranda7af7042013-10-12 22:38:11 -05001840 pixman_region32_subtract(&damage, &damage, opaque);
1841 pixman_region32_union(&view->plane->damage,
1842 &view->plane->damage, &damage);
1843 pixman_region32_fini(&damage);
1844 pixman_region32_copy(&view->clip, opaque);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001845 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001846}
1847
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001848static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001849compositor_accumulate_damage(struct weston_compositor *ec)
1850{
1851 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001852 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001853 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001854
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001855 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001856
1857 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001858 pixman_region32_copy(&plane->clip, &clip);
1859
1860 pixman_region32_init(&opaque);
1861
Jason Ekstranda7af7042013-10-12 22:38:11 -05001862 wl_list_for_each(ev, &ec->view_list, link) {
1863 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001864 continue;
1865
Jason Ekstranda7af7042013-10-12 22:38:11 -05001866 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001867 }
1868
1869 pixman_region32_union(&clip, &clip, &opaque);
1870 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001871 }
1872
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001873 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001874
Jason Ekstranda7af7042013-10-12 22:38:11 -05001875 wl_list_for_each(ev, &ec->view_list, link)
1876 ev->surface->touched = 0;
1877
1878 wl_list_for_each(ev, &ec->view_list, link) {
1879 if (ev->surface->touched)
1880 continue;
1881 ev->surface->touched = 1;
1882
1883 surface_flush_damage(ev->surface);
1884
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001885 /* Both the renderer and the backend have seen the buffer
1886 * by now. If renderer needs the buffer, it has its own
1887 * reference set. If the backend wants to keep the buffer
1888 * around for migrating the surface into a non-primary plane
1889 * later, keep_buffer is true. Otherwise, drop the core
1890 * reference now, and allow early buffer release. This enables
1891 * clients to use single-buffering.
1892 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001893 if (!ev->surface->keep_buffer)
1894 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001895 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001896}
1897
1898static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001899surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001900{
1901 struct weston_subsurface *sub;
1902
Pekka Paalanene67858b2013-04-25 13:57:42 +03001903 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001904 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001905 continue;
1906
Jason Ekstranda7af7042013-10-12 22:38:11 -05001907 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1908 wl_list_init(&sub->surface->views);
1909
1910 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001911 }
1912}
1913
1914static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001915surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001916{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001917 struct weston_subsurface *sub;
1918 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001919
Jason Ekstranda7af7042013-10-12 22:38:11 -05001920 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1921 if (sub->surface == surface)
1922 continue;
1923
George Kiagiadakised04d382014-06-13 18:10:26 +02001924 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1925 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001926 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001927 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001928
1929 surface_free_unused_subsurface_views(sub->surface);
1930 }
1931}
1932
1933static void
1934view_list_add_subsurface_view(struct weston_compositor *compositor,
1935 struct weston_subsurface *sub,
1936 struct weston_view *parent)
1937{
1938 struct weston_subsurface *child;
1939 struct weston_view *view = NULL, *iv;
1940
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001941 if (!weston_surface_is_mapped(sub->surface))
1942 return;
1943
Jason Ekstranda7af7042013-10-12 22:38:11 -05001944 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1945 if (iv->geometry.parent == parent) {
1946 view = iv;
1947 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001948 }
1949 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001950
1951 if (view) {
1952 /* Put it back in the surface's list of views */
1953 wl_list_remove(&view->surface_link);
1954 wl_list_insert(&sub->surface->views, &view->surface_link);
1955 } else {
1956 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001957 weston_view_set_position(view,
1958 sub->position.x,
1959 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001960 weston_view_set_transform_parent(view, parent);
1961 }
1962
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001963 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001964 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001965
Pekka Paalanenb188e912013-11-19 14:03:35 +02001966 if (wl_list_empty(&sub->surface->subsurface_list)) {
1967 wl_list_insert(compositor->view_list.prev, &view->link);
1968 return;
1969 }
1970
1971 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1972 if (child->surface == sub->surface)
1973 wl_list_insert(compositor->view_list.prev, &view->link);
1974 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001975 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001976 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001977}
1978
1979static void
1980view_list_add(struct weston_compositor *compositor,
1981 struct weston_view *view)
1982{
1983 struct weston_subsurface *sub;
1984
1985 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001986
Pekka Paalanenb188e912013-11-19 14:03:35 +02001987 if (wl_list_empty(&view->surface->subsurface_list)) {
1988 wl_list_insert(compositor->view_list.prev, &view->link);
1989 return;
1990 }
1991
1992 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1993 if (sub->surface == view->surface)
1994 wl_list_insert(compositor->view_list.prev, &view->link);
1995 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001996 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001997 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001998}
1999
2000static void
2001weston_compositor_build_view_list(struct weston_compositor *compositor)
2002{
2003 struct weston_view *view;
2004 struct weston_layer *layer;
2005
2006 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002007 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002008 surface_stash_subsurface_views(view->surface);
2009
2010 wl_list_init(&compositor->view_list);
2011 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002012 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002013 view_list_add(compositor, view);
2014 }
2015 }
2016
2017 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002018 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002019 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002020}
2021
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002022static void
2023weston_output_take_feedback_list(struct weston_output *output,
2024 struct weston_surface *surface)
2025{
2026 struct weston_view *view;
2027 struct weston_presentation_feedback *feedback;
2028 uint32_t flags = 0xffffffff;
2029
2030 if (wl_list_empty(&surface->feedback_list))
2031 return;
2032
2033 /* All views must have the flag for the flag to survive. */
2034 wl_list_for_each(view, &surface->views, surface_link) {
2035 /* ignore views that are not on this output at all */
2036 if (view->output_mask & (1u << output->id))
2037 flags &= view->psf_flags;
2038 }
2039
2040 wl_list_for_each(feedback, &surface->feedback_list, link)
2041 feedback->psf_flags = flags;
2042
2043 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2044 wl_list_init(&surface->feedback_list);
2045}
2046
David Herrmann1edf44c2013-10-22 17:11:26 +02002047static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002048weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002049{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002050 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002051 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002052 struct weston_animation *animation, *next;
2053 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002054 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002055 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002056 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002057
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002058 if (output->destroying)
2059 return 0;
2060
Pekka Paalanenb5026542014-11-12 15:09:24 +02002061 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2062
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002063 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002064 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002065
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002066 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002067 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002068 } else {
2069 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002071 ev->psf_flags = 0;
2072 }
2073 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002074
Pekka Paalanene67858b2013-04-25 13:57:42 +03002075 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076 wl_list_for_each(ev, &ec->view_list, link) {
2077 /* Note: This operation is safe to do multiple times on the
2078 * same surface.
2079 */
2080 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002081 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082 &ev->surface->frame_callback_list);
2083 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002084
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002085 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002086 }
2087 }
2088
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002089 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002090
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002091 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002092 pixman_region32_intersect(&output_damage,
2093 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002094 pixman_region32_subtract(&output_damage,
2095 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002096
Scott Moreauccbf29d2012-02-22 14:21:41 -07002097 if (output->dirty)
2098 weston_output_update_matrix(output);
2099
David Herrmann1edf44c2013-10-22 17:11:26 +02002100 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002101
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002102 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002103
Kristian Høgsbergef044142011-06-21 15:02:12 -04002104 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002105
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002106 weston_compositor_repick(ec);
2107 wl_event_loop_dispatch(ec->input_loop, 0);
2108
Jonas Ådahldb773762012-06-13 00:01:21 +02002109 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002110 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002111 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002112 }
2113
Scott Moreaud64cf212012-06-08 19:40:54 -06002114 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002115 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002116 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002117 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002118
Pekka Paalanenb5026542014-11-12 15:09:24 +02002119 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2120
David Herrmann1edf44c2013-10-22 17:11:26 +02002121 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002122}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002123
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002124static int
2125weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002126{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002127 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002128
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002129 wl_event_loop_dispatch(compositor->input_loop, 0);
2130
2131 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002132}
2133
2134WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002135weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002136 const struct timespec *stamp,
2137 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002138{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002139 struct weston_compositor *compositor = output->compositor;
2140 struct wl_event_loop *loop =
2141 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02002142 int fd, r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002143 uint32_t refresh_nsec;
2144
Pekka Paalanenb5026542014-11-12 15:09:24 +02002145 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2146 TLP_VBLANK(stamp), TLP_END);
2147
Pekka Paalanen133e4392014-09-23 22:08:46 -04002148 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2149 weston_presentation_feedback_present_list(&output->feedback_list,
2150 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002151 output->msc,
2152 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002153
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002154 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002155
2156 if (output->repaint_needed &&
2157 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2158 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002159 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002160 if (!r)
2161 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002162 }
2163
2164 output->repaint_scheduled = 0;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002165 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2166
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002167 if (compositor->input_loop_source)
2168 return;
2169
2170 fd = wl_event_loop_get_fd(compositor->input_loop);
2171 compositor->input_loop_source =
2172 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2173 weston_compositor_read_input, compositor);
2174}
2175
2176static void
2177idle_repaint(void *data)
2178{
2179 struct weston_output *output = data;
2180
Jonas Ådahle5a12252013-04-05 23:07:11 +02002181 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002182}
2183
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002184WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002185weston_layer_entry_insert(struct weston_layer_entry *list,
2186 struct weston_layer_entry *entry)
2187{
2188 wl_list_insert(&list->link, &entry->link);
2189 entry->layer = list->layer;
2190}
2191
2192WL_EXPORT void
2193weston_layer_entry_remove(struct weston_layer_entry *entry)
2194{
2195 wl_list_remove(&entry->link);
2196 wl_list_init(&entry->link);
2197 entry->layer = NULL;
2198}
2199
2200WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002201weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2202{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002203 wl_list_init(&layer->view_list.link);
2204 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002205 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002206 if (below != NULL)
2207 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002208}
2209
2210WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002211weston_layer_set_mask(struct weston_layer *layer,
2212 int x, int y, int width, int height)
2213{
2214 struct weston_view *view;
2215
2216 layer->mask.x1 = x;
2217 layer->mask.x2 = x + width;
2218 layer->mask.y1 = y;
2219 layer->mask.y2 = y + height;
2220
2221 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2222 weston_view_geometry_dirty(view);
2223 }
2224}
2225
2226WL_EXPORT void
2227weston_layer_set_mask_infinite(struct weston_layer *layer)
2228{
2229 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2230 UINT32_MAX, UINT32_MAX);
2231}
2232
2233WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002234weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002235{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002236 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002237 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002238
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002239 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2240 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002241 return;
2242
Pekka Paalanenb5026542014-11-12 15:09:24 +02002243 if (!output->repaint_needed)
2244 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2245
Kristian Høgsbergef044142011-06-21 15:02:12 -04002246 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002247 output->repaint_needed = 1;
2248 if (output->repaint_scheduled)
2249 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002250
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002251 wl_event_loop_add_idle(loop, idle_repaint, output);
2252 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002253 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2254
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002255
2256 if (compositor->input_loop_source) {
2257 wl_event_source_remove(compositor->input_loop_source);
2258 compositor->input_loop_source = NULL;
2259 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002260}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002261
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002262WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002263weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2264{
2265 struct weston_output *output;
2266
2267 wl_list_for_each(output, &compositor->output_list, link)
2268 weston_output_schedule_repaint(output);
2269}
2270
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002271static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002272surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002273{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002274 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002275}
2276
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002277static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002278surface_attach(struct wl_client *client,
2279 struct wl_resource *resource,
2280 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2281{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002282 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002283 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002284
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002285 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002286 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002287 if (buffer == NULL) {
2288 wl_client_post_no_memory(client);
2289 return;
2290 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002291 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002292
Pekka Paalanende685b82012-12-04 15:58:12 +02002293 /* Attach, attach, without commit in between does not send
2294 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002295 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002296
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002297 surface->pending.sx = sx;
2298 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002299 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002300}
2301
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002302static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002303surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002304 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002305 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002306{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002307 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002308
Pekka Paalanen8e159182012-10-10 12:49:25 +03002309 pixman_region32_union_rect(&surface->pending.damage,
2310 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002311 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002312}
2313
Kristian Høgsberg33418202011-08-16 23:01:28 -04002314static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002315destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002316{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002317 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002318
2319 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002320 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002321}
2322
2323static void
2324surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002325 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002326{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002327 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002328 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002329
2330 cb = malloc(sizeof *cb);
2331 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002332 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002333 return;
2334 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002335
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002336 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2337 callback);
2338 if (cb->resource == NULL) {
2339 free(cb);
2340 wl_resource_post_no_memory(resource);
2341 return;
2342 }
2343
Jason Ekstranda85118c2013-06-27 20:17:02 -05002344 wl_resource_set_implementation(cb->resource, NULL, cb,
2345 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002346
Pekka Paalanenbc106382012-10-10 12:49:31 +03002347 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002348}
2349
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002350static void
2351surface_set_opaque_region(struct wl_client *client,
2352 struct wl_resource *resource,
2353 struct wl_resource *region_resource)
2354{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002355 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002356 struct weston_region *region;
2357
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002358 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002359 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002360 pixman_region32_copy(&surface->pending.opaque,
2361 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002362 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002363 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002364 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002365}
2366
2367static void
2368surface_set_input_region(struct wl_client *client,
2369 struct wl_resource *resource,
2370 struct wl_resource *region_resource)
2371{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002372 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002373 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002374
2375 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002376 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002377 pixman_region32_copy(&surface->pending.input,
2378 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002379 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002380 pixman_region32_fini(&surface->pending.input);
2381 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002382 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002383}
2384
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002385static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002386weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002387{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002388 struct weston_subsurface *sub;
2389
2390 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2391 parent_link_pending) {
2392 wl_list_remove(&sub->parent_link);
2393 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2394 }
2395}
2396
2397static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002398weston_surface_commit_state(struct weston_surface *surface,
2399 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002400{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002401 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002402 pixman_region32_t opaque;
2403
Alexander Larsson4ea95522013-05-22 14:41:37 +02002404 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002405 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002406 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002407 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002408
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002409 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002410 if (state->newly_attached)
2411 weston_surface_attach(surface, state->buffer);
2412 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002413
Jason Ekstrand7b982072014-05-20 14:33:03 -05002414 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002415 weston_surface_update_size(surface);
2416 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002417 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002418 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002419
Jason Ekstrand7b982072014-05-20 14:33:03 -05002420 state->sx = 0;
2421 state->sy = 0;
2422 state->newly_attached = 0;
2423 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002424
2425 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002426 if (weston_timeline_enabled_ &&
2427 pixman_region32_not_empty(&state->damage))
2428 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002429 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002430 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002431 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002432 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002433 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002434
2435 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002436 pixman_region32_init(&opaque);
2437 pixman_region32_intersect_rect(&opaque, &state->opaque,
2438 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002439
2440 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2441 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002442 wl_list_for_each(view, &surface->views, surface_link)
2443 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002444 }
2445
2446 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002447
2448 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002449 pixman_region32_intersect_rect(&surface->input, &state->input,
2450 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002451
Pekka Paalanenbc106382012-10-10 12:49:31 +03002452 /* wl_surface.frame */
2453 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002454 &state->frame_callback_list);
2455 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002456
2457 /* XXX:
2458 * What should happen with a feedback request, if there
2459 * is no wl_buffer attached for this commit?
2460 */
2461
2462 /* presentation.feedback */
2463 wl_list_insert_list(&surface->feedback_list,
2464 &state->feedback_list);
2465 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002466}
2467
2468static void
2469weston_surface_commit(struct weston_surface *surface)
2470{
2471 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002472
Pekka Paalanene67858b2013-04-25 13:57:42 +03002473 weston_surface_commit_subsurface_order(surface);
2474
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002475 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002476}
2477
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002478static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002479weston_subsurface_commit(struct weston_subsurface *sub);
2480
2481static void
2482weston_subsurface_parent_commit(struct weston_subsurface *sub,
2483 int parent_is_synchronized);
2484
2485static void
2486surface_commit(struct wl_client *client, struct wl_resource *resource)
2487{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002488 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002489 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2490
2491 if (sub) {
2492 weston_subsurface_commit(sub);
2493 return;
2494 }
2495
2496 weston_surface_commit(surface);
2497
2498 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2499 if (sub->surface != surface)
2500 weston_subsurface_parent_commit(sub, 0);
2501 }
2502}
2503
2504static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002505surface_set_buffer_transform(struct wl_client *client,
2506 struct wl_resource *resource, int transform)
2507{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002508 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002509
Jonny Lamba55f1392014-05-30 12:07:15 +02002510 /* if wl_output.transform grows more members this will need to be updated. */
2511 if (transform < 0 ||
2512 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2513 wl_resource_post_error(resource,
2514 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2515 "buffer transform must be a valid transform "
2516 "('%d' specified)", transform);
2517 return;
2518 }
2519
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002520 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002521 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002522}
2523
Alexander Larsson4ea95522013-05-22 14:41:37 +02002524static void
2525surface_set_buffer_scale(struct wl_client *client,
2526 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002527 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002528{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002529 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002530
Jonny Lamba55f1392014-05-30 12:07:15 +02002531 if (scale < 1) {
2532 wl_resource_post_error(resource,
2533 WL_SURFACE_ERROR_INVALID_SCALE,
2534 "buffer scale must be at least one "
2535 "('%d' specified)", scale);
2536 return;
2537 }
2538
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002539 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002540 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002541}
2542
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002543static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002544 surface_destroy,
2545 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002546 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002547 surface_frame,
2548 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002549 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002550 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002551 surface_set_buffer_transform,
2552 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002553};
2554
2555static void
2556compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002557 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002558{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002559 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002560 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002561
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002562 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002563 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002564 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002565 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002566 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002567
Jason Ekstranda85118c2013-06-27 20:17:02 -05002568 surface->resource =
2569 wl_resource_create(client, &wl_surface_interface,
2570 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002571 if (surface->resource == NULL) {
2572 weston_surface_destroy(surface);
2573 wl_resource_post_no_memory(resource);
2574 return;
2575 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002576 wl_resource_set_implementation(surface->resource, &surface_interface,
2577 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002578
2579 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002580}
2581
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002582static void
2583destroy_region(struct wl_resource *resource)
2584{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002585 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002586
2587 pixman_region32_fini(&region->region);
2588 free(region);
2589}
2590
2591static void
2592region_destroy(struct wl_client *client, struct wl_resource *resource)
2593{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002594 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002595}
2596
2597static void
2598region_add(struct wl_client *client, struct wl_resource *resource,
2599 int32_t x, int32_t y, int32_t width, int32_t height)
2600{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002601 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002602
2603 pixman_region32_union_rect(&region->region, &region->region,
2604 x, y, width, height);
2605}
2606
2607static void
2608region_subtract(struct wl_client *client, struct wl_resource *resource,
2609 int32_t x, int32_t y, int32_t width, int32_t height)
2610{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002611 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002612 pixman_region32_t rect;
2613
2614 pixman_region32_init_rect(&rect, x, y, width, height);
2615 pixman_region32_subtract(&region->region, &region->region, &rect);
2616 pixman_region32_fini(&rect);
2617}
2618
2619static const struct wl_region_interface region_interface = {
2620 region_destroy,
2621 region_add,
2622 region_subtract
2623};
2624
2625static void
2626compositor_create_region(struct wl_client *client,
2627 struct wl_resource *resource, uint32_t id)
2628{
2629 struct weston_region *region;
2630
2631 region = malloc(sizeof *region);
2632 if (region == NULL) {
2633 wl_resource_post_no_memory(resource);
2634 return;
2635 }
2636
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002637 pixman_region32_init(&region->region);
2638
Jason Ekstranda85118c2013-06-27 20:17:02 -05002639 region->resource =
2640 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002641 if (region->resource == NULL) {
2642 free(region);
2643 wl_resource_post_no_memory(resource);
2644 return;
2645 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002646 wl_resource_set_implementation(region->resource, &region_interface,
2647 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002648}
2649
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002650static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002651 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002652 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002653};
2654
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002655static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002656weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2657{
2658 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002659
Jason Ekstrand7b982072014-05-20 14:33:03 -05002660 weston_surface_commit_state(surface, &sub->cached);
2661 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002662
2663 weston_surface_commit_subsurface_order(surface);
2664
2665 weston_surface_schedule_repaint(surface);
2666
Jason Ekstrand7b982072014-05-20 14:33:03 -05002667 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002668}
2669
2670static void
2671weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2672{
2673 struct weston_surface *surface = sub->surface;
2674
2675 /*
2676 * If this commit would cause the surface to move by the
2677 * attach(dx, dy) parameters, the old damage region must be
2678 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002679 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002680 */
2681 pixman_region32_translate(&sub->cached.damage,
2682 -surface->pending.sx, -surface->pending.sy);
2683 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2684 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002685 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002686
2687 if (surface->pending.newly_attached) {
2688 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002689 weston_surface_state_set_buffer(&sub->cached,
2690 surface->pending.buffer);
2691 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002692 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002693 weston_presentation_feedback_discard_list(
2694 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002695 }
2696 sub->cached.sx += surface->pending.sx;
2697 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002698
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002699 sub->cached.buffer_viewport.changed |=
2700 surface->pending.buffer_viewport.changed;
2701 sub->cached.buffer_viewport.buffer =
2702 surface->pending.buffer_viewport.buffer;
2703 sub->cached.buffer_viewport.surface =
2704 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002705
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002706 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002707
2708 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2709
2710 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2711
2712 wl_list_insert_list(&sub->cached.frame_callback_list,
2713 &surface->pending.frame_callback_list);
2714 wl_list_init(&surface->pending.frame_callback_list);
2715
Pekka Paalanen133e4392014-09-23 22:08:46 -04002716 wl_list_insert_list(&sub->cached.feedback_list,
2717 &surface->pending.feedback_list);
2718 wl_list_init(&surface->pending.feedback_list);
2719
Jason Ekstrand7b982072014-05-20 14:33:03 -05002720 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002721}
2722
Derek Foreman280e7dd2014-10-03 13:13:42 -05002723static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03002724weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2725{
2726 while (sub) {
2727 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002728 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002729
2730 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002731 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002732
2733 sub = weston_surface_to_subsurface(sub->parent);
2734 }
2735
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01002736 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002737}
2738
2739static void
2740weston_subsurface_commit(struct weston_subsurface *sub)
2741{
2742 struct weston_surface *surface = sub->surface;
2743 struct weston_subsurface *tmp;
2744
2745 /* Recursive check for effectively synchronized. */
2746 if (weston_subsurface_is_synchronized(sub)) {
2747 weston_subsurface_commit_to_cache(sub);
2748 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002749 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002750 /* flush accumulated state from cache */
2751 weston_subsurface_commit_to_cache(sub);
2752 weston_subsurface_commit_from_cache(sub);
2753 } else {
2754 weston_surface_commit(surface);
2755 }
2756
2757 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2758 if (tmp->surface != surface)
2759 weston_subsurface_parent_commit(tmp, 0);
2760 }
2761 }
2762}
2763
2764static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002765weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002766{
2767 struct weston_surface *surface = sub->surface;
2768 struct weston_subsurface *tmp;
2769
Pekka Paalanene67858b2013-04-25 13:57:42 +03002770 /* From now on, commit_from_cache the whole sub-tree, regardless of
2771 * the synchronized mode of each child. This sub-surface or some
2772 * of its ancestors were synchronized, so we are synchronized
2773 * all the way down.
2774 */
2775
Jason Ekstrand7b982072014-05-20 14:33:03 -05002776 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002777 weston_subsurface_commit_from_cache(sub);
2778
2779 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2780 if (tmp->surface != surface)
2781 weston_subsurface_parent_commit(tmp, 1);
2782 }
2783}
2784
2785static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002786weston_subsurface_parent_commit(struct weston_subsurface *sub,
2787 int parent_is_synchronized)
2788{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002789 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002790 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791 wl_list_for_each(view, &sub->surface->views, surface_link)
2792 weston_view_set_position(view,
2793 sub->position.x,
2794 sub->position.y);
2795
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002796 sub->position.set = 0;
2797 }
2798
2799 if (parent_is_synchronized || sub->synchronized)
2800 weston_subsurface_synchronized_commit(sub);
2801}
2802
Pekka Paalanen8274d902014-08-06 19:36:51 +03002803static int
2804subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
2805{
2806 return snprintf(buf, len, "sub-surface");
2807}
2808
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002809static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002810subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002811{
2812 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002813 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002814
Jason Ekstranda7af7042013-10-12 22:38:11 -05002815 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002816 weston_view_set_position(view,
2817 view->geometry.x + dx,
2818 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002819
2820 /* No need to check parent mappedness, because if parent is not
2821 * mapped, parent is not in a visible layer, so this sub-surface
2822 * will not be drawn either.
2823 */
2824 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002825 struct weston_output *output;
2826
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002827 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002828 * because that would call it also for the parent surface,
2829 * which might not be mapped yet. That would lead to
2830 * inconsistent state, where the window could never be
2831 * mapped.
2832 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002833 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002834 * weston_surface_is_mapped() return true, so that when the
2835 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002836 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002837 */
2838 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002839 output = container_of(compositor->output_list.next,
2840 struct weston_output, link);
2841
2842 surface->output = output;
2843 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002844 }
2845}
2846
2847static struct weston_subsurface *
2848weston_surface_to_subsurface(struct weston_surface *surface)
2849{
2850 if (surface->configure == subsurface_configure)
2851 return surface->configure_private;
2852
2853 return NULL;
2854}
2855
Pekka Paalanen01388e22013-04-25 13:57:44 +03002856WL_EXPORT struct weston_surface *
2857weston_surface_get_main_surface(struct weston_surface *surface)
2858{
2859 struct weston_subsurface *sub;
2860
2861 while (surface && (sub = weston_surface_to_subsurface(surface)))
2862 surface = sub->parent;
2863
2864 return surface;
2865}
2866
Pekka Paalanen50b67472014-10-01 15:02:41 +03002867WL_EXPORT int
2868weston_surface_set_role(struct weston_surface *surface,
2869 const char *role_name,
2870 struct wl_resource *error_resource,
2871 uint32_t error_code)
2872{
2873 assert(role_name);
2874
2875 if (surface->role_name == NULL ||
2876 surface->role_name == role_name ||
2877 strcmp(surface->role_name, role_name) == 0) {
2878 surface->role_name = role_name;
2879
2880 return 0;
2881 }
2882
2883 wl_resource_post_error(error_resource, error_code,
2884 "Cannot assign role %s to wl_surface@%d,"
2885 " already has role %s\n",
2886 role_name,
2887 wl_resource_get_id(surface->resource),
2888 surface->role_name);
2889 return -1;
2890}
2891
Pekka Paalanen8274d902014-08-06 19:36:51 +03002892WL_EXPORT void
2893weston_surface_set_label_func(struct weston_surface *surface,
2894 int (*desc)(struct weston_surface *,
2895 char *, size_t))
2896{
2897 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002898 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002899}
2900
Pekka Paalanene67858b2013-04-25 13:57:42 +03002901static void
2902subsurface_set_position(struct wl_client *client,
2903 struct wl_resource *resource, int32_t x, int32_t y)
2904{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002905 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002906
2907 if (!sub)
2908 return;
2909
2910 sub->position.x = x;
2911 sub->position.y = y;
2912 sub->position.set = 1;
2913}
2914
2915static struct weston_subsurface *
2916subsurface_from_surface(struct weston_surface *surface)
2917{
2918 struct weston_subsurface *sub;
2919
2920 sub = weston_surface_to_subsurface(surface);
2921 if (sub)
2922 return sub;
2923
2924 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2925 if (sub->surface == surface)
2926 return sub;
2927
2928 return NULL;
2929}
2930
2931static struct weston_subsurface *
2932subsurface_sibling_check(struct weston_subsurface *sub,
2933 struct weston_surface *surface,
2934 const char *request)
2935{
2936 struct weston_subsurface *sibling;
2937
2938 sibling = subsurface_from_surface(surface);
2939
2940 if (!sibling) {
2941 wl_resource_post_error(sub->resource,
2942 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2943 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002944 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002945 return NULL;
2946 }
2947
2948 if (sibling->parent != sub->parent) {
2949 wl_resource_post_error(sub->resource,
2950 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2951 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002952 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002953 return NULL;
2954 }
2955
2956 return sibling;
2957}
2958
2959static void
2960subsurface_place_above(struct wl_client *client,
2961 struct wl_resource *resource,
2962 struct wl_resource *sibling_resource)
2963{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002964 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002965 struct weston_surface *surface =
2966 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002967 struct weston_subsurface *sibling;
2968
2969 if (!sub)
2970 return;
2971
2972 sibling = subsurface_sibling_check(sub, surface, "place_above");
2973 if (!sibling)
2974 return;
2975
2976 wl_list_remove(&sub->parent_link_pending);
2977 wl_list_insert(sibling->parent_link_pending.prev,
2978 &sub->parent_link_pending);
2979}
2980
2981static void
2982subsurface_place_below(struct wl_client *client,
2983 struct wl_resource *resource,
2984 struct wl_resource *sibling_resource)
2985{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002986 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002987 struct weston_surface *surface =
2988 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002989 struct weston_subsurface *sibling;
2990
2991 if (!sub)
2992 return;
2993
2994 sibling = subsurface_sibling_check(sub, surface, "place_below");
2995 if (!sibling)
2996 return;
2997
2998 wl_list_remove(&sub->parent_link_pending);
2999 wl_list_insert(&sibling->parent_link_pending,
3000 &sub->parent_link_pending);
3001}
3002
3003static void
3004subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3005{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003006 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003007
3008 if (sub)
3009 sub->synchronized = 1;
3010}
3011
3012static void
3013subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3014{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003015 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003016
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003017 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003018 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003019
3020 /* If sub became effectively desynchronized, flush. */
3021 if (!weston_subsurface_is_synchronized(sub))
3022 weston_subsurface_synchronized_commit(sub);
3023 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003024}
3025
3026static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003027weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3028{
3029 wl_list_remove(&sub->parent_link);
3030 wl_list_remove(&sub->parent_link_pending);
3031 wl_list_remove(&sub->parent_destroy_listener.link);
3032 sub->parent = NULL;
3033}
3034
3035static void
3036weston_subsurface_destroy(struct weston_subsurface *sub);
3037
3038static void
3039subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3040{
3041 struct weston_subsurface *sub =
3042 container_of(listener, struct weston_subsurface,
3043 surface_destroy_listener);
3044 assert(data == &sub->surface->resource);
3045
3046 /* The protocol object (wl_resource) is left inert. */
3047 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003048 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003049
3050 weston_subsurface_destroy(sub);
3051}
3052
3053static void
3054subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3055{
3056 struct weston_subsurface *sub =
3057 container_of(listener, struct weston_subsurface,
3058 parent_destroy_listener);
3059 assert(data == &sub->parent->resource);
3060 assert(sub->surface != sub->parent);
3061
3062 if (weston_surface_is_mapped(sub->surface))
3063 weston_surface_unmap(sub->surface);
3064
3065 weston_subsurface_unlink_parent(sub);
3066}
3067
3068static void
3069subsurface_resource_destroy(struct wl_resource *resource)
3070{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003071 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003072
3073 if (sub)
3074 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003075}
3076
3077static void
3078subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3079{
3080 wl_resource_destroy(resource);
3081}
3082
3083static void
3084weston_subsurface_link_parent(struct weston_subsurface *sub,
3085 struct weston_surface *parent)
3086{
3087 sub->parent = parent;
3088 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003089 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003090 &sub->parent_destroy_listener);
3091
3092 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3093 wl_list_insert(&parent->subsurface_list_pending,
3094 &sub->parent_link_pending);
3095}
3096
3097static void
3098weston_subsurface_link_surface(struct weston_subsurface *sub,
3099 struct weston_surface *surface)
3100{
3101 sub->surface = surface;
3102 sub->surface_destroy_listener.notify =
3103 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003104 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003105 &sub->surface_destroy_listener);
3106}
3107
3108static void
3109weston_subsurface_destroy(struct weston_subsurface *sub)
3110{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003111 struct weston_view *view, *next;
3112
Pekka Paalanene67858b2013-04-25 13:57:42 +03003113 assert(sub->surface);
3114
3115 if (sub->resource) {
3116 assert(weston_surface_to_subsurface(sub->surface) == sub);
3117 assert(sub->parent_destroy_listener.notify ==
3118 subsurface_handle_parent_destroy);
3119
George Kiagiadakised04d382014-06-13 18:10:26 +02003120 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3121 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003122 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003123 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003124
Pekka Paalanene67858b2013-04-25 13:57:42 +03003125 if (sub->parent)
3126 weston_subsurface_unlink_parent(sub);
3127
Jason Ekstrand7b982072014-05-20 14:33:03 -05003128 weston_surface_state_fini(&sub->cached);
3129 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003130
3131 sub->surface->configure = NULL;
3132 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003133 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003134 } else {
3135 /* the dummy weston_subsurface for the parent itself */
3136 assert(sub->parent_destroy_listener.notify == NULL);
3137 wl_list_remove(&sub->parent_link);
3138 wl_list_remove(&sub->parent_link_pending);
3139 }
3140
3141 wl_list_remove(&sub->surface_destroy_listener.link);
3142 free(sub);
3143}
3144
3145static const struct wl_subsurface_interface subsurface_implementation = {
3146 subsurface_destroy,
3147 subsurface_set_position,
3148 subsurface_place_above,
3149 subsurface_place_below,
3150 subsurface_set_sync,
3151 subsurface_set_desync
3152};
3153
3154static struct weston_subsurface *
3155weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3156 struct weston_surface *parent)
3157{
3158 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003159 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003160
Bryce Harringtonde16d892014-11-20 22:21:57 -08003161 sub = zalloc(sizeof *sub);
3162 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003163 return NULL;
3164
Jason Ekstranda7af7042013-10-12 22:38:11 -05003165 wl_list_init(&sub->unused_views);
3166
Jason Ekstranda85118c2013-06-27 20:17:02 -05003167 sub->resource =
3168 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003169 if (!sub->resource) {
3170 free(sub);
3171 return NULL;
3172 }
3173
Jason Ekstranda85118c2013-06-27 20:17:02 -05003174 wl_resource_set_implementation(sub->resource,
3175 &subsurface_implementation,
3176 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003177 weston_subsurface_link_surface(sub, surface);
3178 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003179 weston_surface_state_init(&sub->cached);
3180 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003181 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003182
3183 return sub;
3184}
3185
3186/* Create a dummy subsurface for having the parent itself in its
3187 * sub-surface lists. Makes stacking order manipulation easy.
3188 */
3189static struct weston_subsurface *
3190weston_subsurface_create_for_parent(struct weston_surface *parent)
3191{
3192 struct weston_subsurface *sub;
3193
Bryce Harringtonde16d892014-11-20 22:21:57 -08003194 sub = zalloc(sizeof *sub);
3195 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003196 return NULL;
3197
3198 weston_subsurface_link_surface(sub, parent);
3199 sub->parent = parent;
3200 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3201 wl_list_insert(&parent->subsurface_list_pending,
3202 &sub->parent_link_pending);
3203
3204 return sub;
3205}
3206
3207static void
3208subcompositor_get_subsurface(struct wl_client *client,
3209 struct wl_resource *resource,
3210 uint32_t id,
3211 struct wl_resource *surface_resource,
3212 struct wl_resource *parent_resource)
3213{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003214 struct weston_surface *surface =
3215 wl_resource_get_user_data(surface_resource);
3216 struct weston_surface *parent =
3217 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003218 struct weston_subsurface *sub;
3219 static const char where[] = "get_subsurface: wl_subsurface@";
3220
3221 if (surface == parent) {
3222 wl_resource_post_error(resource,
3223 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3224 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003225 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003226 return;
3227 }
3228
3229 if (weston_surface_to_subsurface(surface)) {
3230 wl_resource_post_error(resource,
3231 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3232 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003233 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003234 return;
3235 }
3236
Pekka Paalanen50b67472014-10-01 15:02:41 +03003237 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3238 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003239 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003240
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003241 if (weston_surface_get_main_surface(parent) == surface) {
3242 wl_resource_post_error(resource,
3243 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3244 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003245 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003246 return;
3247 }
3248
Pekka Paalanene67858b2013-04-25 13:57:42 +03003249 /* make sure the parent is in its own list */
3250 if (wl_list_empty(&parent->subsurface_list)) {
3251 if (!weston_subsurface_create_for_parent(parent)) {
3252 wl_resource_post_no_memory(resource);
3253 return;
3254 }
3255 }
3256
3257 sub = weston_subsurface_create(id, surface, parent);
3258 if (!sub) {
3259 wl_resource_post_no_memory(resource);
3260 return;
3261 }
3262
3263 surface->configure = subsurface_configure;
3264 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003265 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003266}
3267
3268static void
3269subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3270{
3271 wl_resource_destroy(resource);
3272}
3273
3274static const struct wl_subcompositor_interface subcompositor_interface = {
3275 subcompositor_destroy,
3276 subcompositor_get_subsurface
3277};
3278
3279static void
3280bind_subcompositor(struct wl_client *client,
3281 void *data, uint32_t version, uint32_t id)
3282{
3283 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003284 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003285
Jason Ekstranda85118c2013-06-27 20:17:02 -05003286 resource =
3287 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003288 if (resource == NULL) {
3289 wl_client_post_no_memory(client);
3290 return;
3291 }
3292 wl_resource_set_implementation(resource, &subcompositor_interface,
3293 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003294}
3295
3296static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003297weston_compositor_dpms(struct weston_compositor *compositor,
3298 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003299{
3300 struct weston_output *output;
3301
3302 wl_list_for_each(output, &compositor->output_list, link)
3303 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003304 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003305}
3306
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003307WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003308weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003309{
Neil Roberts8b62e202013-09-30 13:14:47 +01003310 uint32_t old_state = compositor->state;
3311
3312 /* The state needs to be changed before emitting the wake
3313 * signal because that may try to schedule a repaint which
3314 * will not work if the compositor is still sleeping */
3315 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3316
3317 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003318 case WESTON_COMPOSITOR_SLEEPING:
3319 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3320 /* fall through */
3321 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003322 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003323 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003324 /* fall through */
3325 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003326 wl_event_source_timer_update(compositor->idle_source,
3327 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003328 }
3329}
3330
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003331WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003332weston_compositor_offscreen(struct weston_compositor *compositor)
3333{
3334 switch (compositor->state) {
3335 case WESTON_COMPOSITOR_OFFSCREEN:
3336 return;
3337 case WESTON_COMPOSITOR_SLEEPING:
3338 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3339 /* fall through */
3340 default:
3341 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3342 wl_event_source_timer_update(compositor->idle_source, 0);
3343 }
3344}
3345
3346WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003347weston_compositor_sleep(struct weston_compositor *compositor)
3348{
3349 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3350 return;
3351
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003352 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003353 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3354 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3355}
3356
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003357static int
3358idle_handler(void *data)
3359{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003360 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003361
3362 if (compositor->idle_inhibit)
3363 return 1;
3364
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003365 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003366 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003367
3368 return 1;
3369}
3370
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003371WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003372weston_plane_init(struct weston_plane *plane,
3373 struct weston_compositor *ec,
3374 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003375{
3376 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003377 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003378 plane->x = x;
3379 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003380 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003381
3382 /* Init the link so that the call to wl_list_remove() when releasing
3383 * the plane without ever stacking doesn't lead to a crash */
3384 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003385}
3386
3387WL_EXPORT void
3388weston_plane_release(struct weston_plane *plane)
3389{
Xiong Zhang97116532013-10-23 13:58:31 +08003390 struct weston_view *view;
3391
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003392 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003393 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003394
Xiong Zhang97116532013-10-23 13:58:31 +08003395 wl_list_for_each(view, &plane->compositor->view_list, link) {
3396 if (view->plane == plane)
3397 view->plane = NULL;
3398 }
3399
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003400 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003401}
3402
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003403WL_EXPORT void
3404weston_compositor_stack_plane(struct weston_compositor *ec,
3405 struct weston_plane *plane,
3406 struct weston_plane *above)
3407{
3408 if (above)
3409 wl_list_insert(above->link.prev, &plane->link);
3410 else
3411 wl_list_insert(&ec->plane_list, &plane->link);
3412}
3413
Casey Dahlin9074db52012-04-19 22:50:09 -04003414static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003415{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003416 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003417}
3418
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003419static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003420bind_output(struct wl_client *client,
3421 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003422{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003423 struct weston_output *output = data;
3424 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003425 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003426
Jason Ekstranda85118c2013-06-27 20:17:02 -05003427 resource = wl_resource_create(client, &wl_output_interface,
3428 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003429 if (resource == NULL) {
3430 wl_client_post_no_memory(client);
3431 return;
3432 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003433
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003434 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003435 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003436
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003437 wl_output_send_geometry(resource,
3438 output->x,
3439 output->y,
3440 output->mm_width,
3441 output->mm_height,
3442 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003443 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003444 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003445 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003446 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003447 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003448
3449 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003450 wl_output_send_mode(resource,
3451 mode->flags,
3452 mode->width,
3453 mode->height,
3454 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003455 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003456
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003457 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003458 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003459}
3460
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003461/* Move other outputs when one is removed so the space remains contiguos. */
3462static void
3463weston_compositor_remove_output(struct weston_compositor *compositor,
3464 struct weston_output *remove_output)
3465{
3466 struct weston_output *output;
3467 int offset = 0;
3468
3469 wl_list_for_each(output, &compositor->output_list, link) {
3470 if (output == remove_output) {
3471 offset = output->width;
3472 continue;
3473 }
3474
3475 if (offset > 0) {
3476 weston_output_move(output,
3477 output->x - offset, output->y);
3478 output->dirty = 1;
3479 }
3480 }
3481}
3482
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003483WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003484weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003485{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003486 struct wl_resource *resource;
3487
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003488 output->destroying = 1;
3489
Pekka Paalanen133e4392014-09-23 22:08:46 -04003490 weston_presentation_feedback_discard_list(&output->feedback_list);
3491
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003492 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003493 wl_list_remove(&output->link);
3494
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003495 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003496 wl_signal_emit(&output->destroy_signal, output);
3497
Richard Hughesafe690c2013-05-02 10:10:04 +01003498 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003499 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003500 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003501 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003502
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003503 wl_resource_for_each(resource, &output->resource_list) {
3504 wl_resource_set_destructor(resource, NULL);
3505 }
3506
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003507 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003508}
3509
Scott Moreau1bad5db2012-08-18 01:04:05 -06003510static void
3511weston_output_compute_transform(struct weston_output *output)
3512{
3513 struct weston_matrix transform;
3514 int flip;
3515
3516 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003517 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003518
3519 switch(output->transform) {
3520 case WL_OUTPUT_TRANSFORM_FLIPPED:
3521 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3522 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3523 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003524 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003525 flip = -1;
3526 break;
3527 default:
3528 flip = 1;
3529 break;
3530 }
3531
3532 switch(output->transform) {
3533 case WL_OUTPUT_TRANSFORM_NORMAL:
3534 case WL_OUTPUT_TRANSFORM_FLIPPED:
3535 transform.d[0] = flip;
3536 transform.d[1] = 0;
3537 transform.d[4] = 0;
3538 transform.d[5] = 1;
3539 break;
3540 case WL_OUTPUT_TRANSFORM_90:
3541 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3542 transform.d[0] = 0;
3543 transform.d[1] = -flip;
3544 transform.d[4] = 1;
3545 transform.d[5] = 0;
3546 break;
3547 case WL_OUTPUT_TRANSFORM_180:
3548 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3549 transform.d[0] = -flip;
3550 transform.d[1] = 0;
3551 transform.d[4] = 0;
3552 transform.d[5] = -1;
3553 break;
3554 case WL_OUTPUT_TRANSFORM_270:
3555 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3556 transform.d[0] = 0;
3557 transform.d[1] = flip;
3558 transform.d[4] = -1;
3559 transform.d[5] = 0;
3560 break;
3561 default:
3562 break;
3563 }
3564
3565 weston_matrix_multiply(&output->matrix, &transform);
3566}
3567
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003568WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003569weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003570{
Scott Moreau850ca422012-05-21 15:21:25 -06003571 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003572
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003573 weston_matrix_init(&output->matrix);
3574 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003575 -(output->x + output->width / 2.0),
3576 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003577
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003578 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003579 2.0 / output->width,
3580 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003581
Scott Moreauccbf29d2012-02-22 14:21:41 -07003582 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003583 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003584 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003585 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3586 output->zoom.trans_y, 0);
3587 weston_matrix_scale(&output->matrix, magnification,
3588 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003589 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003590
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003591 weston_output_compute_transform(output);
3592
Scott Moreauccbf29d2012-02-22 14:21:41 -07003593 output->dirty = 0;
3594}
3595
Scott Moreau1bad5db2012-08-18 01:04:05 -06003596static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003597weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003598{
3599 output->transform = transform;
3600
3601 switch (transform) {
3602 case WL_OUTPUT_TRANSFORM_90:
3603 case WL_OUTPUT_TRANSFORM_270:
3604 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3605 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3606 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003607 output->width = output->current_mode->height;
3608 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003609 break;
3610 case WL_OUTPUT_TRANSFORM_NORMAL:
3611 case WL_OUTPUT_TRANSFORM_180:
3612 case WL_OUTPUT_TRANSFORM_FLIPPED:
3613 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003614 output->width = output->current_mode->width;
3615 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003616 break;
3617 default:
3618 break;
3619 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003620
Hardening57388e42013-09-18 23:56:36 +02003621 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003622 output->width /= scale;
3623 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003624}
3625
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003626static void
3627weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003628{
3629 output->x = x;
3630 output->y = y;
3631
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003632 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003633 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003634 output->width,
3635 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003636}
3637
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003638WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003639weston_output_move(struct weston_output *output, int x, int y)
3640{
3641 pixman_region32_t old_region;
3642 struct wl_resource *resource;
3643
3644 output->move_x = x - output->x;
3645 output->move_y = y - output->y;
3646
3647 if (output->move_x == 0 && output->move_y == 0)
3648 return;
3649
3650 pixman_region32_init(&old_region);
3651 pixman_region32_copy(&old_region, &output->region);
3652
3653 weston_output_init_geometry(output, x, y);
3654
3655 output->dirty = 1;
3656
3657 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003658 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003659
3660 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003661 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003662 wl_output_send_geometry(resource,
3663 output->x,
3664 output->y,
3665 output->mm_width,
3666 output->mm_height,
3667 output->subpixel,
3668 output->make,
3669 output->model,
3670 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003671
3672 if (wl_resource_get_version(resource) >= 2)
3673 wl_output_send_done(resource);
3674 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003675}
3676
3677WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003678weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003679 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003680 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003681{
3682 output->compositor = c;
3683 output->x = x;
3684 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003685 output->mm_width = mm_width;
3686 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003687 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003688 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003689
Alexander Larsson0b135062013-05-28 16:23:36 +02003690 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003691 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003692
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003693 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003694 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003695
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003696 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003697 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003698 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003699 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003700 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003701
Casey Dahlin58ba1372012-04-19 22:50:08 -04003702 output->id = ffs(~output->compositor->output_id_pool) - 1;
3703 output->compositor->output_id_pool |= 1 << output->id;
3704
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003705 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003706 wl_global_create(c->wl_display, &wl_output_interface, 2,
3707 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003708 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003709}
3710
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003711WL_EXPORT void
3712weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003713 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003714 wl_fixed_t *x, wl_fixed_t *y)
3715{
3716 wl_fixed_t tx, ty;
3717 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003718 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003719
Hardeningff39efa2013-09-18 23:56:35 +02003720 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3721 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003722
3723 switch(output->transform) {
3724 case WL_OUTPUT_TRANSFORM_NORMAL:
3725 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003726 tx = device_x;
3727 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003728 break;
3729 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003730 tx = device_y;
3731 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003732 break;
3733 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003734 tx = width - device_x;
3735 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003736 break;
3737 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003738 tx = width - device_y;
3739 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003740 break;
3741 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003742 tx = width - device_x;
3743 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003744 break;
3745 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003746 tx = width - device_y;
3747 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003748 break;
3749 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003750 tx = device_x;
3751 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003752 break;
3753 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003754 tx = device_y;
3755 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003756 break;
3757 }
3758
Neil Robertseb5a2002014-05-01 16:13:55 +01003759 tx /= output->current_scale;
3760 ty /= output->current_scale;
3761
3762 if (output->zoom.active) {
3763 zoom_scale = output->zoom.spring_z.current;
3764 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3765 output->width / 2.0f *
3766 (zoom_scale + output->zoom.trans_x));
3767 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3768 output->height / 2.0f *
3769 (zoom_scale + output->zoom.trans_y));
3770 tx = wl_fixed_from_double(zx);
3771 ty = wl_fixed_from_double(zy);
3772 }
3773
3774 *x = tx + wl_fixed_from_int(output->x);
3775 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003776}
3777
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003778static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003779destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003780{
Jonny Lamb74130762013-11-26 18:19:46 +01003781 struct weston_surface *surface =
3782 wl_resource_get_user_data(resource);
3783
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003784 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003785 surface->pending.buffer_viewport.buffer.src_width =
3786 wl_fixed_from_int(-1);
3787 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003788 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003789}
3790
3791static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003792viewport_destroy(struct wl_client *client,
3793 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003794{
3795 wl_resource_destroy(resource);
3796}
3797
3798static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003799viewport_set(struct wl_client *client,
3800 struct wl_resource *resource,
3801 wl_fixed_t src_x,
3802 wl_fixed_t src_y,
3803 wl_fixed_t src_width,
3804 wl_fixed_t src_height,
3805 int32_t dst_width,
3806 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003807{
Jonny Lamb74130762013-11-26 18:19:46 +01003808 struct weston_surface *surface =
3809 wl_resource_get_user_data(resource);
3810
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003811 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003812
Jonny Lamb8ae35902013-11-26 18:19:45 +01003813 if (wl_fixed_to_double(src_width) < 0 ||
3814 wl_fixed_to_double(src_height) < 0) {
3815 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003816 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003817 "source dimensions must be non-negative (%fx%f)",
3818 wl_fixed_to_double(src_width),
3819 wl_fixed_to_double(src_height));
3820 return;
3821 }
3822
3823 if (dst_width <= 0 || dst_height <= 0) {
3824 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003825 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003826 "destination dimensions must be positive (%dx%d)",
3827 dst_width, dst_height);
3828 return;
3829 }
3830
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003831 surface->pending.buffer_viewport.buffer.src_x = src_x;
3832 surface->pending.buffer_viewport.buffer.src_y = src_y;
3833 surface->pending.buffer_viewport.buffer.src_width = src_width;
3834 surface->pending.buffer_viewport.buffer.src_height = src_height;
3835 surface->pending.buffer_viewport.surface.width = dst_width;
3836 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003837 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003838}
3839
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003840static void
3841viewport_set_source(struct wl_client *client,
3842 struct wl_resource *resource,
3843 wl_fixed_t src_x,
3844 wl_fixed_t src_y,
3845 wl_fixed_t src_width,
3846 wl_fixed_t src_height)
3847{
3848 struct weston_surface *surface =
3849 wl_resource_get_user_data(resource);
3850
3851 assert(surface->viewport_resource != NULL);
3852
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003853 if (src_width == wl_fixed_from_int(-1) &&
3854 src_height == wl_fixed_from_int(-1)) {
3855 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003856 surface->pending.buffer_viewport.buffer.src_width =
3857 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003858 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003859 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003860 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003861
3862 if (src_width <= 0 || src_height <= 0) {
3863 wl_resource_post_error(resource,
3864 WL_VIEWPORT_ERROR_BAD_VALUE,
3865 "source size must be positive (%fx%f)",
3866 wl_fixed_to_double(src_width),
3867 wl_fixed_to_double(src_height));
3868 return;
3869 }
3870
3871 surface->pending.buffer_viewport.buffer.src_x = src_x;
3872 surface->pending.buffer_viewport.buffer.src_y = src_y;
3873 surface->pending.buffer_viewport.buffer.src_width = src_width;
3874 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003875 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003876}
3877
3878static void
3879viewport_set_destination(struct wl_client *client,
3880 struct wl_resource *resource,
3881 int32_t dst_width,
3882 int32_t dst_height)
3883{
3884 struct weston_surface *surface =
3885 wl_resource_get_user_data(resource);
3886
3887 assert(surface->viewport_resource != NULL);
3888
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003889 if (dst_width == -1 && dst_height == -1) {
3890 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003891 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003892 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003893 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003894 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003895
3896 if (dst_width <= 0 || dst_height <= 0) {
3897 wl_resource_post_error(resource,
3898 WL_VIEWPORT_ERROR_BAD_VALUE,
3899 "destination size must be positive (%dx%d)",
3900 dst_width, dst_height);
3901 return;
3902 }
3903
3904 surface->pending.buffer_viewport.surface.width = dst_width;
3905 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003906 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003907}
3908
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003909static const struct wl_viewport_interface viewport_interface = {
3910 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003911 viewport_set,
3912 viewport_set_source,
3913 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003914};
3915
3916static void
3917scaler_destroy(struct wl_client *client,
3918 struct wl_resource *resource)
3919{
3920 wl_resource_destroy(resource);
3921}
3922
3923static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003924scaler_get_viewport(struct wl_client *client,
3925 struct wl_resource *scaler,
3926 uint32_t id,
3927 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003928{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003929 int version = wl_resource_get_version(scaler);
3930 struct weston_surface *surface =
3931 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003932 struct wl_resource *resource;
3933
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003934 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003935 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003936 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3937 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003938 return;
3939 }
3940
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003941 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003942 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003943 if (resource == NULL) {
3944 wl_client_post_no_memory(client);
3945 return;
3946 }
3947
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003948 wl_resource_set_implementation(resource, &viewport_interface,
3949 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003950
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003951 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003952}
3953
3954static const struct wl_scaler_interface scaler_interface = {
3955 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003956 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003957};
3958
3959static void
3960bind_scaler(struct wl_client *client,
3961 void *data, uint32_t version, uint32_t id)
3962{
3963 struct wl_resource *resource;
3964
3965 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003966 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003967 if (resource == NULL) {
3968 wl_client_post_no_memory(client);
3969 return;
3970 }
3971
3972 wl_resource_set_implementation(resource, &scaler_interface,
3973 NULL, NULL);
3974}
3975
3976static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04003977destroy_presentation_feedback(struct wl_resource *feedback_resource)
3978{
3979 struct weston_presentation_feedback *feedback;
3980
3981 feedback = wl_resource_get_user_data(feedback_resource);
3982
3983 wl_list_remove(&feedback->link);
3984 free(feedback);
3985}
3986
3987static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003988presentation_destroy(struct wl_client *client, struct wl_resource *resource)
3989{
3990 wl_resource_destroy(resource);
3991}
3992
3993static void
3994presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04003995 struct wl_resource *presentation_resource,
3996 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003997 uint32_t callback)
3998{
Pekka Paalanen133e4392014-09-23 22:08:46 -04003999 struct weston_surface *surface;
4000 struct weston_presentation_feedback *feedback;
4001
4002 surface = wl_resource_get_user_data(surface_resource);
4003
Bryce Harringtonde16d892014-11-20 22:21:57 -08004004 feedback = zalloc(sizeof *feedback);
4005 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004006 goto err_calloc;
4007
4008 feedback->resource = wl_resource_create(client,
4009 &presentation_feedback_interface,
4010 1, callback);
4011 if (!feedback->resource)
4012 goto err_create;
4013
4014 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4015 destroy_presentation_feedback);
4016
4017 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4018
4019 return;
4020
4021err_create:
4022 free(feedback);
4023
4024err_calloc:
4025 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004026}
4027
4028static const struct presentation_interface presentation_implementation = {
4029 presentation_destroy,
4030 presentation_feedback
4031};
4032
4033static void
4034bind_presentation(struct wl_client *client,
4035 void *data, uint32_t version, uint32_t id)
4036{
4037 struct weston_compositor *compositor = data;
4038 struct wl_resource *resource;
4039
4040 resource = wl_resource_create(client, &presentation_interface,
4041 MIN(version, 1), id);
4042 if (resource == NULL) {
4043 wl_client_post_no_memory(client);
4044 return;
4045 }
4046
4047 wl_resource_set_implementation(resource, &presentation_implementation,
4048 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004049 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004050}
4051
4052static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004053compositor_bind(struct wl_client *client,
4054 void *data, uint32_t version, uint32_t id)
4055{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004056 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004057 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004058
Jason Ekstranda85118c2013-06-27 20:17:02 -05004059 resource = wl_resource_create(client, &wl_compositor_interface,
4060 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004061 if (resource == NULL) {
4062 wl_client_post_no_memory(client);
4063 return;
4064 }
4065
4066 wl_resource_set_implementation(resource, &compositor_interface,
4067 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004068}
4069
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004070static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004071log_uname(void)
4072{
4073 struct utsname usys;
4074
4075 uname(&usys);
4076
4077 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4078 usys.version, usys.machine);
4079}
4080
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004081WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004082weston_environment_get_fd(const char *env)
4083{
4084 char *e, *end;
4085 int fd, flags;
4086
4087 e = getenv(env);
4088 if (!e)
4089 return -1;
4090 fd = strtol(e, &end, 0);
4091 if (*end != '\0')
4092 return -1;
4093
4094 flags = fcntl(fd, F_GETFD);
4095 if (flags == -1)
4096 return -1;
4097
4098 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4099 unsetenv(env);
4100
4101 return fd;
4102}
4103
Pekka Paalanenb5026542014-11-12 15:09:24 +02004104static void
4105timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4106 uint32_t key, void *data)
4107{
4108 struct weston_compositor *compositor = data;
4109
4110 if (weston_timeline_enabled_)
4111 weston_timeline_close();
4112 else
4113 weston_timeline_open(compositor);
4114}
4115
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004116WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004117weston_compositor_init(struct weston_compositor *ec,
4118 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004119 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004120 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004121{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004122 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004123 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004124 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004125
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004126 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004127 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004128 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004129 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004130 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004131 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004132 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004133 wl_signal_init(&ec->idle_signal);
4134 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004135 wl_signal_init(&ec->show_input_panel_signal);
4136 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004137 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004138 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004139 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004140 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004141 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004142 wl_signal_init(&ec->session_signal);
4143 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004144
Casey Dahlin58ba1372012-04-19 22:50:08 -04004145 ec->output_id_pool = 0;
4146
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004147 if (!wl_global_create(display, &wl_compositor_interface, 3,
4148 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004149 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004150
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004151 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4152 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004153 return -1;
4154
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004155 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004156 ec, bind_scaler))
4157 return -1;
4158
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004159 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4160 ec, bind_presentation))
4161 return -1;
4162
Jason Ekstranda7af7042013-10-12 22:38:11 -05004163 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004164 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004165 wl_list_init(&ec->layer_list);
4166 wl_list_init(&ec->seat_list);
4167 wl_list_init(&ec->output_list);
4168 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004169 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004170 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004171 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004172 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004173 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004174
Xiong Zhang97116532013-10-23 13:58:31 +08004175 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004176 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004177
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004178 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4179 weston_config_section_get_string(s, "keymap_rules",
4180 (char **) &xkb_names.rules, NULL);
4181 weston_config_section_get_string(s, "keymap_model",
4182 (char **) &xkb_names.model, NULL);
4183 weston_config_section_get_string(s, "keymap_layout",
4184 (char **) &xkb_names.layout, NULL);
4185 weston_config_section_get_string(s, "keymap_variant",
4186 (char **) &xkb_names.variant, NULL);
4187 weston_config_section_get_string(s, "keymap_options",
4188 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004189
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004190 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4191 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004192
Jonny Lamb66a41a02014-08-12 14:58:25 +02004193 weston_config_section_get_int(s, "repeat-rate",
4194 &ec->kb_repeat_rate, 40);
4195 weston_config_section_get_int(s, "repeat-delay",
4196 &ec->kb_repeat_delay, 400);
4197
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004198 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004199
4200 wl_data_device_manager_init(ec->wl_display);
4201
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004202 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004203
Daniel Stone725c2c32012-06-22 14:04:36 +01004204 loop = wl_display_get_event_loop(ec->wl_display);
4205 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4206 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4207
4208 ec->input_loop = wl_event_loop_create();
4209
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004210 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4211 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4212
Pekka Paalanenb5026542014-11-12 15:09:24 +02004213 weston_compositor_add_debug_binding(ec, KEY_T,
4214 timeline_key_binding_handler, ec);
4215
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004216 weston_compositor_schedule_repaint(ec);
4217
Daniel Stone725c2c32012-06-22 14:04:36 +01004218 return 0;
4219}
4220
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004221WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004222weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004223{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004224 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004225
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004226 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004227 if (ec->input_loop_source)
4228 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004229
Matt Roper361d2ad2011-08-29 13:52:23 -07004230 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004231 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004232 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004233
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004234 if (ec->renderer)
4235 ec->renderer->destroy(ec);
4236
Daniel Stone325fc2d2012-05-30 16:31:58 +01004237 weston_binding_list_destroy_all(&ec->key_binding_list);
4238 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004239 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004240 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004241 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004242
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004243 weston_plane_release(&ec->primary_plane);
4244
Jonas Ådahlc97af922012-03-28 22:36:09 +02004245 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004246
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004247 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004248}
4249
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004250WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004251weston_compositor_exit_with_code(struct weston_compositor *compositor,
4252 int exit_code)
4253{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004254 if (compositor->exit_code == EXIT_SUCCESS)
4255 compositor->exit_code = exit_code;
4256
Frederic Plourdec336f062014-10-29 14:44:33 -04004257 wl_display_terminate(compositor->wl_display);
4258}
4259
4260WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004261weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4262 const struct weston_pointer_grab_interface *interface)
4263{
4264 struct weston_seat *seat;
4265
4266 ec->default_pointer_grab = interface;
4267 wl_list_for_each(seat, &ec->seat_list, link) {
4268 if (seat->pointer) {
4269 weston_pointer_set_default_grab(seat->pointer,
4270 interface);
4271 }
4272 }
4273}
4274
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004275WL_EXPORT int
4276weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4277 clockid_t clk_id)
4278{
4279 struct timespec ts;
4280
4281 if (clock_gettime(clk_id, &ts) < 0)
4282 return -1;
4283
4284 compositor->presentation_clock = clk_id;
4285
4286 return 0;
4287}
4288
4289/*
4290 * For choosing the software clock, when the display hardware or API
4291 * does not expose a compatible presentation timestamp.
4292 */
4293WL_EXPORT int
4294weston_compositor_set_presentation_clock_software(
4295 struct weston_compositor *compositor)
4296{
4297 /* In order of preference */
4298 static const clockid_t clocks[] = {
4299 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4300 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4301 CLOCK_MONOTONIC, /* no jumps, may crawl */
4302 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4303 CLOCK_REALTIME /* may jump and crawl */
4304 };
4305 unsigned i;
4306
4307 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4308 if (weston_compositor_set_presentation_clock(compositor,
4309 clocks[i]) == 0)
4310 return 0;
4311
4312 weston_log("Error: no suitable presentation clock available.\n");
4313
4314 return -1;
4315}
4316
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004317WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004318weston_version(int *major, int *minor, int *micro)
4319{
4320 *major = WESTON_VERSION_MAJOR;
4321 *minor = WESTON_VERSION_MINOR;
4322 *micro = WESTON_VERSION_MICRO;
4323}
4324
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004325static const char *
4326clock_name(clockid_t clk_id)
4327{
4328 static const char *names[] = {
4329 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4330 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4331 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4332 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4333 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4334 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4335 };
4336
4337 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4338 return "unknown";
4339
4340 return names[clk_id];
4341}
4342
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004343static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004344 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004345 const char *desc;
4346} capability_strings[] = {
4347 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004348 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004349};
4350
4351static void
4352weston_compositor_log_capabilities(struct weston_compositor *compositor)
4353{
4354 unsigned i;
4355 int yes;
4356
4357 weston_log("Compositor capabilities:\n");
4358 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4359 yes = compositor->capabilities & capability_strings[i].bit;
4360 weston_log_continue(STAMP_SPACE "%s %s\n",
4361 capability_strings[i].desc,
4362 yes ? "yes" : "no");
4363 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004364
4365 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4366 clock_name(compositor->presentation_clock),
4367 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004368}
4369
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004370static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004371{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004372 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004373
Martin Minarik6d118362012-06-07 18:01:59 +02004374 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004375 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004376
4377 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004378}
4379
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004380#ifdef HAVE_LIBUNWIND
4381
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004382static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004383print_backtrace(void)
4384{
4385 unw_cursor_t cursor;
4386 unw_context_t context;
4387 unw_word_t off;
4388 unw_proc_info_t pip;
4389 int ret, i = 0;
4390 char procname[256];
4391 const char *filename;
4392 Dl_info dlinfo;
4393
4394 pip.unwind_info = NULL;
4395 ret = unw_getcontext(&context);
4396 if (ret) {
4397 weston_log("unw_getcontext: %d\n", ret);
4398 return;
4399 }
4400
4401 ret = unw_init_local(&cursor, &context);
4402 if (ret) {
4403 weston_log("unw_init_local: %d\n", ret);
4404 return;
4405 }
4406
4407 ret = unw_step(&cursor);
4408 while (ret > 0) {
4409 ret = unw_get_proc_info(&cursor, &pip);
4410 if (ret) {
4411 weston_log("unw_get_proc_info: %d\n", ret);
4412 break;
4413 }
4414
4415 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4416 if (ret && ret != -UNW_ENOMEM) {
4417 if (ret != -UNW_EUNSPEC)
4418 weston_log("unw_get_proc_name: %d\n", ret);
4419 procname[0] = '?';
4420 procname[1] = 0;
4421 }
4422
4423 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4424 *dlinfo.dli_fname)
4425 filename = dlinfo.dli_fname;
4426 else
4427 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004428
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004429 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4430 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4431
4432 ret = unw_step(&cursor);
4433 if (ret < 0)
4434 weston_log("unw_step: %d\n", ret);
4435 }
4436}
4437
4438#else
4439
4440static void
4441print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004442{
4443 void *buffer[32];
4444 int i, count;
4445 Dl_info info;
4446
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004447 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4448 for (i = 0; i < count; i++) {
4449 dladdr(buffer[i], &info);
4450 weston_log(" [%016lx] %s (%s)\n",
4451 (long) buffer[i],
4452 info.dli_sname ? info.dli_sname : "--",
4453 info.dli_fname);
4454 }
4455}
4456
4457#endif
4458
4459static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004460on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004461{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004462 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004463 * then call the backend restore function, which will switch
4464 * back to the vt we launched from or ungrab X etc and then
4465 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004466 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004467 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004468 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004469
Peter Maatmane5b42e42013-03-27 22:38:53 +01004470 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004471
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004472 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004473
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004474 segv_compositor->restore(segv_compositor);
4475
4476 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004477}
4478
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004479WL_EXPORT void *
4480weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004481{
4482 char path[PATH_MAX];
4483 void *module, *init;
4484
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004485 if (name == NULL)
4486 return NULL;
4487
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004488 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004489 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004490 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004491 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004492
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004493 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4494 if (module) {
4495 weston_log("Module '%s' already loaded\n", path);
4496 dlclose(module);
4497 return NULL;
4498 }
4499
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004500 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004501 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004502 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004503 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004504 return NULL;
4505 }
4506
4507 init = dlsym(module, entrypoint);
4508 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004509 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004510 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004511 return NULL;
4512 }
4513
4514 return init;
4515}
4516
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004517static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004518load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004519 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004520{
4521 const char *p, *end;
4522 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004523 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004524 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004525
4526 if (modules == NULL)
4527 return 0;
4528
4529 p = modules;
4530 while (*p) {
4531 end = strchrnul(p, ',');
4532 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004533 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004534 if (!module_init)
4535 return -1;
4536 if (module_init(ec, argc, argv) < 0)
4537 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004538 p = end;
4539 while (*p == ',')
4540 p++;
4541
4542 }
4543
4544 return 0;
4545}
4546
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004547static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004548 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4549
4550static const char xdg_wrong_message[] =
4551 "fatal: environment variable XDG_RUNTIME_DIR\n"
4552 "is set to \"%s\", which is not a directory.\n";
4553
4554static const char xdg_wrong_mode_message[] =
4555 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004556 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4557 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004558
4559static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004560 "Refer to your distribution on how to get it, or\n"
4561 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4562 "on how to implement it.\n";
4563
Martin Minarik37032f82012-06-18 20:15:18 +02004564static void
4565verify_xdg_runtime_dir(void)
4566{
4567 char *dir = getenv("XDG_RUNTIME_DIR");
4568 struct stat s;
4569
4570 if (!dir) {
4571 weston_log(xdg_error_message);
4572 weston_log_continue(xdg_detail_message);
4573 exit(EXIT_FAILURE);
4574 }
4575
4576 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4577 weston_log(xdg_wrong_message, dir);
4578 weston_log_continue(xdg_detail_message);
4579 exit(EXIT_FAILURE);
4580 }
4581
4582 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4583 weston_log(xdg_wrong_mode_message,
4584 dir, s.st_mode & 0777, s.st_uid);
4585 weston_log_continue(xdg_detail_message);
4586 }
4587}
4588
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004589static int
4590usage(int error_code)
4591{
4592 fprintf(stderr,
4593 "Usage: weston [OPTIONS]\n\n"
4594 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4595 "Weston supports multiple backends, and depending on which backend is in use\n"
4596 "different options will be accepted.\n\n"
4597
4598
4599 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004600 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004601 " -B, --backend=MODULE\tBackend module, one of\n"
4602#if defined(BUILD_DRM_COMPOSITOR)
4603 "\t\t\t\tdrm-backend.so\n"
4604#endif
4605#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02004606 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004607#endif
4608#if defined(BUILD_X11_COMPOSITOR)
4609 "\t\t\t\tx11-backend.so\n"
4610#endif
4611#if defined(BUILD_WAYLAND_COMPOSITOR)
4612 "\t\t\t\twayland-backend.so\n"
4613#endif
4614#if defined(BUILD_RDP_COMPOSITOR)
4615 "\t\t\t\trdp-backend.so\n"
4616#endif
4617#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4618 "\t\t\t\trpi-backend.so\n"
4619#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05004620 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004621 " -S, --socket=NAME\tName of socket to listen on\n"
4622 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004623 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004624 " --log=FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004625 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004626 " -h, --help\t\tThis help message\n\n");
4627
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004628#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004629 fprintf(stderr,
4630 "Options for drm-backend.so:\n\n"
4631 " --connector=ID\tBring up only this connector\n"
4632 " --seat=SEAT\t\tThe seat that weston should run on\n"
4633 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004634 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004635 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004636#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004637
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004638#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004639 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004640 "Options for fbdev-backend.so:\n\n"
4641 " --tty=TTY\t\tThe tty to use\n"
4642 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004643#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004644
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004645#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004646 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004647 "Options for x11-backend.so:\n\n"
4648 " --width=WIDTH\t\tWidth of X window\n"
4649 " --height=HEIGHT\tHeight of X window\n"
4650 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004651 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004652 " --output-count=COUNT\tCreate multiple outputs\n"
4653 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004654#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004655
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004656#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004657 fprintf(stderr,
4658 "Options for wayland-backend.so:\n\n"
4659 " --width=WIDTH\t\tWidth of Wayland surface\n"
4660 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004661 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004662 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004663 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004664 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004665 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004666 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004667#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004668
Pekka Paalanene31e0532013-05-22 18:03:07 +03004669#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4670 fprintf(stderr,
4671 "Options for rpi-backend.so:\n\n"
4672 " --tty=TTY\t\tThe tty to use\n"
4673 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4674 " --transform=TR\tThe output transformation, TR is one of:\n"
4675 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004676 " --opaque-regions\tEnable support for opaque regions, can be "
4677 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004678 "\n");
4679#endif
4680
Hardeningc077a842013-07-08 00:51:35 +02004681#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07004682 fprintf(stderr,
4683 "Options for rdp-backend.so:\n\n"
4684 " --width=WIDTH\t\tWidth of desktop\n"
4685 " --height=HEIGHT\tHeight of desktop\n"
4686 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4687 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07004688 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07004689 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
4690 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4691 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4692 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4693 "\n");
Hardeningc077a842013-07-08 00:51:35 +02004694#endif
4695
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08004696#if defined(BUILD_HEADLESS_COMPOSITOR)
4697 fprintf(stderr,
4698 "Options for headless-backend.so:\n\n"
4699 " --width=WIDTH\t\tWidth of memory surface\n"
4700 " --height=HEIGHT\tHeight of memory surface\n"
4701 " --transform=TR\tThe output transformation, TR is one of:\n"
4702 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
4703 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
4704#endif
4705
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004706 exit(error_code);
4707}
4708
Peter Maatmane5b42e42013-03-27 22:38:53 +01004709static void
4710catch_signals(void)
4711{
4712 struct sigaction action;
4713
4714 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4715 action.sa_sigaction = on_caught_signal;
4716 sigemptyset(&action.sa_mask);
4717 sigaction(SIGSEGV, &action, NULL);
4718 sigaction(SIGABRT, &action, NULL);
4719}
4720
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004721static void
4722handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4723{
4724 struct wl_client *client = data;
4725
4726 weston_log("Primary client died. Closing...\n");
4727
4728 wl_display_terminate(wl_client_get_display(client));
4729}
4730
Ryo Munakatad8deff62014-09-06 07:32:05 +09004731static char *
4732weston_choose_default_backend(void)
4733{
4734 char *backend = NULL;
4735
4736 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4737 backend = strdup("wayland-backend.so");
4738 else if (getenv("DISPLAY"))
4739 backend = strdup("x11-backend.so");
4740 else
4741 backend = strdup(WESTON_NATIVE_BACKEND);
4742
4743 return backend;
4744}
4745
4746static int
4747weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4748{
4749 if (socket_name) {
4750 if (wl_display_add_socket(display, socket_name)) {
4751 weston_log("fatal: failed to add socket: %m\n");
4752 return -1;
4753 }
4754 } else {
4755 socket_name = wl_display_add_socket_auto(display);
4756 if (!socket_name) {
4757 weston_log("fatal: failed to add socket: %m\n");
4758 return -1;
4759 }
4760 }
4761
4762 setenv("WAYLAND_DISPLAY", socket_name, 1);
4763
4764 return 0;
4765}
4766
Derek Foreman64a3df02014-10-23 12:24:18 -05004767static const struct { const char *name; uint32_t token; } transforms[] = {
4768 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
4769 { "90", WL_OUTPUT_TRANSFORM_90 },
4770 { "180", WL_OUTPUT_TRANSFORM_180 },
4771 { "270", WL_OUTPUT_TRANSFORM_270 },
4772 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
4773 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
4774 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
4775 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
4776};
4777
4778WL_EXPORT int
4779weston_parse_transform(const char *transform, uint32_t *out)
4780{
4781 unsigned int i;
4782
4783 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4784 if (strcmp(transforms[i].name, transform) == 0) {
4785 *out = transforms[i].token;
4786 return 0;
4787 }
4788
4789 *out = WL_OUTPUT_TRANSFORM_NORMAL;
4790 return -1;
4791}
4792
4793WL_EXPORT const char *
4794weston_transform_to_string(uint32_t output_transform)
4795{
4796 unsigned int i;
4797
4798 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4799 if (transforms[i].token == output_transform)
4800 return transforms[i].name;
4801
4802 return "<illegal value>";
4803}
4804
4805
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004806int main(int argc, char *argv[])
4807{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004808 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004809 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004810 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004811 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004812 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004813 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004814 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004815 int *argc, char *argv[],
4816 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004817 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004818 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004819 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004820 char *modules = NULL;
4821 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004822 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004823 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04004824 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004825 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004826 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004827 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004828 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004829 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004830 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004831 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004832 struct wl_client *primary_client;
4833 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004834 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004835
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004836 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004837 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4838 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004839 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4840 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004841 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004842 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004843 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004844 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004845 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004846 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004847
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004848 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004849
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004850 if (help)
4851 usage(EXIT_SUCCESS);
4852
Scott Moreau12245142012-08-29 15:15:58 -06004853 if (version) {
4854 printf(PACKAGE_STRING "\n");
4855 return EXIT_SUCCESS;
4856 }
4857
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004858 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004859
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004860 weston_log("%s\n"
4861 STAMP_SPACE "%s\n"
4862 STAMP_SPACE "Bug reports to: %s\n"
4863 STAMP_SPACE "Build: %s\n",
4864 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004865 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004866 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004867
Martin Minarik37032f82012-06-18 20:15:18 +02004868 verify_xdg_runtime_dir();
4869
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004870 display = wl_display_create();
4871
Tiago Vignatti2116b892011-08-08 05:52:59 -07004872 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004873 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4874 display);
4875 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4876 display);
4877 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4878 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004879
4880 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004881 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4882 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004883
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304884 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4885 ret = EXIT_FAILURE;
4886 goto out_signals;
4887 }
4888
Pekka Paalanen588bee12014-05-07 16:26:25 +03004889 if (noconfig == 0)
4890 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004891 if (config != NULL) {
4892 weston_log("Using config file '%s'\n",
4893 weston_config_get_full_path(config));
4894 } else {
4895 weston_log("Starting with no config file.\n");
4896 }
4897 section = weston_config_get_section(config, "core", NULL, NULL);
4898
Ryo Munakata03faed22014-09-06 07:32:51 +09004899 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004900 weston_config_section_get_string(section, "backend", &backend,
4901 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09004902 if (!backend)
4903 backend = weston_choose_default_backend();
4904 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004905
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004906 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304907 if (!backend_init) {
4908 ret = EXIT_FAILURE;
4909 goto out_signals;
4910 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004911
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004912 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004913 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004914 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304915 ret = EXIT_FAILURE;
4916 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004917 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004918
Peter Maatmane5b42e42013-03-27 22:38:53 +01004919 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004920 segv_compositor = ec;
4921
Frederic Plourde4a84c832014-10-30 15:06:34 -04004922 if (idle_time < 0)
4923 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
4924 if (idle_time < 0)
4925 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004926 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004927 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04004928 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004929
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004930 for (i = 1; i < argc; i++)
4931 weston_log("fatal: unhandled option: %s\n", argv[i]);
4932 if (argc > 1) {
4933 ret = EXIT_FAILURE;
4934 goto out;
4935 }
4936
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004937 weston_compositor_log_capabilities(ec);
4938
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004939 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4940 if (server_socket) {
4941 weston_log("Running with single client\n");
4942 fd = strtol(server_socket, &end, 0);
4943 if (*end != '\0')
4944 fd = -1;
4945 } else {
4946 fd = -1;
4947 }
4948
4949 if (fd != -1) {
4950 primary_client = wl_client_create(display, fd);
4951 if (!primary_client) {
4952 weston_log("fatal: failed to add client: %m\n");
4953 ret = EXIT_FAILURE;
4954 goto out;
4955 }
4956 primary_client_destroyed.notify =
4957 handle_primary_client_destroyed;
4958 wl_client_add_destroy_listener(primary_client,
4959 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09004960 } else if (weston_create_listening_socket(display, socket_name)) {
4961 ret = EXIT_FAILURE;
4962 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004963 }
4964
Ryo Munakata03faed22014-09-06 07:32:51 +09004965 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004966 weston_config_section_get_string(section, "shell", &shell,
4967 "desktop-shell.so");
4968
Ryo Munakata03faed22014-09-06 07:32:51 +09004969 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004970 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004971
4972 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09004973 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004974 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004975
4976 if (load_modules(ec, option_modules, &argc, argv) < 0)
4977 goto out;
4978
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004979 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4980 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4981 if (numlock_on) {
4982 wl_list_for_each(seat, &ec->seat_list, link) {
4983 if (seat->keyboard)
4984 weston_keyboard_set_locks(seat->keyboard,
4985 WESTON_NUM_LOCK,
4986 WESTON_NUM_LOCK);
4987 }
4988 }
4989
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004990 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004991
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004992 wl_display_run(display);
4993
Frederic Plourdec336f062014-10-29 14:44:33 -04004994 /* Allow for setting return exit code after
4995 * wl_display_run returns normally. This is
4996 * useful for devs/testers and automated tests
4997 * that want to indicate failure status to
4998 * testing infrastructure above
4999 */
5000 ret = ec->exit_code;
5001
Ryo Munakata03faed22014-09-06 07:32:51 +09005002out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005003 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005004 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005005
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005006 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005007
Daniel Stone855028f2012-05-01 20:37:10 +01005008 weston_compositor_xkb_destroy(ec);
5009
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005010 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305011
5012out_signals:
5013 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
5014 if (signals[i])
5015 wl_event_source_remove(signals[i]);
5016
Tiago Vignatti9e2be082011-12-19 00:04:46 +02005017 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005018
Martin Minarik19e6f262012-06-07 13:08:46 +02005019 weston_log_file_close();
5020
Ryo Munakata03faed22014-09-06 07:32:51 +09005021 free(backend);
5022 free(shell);
5023 free(socket_name);
5024 free(option_modules);
5025 free(log);
5026 free(modules);
5027
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005028 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005029}