blob: 6ca297add616eb79c05ef7997d97ba65b4c20090 [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 Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 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>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050049
Marcin Slusarz554a0da2013-02-18 13:27:22 -050050#ifdef HAVE_LIBUNWIND
51#define UNW_LOCAL_ONLY
52#include <libunwind.h>
53#endif
54
Kristian Høgsberg82863022010-06-04 21:52:02 -040055#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010056#include "scaler-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030057#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040058#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050059#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060
Kristian Høgsberg27da5382011-06-21 17:32:25 -040061static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040062static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063
64static int
65sigchld_handler(int signal_number, void *data)
66{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040068 int status;
69 pid_t pid;
70
Bill Spitzak027b9622012-03-17 15:22:03 -070071 pid = waitpid(-1, &status, WNOHANG);
72 if (!pid)
73 return 1;
74
Kristian Høgsberg27da5382011-06-21 17:32:25 -040075 wl_list_for_each(p, &child_process_list, link) {
76 if (p->pid == pid)
77 break;
78 }
79
80 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020081 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040082 return 1;
83 }
84
85 wl_list_remove(&p->link);
86 p->cleanup(p, status);
87
88 return 1;
89}
90
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020091static void
Alexander Larsson0b135062013-05-28 16:23:36 +020092weston_output_transform_scale_init(struct weston_output *output,
93 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094
Rob Bradford27b17932013-06-26 18:08:46 +010095static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050096weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010097
Alex Wu2dda6042012-04-17 17:20:47 +080098WL_EXPORT int
Hardening57388e42013-09-18 23:56:36 +020099weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
100 int32_t scale, enum weston_mode_switch_op op)
Alex Wu2dda6042012-04-17 17:20:47 +0800101{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200102 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200103 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200104 pixman_region32_t old_output_region;
Hardening57388e42013-09-18 23:56:36 +0200105 int ret, notify_mode_changed, notify_scale_changed;
106 int temporary_mode, temporary_scale;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200107
Alex Wu2dda6042012-04-17 17:20:47 +0800108 if (!output->switch_mode)
109 return -1;
110
Hardening57388e42013-09-18 23:56:36 +0200111 temporary_mode = (output->original_mode != 0);
112 temporary_scale = (output->current_scale != output->original_scale);
113 ret = 0;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200114
Hardening57388e42013-09-18 23:56:36 +0200115 notify_mode_changed = 0;
116 notify_scale_changed = 0;
117 switch(op) {
118 case WESTON_MODE_SWITCH_SET_NATIVE:
119 output->native_mode = mode;
120 if (!temporary_mode) {
121 notify_mode_changed = 1;
122 ret = output->switch_mode(output, mode);
123 if (ret < 0)
124 return ret;
125 }
126
127 output->native_scale = scale;
128 if(!temporary_scale)
129 notify_scale_changed = 1;
130 break;
131 case WESTON_MODE_SWITCH_SET_TEMPORARY:
132 if (!temporary_mode)
133 output->original_mode = output->native_mode;
134 if (!temporary_scale)
135 output->original_scale = output->native_scale;
136
137 ret = output->switch_mode(output, mode);
138 if (ret < 0)
139 return ret;
140
Hardening57388e42013-09-18 23:56:36 +0200141 output->current_scale = scale;
142 break;
143 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
144 if (!temporary_mode) {
145 weston_log("already in the native mode\n");
146 return -1;
147 }
148
149 notify_mode_changed = (output->original_mode != output->native_mode);
150
151 ret = output->switch_mode(output, mode);
152 if (ret < 0)
153 return ret;
154
155 if (output->original_scale != output->native_scale) {
156 notify_scale_changed = 1;
157 scale = output->native_scale;
158 output->original_scale = scale;
159 }
160 output->original_mode = 0;
161
162 output->current_scale = output->native_scale;
163 break;
164 default:
165 weston_log("unknown weston_switch_mode_op %d\n", op);
166 break;
167 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200168
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200169 pixman_region32_init(&old_output_region);
170 pixman_region32_copy(&old_output_region, &output->region);
171
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200172 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200173 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200174
175 pixman_region32_init(&output->previous_damage);
176 pixman_region32_init_rect(&output->region, output->x, output->y,
177 output->width, output->height);
178
179 weston_output_update_matrix(output);
180
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200181 /* If a pointer falls outside the outputs new geometry, move it to its
182 * lower-right corner */
183 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400184 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200185 int32_t x, y;
186
187 if (!pointer)
188 continue;
189
190 x = wl_fixed_to_int(pointer->x);
191 y = wl_fixed_to_int(pointer->y);
192
193 if (!pixman_region32_contains_point(&old_output_region,
194 x, y, NULL) ||
195 pixman_region32_contains_point(&output->region,
196 x, y, NULL))
197 continue;
198
199 if (x >= output->x + output->width)
200 x = output->x + output->width - 1;
201 if (y >= output->y + output->height)
202 y = output->y + output->height - 1;
203
204 pointer->x = wl_fixed_from_int(x);
205 pointer->y = wl_fixed_from_int(y);
206 }
207
208 pixman_region32_fini(&old_output_region);
209
Hardening57388e42013-09-18 23:56:36 +0200210 /* notify clients of the changes */
211 if (notify_mode_changed || notify_scale_changed) {
212 wl_resource_for_each(resource, &output->resource_list) {
213 if(notify_mode_changed) {
214 wl_output_send_mode(resource,
215 mode->flags | WL_OUTPUT_MODE_CURRENT,
216 mode->width,
217 mode->height,
218 mode->refresh);
219 }
220
221 if (notify_scale_changed)
222 wl_output_send_scale(resource, scale);
223
224 if (wl_resource_get_version(resource) >= 2)
225 wl_output_send_done(resource);
226 }
227 }
228
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200229 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800230}
231
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400232WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500233weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400234{
235 wl_list_insert(&child_process_list, &process->link);
236}
237
Benjamin Franzke06286262011-05-06 19:12:33 +0200238static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200239child_client_exec(int sockfd, const char *path)
240{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500241 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200243 sigset_t allsigs;
244
245 /* do not give our signal mask to the new process */
246 sigfillset(&allsigs);
247 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200248
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100249 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
250 if (seteuid(getuid()) == -1) {
251 weston_log("compositor: failed seteuid\n");
252 return;
253 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500254
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500255 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
256 * non-CLOEXEC fd to pass through exec. */
257 clientfd = dup(sockfd);
258 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200259 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500260 return;
261 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200262
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500263 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200264 setenv("WAYLAND_SOCKET", s, 1);
265
266 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200267 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200268 path);
269}
270
271WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272weston_client_launch(struct weston_compositor *compositor,
273 struct weston_process *proc,
274 const char *path,
275 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200276{
277 int sv[2];
278 pid_t pid;
279 struct wl_client *client;
280
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300281 weston_log("launching '%s'\n", path);
282
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300283 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200284 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200285 "socketpair failed while launching '%s': %m\n",
286 path);
287 return NULL;
288 }
289
290 pid = fork();
291 if (pid == -1) {
292 close(sv[0]);
293 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200295 "fork failed while launching '%s': %m\n", path);
296 return NULL;
297 }
298
299 if (pid == 0) {
300 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700301 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200302 }
303
304 close(sv[1]);
305
306 client = wl_client_create(compositor->wl_display, sv[0]);
307 if (!client) {
308 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200310 "wl_client_create failed while launching '%s'.\n",
311 path);
312 return NULL;
313 }
314
315 proc->pid = pid;
316 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200318
319 return client;
320}
321
322static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300323surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
324{
325 struct weston_surface *surface =
326 container_of(listener, struct weston_surface,
327 pending.buffer_destroy_listener);
328
329 surface->pending.buffer = NULL;
330}
331
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200332static void
333empty_region(pixman_region32_t *region)
334{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300335 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200336 pixman_region32_init(region);
337}
338
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300339static void
340region_init_infinite(pixman_region32_t *region)
341{
342 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
343 UINT32_MAX, UINT32_MAX);
344}
345
Pekka Paalanene67858b2013-04-25 13:57:42 +0300346static struct weston_subsurface *
347weston_surface_to_subsurface(struct weston_surface *surface);
348
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200349static void
350weston_view_output_move_handler(struct wl_listener *listener,
351 void *data)
352{
353 struct weston_view *ev;
354 struct weston_output *output = data;
355
356 ev = container_of(listener, struct weston_view,
357 output_move_listener);
358
359 /* the child window's view->geometry is a relative coordinate to
360 * parent view, no need to move child_view. */
361 if (ev->geometry.parent)
362 return;
363
364 weston_view_set_position(ev,
365 ev->geometry.x + output->move_x,
366 ev->geometry.y + output->move_y);
367}
368
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200369static void
370weston_view_output_destroy_handler(struct wl_listener *listener,
371 void *data)
372{
373 struct weston_view *ev;
374 struct weston_compositor *ec;
375 struct weston_output *output, *first_output;
376 float x, y;
377 int visible;
378
379 ev = container_of(listener, struct weston_view,
380 output_destroy_listener);
381
382 ec = ev->surface->compositor;
383
384 /* the child window's view->geometry is a relative coordinate to
385 * parent view, no need to move child_view. */
386 if (ev->geometry.parent)
387 return;
388
389 x = ev->geometry.x;
390 y = ev->geometry.y;
391
392 /* At this point the destroyed output is not in the list anymore.
393 * If the view is still visible somewhere, we leave where it is,
394 * otherwise, move it to the first output. */
395 visible = 0;
396 wl_list_for_each(output, &ec->output_list, link) {
397 if (pixman_region32_contains_point(&output->region,
398 x, y, NULL)) {
399 visible = 1;
400 break;
401 }
402 }
403
404 if (!visible) {
405 first_output = container_of(ec->output_list.next,
406 struct weston_output, link);
407
408 x = first_output->x + first_output->width / 4;
409 y = first_output->y + first_output->height / 4;
410 }
411
412 weston_view_set_position(ev, x, y);
Zhang, Xiong Y31236932013-12-13 22:10:57 +0200413
414 if (ev->surface->output_destroyed)
415 ev->surface->output_destroyed(ev->surface);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200416}
417
Jason Ekstranda7af7042013-10-12 22:38:11 -0500418WL_EXPORT struct weston_view *
419weston_view_create(struct weston_surface *surface)
420{
421 struct weston_view *view;
422
423 view = calloc(1, sizeof *view);
424 if (view == NULL)
425 return NULL;
426
427 view->surface = surface;
428
Jason Ekstranda7af7042013-10-12 22:38:11 -0500429 /* Assign to surface */
430 wl_list_insert(&surface->views, &view->surface_link);
431
432 wl_signal_init(&view->destroy_signal);
433 wl_list_init(&view->link);
434 wl_list_init(&view->layer_link);
435
Xiong Zhang97116532013-10-23 13:58:31 +0800436 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500437
438 pixman_region32_init(&view->clip);
439
440 view->alpha = 1.0;
441 pixman_region32_init(&view->transform.opaque);
442
443 wl_list_init(&view->geometry.transformation_list);
444 wl_list_insert(&view->geometry.transformation_list,
445 &view->transform.position.link);
446 weston_matrix_init(&view->transform.position.matrix);
447 wl_list_init(&view->geometry.child_list);
448 pixman_region32_init(&view->transform.boundingbox);
449 view->transform.dirty = 1;
450
451 view->output = NULL;
452
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200453 view->output_move_listener.notify = weston_view_output_move_handler;
Kristian Høgsbergdf42a802013-12-17 14:58:19 -0800454 wl_list_init(&view->output_move_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200455 view->output_destroy_listener.notify =
456 weston_view_output_destroy_handler;
Kristian Høgsbergdf42a802013-12-17 14:58:19 -0800457 wl_list_init(&view->output_destroy_listener.link);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200458
Jason Ekstranda7af7042013-10-12 22:38:11 -0500459 return view;
460}
461
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500462WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500463weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500464{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500465 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400466
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200467 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400468 if (surface == NULL)
469 return NULL;
470
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500471 wl_signal_init(&surface->destroy_signal);
472
473 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500474
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500475 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200476 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400477
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100478 surface->buffer_viewport.transform = WL_OUTPUT_TRANSFORM_NORMAL;
479 surface->buffer_viewport.scale = 1;
Jonny Lamb74130762013-11-26 18:19:46 +0100480 surface->buffer_viewport.scaler_set = 0;
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100481 surface->pending.buffer_viewport = surface->buffer_viewport;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400482 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100483 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200484
Jonny Lamb74130762013-11-26 18:19:46 +0100485 surface->surface_scaler_resource = NULL;
486
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400487 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500488 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300489 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400490
Jason Ekstranda7af7042013-10-12 22:38:11 -0500491 wl_list_init(&surface->views);
492
493 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500494
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300495 surface->pending.buffer_destroy_listener.notify =
496 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300497 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300498 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300499 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300500 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300501
Pekka Paalanene67858b2013-04-25 13:57:42 +0300502 wl_list_init(&surface->subsurface_list);
503 wl_list_init(&surface->subsurface_list_pending);
504
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400505 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500506}
507
Alex Wu8811bf92012-02-28 18:07:54 +0800508WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500509weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200510 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500511{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100512 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500513}
514
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400515WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500516weston_view_to_global_float(struct weston_view *view,
517 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200518{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500519 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200520 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
521
Jason Ekstranda7af7042013-10-12 22:38:11 -0500522 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200523
524 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200525 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700526 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200527 v.f[3]);
528 *x = 0;
529 *y = 0;
530 return;
531 }
532
533 *x = v.f[0] / v.f[3];
534 *y = v.f[1] / v.f[3];
535 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500536 *x = sx + view->geometry.x;
537 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200538 }
539}
540
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500541WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200542weston_transformed_coord(int width, int height,
543 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200544 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200545 float sx, float sy, float *bx, float *by)
546{
547 switch (transform) {
548 case WL_OUTPUT_TRANSFORM_NORMAL:
549 default:
550 *bx = sx;
551 *by = sy;
552 break;
553 case WL_OUTPUT_TRANSFORM_FLIPPED:
554 *bx = width - sx;
555 *by = sy;
556 break;
557 case WL_OUTPUT_TRANSFORM_90:
558 *bx = height - sy;
559 *by = sx;
560 break;
561 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
562 *bx = height - sy;
563 *by = width - sx;
564 break;
565 case WL_OUTPUT_TRANSFORM_180:
566 *bx = width - sx;
567 *by = height - sy;
568 break;
569 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
570 *bx = sx;
571 *by = height - sy;
572 break;
573 case WL_OUTPUT_TRANSFORM_270:
574 *bx = sy;
575 *by = width - sx;
576 break;
577 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
578 *bx = sy;
579 *by = sx;
580 break;
581 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200582
583 *bx *= scale;
584 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200585}
586
587WL_EXPORT pixman_box32_t
588weston_transformed_rect(int width, int height,
589 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200590 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200591 pixman_box32_t rect)
592{
593 float x1, x2, y1, y2;
594
595 pixman_box32_t ret;
596
Alexander Larsson4ea95522013-05-22 14:41:37 +0200597 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200598 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200599 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200600 rect.x2, rect.y2, &x2, &y2);
601
602 if (x1 <= x2) {
603 ret.x1 = x1;
604 ret.x2 = x2;
605 } else {
606 ret.x1 = x2;
607 ret.x2 = x1;
608 }
609
610 if (y1 <= y2) {
611 ret.y1 = y1;
612 ret.y2 = y2;
613 } else {
614 ret.y1 = y2;
615 ret.y2 = y1;
616 }
617
618 return ret;
619}
620
621WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500622weston_transformed_region(int width, int height,
623 enum wl_output_transform transform,
624 int32_t scale,
625 pixman_region32_t *src, pixman_region32_t *dest)
626{
627 pixman_box32_t *src_rects, *dest_rects;
628 int nrects, i;
629
630 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
631 if (src != dest)
632 pixman_region32_copy(dest, src);
633 return;
634 }
635
636 src_rects = pixman_region32_rectangles(src, &nrects);
637 dest_rects = malloc(nrects * sizeof(*dest_rects));
638 if (!dest_rects)
639 return;
640
641 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
642 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
643 } else {
644 for (i = 0; i < nrects; i++) {
645 switch (transform) {
646 default:
647 case WL_OUTPUT_TRANSFORM_NORMAL:
648 dest_rects[i].x1 = src_rects[i].x1;
649 dest_rects[i].y1 = src_rects[i].y1;
650 dest_rects[i].x2 = src_rects[i].x2;
651 dest_rects[i].y2 = src_rects[i].y2;
652 break;
653 case WL_OUTPUT_TRANSFORM_90:
654 dest_rects[i].x1 = height - src_rects[i].y2;
655 dest_rects[i].y1 = src_rects[i].x1;
656 dest_rects[i].x2 = height - src_rects[i].y1;
657 dest_rects[i].y2 = src_rects[i].x2;
658 break;
659 case WL_OUTPUT_TRANSFORM_180:
660 dest_rects[i].x1 = width - src_rects[i].x2;
661 dest_rects[i].y1 = height - src_rects[i].y2;
662 dest_rects[i].x2 = width - src_rects[i].x1;
663 dest_rects[i].y2 = height - src_rects[i].y1;
664 break;
665 case WL_OUTPUT_TRANSFORM_270:
666 dest_rects[i].x1 = src_rects[i].y1;
667 dest_rects[i].y1 = width - src_rects[i].x2;
668 dest_rects[i].x2 = src_rects[i].y2;
669 dest_rects[i].y2 = width - src_rects[i].x1;
670 break;
671 case WL_OUTPUT_TRANSFORM_FLIPPED:
672 dest_rects[i].x1 = width - src_rects[i].x2;
673 dest_rects[i].y1 = src_rects[i].y1;
674 dest_rects[i].x2 = width - src_rects[i].x1;
675 dest_rects[i].y2 = src_rects[i].y2;
676 break;
677 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
678 dest_rects[i].x1 = height - src_rects[i].y2;
679 dest_rects[i].y1 = width - src_rects[i].x2;
680 dest_rects[i].x2 = height - src_rects[i].y1;
681 dest_rects[i].y2 = width - src_rects[i].x1;
682 break;
683 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
684 dest_rects[i].x1 = src_rects[i].x1;
685 dest_rects[i].y1 = height - src_rects[i].y2;
686 dest_rects[i].x2 = src_rects[i].x2;
687 dest_rects[i].y2 = height - src_rects[i].y1;
688 break;
689 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
690 dest_rects[i].x1 = src_rects[i].y1;
691 dest_rects[i].y1 = src_rects[i].x1;
692 dest_rects[i].x2 = src_rects[i].y2;
693 dest_rects[i].y2 = src_rects[i].x2;
694 break;
695 }
696 }
697 }
698
699 if (scale != 1) {
700 for (i = 0; i < nrects; i++) {
701 dest_rects[i].x1 *= scale;
702 dest_rects[i].x2 *= scale;
703 dest_rects[i].y1 *= scale;
704 dest_rects[i].y2 *= scale;
705 }
706 }
707
708 pixman_region32_clear(dest);
709 pixman_region32_init_rects(dest, dest_rects, nrects);
710 free(dest_rects);
711}
712
Jonny Lamb74130762013-11-26 18:19:46 +0100713static void
714scaler_surface_to_buffer(struct weston_surface *surface,
715 float sx, float sy, float *bx, float *by)
716{
717 if (surface->buffer_viewport.scaler_set) {
718 double a, b;
719
720 a = sx / surface->buffer_viewport.dst_width;
721 b = a * wl_fixed_to_double(surface->buffer_viewport.src_width);
722 *bx = b + wl_fixed_to_double(surface->buffer_viewport.src_x);
723
724 a = sy / surface->buffer_viewport.dst_height;
725 b = a * wl_fixed_to_double(surface->buffer_viewport.src_height);
726 *by = b + wl_fixed_to_double(surface->buffer_viewport.src_y);
727 } else {
728 *bx = sx;
729 *by = sy;
730 }
731}
732
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500733WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200734weston_surface_to_buffer_float(struct weston_surface *surface,
735 float sx, float sy, float *bx, float *by)
736{
Jonny Lamb74130762013-11-26 18:19:46 +0100737 /* first transform coordinates if the scaler is set */
738 scaler_surface_to_buffer(surface, sx, sy, bx, by);
739
Jason Ekstranda7af7042013-10-12 22:38:11 -0500740 weston_transformed_coord(surface->width,
741 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100742 surface->buffer_viewport.transform,
743 surface->buffer_viewport.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100744 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200745}
746
Alexander Larsson4ea95522013-05-22 14:41:37 +0200747WL_EXPORT void
748weston_surface_to_buffer(struct weston_surface *surface,
749 int sx, int sy, int *bx, int *by)
750{
751 float bxf, byf;
752
Jonny Lamb74130762013-11-26 18:19:46 +0100753 weston_surface_to_buffer_float(surface,
754 sx, sy, &bxf, &byf);
755
Alexander Larsson4ea95522013-05-22 14:41:37 +0200756 *bx = floorf(bxf);
757 *by = floorf(byf);
758}
759
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200760WL_EXPORT pixman_box32_t
761weston_surface_to_buffer_rect(struct weston_surface *surface,
762 pixman_box32_t rect)
763{
Jonny Lamb74130762013-11-26 18:19:46 +0100764 float xf, yf;
765
766 /* first transform box coordinates if the scaler is set */
767 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
768 rect.x1 = floorf(xf);
769 rect.y1 = floorf(yf);
770
771 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
772 rect.x2 = floorf(xf);
773 rect.y2 = floorf(yf);
774
Jason Ekstranda7af7042013-10-12 22:38:11 -0500775 return weston_transformed_rect(surface->width,
776 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100777 surface->buffer_viewport.transform,
778 surface->buffer_viewport.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200779 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200780}
781
782WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500783weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400784 struct weston_plane *plane)
785{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500786 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400787 return;
788
Jason Ekstranda7af7042013-10-12 22:38:11 -0500789 weston_view_damage_below(view);
790 view->plane = plane;
791 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400792}
793
794WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500795weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200796{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500797 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200798
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500799 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500800 pixman_region32_subtract(&damage, &view->transform.boundingbox,
801 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800802 if (view->plane)
803 pixman_region32_union(&view->plane->damage,
804 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500805 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800806 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200807}
808
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400809static void
810weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
811{
812 uint32_t different = es->output_mask ^ mask;
813 uint32_t entered = mask & different;
814 uint32_t left = es->output_mask & different;
815 struct weston_output *output;
816 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500817 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400818
819 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500820 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400821 return;
822 if (different == 0)
823 return;
824
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500825 client = wl_resource_get_client(es->resource);
826
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400827 wl_list_for_each(output, &es->compositor->output_list, link) {
828 if (1 << output->id & different)
829 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500830 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400831 client);
832 if (resource == NULL)
833 continue;
834 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500835 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400836 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500837 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400838 }
839}
840
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200841
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400842static void
843weston_surface_assign_output(struct weston_surface *es)
844{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500845 struct weston_output *new_output;
846 struct weston_view *view;
847 pixman_region32_t region;
848 uint32_t max, area, mask;
849 pixman_box32_t *e;
850
851 new_output = NULL;
852 max = 0;
853 mask = 0;
854 pixman_region32_init(&region);
855 wl_list_for_each(view, &es->views, surface_link) {
856 if (!view->output)
857 continue;
858
859 pixman_region32_intersect(&region, &view->transform.boundingbox,
860 &view->output->region);
861
862 e = pixman_region32_extents(&region);
863 area = (e->x2 - e->x1) * (e->y2 - e->y1);
864
865 mask |= view->output_mask;
866
867 if (area >= max) {
868 new_output = view->output;
869 max = area;
870 }
871 }
872 pixman_region32_fini(&region);
873
874 es->output = new_output;
875 weston_surface_update_output_mask(es, mask);
876}
877
878static void
879weston_view_assign_output(struct weston_view *ev)
880{
881 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400882 struct weston_output *output, *new_output;
883 pixman_region32_t region;
884 uint32_t max, area, mask;
885 pixman_box32_t *e;
886
887 new_output = NULL;
888 max = 0;
889 mask = 0;
890 pixman_region32_init(&region);
891 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500892 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400893 &output->region);
894
895 e = pixman_region32_extents(&region);
896 area = (e->x2 - e->x1) * (e->y2 - e->y1);
897
898 if (area > 0)
899 mask |= 1 << output->id;
900
901 if (area >= max) {
902 new_output = output;
903 max = area;
904 }
905 }
906 pixman_region32_fini(&region);
907
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200908 if (ev->output_mask != 0) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200909 wl_list_remove(&ev->output_move_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200910 wl_list_remove(&ev->output_destroy_listener.link);
911 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200912
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200913 if (mask != 0) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200914 wl_signal_add(&new_output->move_signal,
915 &ev->output_move_listener);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200916 wl_signal_add(&new_output->destroy_signal,
917 &ev->output_destroy_listener);
918 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200919
Jason Ekstranda7af7042013-10-12 22:38:11 -0500920 ev->output = new_output;
921 ev->output_mask = mask;
922
923 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400924}
925
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200926static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500927view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
928 int32_t width, int32_t height,
929 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200930{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200931 float min_x = HUGE_VALF, min_y = HUGE_VALF;
932 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200933 int32_t s[4][2] = {
934 { sx, sy },
935 { sx, sy + height },
936 { sx + width, sy },
937 { sx + width, sy + height }
938 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200939 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200940 int i;
941
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300942 if (width == 0 || height == 0) {
943 /* avoid rounding empty bbox to 1x1 */
944 pixman_region32_init(bbox);
945 return;
946 }
947
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200948 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200949 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500950 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200951 if (x < min_x)
952 min_x = x;
953 if (x > max_x)
954 max_x = x;
955 if (y < min_y)
956 min_y = y;
957 if (y > max_y)
958 max_y = y;
959 }
960
Pekka Paalanen219b9822012-02-08 15:38:37 +0200961 int_x = floorf(min_x);
962 int_y = floorf(min_y);
963 pixman_region32_init_rect(bbox, int_x, int_y,
964 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200965}
966
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200967static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200969{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500970 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200971
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200972 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500973 view->geometry.x = roundf(view->geometry.x);
974 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200975
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500976 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
978 view->transform.position.matrix.d[12] = view->geometry.x;
979 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500980
Jason Ekstranda7af7042013-10-12 22:38:11 -0500981 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500982
Jason Ekstranda7af7042013-10-12 22:38:11 -0500983 view->transform.inverse = view->transform.position.matrix;
984 view->transform.inverse.d[12] = -view->geometry.x;
985 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500986
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 pixman_region32_init_rect(&view->transform.boundingbox,
988 view->geometry.x,
989 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600990 view->surface->width,
991 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500992
Jason Ekstranda7af7042013-10-12 22:38:11 -0500993 if (view->alpha == 1.0) {
994 pixman_region32_copy(&view->transform.opaque,
995 &view->surface->opaque);
996 pixman_region32_translate(&view->transform.opaque,
997 view->geometry.x,
998 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500999 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001000}
1001
1002static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001004{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 struct weston_view *parent = view->geometry.parent;
1006 struct weston_matrix *matrix = &view->transform.matrix;
1007 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001008 struct weston_transform *tform;
1009
Jason Ekstranda7af7042013-10-12 22:38:11 -05001010 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001011
1012 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1014 view->transform.position.matrix.d[12] = view->geometry.x;
1015 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001016
1017 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001018 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001019 weston_matrix_multiply(matrix, &tform->matrix);
1020
Pekka Paalanen483243f2013-03-08 14:56:50 +02001021 if (parent)
1022 weston_matrix_multiply(matrix, &parent->transform.matrix);
1023
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001024 if (weston_matrix_invert(inverse, matrix) < 0) {
1025 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001026 weston_log("error: weston_view %p"
1027 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001028 return -1;
1029 }
1030
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001031 view_compute_bbox(view, 0, 0,
1032 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001033 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001034
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001035 return 0;
1036}
1037
1038WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001039weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001040{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001042
Jason Ekstranda7af7042013-10-12 22:38:11 -05001043 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001044 return;
1045
Pekka Paalanen483243f2013-03-08 14:56:50 +02001046 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001047 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001048
Jason Ekstranda7af7042013-10-12 22:38:11 -05001049 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001050
Jason Ekstranda7af7042013-10-12 22:38:11 -05001051 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001052
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053 pixman_region32_fini(&view->transform.boundingbox);
1054 pixman_region32_fini(&view->transform.opaque);
1055 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001056
Pekka Paalanencd403622012-01-25 13:37:39 +02001057 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001058 if (view->geometry.transformation_list.next ==
1059 &view->transform.position.link &&
1060 view->geometry.transformation_list.prev ==
1061 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001062 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001063 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001064 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001065 if (weston_view_update_transform_enable(view) < 0)
1066 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001067 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001068
Jason Ekstranda7af7042013-10-12 22:38:11 -05001069 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001070
Jason Ekstranda7af7042013-10-12 22:38:11 -05001071 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001072
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073 wl_signal_emit(&view->surface->compositor->transform_signal,
1074 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001075}
1076
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001077WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001078weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001079{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001081
1082 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001083 * The invariant: if view->geometry.dirty, then all views
1084 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001085 * Corollary: if not parent->geometry.dirty, then all ancestors
1086 * are not dirty.
1087 */
1088
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001090 return;
1091
Jason Ekstranda7af7042013-10-12 22:38:11 -05001092 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001093
Jason Ekstranda7af7042013-10-12 22:38:11 -05001094 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001095 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001096 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001097}
1098
1099WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100weston_view_to_global_fixed(struct weston_view *view,
1101 wl_fixed_t vx, wl_fixed_t vy,
1102 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001103{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001104 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001105
Jason Ekstranda7af7042013-10-12 22:38:11 -05001106 weston_view_to_global_float(view,
1107 wl_fixed_to_double(vx),
1108 wl_fixed_to_double(vy),
1109 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001110 *x = wl_fixed_from_double(xf);
1111 *y = wl_fixed_from_double(yf);
1112}
1113
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001114WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115weston_view_from_global_float(struct weston_view *view,
1116 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001117{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001118 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001119 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1120
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001122
1123 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001124 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001126 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001127 *vx = 0;
1128 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001129 return;
1130 }
1131
Jason Ekstranda7af7042013-10-12 22:38:11 -05001132 *vx = v.f[0] / v.f[3];
1133 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001134 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 *vx = x - view->geometry.x;
1136 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001137 }
1138}
1139
1140WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141weston_view_from_global_fixed(struct weston_view *view,
1142 wl_fixed_t x, wl_fixed_t y,
1143 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001144{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001146
Jason Ekstranda7af7042013-10-12 22:38:11 -05001147 weston_view_from_global_float(view,
1148 wl_fixed_to_double(x),
1149 wl_fixed_to_double(y),
1150 &vxf, &vyf);
1151 *vx = wl_fixed_from_double(vxf);
1152 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001153}
1154
1155WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156weston_view_from_global(struct weston_view *view,
1157 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001158{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001160
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1162 *vx = floorf(vxf);
1163 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001164}
1165
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001166WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001167weston_surface_schedule_repaint(struct weston_surface *surface)
1168{
1169 struct weston_output *output;
1170
1171 wl_list_for_each(output, &surface->compositor->output_list, link)
1172 if (surface->output_mask & (1 << output->id))
1173 weston_output_schedule_repaint(output);
1174}
1175
1176WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001177weston_view_schedule_repaint(struct weston_view *view)
1178{
1179 struct weston_output *output;
1180
1181 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1182 if (view->output_mask & (1 << output->id))
1183 weston_output_schedule_repaint(output);
1184}
1185
1186WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001187weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001188{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001189 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190 0, 0, surface->width,
1191 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001192
Kristian Høgsberg98238702012-08-03 16:29:12 -04001193 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001194}
1195
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001196WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001198{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001199 if (view->geometry.x == x && view->geometry.y == y)
1200 return;
1201
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202 view->geometry.x = x;
1203 view->geometry.y = y;
1204 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001205}
1206
Pekka Paalanen483243f2013-03-08 14:56:50 +02001207static void
1208transform_parent_handle_parent_destroy(struct wl_listener *listener,
1209 void *data)
1210{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001211 struct weston_view *view =
1212 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001213 geometry.parent_destroy_listener);
1214
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001216}
1217
1218WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219weston_view_set_transform_parent(struct weston_view *view,
1220 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001221{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 if (view->geometry.parent) {
1223 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1224 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001225 }
1226
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001228
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001230 transform_parent_handle_parent_destroy;
1231 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001232 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001234 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001236 }
1237
Jason Ekstranda7af7042013-10-12 22:38:11 -05001238 weston_view_geometry_dirty(view);
1239}
1240
1241WL_EXPORT int
1242weston_view_is_mapped(struct weston_view *view)
1243{
1244 if (view->output)
1245 return 1;
1246 else
1247 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001248}
1249
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001250WL_EXPORT int
1251weston_surface_is_mapped(struct weston_surface *surface)
1252{
1253 if (surface->output)
1254 return 1;
1255 else
1256 return 0;
1257}
1258
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001259static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001260surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001261{
1262 struct weston_view *view;
1263
1264 if (surface->width == width && surface->height == height)
1265 return;
1266
1267 surface->width = width;
1268 surface->height = height;
1269
1270 wl_list_for_each(view, &surface->views, surface_link)
1271 weston_view_geometry_dirty(view);
1272}
1273
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001274WL_EXPORT void
1275weston_surface_set_size(struct weston_surface *surface,
1276 int32_t width, int32_t height)
1277{
1278 assert(!surface->resource);
1279 surface_set_size(surface, width, height);
1280}
1281
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001282static void
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001283weston_surface_set_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001284{
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001285 int32_t width, height;
1286
1287 if (!surface->buffer_ref.buffer) {
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001288 surface_set_size(surface, 0, 0);
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001289 return;
1290 }
1291
Jonny Lamb74130762013-11-26 18:19:46 +01001292 if (surface->buffer_viewport.scaler_set) {
1293 surface->width = surface->buffer_viewport.dst_width;
1294 surface->height = surface->buffer_viewport.dst_height;
1295 return;
1296 }
1297
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001298 switch (surface->buffer_viewport.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001299 case WL_OUTPUT_TRANSFORM_90:
1300 case WL_OUTPUT_TRANSFORM_270:
1301 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1302 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001303 width = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001304 height = surface->buffer_ref.buffer->width;
1305 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001306 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001307 width = surface->buffer_ref.buffer->width;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001308 height = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001309 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001310 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001311
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001312 width = width / surface->buffer_viewport.scale;
1313 height = height / surface->buffer_viewport.scale;
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001314 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001315}
1316
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001317WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001318weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001319{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001320 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001321
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001322 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001323
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001324 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001325}
1326
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327WL_EXPORT struct weston_view *
1328weston_compositor_pick_view(struct weston_compositor *compositor,
1329 wl_fixed_t x, wl_fixed_t y,
1330 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001331{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001333
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334 wl_list_for_each(view, &compositor->view_list, link) {
1335 weston_view_from_global_fixed(view, x, y, vx, vy);
1336 if (pixman_region32_contains_point(&view->surface->input,
1337 wl_fixed_to_int(*vx),
1338 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001339 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001341 }
1342
1343 return NULL;
1344}
1345
1346static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001347weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001348{
Daniel Stone37816df2012-05-16 18:45:18 +01001349 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001350
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001351 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001352 return;
1353
Daniel Stone37816df2012-05-16 18:45:18 +01001354 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001355 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001356}
1357
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001358WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001360{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001361 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001362
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 if (!weston_view_is_mapped(view))
1364 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001365
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 weston_view_damage_below(view);
1367 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001368 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 wl_list_remove(&view->layer_link);
1370 wl_list_init(&view->layer_link);
1371 wl_list_remove(&view->link);
1372 wl_list_init(&view->link);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001373 wl_list_remove(&view->output_move_listener.link);
1374 wl_list_init(&view->output_move_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +02001375 wl_list_remove(&view->output_destroy_listener.link);
1376 wl_list_init(&view->output_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 view->output_mask = 0;
1378 weston_surface_assign_output(view->surface);
1379
1380 if (weston_surface_is_mapped(view->surface))
1381 return;
1382
1383 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1384 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001385 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001387 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001388 NULL,
1389 wl_fixed_from_int(0),
1390 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001392 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001393 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001394}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001395
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396WL_EXPORT void
1397weston_surface_unmap(struct weston_surface *surface)
1398{
1399 struct weston_view *view;
1400
1401 wl_list_for_each(view, &surface->views, surface_link)
1402 weston_view_unmap(view);
1403 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001404}
1405
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001406struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001407 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001408 struct wl_list link;
1409};
1410
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411WL_EXPORT void
1412weston_view_destroy(struct weston_view *view)
1413{
1414 wl_signal_emit(&view->destroy_signal, view);
1415
1416 assert(wl_list_empty(&view->geometry.child_list));
1417
1418 if (weston_view_is_mapped(view)) {
1419 weston_view_unmap(view);
1420 weston_compositor_build_view_list(view->surface->compositor);
1421 }
1422
1423 wl_list_remove(&view->link);
1424 wl_list_remove(&view->layer_link);
1425
1426 pixman_region32_fini(&view->clip);
1427 pixman_region32_fini(&view->transform.boundingbox);
1428
1429 weston_view_set_transform_parent(view, NULL);
1430
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431 wl_list_remove(&view->surface_link);
1432
1433 free(view);
1434}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001435
1436WL_EXPORT void
1437weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001438{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001439 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001441
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001442 if (--surface->ref_count > 0)
1443 return;
1444
1445 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1446
Pekka Paalanene67858b2013-04-25 13:57:42 +03001447 assert(wl_list_empty(&surface->subsurface_list_pending));
1448 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001449
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1451 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001452
Pekka Paalanenbc106382012-10-10 12:49:31 +03001453 wl_list_for_each_safe(cb, next,
1454 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001455 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001456
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001457 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001458 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001459 pixman_region32_fini(&surface->pending.damage);
1460
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001461 if (surface->pending.buffer)
1462 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1463
Pekka Paalanende685b82012-12-04 15:58:12 +02001464 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001465
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001466 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001467 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001468 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001469
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001470 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001471 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001472
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001473 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001474}
1475
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001476static void
1477destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001478{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001479 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001480
Giulio Camuffo0d379742013-11-15 22:06:15 +01001481 /* Set the resource to NULL, since we don't want to leave a
1482 * dangling pointer if the surface was refcounted and survives
1483 * the weston_surface_destroy() call. */
1484 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001485 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001486}
1487
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001488static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001489weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1490{
1491 struct weston_buffer *buffer =
1492 container_of(listener, struct weston_buffer, destroy_listener);
1493
1494 wl_signal_emit(&buffer->destroy_signal, buffer);
1495 free(buffer);
1496}
1497
1498struct weston_buffer *
1499weston_buffer_from_resource(struct wl_resource *resource)
1500{
1501 struct weston_buffer *buffer;
1502 struct wl_listener *listener;
1503
1504 listener = wl_resource_get_destroy_listener(resource,
1505 weston_buffer_destroy_handler);
1506
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001507 if (listener)
1508 return container_of(listener, struct weston_buffer,
1509 destroy_listener);
1510
1511 buffer = zalloc(sizeof *buffer);
1512 if (buffer == NULL)
1513 return NULL;
1514
1515 buffer->resource = resource;
1516 wl_signal_init(&buffer->destroy_signal);
1517 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001518 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001519 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001520
1521 return buffer;
1522}
1523
1524static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001525weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1526 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001527{
Pekka Paalanende685b82012-12-04 15:58:12 +02001528 struct weston_buffer_reference *ref =
1529 container_of(listener, struct weston_buffer_reference,
1530 destroy_listener);
1531
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001532 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001533 ref->buffer = NULL;
1534}
1535
1536WL_EXPORT void
1537weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001538 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001539{
1540 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001541 ref->buffer->busy_count--;
1542 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001543 assert(wl_resource_get_client(ref->buffer->resource));
1544 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001545 WL_BUFFER_RELEASE);
1546 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001547 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001548 }
1549
Pekka Paalanende685b82012-12-04 15:58:12 +02001550 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001551 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001552 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001553 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001554 }
1555
Pekka Paalanende685b82012-12-04 15:58:12 +02001556 ref->buffer = buffer;
1557 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1558}
1559
1560static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001561weston_surface_attach(struct weston_surface *surface,
1562 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001563{
1564 weston_buffer_reference(&surface->buffer_ref, buffer);
1565
Pekka Paalanena6421c42012-12-04 15:58:10 +02001566 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001567 if (weston_surface_is_mapped(surface))
1568 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001569 }
1570
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001571 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001572}
1573
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001574WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001575weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001576{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001577 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001578
1579 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001580 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001581}
1582
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001583WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001584weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001585{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001586 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001587
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001588 pixman_region32_union(&compositor->primary_plane.damage,
1589 &compositor->primary_plane.damage,
1590 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001591 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001592}
1593
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001594static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001595surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001596{
Pekka Paalanende685b82012-12-04 15:58:12 +02001597 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001598 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001599 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001600
Jason Ekstranda7af7042013-10-12 22:38:11 -05001601 empty_region(&surface->damage);
1602}
1603
1604static void
1605view_accumulate_damage(struct weston_view *view,
1606 pixman_region32_t *opaque)
1607{
1608 pixman_region32_t damage;
1609
1610 pixman_region32_init(&damage);
1611 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001612 pixman_box32_t *extents;
1613
Jason Ekstranda7af7042013-10-12 22:38:11 -05001614 extents = pixman_region32_extents(&view->surface->damage);
1615 view_compute_bbox(view, extents->x1, extents->y1,
1616 extents->x2 - extents->x1,
1617 extents->y2 - extents->y1,
1618 &damage);
1619 pixman_region32_translate(&damage,
1620 -view->plane->x,
1621 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001622 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001623 pixman_region32_copy(&damage, &view->surface->damage);
1624 pixman_region32_translate(&damage,
1625 view->geometry.x - view->plane->x,
1626 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001627 }
1628
Jason Ekstranda7af7042013-10-12 22:38:11 -05001629 pixman_region32_subtract(&damage, &damage, opaque);
1630 pixman_region32_union(&view->plane->damage,
1631 &view->plane->damage, &damage);
1632 pixman_region32_fini(&damage);
1633 pixman_region32_copy(&view->clip, opaque);
1634 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001635}
1636
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001637static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001638compositor_accumulate_damage(struct weston_compositor *ec)
1639{
1640 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001641 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001642 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001643
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001644 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001645
1646 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001647 pixman_region32_copy(&plane->clip, &clip);
1648
1649 pixman_region32_init(&opaque);
1650
Jason Ekstranda7af7042013-10-12 22:38:11 -05001651 wl_list_for_each(ev, &ec->view_list, link) {
1652 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001653 continue;
1654
Jason Ekstranda7af7042013-10-12 22:38:11 -05001655 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001656 }
1657
1658 pixman_region32_union(&clip, &clip, &opaque);
1659 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001660 }
1661
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001662 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001663
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 wl_list_for_each(ev, &ec->view_list, link)
1665 ev->surface->touched = 0;
1666
1667 wl_list_for_each(ev, &ec->view_list, link) {
1668 if (ev->surface->touched)
1669 continue;
1670 ev->surface->touched = 1;
1671
1672 surface_flush_damage(ev->surface);
1673
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001674 /* Both the renderer and the backend have seen the buffer
1675 * by now. If renderer needs the buffer, it has its own
1676 * reference set. If the backend wants to keep the buffer
1677 * around for migrating the surface into a non-primary plane
1678 * later, keep_buffer is true. Otherwise, drop the core
1679 * reference now, and allow early buffer release. This enables
1680 * clients to use single-buffering.
1681 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 if (!ev->surface->keep_buffer)
1683 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001684 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001685}
1686
1687static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001689{
1690 struct weston_subsurface *sub;
1691
Pekka Paalanene67858b2013-04-25 13:57:42 +03001692 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001693 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001694 continue;
1695
Jason Ekstranda7af7042013-10-12 22:38:11 -05001696 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1697 wl_list_init(&sub->surface->views);
1698
1699 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001700 }
1701}
1702
1703static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001704surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001705{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706 struct weston_subsurface *sub;
1707 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001708
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1710 if (sub->surface == surface)
1711 continue;
1712
1713 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1714 weston_view_destroy(view);
1715
1716 surface_free_unused_subsurface_views(sub->surface);
1717 }
1718}
1719
1720static void
1721view_list_add_subsurface_view(struct weston_compositor *compositor,
1722 struct weston_subsurface *sub,
1723 struct weston_view *parent)
1724{
1725 struct weston_subsurface *child;
1726 struct weston_view *view = NULL, *iv;
1727
1728 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1729 if (iv->geometry.parent == parent) {
1730 view = iv;
1731 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001732 }
1733 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001734
1735 if (view) {
1736 /* Put it back in the surface's list of views */
1737 wl_list_remove(&view->surface_link);
1738 wl_list_insert(&sub->surface->views, &view->surface_link);
1739 } else {
1740 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001741 weston_view_set_position(view,
1742 sub->position.x,
1743 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744 weston_view_set_transform_parent(view, parent);
1745 }
1746
1747 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001748
Pekka Paalanenb188e912013-11-19 14:03:35 +02001749 if (wl_list_empty(&sub->surface->subsurface_list)) {
1750 wl_list_insert(compositor->view_list.prev, &view->link);
1751 return;
1752 }
1753
1754 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1755 if (child->surface == sub->surface)
1756 wl_list_insert(compositor->view_list.prev, &view->link);
1757 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001758 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001759 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001760}
1761
1762static void
1763view_list_add(struct weston_compositor *compositor,
1764 struct weston_view *view)
1765{
1766 struct weston_subsurface *sub;
1767
1768 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001769
Pekka Paalanenb188e912013-11-19 14:03:35 +02001770 if (wl_list_empty(&view->surface->subsurface_list)) {
1771 wl_list_insert(compositor->view_list.prev, &view->link);
1772 return;
1773 }
1774
1775 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1776 if (sub->surface == view->surface)
1777 wl_list_insert(compositor->view_list.prev, &view->link);
1778 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001779 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001780 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001781}
1782
1783static void
1784weston_compositor_build_view_list(struct weston_compositor *compositor)
1785{
1786 struct weston_view *view;
1787 struct weston_layer *layer;
1788
1789 wl_list_for_each(layer, &compositor->layer_list, link)
1790 wl_list_for_each(view, &layer->view_list, layer_link)
1791 surface_stash_subsurface_views(view->surface);
1792
1793 wl_list_init(&compositor->view_list);
1794 wl_list_for_each(layer, &compositor->layer_list, link) {
1795 wl_list_for_each(view, &layer->view_list, layer_link) {
1796 view_list_add(compositor, view);
1797 }
1798 }
1799
1800 wl_list_for_each(layer, &compositor->layer_list, link)
1801 wl_list_for_each(view, &layer->view_list, layer_link)
1802 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001803}
1804
David Herrmann1edf44c2013-10-22 17:11:26 +02001805static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001806weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001807{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001808 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001809 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001810 struct weston_animation *animation, *next;
1811 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001812 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001813 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001814 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001815
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001816 if (output->destroying)
1817 return 0;
1818
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001819 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001820 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001821
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001822 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001823 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001824 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825 wl_list_for_each(ev, &ec->view_list, link)
1826 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001827
Pekka Paalanene67858b2013-04-25 13:57:42 +03001828 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 wl_list_for_each(ev, &ec->view_list, link) {
1830 /* Note: This operation is safe to do multiple times on the
1831 * same surface.
1832 */
1833 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001834 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 &ev->surface->frame_callback_list);
1836 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001837 }
1838 }
1839
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001840 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001841
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001842 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001843 pixman_region32_intersect(&output_damage,
1844 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001845 pixman_region32_subtract(&output_damage,
1846 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001847
Scott Moreauccbf29d2012-02-22 14:21:41 -07001848 if (output->dirty)
1849 weston_output_update_matrix(output);
1850
David Herrmann1edf44c2013-10-22 17:11:26 +02001851 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001852
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001853 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001854
Kristian Høgsbergef044142011-06-21 15:02:12 -04001855 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001856
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001857 weston_compositor_repick(ec);
1858 wl_event_loop_dispatch(ec->input_loop, 0);
1859
Jonas Ådahldb773762012-06-13 00:01:21 +02001860 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001861 wl_callback_send_done(cb->resource, msecs);
1862 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001863 }
1864
Scott Moreaud64cf212012-06-08 19:40:54 -06001865 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001866 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001867 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001868 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001869
1870 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001871}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001872
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001873static int
1874weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001875{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001876 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001877
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001878 wl_event_loop_dispatch(compositor->input_loop, 0);
1879
1880 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001881}
1882
1883WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001884weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001885{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001886 struct weston_compositor *compositor = output->compositor;
1887 struct wl_event_loop *loop =
1888 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001889 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001890
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001891 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001892
1893 if (output->repaint_needed &&
1894 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1895 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001896 r = weston_output_repaint(output, msecs);
1897 if (!r)
1898 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001899 }
1900
1901 output->repaint_scheduled = 0;
1902 if (compositor->input_loop_source)
1903 return;
1904
1905 fd = wl_event_loop_get_fd(compositor->input_loop);
1906 compositor->input_loop_source =
1907 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1908 weston_compositor_read_input, compositor);
1909}
1910
1911static void
1912idle_repaint(void *data)
1913{
1914 struct weston_output *output = data;
1915
Jonas Ådahle5a12252013-04-05 23:07:11 +02001916 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001917}
1918
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001919WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001920weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1921{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001922 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001923 if (below != NULL)
1924 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001925}
1926
1927WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001928weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001929{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001930 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001931 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001932
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001933 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1934 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001935 return;
1936
Kristian Høgsbergef044142011-06-21 15:02:12 -04001937 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001938 output->repaint_needed = 1;
1939 if (output->repaint_scheduled)
1940 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001941
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001942 wl_event_loop_add_idle(loop, idle_repaint, output);
1943 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001944
1945 if (compositor->input_loop_source) {
1946 wl_event_source_remove(compositor->input_loop_source);
1947 compositor->input_loop_source = NULL;
1948 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001949}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001950
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001951WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001952weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1953{
1954 struct weston_output *output;
1955
1956 wl_list_for_each(output, &compositor->output_list, link)
1957 weston_output_schedule_repaint(output);
1958}
1959
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001960static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001961surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001962{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001963 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001964}
1965
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001966static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001967surface_attach(struct wl_client *client,
1968 struct wl_resource *resource,
1969 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1970{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001971 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001972 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001973
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001974 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001975 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001976 if (buffer == NULL) {
1977 wl_client_post_no_memory(client);
1978 return;
1979 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001980 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001981
Pekka Paalanende685b82012-12-04 15:58:12 +02001982 /* Attach, attach, without commit in between does not send
1983 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001984 if (surface->pending.buffer)
1985 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001986
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001987 surface->pending.sx = sx;
1988 surface->pending.sy = sy;
1989 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001990 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001991 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001992 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001993 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001994 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001995}
1996
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001997static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001998surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001999 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002000 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002001{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002002 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002003
Pekka Paalanen8e159182012-10-10 12:49:25 +03002004 pixman_region32_union_rect(&surface->pending.damage,
2005 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002006 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002007}
2008
Kristian Høgsberg33418202011-08-16 23:01:28 -04002009static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002010destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002011{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002012 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002013
2014 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002015 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002016}
2017
2018static void
2019surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002020 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002021{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002022 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002023 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002024
2025 cb = malloc(sizeof *cb);
2026 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002027 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002028 return;
2029 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002030
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002031 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2032 callback);
2033 if (cb->resource == NULL) {
2034 free(cb);
2035 wl_resource_post_no_memory(resource);
2036 return;
2037 }
2038
Jason Ekstranda85118c2013-06-27 20:17:02 -05002039 wl_resource_set_implementation(cb->resource, NULL, cb,
2040 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002041
Pekka Paalanenbc106382012-10-10 12:49:31 +03002042 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002043}
2044
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002045static void
2046surface_set_opaque_region(struct wl_client *client,
2047 struct wl_resource *resource,
2048 struct wl_resource *region_resource)
2049{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002050 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002051 struct weston_region *region;
2052
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002053 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002054 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002055 pixman_region32_copy(&surface->pending.opaque,
2056 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002057 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03002058 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002059 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002060}
2061
2062static void
2063surface_set_input_region(struct wl_client *client,
2064 struct wl_resource *resource,
2065 struct wl_resource *region_resource)
2066{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002067 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002068 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002069
2070 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002071 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002072 pixman_region32_copy(&surface->pending.input,
2073 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002074 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002075 pixman_region32_fini(&surface->pending.input);
2076 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002077 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002078}
2079
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002080static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002081weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002082{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002083 struct weston_subsurface *sub;
2084
2085 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2086 parent_link_pending) {
2087 wl_list_remove(&sub->parent_link);
2088 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2089 }
2090}
2091
2092static void
2093weston_surface_commit(struct weston_surface *surface)
2094{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002095 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002096 pixman_region32_t opaque;
2097
Alexander Larsson4ea95522013-05-22 14:41:37 +02002098 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002099 /* wl_surface.set_buffer_scale */
Jonny Lamb74130762013-11-26 18:19:46 +01002100 /* wl_surface_scaler.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002101 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002102
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002103 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01002104 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002105 weston_surface_attach(surface, surface->pending.buffer);
2106
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002107 weston_surface_set_size_from_buffer(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01002108
2109 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02002110 surface->configure(surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002111 surface->pending.sx, surface->pending.sy);
Giulio Camuffo184df502013-02-21 11:29:21 +01002112
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05002113 if (surface->pending.buffer)
2114 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
2115 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11002116 surface->pending.sx = 0;
2117 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01002118 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002119
2120 /* wl_surface.damage */
2121 pixman_region32_union(&surface->damage, &surface->damage,
2122 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002123 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2124 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002125 surface->width,
2126 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002127 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002128
2129 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002130 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 surface->width,
2132 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002133 pixman_region32_intersect(&opaque,
2134 &opaque, &surface->pending.opaque);
2135
2136 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2137 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002138 wl_list_for_each(view, &surface->views, surface_link)
2139 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002140 }
2141
2142 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002143
2144 /* wl_surface.set_input_region */
2145 pixman_region32_fini(&surface->input);
2146 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002147 surface->width,
2148 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002149 pixman_region32_intersect(&surface->input,
2150 &surface->input, &surface->pending.input);
2151
Pekka Paalanenbc106382012-10-10 12:49:31 +03002152 /* wl_surface.frame */
2153 wl_list_insert_list(&surface->frame_callback_list,
2154 &surface->pending.frame_callback_list);
2155 wl_list_init(&surface->pending.frame_callback_list);
2156
Pekka Paalanene67858b2013-04-25 13:57:42 +03002157 weston_surface_commit_subsurface_order(surface);
2158
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002159 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002160}
2161
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002162static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002163weston_subsurface_commit(struct weston_subsurface *sub);
2164
2165static void
2166weston_subsurface_parent_commit(struct weston_subsurface *sub,
2167 int parent_is_synchronized);
2168
2169static void
2170surface_commit(struct wl_client *client, struct wl_resource *resource)
2171{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002172 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002173 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2174
2175 if (sub) {
2176 weston_subsurface_commit(sub);
2177 return;
2178 }
2179
2180 weston_surface_commit(surface);
2181
2182 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2183 if (sub->surface != surface)
2184 weston_subsurface_parent_commit(sub, 0);
2185 }
2186}
2187
2188static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002189surface_set_buffer_transform(struct wl_client *client,
2190 struct wl_resource *resource, int transform)
2191{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002192 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002193
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002194 surface->pending.buffer_viewport.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002195}
2196
Alexander Larsson4ea95522013-05-22 14:41:37 +02002197static void
2198surface_set_buffer_scale(struct wl_client *client,
2199 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002200 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002201{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002202 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002203
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002204 surface->pending.buffer_viewport.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002205}
2206
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002207static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002208 surface_destroy,
2209 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002210 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002211 surface_frame,
2212 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002213 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002214 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002215 surface_set_buffer_transform,
2216 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002217};
2218
2219static void
2220compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002221 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002222{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002223 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002224 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002225
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002226 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002227 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002228 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002229 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002230 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002231
Jason Ekstranda85118c2013-06-27 20:17:02 -05002232 surface->resource =
2233 wl_resource_create(client, &wl_surface_interface,
2234 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002235 if (surface->resource == NULL) {
2236 weston_surface_destroy(surface);
2237 wl_resource_post_no_memory(resource);
2238 return;
2239 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002240 wl_resource_set_implementation(surface->resource, &surface_interface,
2241 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002242}
2243
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002244static void
2245destroy_region(struct wl_resource *resource)
2246{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002247 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002248
2249 pixman_region32_fini(&region->region);
2250 free(region);
2251}
2252
2253static void
2254region_destroy(struct wl_client *client, struct wl_resource *resource)
2255{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002256 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002257}
2258
2259static void
2260region_add(struct wl_client *client, struct wl_resource *resource,
2261 int32_t x, int32_t y, int32_t width, int32_t height)
2262{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002263 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002264
2265 pixman_region32_union_rect(&region->region, &region->region,
2266 x, y, width, height);
2267}
2268
2269static void
2270region_subtract(struct wl_client *client, struct wl_resource *resource,
2271 int32_t x, int32_t y, int32_t width, int32_t height)
2272{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002273 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002274 pixman_region32_t rect;
2275
2276 pixman_region32_init_rect(&rect, x, y, width, height);
2277 pixman_region32_subtract(&region->region, &region->region, &rect);
2278 pixman_region32_fini(&rect);
2279}
2280
2281static const struct wl_region_interface region_interface = {
2282 region_destroy,
2283 region_add,
2284 region_subtract
2285};
2286
2287static void
2288compositor_create_region(struct wl_client *client,
2289 struct wl_resource *resource, uint32_t id)
2290{
2291 struct weston_region *region;
2292
2293 region = malloc(sizeof *region);
2294 if (region == NULL) {
2295 wl_resource_post_no_memory(resource);
2296 return;
2297 }
2298
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002299 pixman_region32_init(&region->region);
2300
Jason Ekstranda85118c2013-06-27 20:17:02 -05002301 region->resource =
2302 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002303 if (region->resource == NULL) {
2304 free(region);
2305 wl_resource_post_no_memory(resource);
2306 return;
2307 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002308 wl_resource_set_implementation(region->resource, &region_interface,
2309 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002310}
2311
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002312static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002313 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002314 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002315};
2316
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002317static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002318weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2319{
2320 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002321 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002322 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002323
Alexander Larsson4ea95522013-05-22 14:41:37 +02002324 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002325 /* wl_surface.set_buffer_scale */
Jonny Lamb74130762013-11-26 18:19:46 +01002326 /* wl_surface_scaler.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002327 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002328
Pekka Paalanene67858b2013-04-25 13:57:42 +03002329 /* wl_surface.attach */
2330 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2331 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2332 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2333
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002334 weston_surface_set_size_from_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002335
2336 if (surface->configure && sub->cached.newly_attached)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002337 surface->configure(surface, sub->cached.sx, sub->cached.sy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002338 sub->cached.sx = 0;
2339 sub->cached.sy = 0;
2340 sub->cached.newly_attached = 0;
2341
2342 /* wl_surface.damage */
2343 pixman_region32_union(&surface->damage, &surface->damage,
2344 &sub->cached.damage);
2345 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2346 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002347 surface->width,
2348 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002349 empty_region(&sub->cached.damage);
2350
2351 /* wl_surface.set_opaque_region */
2352 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002353 surface->width,
2354 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002355 pixman_region32_intersect(&opaque,
2356 &opaque, &sub->cached.opaque);
2357
2358 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2359 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002360 wl_list_for_each(view, &surface->views, surface_link)
2361 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002362 }
2363
2364 pixman_region32_fini(&opaque);
2365
2366 /* wl_surface.set_input_region */
2367 pixman_region32_fini(&surface->input);
2368 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002369 surface->width,
2370 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002371 pixman_region32_intersect(&surface->input,
2372 &surface->input, &sub->cached.input);
2373
2374 /* wl_surface.frame */
2375 wl_list_insert_list(&surface->frame_callback_list,
2376 &sub->cached.frame_callback_list);
2377 wl_list_init(&sub->cached.frame_callback_list);
2378
2379 weston_surface_commit_subsurface_order(surface);
2380
2381 weston_surface_schedule_repaint(surface);
2382
2383 sub->cached.has_data = 0;
2384}
2385
2386static void
2387weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2388{
2389 struct weston_surface *surface = sub->surface;
2390
2391 /*
2392 * If this commit would cause the surface to move by the
2393 * attach(dx, dy) parameters, the old damage region must be
2394 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002395 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002396 */
2397 pixman_region32_translate(&sub->cached.damage,
2398 -surface->pending.sx, -surface->pending.sy);
2399 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2400 &surface->pending.damage);
2401 empty_region(&surface->pending.damage);
2402
2403 if (surface->pending.newly_attached) {
2404 sub->cached.newly_attached = 1;
2405 weston_buffer_reference(&sub->cached.buffer_ref,
2406 surface->pending.buffer);
2407 }
2408 sub->cached.sx += surface->pending.sx;
2409 sub->cached.sy += surface->pending.sy;
2410 surface->pending.sx = 0;
2411 surface->pending.sy = 0;
2412 surface->pending.newly_attached = 0;
2413
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002414 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002415
2416 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2417
2418 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2419
2420 wl_list_insert_list(&sub->cached.frame_callback_list,
2421 &surface->pending.frame_callback_list);
2422 wl_list_init(&surface->pending.frame_callback_list);
2423
2424 sub->cached.has_data = 1;
2425}
2426
2427static int
2428weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2429{
2430 while (sub) {
2431 if (sub->synchronized)
2432 return 1;
2433
2434 if (!sub->parent)
2435 return 0;
2436
2437 sub = weston_surface_to_subsurface(sub->parent);
2438 }
2439
2440 return 0;
2441}
2442
2443static void
2444weston_subsurface_commit(struct weston_subsurface *sub)
2445{
2446 struct weston_surface *surface = sub->surface;
2447 struct weston_subsurface *tmp;
2448
2449 /* Recursive check for effectively synchronized. */
2450 if (weston_subsurface_is_synchronized(sub)) {
2451 weston_subsurface_commit_to_cache(sub);
2452 } else {
2453 if (sub->cached.has_data) {
2454 /* flush accumulated state from cache */
2455 weston_subsurface_commit_to_cache(sub);
2456 weston_subsurface_commit_from_cache(sub);
2457 } else {
2458 weston_surface_commit(surface);
2459 }
2460
2461 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2462 if (tmp->surface != surface)
2463 weston_subsurface_parent_commit(tmp, 0);
2464 }
2465 }
2466}
2467
2468static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002469weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002470{
2471 struct weston_surface *surface = sub->surface;
2472 struct weston_subsurface *tmp;
2473
Pekka Paalanene67858b2013-04-25 13:57:42 +03002474 /* From now on, commit_from_cache the whole sub-tree, regardless of
2475 * the synchronized mode of each child. This sub-surface or some
2476 * of its ancestors were synchronized, so we are synchronized
2477 * all the way down.
2478 */
2479
2480 if (sub->cached.has_data)
2481 weston_subsurface_commit_from_cache(sub);
2482
2483 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2484 if (tmp->surface != surface)
2485 weston_subsurface_parent_commit(tmp, 1);
2486 }
2487}
2488
2489static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002490weston_subsurface_parent_commit(struct weston_subsurface *sub,
2491 int parent_is_synchronized)
2492{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002493 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002494 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002495 wl_list_for_each(view, &sub->surface->views, surface_link)
2496 weston_view_set_position(view,
2497 sub->position.x,
2498 sub->position.y);
2499
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002500 sub->position.set = 0;
2501 }
2502
2503 if (parent_is_synchronized || sub->synchronized)
2504 weston_subsurface_synchronized_commit(sub);
2505}
2506
2507static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002508subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002509{
2510 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002511 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002512
Jason Ekstranda7af7042013-10-12 22:38:11 -05002513 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002514 weston_view_set_position(view,
2515 view->geometry.x + dx,
2516 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002517
2518 /* No need to check parent mappedness, because if parent is not
2519 * mapped, parent is not in a visible layer, so this sub-surface
2520 * will not be drawn either.
2521 */
2522 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002523 /* Cannot call weston_surface_update_transform(),
2524 * because that would call it also for the parent surface,
2525 * which might not be mapped yet. That would lead to
2526 * inconsistent state, where the window could never be
2527 * mapped.
2528 *
2529 * Instead just assing any output, to make
2530 * weston_surface_is_mapped() return true, so that when the
2531 * parent surface does get mapped, this one will get
2532 * included, too. See surface_list_add().
2533 */
2534 assert(!wl_list_empty(&compositor->output_list));
2535 surface->output = container_of(compositor->output_list.next,
2536 struct weston_output, link);
2537 }
2538}
2539
2540static struct weston_subsurface *
2541weston_surface_to_subsurface(struct weston_surface *surface)
2542{
2543 if (surface->configure == subsurface_configure)
2544 return surface->configure_private;
2545
2546 return NULL;
2547}
2548
Pekka Paalanen01388e22013-04-25 13:57:44 +03002549WL_EXPORT struct weston_surface *
2550weston_surface_get_main_surface(struct weston_surface *surface)
2551{
2552 struct weston_subsurface *sub;
2553
2554 while (surface && (sub = weston_surface_to_subsurface(surface)))
2555 surface = sub->parent;
2556
2557 return surface;
2558}
2559
Pekka Paalanene67858b2013-04-25 13:57:42 +03002560static void
2561subsurface_set_position(struct wl_client *client,
2562 struct wl_resource *resource, int32_t x, int32_t y)
2563{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002564 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002565
2566 if (!sub)
2567 return;
2568
2569 sub->position.x = x;
2570 sub->position.y = y;
2571 sub->position.set = 1;
2572}
2573
2574static struct weston_subsurface *
2575subsurface_from_surface(struct weston_surface *surface)
2576{
2577 struct weston_subsurface *sub;
2578
2579 sub = weston_surface_to_subsurface(surface);
2580 if (sub)
2581 return sub;
2582
2583 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2584 if (sub->surface == surface)
2585 return sub;
2586
2587 return NULL;
2588}
2589
2590static struct weston_subsurface *
2591subsurface_sibling_check(struct weston_subsurface *sub,
2592 struct weston_surface *surface,
2593 const char *request)
2594{
2595 struct weston_subsurface *sibling;
2596
2597 sibling = subsurface_from_surface(surface);
2598
2599 if (!sibling) {
2600 wl_resource_post_error(sub->resource,
2601 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2602 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002603 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002604 return NULL;
2605 }
2606
2607 if (sibling->parent != sub->parent) {
2608 wl_resource_post_error(sub->resource,
2609 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2610 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002611 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002612 return NULL;
2613 }
2614
2615 return sibling;
2616}
2617
2618static void
2619subsurface_place_above(struct wl_client *client,
2620 struct wl_resource *resource,
2621 struct wl_resource *sibling_resource)
2622{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002623 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002624 struct weston_surface *surface =
2625 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002626 struct weston_subsurface *sibling;
2627
2628 if (!sub)
2629 return;
2630
2631 sibling = subsurface_sibling_check(sub, surface, "place_above");
2632 if (!sibling)
2633 return;
2634
2635 wl_list_remove(&sub->parent_link_pending);
2636 wl_list_insert(sibling->parent_link_pending.prev,
2637 &sub->parent_link_pending);
2638}
2639
2640static void
2641subsurface_place_below(struct wl_client *client,
2642 struct wl_resource *resource,
2643 struct wl_resource *sibling_resource)
2644{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002645 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002646 struct weston_surface *surface =
2647 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002648 struct weston_subsurface *sibling;
2649
2650 if (!sub)
2651 return;
2652
2653 sibling = subsurface_sibling_check(sub, surface, "place_below");
2654 if (!sibling)
2655 return;
2656
2657 wl_list_remove(&sub->parent_link_pending);
2658 wl_list_insert(&sibling->parent_link_pending,
2659 &sub->parent_link_pending);
2660}
2661
2662static void
2663subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2664{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002665 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002666
2667 if (sub)
2668 sub->synchronized = 1;
2669}
2670
2671static void
2672subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2673{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002674 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002675
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002676 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002677 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002678
2679 /* If sub became effectively desynchronized, flush. */
2680 if (!weston_subsurface_is_synchronized(sub))
2681 weston_subsurface_synchronized_commit(sub);
2682 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002683}
2684
2685static void
2686weston_subsurface_cache_init(struct weston_subsurface *sub)
2687{
2688 pixman_region32_init(&sub->cached.damage);
2689 pixman_region32_init(&sub->cached.opaque);
2690 pixman_region32_init(&sub->cached.input);
2691 wl_list_init(&sub->cached.frame_callback_list);
2692 sub->cached.buffer_ref.buffer = NULL;
2693}
2694
2695static void
2696weston_subsurface_cache_fini(struct weston_subsurface *sub)
2697{
2698 struct weston_frame_callback *cb, *tmp;
2699
2700 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002701 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002702
2703 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2704 pixman_region32_fini(&sub->cached.damage);
2705 pixman_region32_fini(&sub->cached.opaque);
2706 pixman_region32_fini(&sub->cached.input);
2707}
2708
2709static void
2710weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2711{
2712 wl_list_remove(&sub->parent_link);
2713 wl_list_remove(&sub->parent_link_pending);
2714 wl_list_remove(&sub->parent_destroy_listener.link);
2715 sub->parent = NULL;
2716}
2717
2718static void
2719weston_subsurface_destroy(struct weston_subsurface *sub);
2720
2721static void
2722subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2723{
2724 struct weston_subsurface *sub =
2725 container_of(listener, struct weston_subsurface,
2726 surface_destroy_listener);
2727 assert(data == &sub->surface->resource);
2728
2729 /* The protocol object (wl_resource) is left inert. */
2730 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002731 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002732
2733 weston_subsurface_destroy(sub);
2734}
2735
2736static void
2737subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2738{
2739 struct weston_subsurface *sub =
2740 container_of(listener, struct weston_subsurface,
2741 parent_destroy_listener);
2742 assert(data == &sub->parent->resource);
2743 assert(sub->surface != sub->parent);
2744
2745 if (weston_surface_is_mapped(sub->surface))
2746 weston_surface_unmap(sub->surface);
2747
2748 weston_subsurface_unlink_parent(sub);
2749}
2750
2751static void
2752subsurface_resource_destroy(struct wl_resource *resource)
2753{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002754 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002755
2756 if (sub)
2757 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002758}
2759
2760static void
2761subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2762{
2763 wl_resource_destroy(resource);
2764}
2765
2766static void
2767weston_subsurface_link_parent(struct weston_subsurface *sub,
2768 struct weston_surface *parent)
2769{
2770 sub->parent = parent;
2771 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002772 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002773 &sub->parent_destroy_listener);
2774
2775 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2776 wl_list_insert(&parent->subsurface_list_pending,
2777 &sub->parent_link_pending);
2778}
2779
2780static void
2781weston_subsurface_link_surface(struct weston_subsurface *sub,
2782 struct weston_surface *surface)
2783{
2784 sub->surface = surface;
2785 sub->surface_destroy_listener.notify =
2786 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002787 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002788 &sub->surface_destroy_listener);
2789}
2790
2791static void
2792weston_subsurface_destroy(struct weston_subsurface *sub)
2793{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002794 struct weston_view *view, *next;
2795
Pekka Paalanene67858b2013-04-25 13:57:42 +03002796 assert(sub->surface);
2797
2798 if (sub->resource) {
2799 assert(weston_surface_to_subsurface(sub->surface) == sub);
2800 assert(sub->parent_destroy_listener.notify ==
2801 subsurface_handle_parent_destroy);
2802
Jason Ekstranda7af7042013-10-12 22:38:11 -05002803 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2804 weston_view_destroy(view);
2805
Pekka Paalanene67858b2013-04-25 13:57:42 +03002806 if (sub->parent)
2807 weston_subsurface_unlink_parent(sub);
2808
2809 weston_subsurface_cache_fini(sub);
2810
2811 sub->surface->configure = NULL;
2812 sub->surface->configure_private = NULL;
2813 } else {
2814 /* the dummy weston_subsurface for the parent itself */
2815 assert(sub->parent_destroy_listener.notify == NULL);
2816 wl_list_remove(&sub->parent_link);
2817 wl_list_remove(&sub->parent_link_pending);
2818 }
2819
2820 wl_list_remove(&sub->surface_destroy_listener.link);
2821 free(sub);
2822}
2823
2824static const struct wl_subsurface_interface subsurface_implementation = {
2825 subsurface_destroy,
2826 subsurface_set_position,
2827 subsurface_place_above,
2828 subsurface_place_below,
2829 subsurface_set_sync,
2830 subsurface_set_desync
2831};
2832
2833static struct weston_subsurface *
2834weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2835 struct weston_surface *parent)
2836{
2837 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002838 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002839
2840 sub = calloc(1, sizeof *sub);
2841 if (!sub)
2842 return NULL;
2843
Jason Ekstranda7af7042013-10-12 22:38:11 -05002844 wl_list_init(&sub->unused_views);
2845
Jason Ekstranda85118c2013-06-27 20:17:02 -05002846 sub->resource =
2847 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002848 if (!sub->resource) {
2849 free(sub);
2850 return NULL;
2851 }
2852
Jason Ekstranda85118c2013-06-27 20:17:02 -05002853 wl_resource_set_implementation(sub->resource,
2854 &subsurface_implementation,
2855 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002856 weston_subsurface_link_surface(sub, surface);
2857 weston_subsurface_link_parent(sub, parent);
2858 weston_subsurface_cache_init(sub);
2859 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002860
2861 return sub;
2862}
2863
2864/* Create a dummy subsurface for having the parent itself in its
2865 * sub-surface lists. Makes stacking order manipulation easy.
2866 */
2867static struct weston_subsurface *
2868weston_subsurface_create_for_parent(struct weston_surface *parent)
2869{
2870 struct weston_subsurface *sub;
2871
2872 sub = calloc(1, sizeof *sub);
2873 if (!sub)
2874 return NULL;
2875
2876 weston_subsurface_link_surface(sub, parent);
2877 sub->parent = parent;
2878 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2879 wl_list_insert(&parent->subsurface_list_pending,
2880 &sub->parent_link_pending);
2881
2882 return sub;
2883}
2884
2885static void
2886subcompositor_get_subsurface(struct wl_client *client,
2887 struct wl_resource *resource,
2888 uint32_t id,
2889 struct wl_resource *surface_resource,
2890 struct wl_resource *parent_resource)
2891{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002892 struct weston_surface *surface =
2893 wl_resource_get_user_data(surface_resource);
2894 struct weston_surface *parent =
2895 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002896 struct weston_subsurface *sub;
2897 static const char where[] = "get_subsurface: wl_subsurface@";
2898
2899 if (surface == parent) {
2900 wl_resource_post_error(resource,
2901 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2902 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002903 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002904 return;
2905 }
2906
2907 if (weston_surface_to_subsurface(surface)) {
2908 wl_resource_post_error(resource,
2909 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2910 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002911 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002912 return;
2913 }
2914
2915 if (surface->configure) {
2916 wl_resource_post_error(resource,
2917 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2918 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002919 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002920 return;
2921 }
2922
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002923 if (weston_surface_get_main_surface(parent) == surface) {
2924 wl_resource_post_error(resource,
2925 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2926 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002927 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002928 return;
2929 }
2930
Pekka Paalanene67858b2013-04-25 13:57:42 +03002931 /* make sure the parent is in its own list */
2932 if (wl_list_empty(&parent->subsurface_list)) {
2933 if (!weston_subsurface_create_for_parent(parent)) {
2934 wl_resource_post_no_memory(resource);
2935 return;
2936 }
2937 }
2938
2939 sub = weston_subsurface_create(id, surface, parent);
2940 if (!sub) {
2941 wl_resource_post_no_memory(resource);
2942 return;
2943 }
2944
2945 surface->configure = subsurface_configure;
2946 surface->configure_private = sub;
2947}
2948
2949static void
2950subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2951{
2952 wl_resource_destroy(resource);
2953}
2954
2955static const struct wl_subcompositor_interface subcompositor_interface = {
2956 subcompositor_destroy,
2957 subcompositor_get_subsurface
2958};
2959
2960static void
2961bind_subcompositor(struct wl_client *client,
2962 void *data, uint32_t version, uint32_t id)
2963{
2964 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002965 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002966
Jason Ekstranda85118c2013-06-27 20:17:02 -05002967 resource =
2968 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002969 if (resource == NULL) {
2970 wl_client_post_no_memory(client);
2971 return;
2972 }
2973 wl_resource_set_implementation(resource, &subcompositor_interface,
2974 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002975}
2976
2977static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002978weston_compositor_dpms(struct weston_compositor *compositor,
2979 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002980{
2981 struct weston_output *output;
2982
2983 wl_list_for_each(output, &compositor->output_list, link)
2984 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002985 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002986}
2987
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002988WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002989weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002990{
Neil Roberts8b62e202013-09-30 13:14:47 +01002991 uint32_t old_state = compositor->state;
2992
2993 /* The state needs to be changed before emitting the wake
2994 * signal because that may try to schedule a repaint which
2995 * will not work if the compositor is still sleeping */
2996 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2997
2998 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002999 case WESTON_COMPOSITOR_SLEEPING:
3000 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3001 /* fall through */
3002 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003003 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003004 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003005 /* fall through */
3006 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003007 wl_event_source_timer_update(compositor->idle_source,
3008 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003009 }
3010}
3011
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003012WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003013weston_compositor_offscreen(struct weston_compositor *compositor)
3014{
3015 switch (compositor->state) {
3016 case WESTON_COMPOSITOR_OFFSCREEN:
3017 return;
3018 case WESTON_COMPOSITOR_SLEEPING:
3019 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3020 /* fall through */
3021 default:
3022 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3023 wl_event_source_timer_update(compositor->idle_source, 0);
3024 }
3025}
3026
3027WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003028weston_compositor_sleep(struct weston_compositor *compositor)
3029{
3030 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3031 return;
3032
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003033 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003034 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3035 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3036}
3037
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003038static int
3039idle_handler(void *data)
3040{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003041 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003042
3043 if (compositor->idle_inhibit)
3044 return 1;
3045
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003046 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003047 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003048
3049 return 1;
3050}
3051
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003052WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003053weston_plane_init(struct weston_plane *plane,
3054 struct weston_compositor *ec,
3055 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003056{
3057 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003058 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003059 plane->x = x;
3060 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003061 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003062
3063 /* Init the link so that the call to wl_list_remove() when releasing
3064 * the plane without ever stacking doesn't lead to a crash */
3065 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003066}
3067
3068WL_EXPORT void
3069weston_plane_release(struct weston_plane *plane)
3070{
Xiong Zhang97116532013-10-23 13:58:31 +08003071 struct weston_view *view;
3072
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003073 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003074 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003075
Xiong Zhang97116532013-10-23 13:58:31 +08003076 wl_list_for_each(view, &plane->compositor->view_list, link) {
3077 if (view->plane == plane)
3078 view->plane = NULL;
3079 }
3080
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003081 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003082}
3083
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003084WL_EXPORT void
3085weston_compositor_stack_plane(struct weston_compositor *ec,
3086 struct weston_plane *plane,
3087 struct weston_plane *above)
3088{
3089 if (above)
3090 wl_list_insert(above->link.prev, &plane->link);
3091 else
3092 wl_list_insert(&ec->plane_list, &plane->link);
3093}
3094
Casey Dahlin9074db52012-04-19 22:50:09 -04003095static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003096{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003097 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003098}
3099
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003100static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003101bind_output(struct wl_client *client,
3102 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003103{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003104 struct weston_output *output = data;
3105 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003106 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003107
Jason Ekstranda85118c2013-06-27 20:17:02 -05003108 resource = wl_resource_create(client, &wl_output_interface,
3109 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003110 if (resource == NULL) {
3111 wl_client_post_no_memory(client);
3112 return;
3113 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003114
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003115 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003116 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003117
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003118 wl_output_send_geometry(resource,
3119 output->x,
3120 output->y,
3121 output->mm_width,
3122 output->mm_height,
3123 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003124 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003125 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003126 if (version >= 2)
3127 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003128 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003129
3130 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003131 wl_output_send_mode(resource,
3132 mode->flags,
3133 mode->width,
3134 mode->height,
3135 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003136 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003137
3138 if (version >= 2)
3139 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003140}
3141
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003142/* Move other outputs when one is removed so the space remains contiguos. */
3143static void
3144weston_compositor_remove_output(struct weston_compositor *compositor,
3145 struct weston_output *remove_output)
3146{
3147 struct weston_output *output;
3148 int offset = 0;
3149
3150 wl_list_for_each(output, &compositor->output_list, link) {
3151 if (output == remove_output) {
3152 offset = output->width;
3153 continue;
3154 }
3155
3156 if (offset > 0) {
3157 weston_output_move(output,
3158 output->x - offset, output->y);
3159 output->dirty = 1;
3160 }
3161 }
3162}
3163
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02003164static void
3165weston_compositor_verify_pointers(struct weston_compositor *ec)
3166{
3167 struct weston_seat *seat;
3168
3169 wl_list_for_each(seat, &ec->seat_list, link) {
3170 if (!seat->pointer)
3171 continue;
3172
3173 weston_pointer_verify(seat->pointer);
3174 }
3175}
3176
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003177WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003178weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003179{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003180 output->destroying = 1;
3181
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003182 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003183 wl_list_remove(&output->link);
3184
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02003185 weston_compositor_verify_pointers(output->compositor);
3186
Richard Hughes64ddde12013-05-01 21:52:10 +01003187 wl_signal_emit(&output->destroy_signal, output);
3188
Richard Hughesafe690c2013-05-02 10:10:04 +01003189 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003190 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003191 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003192 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003193
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003194 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003195}
3196
Scott Moreau1bad5db2012-08-18 01:04:05 -06003197static void
3198weston_output_compute_transform(struct weston_output *output)
3199{
3200 struct weston_matrix transform;
3201 int flip;
3202
3203 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003204 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003205
3206 switch(output->transform) {
3207 case WL_OUTPUT_TRANSFORM_FLIPPED:
3208 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3209 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3210 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003211 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003212 flip = -1;
3213 break;
3214 default:
3215 flip = 1;
3216 break;
3217 }
3218
3219 switch(output->transform) {
3220 case WL_OUTPUT_TRANSFORM_NORMAL:
3221 case WL_OUTPUT_TRANSFORM_FLIPPED:
3222 transform.d[0] = flip;
3223 transform.d[1] = 0;
3224 transform.d[4] = 0;
3225 transform.d[5] = 1;
3226 break;
3227 case WL_OUTPUT_TRANSFORM_90:
3228 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3229 transform.d[0] = 0;
3230 transform.d[1] = -flip;
3231 transform.d[4] = 1;
3232 transform.d[5] = 0;
3233 break;
3234 case WL_OUTPUT_TRANSFORM_180:
3235 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3236 transform.d[0] = -flip;
3237 transform.d[1] = 0;
3238 transform.d[4] = 0;
3239 transform.d[5] = -1;
3240 break;
3241 case WL_OUTPUT_TRANSFORM_270:
3242 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3243 transform.d[0] = 0;
3244 transform.d[1] = flip;
3245 transform.d[4] = -1;
3246 transform.d[5] = 0;
3247 break;
3248 default:
3249 break;
3250 }
3251
3252 weston_matrix_multiply(&output->matrix, &transform);
3253}
3254
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003255WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003256weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003257{
Scott Moreau850ca422012-05-21 15:21:25 -06003258 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003259 struct weston_matrix camera;
3260 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003261
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003262 weston_matrix_init(&output->matrix);
3263 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003264 -(output->x + output->width / 2.0),
3265 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003266
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003267 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003268 2.0 / output->width,
3269 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003270
3271 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003272
Scott Moreauccbf29d2012-02-22 14:21:41 -07003273 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003274 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003275 weston_matrix_init(&camera);
3276 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003277 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003278 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003279 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003280 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003281 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003282 weston_matrix_multiply(&output->matrix, &modelview);
3283 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003284
Scott Moreauccbf29d2012-02-22 14:21:41 -07003285 output->dirty = 0;
3286}
3287
Scott Moreau1bad5db2012-08-18 01:04:05 -06003288static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003289weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003290{
3291 output->transform = transform;
3292
3293 switch (transform) {
3294 case WL_OUTPUT_TRANSFORM_90:
3295 case WL_OUTPUT_TRANSFORM_270:
3296 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3297 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3298 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003299 output->width = output->current_mode->height;
3300 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003301 break;
3302 case WL_OUTPUT_TRANSFORM_NORMAL:
3303 case WL_OUTPUT_TRANSFORM_180:
3304 case WL_OUTPUT_TRANSFORM_FLIPPED:
3305 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003306 output->width = output->current_mode->width;
3307 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003308 break;
3309 default:
3310 break;
3311 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003312
Hardening57388e42013-09-18 23:56:36 +02003313 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003314 output->width /= scale;
3315 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003316}
3317
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003318static void
3319weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003320{
3321 output->x = x;
3322 output->y = y;
3323
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003324 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003325 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003326 output->width,
3327 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003328}
3329
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003330WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003331weston_output_move(struct weston_output *output, int x, int y)
3332{
3333 pixman_region32_t old_region;
3334 struct wl_resource *resource;
3335
3336 output->move_x = x - output->x;
3337 output->move_y = y - output->y;
3338
3339 if (output->move_x == 0 && output->move_y == 0)
3340 return;
3341
3342 pixman_region32_init(&old_region);
3343 pixman_region32_copy(&old_region, &output->region);
3344
3345 weston_output_init_geometry(output, x, y);
3346
3347 output->dirty = 1;
3348
3349 /* Move views on this output. */
3350 wl_signal_emit(&output->move_signal, output);
3351
3352 /* Notify clients of the change for output position. */
3353 wl_resource_for_each(resource, &output->resource_list)
3354 wl_output_send_geometry(resource,
3355 output->x,
3356 output->y,
3357 output->mm_width,
3358 output->mm_height,
3359 output->subpixel,
3360 output->make,
3361 output->model,
3362 output->transform);
3363}
3364
3365WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003366weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003367 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003368 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003369{
3370 output->compositor = c;
3371 output->x = x;
3372 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003373 output->mm_width = mm_width;
3374 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003375 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003376 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003377
Alexander Larsson0b135062013-05-28 16:23:36 +02003378 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003379 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003380
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003381 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003382 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003383
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003384 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003385 wl_signal_init(&output->destroy_signal);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003386 wl_signal_init(&output->move_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003387 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003388 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003389
Casey Dahlin58ba1372012-04-19 22:50:08 -04003390 output->id = ffs(~output->compositor->output_id_pool) - 1;
3391 output->compositor->output_id_pool |= 1 << output->id;
3392
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003393 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003394 wl_global_create(c->wl_display, &wl_output_interface, 2,
3395 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003396 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003397}
3398
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003399WL_EXPORT void
3400weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003401 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003402 wl_fixed_t *x, wl_fixed_t *y)
3403{
3404 wl_fixed_t tx, ty;
3405 wl_fixed_t width, height;
3406
Hardeningff39efa2013-09-18 23:56:35 +02003407 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3408 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003409
3410 switch(output->transform) {
3411 case WL_OUTPUT_TRANSFORM_NORMAL:
3412 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003413 tx = device_x;
3414 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003415 break;
3416 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003417 tx = device_y;
3418 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003419 break;
3420 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003421 tx = width - device_x;
3422 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003423 break;
3424 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003425 tx = width - device_y;
3426 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003427 break;
3428 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003429 tx = width - device_x;
3430 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003431 break;
3432 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003433 tx = width - device_y;
3434 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003435 break;
3436 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003437 tx = device_x;
3438 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003439 break;
3440 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003441 tx = device_y;
3442 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003443 break;
3444 }
3445
Hardeningff39efa2013-09-18 23:56:35 +02003446 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3447 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003448}
3449
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003450static void
Jonny Lamb8ae35902013-11-26 18:19:45 +01003451destroy_surface_scaler(struct wl_resource *resource)
3452{
Jonny Lamb74130762013-11-26 18:19:46 +01003453 struct weston_surface *surface =
3454 wl_resource_get_user_data(resource);
3455
3456 surface->surface_scaler_resource = NULL;
3457 surface->pending.buffer_viewport.scaler_set = 0;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003458}
3459
3460static void
3461surface_scaler_destroy(struct wl_client *client,
3462 struct wl_resource *resource)
3463{
3464 wl_resource_destroy(resource);
3465}
3466
3467static void
3468surface_scaler_set(struct wl_client *client,
3469 struct wl_resource *resource,
3470 wl_fixed_t src_x,
3471 wl_fixed_t src_y,
3472 wl_fixed_t src_width,
3473 wl_fixed_t src_height,
3474 int32_t dst_width,
3475 int32_t dst_height)
3476{
Jonny Lamb74130762013-11-26 18:19:46 +01003477 struct weston_surface *surface =
3478 wl_resource_get_user_data(resource);
3479
3480 assert(surface->surface_scaler_resource != NULL);
3481
Jonny Lamb8ae35902013-11-26 18:19:45 +01003482 if (wl_fixed_to_double(src_width) < 0 ||
3483 wl_fixed_to_double(src_height) < 0) {
3484 wl_resource_post_error(resource,
3485 WL_SURFACE_SCALER_ERROR_BAD_VALUE,
3486 "source dimensions must be non-negative (%fx%f)",
3487 wl_fixed_to_double(src_width),
3488 wl_fixed_to_double(src_height));
3489 return;
3490 }
3491
3492 if (dst_width <= 0 || dst_height <= 0) {
3493 wl_resource_post_error(resource,
3494 WL_SURFACE_SCALER_ERROR_BAD_VALUE,
3495 "destination dimensions must be positive (%dx%d)",
3496 dst_width, dst_height);
3497 return;
3498 }
3499
Jonny Lamb74130762013-11-26 18:19:46 +01003500 surface->pending.buffer_viewport.scaler_set = 1;
3501
3502 surface->pending.buffer_viewport.src_x = src_x;
3503 surface->pending.buffer_viewport.src_y = src_y;
3504 surface->pending.buffer_viewport.src_width = src_width;
3505 surface->pending.buffer_viewport.src_height = src_height;
3506 surface->pending.buffer_viewport.dst_width = dst_width;
3507 surface->pending.buffer_viewport.dst_height = dst_height;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003508}
3509
3510static const struct wl_surface_scaler_interface surface_scaler_interface = {
3511 surface_scaler_destroy,
3512 surface_scaler_set
3513};
3514
3515static void
3516scaler_destroy(struct wl_client *client,
3517 struct wl_resource *resource)
3518{
3519 wl_resource_destroy(resource);
3520}
3521
3522static void
3523scaler_get_surface_scaler(struct wl_client *client,
3524 struct wl_resource *scaler,
3525 uint32_t id,
3526 struct wl_resource *surface_resource)
3527{
3528 struct weston_surface *surface = wl_resource_get_user_data(surface_resource);
3529 struct wl_resource *resource;
3530
Jonny Lamb74130762013-11-26 18:19:46 +01003531 if (surface->surface_scaler_resource) {
3532 wl_resource_post_error(scaler,
3533 WL_SCALER_ERROR_SCALER_EXISTS,
3534 "a surface scaler for that surface already exists");
3535 return;
3536 }
3537
Jonny Lamb8ae35902013-11-26 18:19:45 +01003538 resource = wl_resource_create(client, &wl_surface_scaler_interface,
3539 1, id);
3540 if (resource == NULL) {
3541 wl_client_post_no_memory(client);
3542 return;
3543 }
3544
3545 wl_resource_set_implementation(resource, &surface_scaler_interface,
3546 surface, destroy_surface_scaler);
Jonny Lamb74130762013-11-26 18:19:46 +01003547
3548 surface->surface_scaler_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003549}
3550
3551static const struct wl_scaler_interface scaler_interface = {
3552 scaler_destroy,
3553 scaler_get_surface_scaler
3554};
3555
3556static void
3557bind_scaler(struct wl_client *client,
3558 void *data, uint32_t version, uint32_t id)
3559{
3560 struct wl_resource *resource;
3561
3562 resource = wl_resource_create(client, &wl_scaler_interface,
3563 1, id);
3564 if (resource == NULL) {
3565 wl_client_post_no_memory(client);
3566 return;
3567 }
3568
3569 wl_resource_set_implementation(resource, &scaler_interface,
3570 NULL, NULL);
3571}
3572
3573static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003574compositor_bind(struct wl_client *client,
3575 void *data, uint32_t version, uint32_t id)
3576{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003577 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003578 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003579
Jason Ekstranda85118c2013-06-27 20:17:02 -05003580 resource = wl_resource_create(client, &wl_compositor_interface,
3581 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003582 if (resource == NULL) {
3583 wl_client_post_no_memory(client);
3584 return;
3585 }
3586
3587 wl_resource_set_implementation(resource, &compositor_interface,
3588 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003589}
3590
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003591static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003592log_uname(void)
3593{
3594 struct utsname usys;
3595
3596 uname(&usys);
3597
3598 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3599 usys.version, usys.machine);
3600}
3601
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003602WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003603weston_environment_get_fd(const char *env)
3604{
3605 char *e, *end;
3606 int fd, flags;
3607
3608 e = getenv(env);
3609 if (!e)
3610 return -1;
3611 fd = strtol(e, &end, 0);
3612 if (*end != '\0')
3613 return -1;
3614
3615 flags = fcntl(fd, F_GETFD);
3616 if (flags == -1)
3617 return -1;
3618
3619 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3620 unsetenv(env);
3621
3622 return fd;
3623}
3624
3625WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003626weston_compositor_init(struct weston_compositor *ec,
3627 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003628 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003629 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003630{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003631 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003632 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003633 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003634
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003635 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003636 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003637 wl_signal_init(&ec->destroy_signal);
3638 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003639 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003640 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003641 wl_signal_init(&ec->idle_signal);
3642 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003643 wl_signal_init(&ec->show_input_panel_signal);
3644 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003645 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003646 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003647 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003648 wl_signal_init(&ec->session_signal);
3649 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003650
Casey Dahlin58ba1372012-04-19 22:50:08 -04003651 ec->output_id_pool = 0;
3652
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003653 if (!wl_global_create(display, &wl_compositor_interface, 3,
3654 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003655 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003656
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003657 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3658 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003659 return -1;
3660
Jonny Lamb8ae35902013-11-26 18:19:45 +01003661 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 1,
3662 ec, bind_scaler))
3663 return -1;
3664
Jason Ekstranda7af7042013-10-12 22:38:11 -05003665 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003666 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003667 wl_list_init(&ec->layer_list);
3668 wl_list_init(&ec->seat_list);
3669 wl_list_init(&ec->output_list);
3670 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003671 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003672 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003673 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003674 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003675 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003676
Xiong Zhang97116532013-10-23 13:58:31 +08003677 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003678 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003679
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003680 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3681 weston_config_section_get_string(s, "keymap_rules",
3682 (char **) &xkb_names.rules, NULL);
3683 weston_config_section_get_string(s, "keymap_model",
3684 (char **) &xkb_names.model, NULL);
3685 weston_config_section_get_string(s, "keymap_layout",
3686 (char **) &xkb_names.layout, NULL);
3687 weston_config_section_get_string(s, "keymap_variant",
3688 (char **) &xkb_names.variant, NULL);
3689 weston_config_section_get_string(s, "keymap_options",
3690 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003691
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003692 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3693 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003694
3695 ec->ping_handler = NULL;
3696
3697 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003698 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003699
3700 wl_data_device_manager_init(ec->wl_display);
3701
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003702 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003703
Daniel Stone725c2c32012-06-22 14:04:36 +01003704 loop = wl_display_get_event_loop(ec->wl_display);
3705 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3706 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3707
3708 ec->input_loop = wl_event_loop_create();
3709
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003710 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3711 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3712
3713 weston_compositor_schedule_repaint(ec);
3714
Daniel Stone725c2c32012-06-22 14:04:36 +01003715 return 0;
3716}
3717
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003718WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003719weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003720{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003721 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003722
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003723 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003724 if (ec->input_loop_source)
3725 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003726
Matt Roper361d2ad2011-08-29 13:52:23 -07003727 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003728 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003729 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003730
Daniel Stone325fc2d2012-05-30 16:31:58 +01003731 weston_binding_list_destroy_all(&ec->key_binding_list);
3732 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003733 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003734 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003735 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003736
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003737 weston_plane_release(&ec->primary_plane);
3738
Jonas Ådahlc97af922012-03-28 22:36:09 +02003739 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003740
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003741 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003742}
3743
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003744WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003745weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3746 const struct weston_pointer_grab_interface *interface)
3747{
3748 struct weston_seat *seat;
3749
3750 ec->default_pointer_grab = interface;
3751 wl_list_for_each(seat, &ec->seat_list, link) {
3752 if (seat->pointer) {
3753 weston_pointer_set_default_grab(seat->pointer,
3754 interface);
3755 }
3756 }
3757}
3758
3759WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003760weston_version(int *major, int *minor, int *micro)
3761{
3762 *major = WESTON_VERSION_MAJOR;
3763 *minor = WESTON_VERSION_MINOR;
3764 *micro = WESTON_VERSION_MICRO;
3765}
3766
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003767static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003768 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003769 const char *desc;
3770} capability_strings[] = {
3771 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003772 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003773};
3774
3775static void
3776weston_compositor_log_capabilities(struct weston_compositor *compositor)
3777{
3778 unsigned i;
3779 int yes;
3780
3781 weston_log("Compositor capabilities:\n");
3782 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3783 yes = compositor->capabilities & capability_strings[i].bit;
3784 weston_log_continue(STAMP_SPACE "%s %s\n",
3785 capability_strings[i].desc,
3786 yes ? "yes" : "no");
3787 }
3788}
3789
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003790static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003791{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003792 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003793
Martin Minarik6d118362012-06-07 18:01:59 +02003794 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003795 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003796
3797 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003798}
3799
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003800#ifdef HAVE_LIBUNWIND
3801
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003802static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003803print_backtrace(void)
3804{
3805 unw_cursor_t cursor;
3806 unw_context_t context;
3807 unw_word_t off;
3808 unw_proc_info_t pip;
3809 int ret, i = 0;
3810 char procname[256];
3811 const char *filename;
3812 Dl_info dlinfo;
3813
3814 pip.unwind_info = NULL;
3815 ret = unw_getcontext(&context);
3816 if (ret) {
3817 weston_log("unw_getcontext: %d\n", ret);
3818 return;
3819 }
3820
3821 ret = unw_init_local(&cursor, &context);
3822 if (ret) {
3823 weston_log("unw_init_local: %d\n", ret);
3824 return;
3825 }
3826
3827 ret = unw_step(&cursor);
3828 while (ret > 0) {
3829 ret = unw_get_proc_info(&cursor, &pip);
3830 if (ret) {
3831 weston_log("unw_get_proc_info: %d\n", ret);
3832 break;
3833 }
3834
3835 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3836 if (ret && ret != -UNW_ENOMEM) {
3837 if (ret != -UNW_EUNSPEC)
3838 weston_log("unw_get_proc_name: %d\n", ret);
3839 procname[0] = '?';
3840 procname[1] = 0;
3841 }
3842
3843 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3844 *dlinfo.dli_fname)
3845 filename = dlinfo.dli_fname;
3846 else
3847 filename = "?";
3848
3849 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3850 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3851
3852 ret = unw_step(&cursor);
3853 if (ret < 0)
3854 weston_log("unw_step: %d\n", ret);
3855 }
3856}
3857
3858#else
3859
3860static void
3861print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003862{
3863 void *buffer[32];
3864 int i, count;
3865 Dl_info info;
3866
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003867 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3868 for (i = 0; i < count; i++) {
3869 dladdr(buffer[i], &info);
3870 weston_log(" [%016lx] %s (%s)\n",
3871 (long) buffer[i],
3872 info.dli_sname ? info.dli_sname : "--",
3873 info.dli_fname);
3874 }
3875}
3876
3877#endif
3878
3879static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003880on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003881{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003882 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003883 * then call the backend restore function, which will switch
3884 * back to the vt we launched from or ungrab X etc and then
3885 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003886 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003887 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003888 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003889
Peter Maatmane5b42e42013-03-27 22:38:53 +01003890 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003891
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003892 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003893
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003894 segv_compositor->restore(segv_compositor);
3895
3896 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003897}
3898
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003899WL_EXPORT void *
3900weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003901{
3902 char path[PATH_MAX];
3903 void *module, *init;
3904
3905 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003906 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003907 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003908 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003909
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003910 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3911 if (module) {
3912 weston_log("Module '%s' already loaded\n", path);
3913 dlclose(module);
3914 return NULL;
3915 }
3916
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003917 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003918 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003919 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003920 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003921 return NULL;
3922 }
3923
3924 init = dlsym(module, entrypoint);
3925 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003926 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003927 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003928 return NULL;
3929 }
3930
3931 return init;
3932}
3933
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003934static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003935load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003936 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003937{
3938 const char *p, *end;
3939 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003940 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003941 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003942
3943 if (modules == NULL)
3944 return 0;
3945
3946 p = modules;
3947 while (*p) {
3948 end = strchrnul(p, ',');
3949 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003950 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003951 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003952 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003953 p = end;
3954 while (*p == ',')
3955 p++;
3956
3957 }
3958
3959 return 0;
3960}
3961
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003962static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003963 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3964
3965static const char xdg_wrong_message[] =
3966 "fatal: environment variable XDG_RUNTIME_DIR\n"
3967 "is set to \"%s\", which is not a directory.\n";
3968
3969static const char xdg_wrong_mode_message[] =
3970 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3971 "correctly. Unix access mode must be 0700 but is %o,\n"
3972 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003973 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003974
3975static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003976 "Refer to your distribution on how to get it, or\n"
3977 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3978 "on how to implement it.\n";
3979
Martin Minarik37032f82012-06-18 20:15:18 +02003980static void
3981verify_xdg_runtime_dir(void)
3982{
3983 char *dir = getenv("XDG_RUNTIME_DIR");
3984 struct stat s;
3985
3986 if (!dir) {
3987 weston_log(xdg_error_message);
3988 weston_log_continue(xdg_detail_message);
3989 exit(EXIT_FAILURE);
3990 }
3991
3992 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3993 weston_log(xdg_wrong_message, dir);
3994 weston_log_continue(xdg_detail_message);
3995 exit(EXIT_FAILURE);
3996 }
3997
3998 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3999 weston_log(xdg_wrong_mode_message,
4000 dir, s.st_mode & 0777, s.st_uid);
4001 weston_log_continue(xdg_detail_message);
4002 }
4003}
4004
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004005static int
4006usage(int error_code)
4007{
4008 fprintf(stderr,
4009 "Usage: weston [OPTIONS]\n\n"
4010 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4011 "Weston supports multiple backends, and depending on which backend is in use\n"
4012 "different options will be accepted.\n\n"
4013
4014
4015 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004016 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004017 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004018 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4019 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004020 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004021 " -S, --socket=NAME\tName of socket to listen on\n"
4022 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004023 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004024 " --log==FILE\t\tLog to the given file\n"
4025 " -h, --help\t\tThis help message\n\n");
4026
4027 fprintf(stderr,
4028 "Options for drm-backend.so:\n\n"
4029 " --connector=ID\tBring up only this connector\n"
4030 " --seat=SEAT\t\tThe seat that weston should run on\n"
4031 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004032 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004033 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4034
4035 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004036 "Options for fbdev-backend.so:\n\n"
4037 " --tty=TTY\t\tThe tty to use\n"
4038 " --device=DEVICE\tThe framebuffer device to use\n\n");
4039
4040 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004041 "Options for x11-backend.so:\n\n"
4042 " --width=WIDTH\t\tWidth of X window\n"
4043 " --height=HEIGHT\tHeight of X window\n"
4044 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004045 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004046 " --output-count=COUNT\tCreate multiple outputs\n"
4047 " --no-input\t\tDont create input devices\n\n");
4048
4049 fprintf(stderr,
4050 "Options for wayland-backend.so:\n\n"
4051 " --width=WIDTH\t\tWidth of Wayland surface\n"
4052 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004053 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004054 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004055 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004056 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004057 " --display=DISPLAY\tWayland display to connect to\n\n");
4058
Pekka Paalanene31e0532013-05-22 18:03:07 +03004059#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4060 fprintf(stderr,
4061 "Options for rpi-backend.so:\n\n"
4062 " --tty=TTY\t\tThe tty to use\n"
4063 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4064 " --transform=TR\tThe output transformation, TR is one of:\n"
4065 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004066 " --opaque-regions\tEnable support for opaque regions, can be "
4067 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004068 "\n");
4069#endif
4070
Hardeningc077a842013-07-08 00:51:35 +02004071#if defined(BUILD_RDP_COMPOSITOR)
4072 fprintf(stderr,
4073 "Options for rdp-backend.so:\n\n"
4074 " --width=WIDTH\t\tWidth of desktop\n"
4075 " --height=HEIGHT\tHeight of desktop\n"
4076 " --extra-modes=MODES\t\n"
4077 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4078 " --address=ADDR\tThe address to bind\n"
4079 " --port=PORT\tThe port to listen on\n"
4080 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4081 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4082 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4083 "\n");
4084#endif
4085
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004086 exit(error_code);
4087}
4088
Peter Maatmane5b42e42013-03-27 22:38:53 +01004089static void
4090catch_signals(void)
4091{
4092 struct sigaction action;
4093
4094 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4095 action.sa_sigaction = on_caught_signal;
4096 sigemptyset(&action.sa_mask);
4097 sigaction(SIGSEGV, &action, NULL);
4098 sigaction(SIGABRT, &action, NULL);
4099}
4100
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004101int main(int argc, char *argv[])
4102{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004103 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004104 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004105 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004106 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004107 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004108 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004109 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004110 int *argc, char *argv[],
4111 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07004112 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004113 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004114 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004115 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004116 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004117 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004118 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004119 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004120 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06004121 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004122 struct weston_config *config;
4123 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004124
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004125 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004126 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05004127 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004128 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4129 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004130 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004131 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004132 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004133 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004134 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004135
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004136 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004137
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004138 if (help)
4139 usage(EXIT_SUCCESS);
4140
Scott Moreau12245142012-08-29 15:15:58 -06004141 if (version) {
4142 printf(PACKAGE_STRING "\n");
4143 return EXIT_SUCCESS;
4144 }
4145
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004146 weston_log_file_open(log);
4147
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004148 weston_log("%s\n"
4149 STAMP_SPACE "%s\n"
4150 STAMP_SPACE "Bug reports to: %s\n"
4151 STAMP_SPACE "Build: %s\n",
4152 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004153 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004154 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004155
Martin Minarik37032f82012-06-18 20:15:18 +02004156 verify_xdg_runtime_dir();
4157
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004158 display = wl_display_create();
4159
Tiago Vignatti2116b892011-08-08 05:52:59 -07004160 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004161 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4162 display);
4163 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4164 display);
4165 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4166 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004167
4168 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004169 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4170 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004171
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004172 config = weston_config_parse("weston.ini");
4173 if (config != NULL) {
4174 weston_log("Using config file '%s'\n",
4175 weston_config_get_full_path(config));
4176 } else {
4177 weston_log("Starting with no config file.\n");
4178 }
4179 section = weston_config_get_section(config, "core", NULL, NULL);
4180
4181 weston_config_section_get_string(section, "backend", &backend, NULL);
4182 if (option_backend) {
4183 backend = option_backend;
4184 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004185 if (!backend) {
4186 if (getenv("WAYLAND_DISPLAY"))
4187 backend = "wayland-backend.so";
4188 else if (getenv("DISPLAY"))
4189 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004190 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02004191 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004192 }
4193
Jason Ekstranda985da42013-08-22 17:28:03 -05004194 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004195
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004196 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004197 if (!backend_init)
4198 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004199
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004200 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004201 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004202 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004203 exit(EXIT_FAILURE);
4204 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004205
Peter Maatmane5b42e42013-03-27 22:38:53 +01004206 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004207 segv_compositor = ec;
4208
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004209 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004210 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004211
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004212 setenv("WAYLAND_DISPLAY", socket_name, 1);
4213
Jason Ekstranda985da42013-08-22 17:28:03 -05004214 if (!shell)
4215 weston_config_section_get_string(section, "shell", &shell,
4216 "desktop-shell.so");
4217 if (load_modules(ec, shell, &argc, argv) < 0)
4218 goto out;
4219
Ossama Othmana50e6e42013-05-14 09:48:26 -07004220 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004221 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004222 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004223 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004224
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004225 for (i = 1; i < argc; i++)
4226 weston_log("fatal: unhandled option: %s\n", argv[i]);
4227 if (argc > 1) {
4228 ret = EXIT_FAILURE;
4229 goto out;
4230 }
4231
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004232 weston_compositor_log_capabilities(ec);
4233
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004234 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004235 weston_log("fatal: failed to add socket: %m\n");
4236 ret = EXIT_FAILURE;
4237 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004238 }
4239
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004240 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004241
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004242 wl_display_run(display);
4243
4244 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004245 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004246 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004247
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004248 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004249
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004250 for (i = ARRAY_LENGTH(signals); i;)
4251 wl_event_source_remove(signals[--i]);
4252
Daniel Stone855028f2012-05-01 20:37:10 +01004253 weston_compositor_xkb_destroy(ec);
4254
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004255 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004256 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004257
Martin Minarik19e6f262012-06-07 13:08:46 +02004258 weston_log_file_close();
4259
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004260 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004261}