blob: 8bb381da96fdaf4063ba7b8064dae003f6dc81c7 [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);
Ander Conselvan de Oliveira24dff2b2013-12-19 18:34:28 +0200416
417 wl_list_remove(&ev->output_move_listener.link);
418 wl_list_remove(&ev->output_destroy_listener.link);
419
420 wl_list_init(&ev->output_move_listener.link);
421 wl_list_init(&ev->output_destroy_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200422}
423
Jason Ekstranda7af7042013-10-12 22:38:11 -0500424WL_EXPORT struct weston_view *
425weston_view_create(struct weston_surface *surface)
426{
427 struct weston_view *view;
428
429 view = calloc(1, sizeof *view);
430 if (view == NULL)
431 return NULL;
432
433 view->surface = surface;
434
Jason Ekstranda7af7042013-10-12 22:38:11 -0500435 /* Assign to surface */
436 wl_list_insert(&surface->views, &view->surface_link);
437
438 wl_signal_init(&view->destroy_signal);
439 wl_list_init(&view->link);
440 wl_list_init(&view->layer_link);
441
Xiong Zhang97116532013-10-23 13:58:31 +0800442 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500443
444 pixman_region32_init(&view->clip);
445
446 view->alpha = 1.0;
447 pixman_region32_init(&view->transform.opaque);
448
449 wl_list_init(&view->geometry.transformation_list);
450 wl_list_insert(&view->geometry.transformation_list,
451 &view->transform.position.link);
452 weston_matrix_init(&view->transform.position.matrix);
453 wl_list_init(&view->geometry.child_list);
454 pixman_region32_init(&view->transform.boundingbox);
455 view->transform.dirty = 1;
456
457 view->output = NULL;
458
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200459 view->output_move_listener.notify = weston_view_output_move_handler;
Kristian Høgsbergdf42a802013-12-17 14:58:19 -0800460 wl_list_init(&view->output_move_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200461 view->output_destroy_listener.notify =
462 weston_view_output_destroy_handler;
Kristian Høgsbergdf42a802013-12-17 14:58:19 -0800463 wl_list_init(&view->output_destroy_listener.link);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200464
Jason Ekstranda7af7042013-10-12 22:38:11 -0500465 return view;
466}
467
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500468WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500469weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500470{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500471 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400472
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200473 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400474 if (surface == NULL)
475 return NULL;
476
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500477 wl_signal_init(&surface->destroy_signal);
478
479 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500480
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500481 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200482 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400483
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100484 surface->buffer_viewport.transform = WL_OUTPUT_TRANSFORM_NORMAL;
485 surface->buffer_viewport.scale = 1;
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200486 surface->buffer_viewport.viewport_set = 0;
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100487 surface->pending.buffer_viewport = surface->buffer_viewport;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400488 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100489 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200490
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200491 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100492
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400493 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500494 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300495 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400496
Jason Ekstranda7af7042013-10-12 22:38:11 -0500497 wl_list_init(&surface->views);
498
499 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500500
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300501 surface->pending.buffer_destroy_listener.notify =
502 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300503 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300504 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300505 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300506 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300507
Pekka Paalanene67858b2013-04-25 13:57:42 +0300508 wl_list_init(&surface->subsurface_list);
509 wl_list_init(&surface->subsurface_list_pending);
510
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400511 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500512}
513
Alex Wu8811bf92012-02-28 18:07:54 +0800514WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500515weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200516 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500517{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100518 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500519}
520
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400521WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500522weston_view_to_global_float(struct weston_view *view,
523 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200524{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500525 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200526 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
527
Jason Ekstranda7af7042013-10-12 22:38:11 -0500528 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200529
530 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200531 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700532 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200533 v.f[3]);
534 *x = 0;
535 *y = 0;
536 return;
537 }
538
539 *x = v.f[0] / v.f[3];
540 *y = v.f[1] / v.f[3];
541 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500542 *x = sx + view->geometry.x;
543 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200544 }
545}
546
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500547WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200548weston_transformed_coord(int width, int height,
549 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200550 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200551 float sx, float sy, float *bx, float *by)
552{
553 switch (transform) {
554 case WL_OUTPUT_TRANSFORM_NORMAL:
555 default:
556 *bx = sx;
557 *by = sy;
558 break;
559 case WL_OUTPUT_TRANSFORM_FLIPPED:
560 *bx = width - sx;
561 *by = sy;
562 break;
563 case WL_OUTPUT_TRANSFORM_90:
564 *bx = height - sy;
565 *by = sx;
566 break;
567 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
568 *bx = height - sy;
569 *by = width - sx;
570 break;
571 case WL_OUTPUT_TRANSFORM_180:
572 *bx = width - sx;
573 *by = height - sy;
574 break;
575 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
576 *bx = sx;
577 *by = height - sy;
578 break;
579 case WL_OUTPUT_TRANSFORM_270:
580 *bx = sy;
581 *by = width - sx;
582 break;
583 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
584 *bx = sy;
585 *by = sx;
586 break;
587 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200588
589 *bx *= scale;
590 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200591}
592
593WL_EXPORT pixman_box32_t
594weston_transformed_rect(int width, int height,
595 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200596 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200597 pixman_box32_t rect)
598{
599 float x1, x2, y1, y2;
600
601 pixman_box32_t ret;
602
Alexander Larsson4ea95522013-05-22 14:41:37 +0200603 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200604 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200605 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200606 rect.x2, rect.y2, &x2, &y2);
607
608 if (x1 <= x2) {
609 ret.x1 = x1;
610 ret.x2 = x2;
611 } else {
612 ret.x1 = x2;
613 ret.x2 = x1;
614 }
615
616 if (y1 <= y2) {
617 ret.y1 = y1;
618 ret.y2 = y2;
619 } else {
620 ret.y1 = y2;
621 ret.y2 = y1;
622 }
623
624 return ret;
625}
626
627WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500628weston_transformed_region(int width, int height,
629 enum wl_output_transform transform,
630 int32_t scale,
631 pixman_region32_t *src, pixman_region32_t *dest)
632{
633 pixman_box32_t *src_rects, *dest_rects;
634 int nrects, i;
635
636 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
637 if (src != dest)
638 pixman_region32_copy(dest, src);
639 return;
640 }
641
642 src_rects = pixman_region32_rectangles(src, &nrects);
643 dest_rects = malloc(nrects * sizeof(*dest_rects));
644 if (!dest_rects)
645 return;
646
647 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
648 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
649 } else {
650 for (i = 0; i < nrects; i++) {
651 switch (transform) {
652 default:
653 case WL_OUTPUT_TRANSFORM_NORMAL:
654 dest_rects[i].x1 = src_rects[i].x1;
655 dest_rects[i].y1 = src_rects[i].y1;
656 dest_rects[i].x2 = src_rects[i].x2;
657 dest_rects[i].y2 = src_rects[i].y2;
658 break;
659 case WL_OUTPUT_TRANSFORM_90:
660 dest_rects[i].x1 = height - src_rects[i].y2;
661 dest_rects[i].y1 = src_rects[i].x1;
662 dest_rects[i].x2 = height - src_rects[i].y1;
663 dest_rects[i].y2 = src_rects[i].x2;
664 break;
665 case WL_OUTPUT_TRANSFORM_180:
666 dest_rects[i].x1 = width - src_rects[i].x2;
667 dest_rects[i].y1 = height - src_rects[i].y2;
668 dest_rects[i].x2 = width - src_rects[i].x1;
669 dest_rects[i].y2 = height - src_rects[i].y1;
670 break;
671 case WL_OUTPUT_TRANSFORM_270:
672 dest_rects[i].x1 = src_rects[i].y1;
673 dest_rects[i].y1 = width - src_rects[i].x2;
674 dest_rects[i].x2 = src_rects[i].y2;
675 dest_rects[i].y2 = width - src_rects[i].x1;
676 break;
677 case WL_OUTPUT_TRANSFORM_FLIPPED:
678 dest_rects[i].x1 = width - src_rects[i].x2;
679 dest_rects[i].y1 = src_rects[i].y1;
680 dest_rects[i].x2 = width - src_rects[i].x1;
681 dest_rects[i].y2 = src_rects[i].y2;
682 break;
683 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
684 dest_rects[i].x1 = height - src_rects[i].y2;
685 dest_rects[i].y1 = width - src_rects[i].x2;
686 dest_rects[i].x2 = height - src_rects[i].y1;
687 dest_rects[i].y2 = width - src_rects[i].x1;
688 break;
689 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
690 dest_rects[i].x1 = src_rects[i].x1;
691 dest_rects[i].y1 = height - src_rects[i].y2;
692 dest_rects[i].x2 = src_rects[i].x2;
693 dest_rects[i].y2 = height - src_rects[i].y1;
694 break;
695 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
696 dest_rects[i].x1 = src_rects[i].y1;
697 dest_rects[i].y1 = src_rects[i].x1;
698 dest_rects[i].x2 = src_rects[i].y2;
699 dest_rects[i].y2 = src_rects[i].x2;
700 break;
701 }
702 }
703 }
704
705 if (scale != 1) {
706 for (i = 0; i < nrects; i++) {
707 dest_rects[i].x1 *= scale;
708 dest_rects[i].x2 *= scale;
709 dest_rects[i].y1 *= scale;
710 dest_rects[i].y2 *= scale;
711 }
712 }
713
714 pixman_region32_clear(dest);
715 pixman_region32_init_rects(dest, dest_rects, nrects);
716 free(dest_rects);
717}
718
Jonny Lamb74130762013-11-26 18:19:46 +0100719static void
720scaler_surface_to_buffer(struct weston_surface *surface,
721 float sx, float sy, float *bx, float *by)
722{
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200723 if (surface->buffer_viewport.viewport_set) {
Jonny Lamb74130762013-11-26 18:19:46 +0100724 double a, b;
725
726 a = sx / surface->buffer_viewport.dst_width;
727 b = a * wl_fixed_to_double(surface->buffer_viewport.src_width);
728 *bx = b + wl_fixed_to_double(surface->buffer_viewport.src_x);
729
730 a = sy / surface->buffer_viewport.dst_height;
731 b = a * wl_fixed_to_double(surface->buffer_viewport.src_height);
732 *by = b + wl_fixed_to_double(surface->buffer_viewport.src_y);
733 } else {
734 *bx = sx;
735 *by = sy;
736 }
737}
738
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500739WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200740weston_surface_to_buffer_float(struct weston_surface *surface,
741 float sx, float sy, float *bx, float *by)
742{
Jonny Lamb74130762013-11-26 18:19:46 +0100743 /* first transform coordinates if the scaler is set */
744 scaler_surface_to_buffer(surface, sx, sy, bx, by);
745
Jason Ekstranda7af7042013-10-12 22:38:11 -0500746 weston_transformed_coord(surface->width,
747 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100748 surface->buffer_viewport.transform,
749 surface->buffer_viewport.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100750 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200751}
752
Alexander Larsson4ea95522013-05-22 14:41:37 +0200753WL_EXPORT void
754weston_surface_to_buffer(struct weston_surface *surface,
755 int sx, int sy, int *bx, int *by)
756{
757 float bxf, byf;
758
Jonny Lamb74130762013-11-26 18:19:46 +0100759 weston_surface_to_buffer_float(surface,
760 sx, sy, &bxf, &byf);
761
Alexander Larsson4ea95522013-05-22 14:41:37 +0200762 *bx = floorf(bxf);
763 *by = floorf(byf);
764}
765
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200766WL_EXPORT pixman_box32_t
767weston_surface_to_buffer_rect(struct weston_surface *surface,
768 pixman_box32_t rect)
769{
Jonny Lamb74130762013-11-26 18:19:46 +0100770 float xf, yf;
771
772 /* first transform box coordinates if the scaler is set */
773 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
774 rect.x1 = floorf(xf);
775 rect.y1 = floorf(yf);
776
777 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
778 rect.x2 = floorf(xf);
779 rect.y2 = floorf(yf);
780
Jason Ekstranda7af7042013-10-12 22:38:11 -0500781 return weston_transformed_rect(surface->width,
782 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100783 surface->buffer_viewport.transform,
784 surface->buffer_viewport.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200785 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200786}
787
788WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500789weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400790 struct weston_plane *plane)
791{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500792 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400793 return;
794
Jason Ekstranda7af7042013-10-12 22:38:11 -0500795 weston_view_damage_below(view);
796 view->plane = plane;
797 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400798}
799
800WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500801weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200802{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500803 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200804
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500805 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500806 pixman_region32_subtract(&damage, &view->transform.boundingbox,
807 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800808 if (view->plane)
809 pixman_region32_union(&view->plane->damage,
810 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500811 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800812 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200813}
814
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400815static void
816weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
817{
818 uint32_t different = es->output_mask ^ mask;
819 uint32_t entered = mask & different;
820 uint32_t left = es->output_mask & different;
821 struct weston_output *output;
822 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500823 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400824
825 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500826 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400827 return;
828 if (different == 0)
829 return;
830
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500831 client = wl_resource_get_client(es->resource);
832
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400833 wl_list_for_each(output, &es->compositor->output_list, link) {
834 if (1 << output->id & different)
835 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500836 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400837 client);
838 if (resource == NULL)
839 continue;
840 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500841 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400842 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500843 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400844 }
845}
846
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200847
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400848static void
849weston_surface_assign_output(struct weston_surface *es)
850{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500851 struct weston_output *new_output;
852 struct weston_view *view;
853 pixman_region32_t region;
854 uint32_t max, area, mask;
855 pixman_box32_t *e;
856
857 new_output = NULL;
858 max = 0;
859 mask = 0;
860 pixman_region32_init(&region);
861 wl_list_for_each(view, &es->views, surface_link) {
862 if (!view->output)
863 continue;
864
865 pixman_region32_intersect(&region, &view->transform.boundingbox,
866 &view->output->region);
867
868 e = pixman_region32_extents(&region);
869 area = (e->x2 - e->x1) * (e->y2 - e->y1);
870
871 mask |= view->output_mask;
872
873 if (area >= max) {
874 new_output = view->output;
875 max = area;
876 }
877 }
878 pixman_region32_fini(&region);
879
880 es->output = new_output;
881 weston_surface_update_output_mask(es, mask);
882}
883
884static void
885weston_view_assign_output(struct weston_view *ev)
886{
887 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400888 struct weston_output *output, *new_output;
889 pixman_region32_t region;
890 uint32_t max, area, mask;
891 pixman_box32_t *e;
892
893 new_output = NULL;
894 max = 0;
895 mask = 0;
896 pixman_region32_init(&region);
897 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500898 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400899 &output->region);
900
901 e = pixman_region32_extents(&region);
902 area = (e->x2 - e->x1) * (e->y2 - e->y1);
903
904 if (area > 0)
905 mask |= 1 << output->id;
906
907 if (area >= max) {
908 new_output = output;
909 max = area;
910 }
911 }
912 pixman_region32_fini(&region);
913
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200914 if (ev->output_mask != 0) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200915 wl_list_remove(&ev->output_move_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200916 wl_list_remove(&ev->output_destroy_listener.link);
917 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200918
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200919 if (mask != 0) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200920 wl_signal_add(&new_output->move_signal,
921 &ev->output_move_listener);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200922 wl_signal_add(&new_output->destroy_signal,
923 &ev->output_destroy_listener);
924 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200925
Jason Ekstranda7af7042013-10-12 22:38:11 -0500926 ev->output = new_output;
927 ev->output_mask = mask;
928
929 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400930}
931
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200932static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500933view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
934 int32_t width, int32_t height,
935 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200936{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200937 float min_x = HUGE_VALF, min_y = HUGE_VALF;
938 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200939 int32_t s[4][2] = {
940 { sx, sy },
941 { sx, sy + height },
942 { sx + width, sy },
943 { sx + width, sy + height }
944 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200945 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200946 int i;
947
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300948 if (width == 0 || height == 0) {
949 /* avoid rounding empty bbox to 1x1 */
950 pixman_region32_init(bbox);
951 return;
952 }
953
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200954 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200955 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500956 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200957 if (x < min_x)
958 min_x = x;
959 if (x > max_x)
960 max_x = x;
961 if (y < min_y)
962 min_y = y;
963 if (y > max_y)
964 max_y = y;
965 }
966
Pekka Paalanen219b9822012-02-08 15:38:37 +0200967 int_x = floorf(min_x);
968 int_y = floorf(min_y);
969 pixman_region32_init_rect(bbox, int_x, int_y,
970 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200971}
972
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200973static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500974weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200975{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200977
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200978 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 view->geometry.x = roundf(view->geometry.x);
980 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200981
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500982 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500983 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
984 view->transform.position.matrix.d[12] = view->geometry.x;
985 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500986
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500988
Jason Ekstranda7af7042013-10-12 22:38:11 -0500989 view->transform.inverse = view->transform.position.matrix;
990 view->transform.inverse.d[12] = -view->geometry.x;
991 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500992
Jason Ekstranda7af7042013-10-12 22:38:11 -0500993 pixman_region32_init_rect(&view->transform.boundingbox,
994 view->geometry.x,
995 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600996 view->surface->width,
997 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500998
Jason Ekstranda7af7042013-10-12 22:38:11 -0500999 if (view->alpha == 1.0) {
1000 pixman_region32_copy(&view->transform.opaque,
1001 &view->surface->opaque);
1002 pixman_region32_translate(&view->transform.opaque,
1003 view->geometry.x,
1004 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001005 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001006}
1007
1008static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001009weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001010{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001011 struct weston_view *parent = view->geometry.parent;
1012 struct weston_matrix *matrix = &view->transform.matrix;
1013 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001014 struct weston_transform *tform;
1015
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001017
1018 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1020 view->transform.position.matrix.d[12] = view->geometry.x;
1021 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001022
1023 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001024 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001025 weston_matrix_multiply(matrix, &tform->matrix);
1026
Pekka Paalanen483243f2013-03-08 14:56:50 +02001027 if (parent)
1028 weston_matrix_multiply(matrix, &parent->transform.matrix);
1029
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001030 if (weston_matrix_invert(inverse, matrix) < 0) {
1031 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 weston_log("error: weston_view %p"
1033 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001034 return -1;
1035 }
1036
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001037 view_compute_bbox(view, 0, 0,
1038 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001039 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001040
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001041 return 0;
1042}
1043
1044WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001045weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001046{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001047 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001048
Jason Ekstranda7af7042013-10-12 22:38:11 -05001049 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001050 return;
1051
Pekka Paalanen483243f2013-03-08 14:56:50 +02001052 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001054
Jason Ekstranda7af7042013-10-12 22:38:11 -05001055 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001056
Jason Ekstranda7af7042013-10-12 22:38:11 -05001057 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001058
Jason Ekstranda7af7042013-10-12 22:38:11 -05001059 pixman_region32_fini(&view->transform.boundingbox);
1060 pixman_region32_fini(&view->transform.opaque);
1061 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001062
Pekka Paalanencd403622012-01-25 13:37:39 +02001063 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001064 if (view->geometry.transformation_list.next ==
1065 &view->transform.position.link &&
1066 view->geometry.transformation_list.prev ==
1067 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001068 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001069 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001070 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001071 if (weston_view_update_transform_enable(view) < 0)
1072 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001073 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001074
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001076
Jason Ekstranda7af7042013-10-12 22:38:11 -05001077 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001078
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079 wl_signal_emit(&view->surface->compositor->transform_signal,
1080 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001081}
1082
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001083WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001084weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001085{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001086 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001087
1088 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 * The invariant: if view->geometry.dirty, then all views
1090 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001091 * Corollary: if not parent->geometry.dirty, then all ancestors
1092 * are not dirty.
1093 */
1094
Jason Ekstranda7af7042013-10-12 22:38:11 -05001095 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001096 return;
1097
Jason Ekstranda7af7042013-10-12 22:38:11 -05001098 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001099
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001101 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001102 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001103}
1104
1105WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001106weston_view_to_global_fixed(struct weston_view *view,
1107 wl_fixed_t vx, wl_fixed_t vy,
1108 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001109{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001110 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001111
Jason Ekstranda7af7042013-10-12 22:38:11 -05001112 weston_view_to_global_float(view,
1113 wl_fixed_to_double(vx),
1114 wl_fixed_to_double(vy),
1115 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001116 *x = wl_fixed_from_double(xf);
1117 *y = wl_fixed_from_double(yf);
1118}
1119
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001120WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121weston_view_from_global_float(struct weston_view *view,
1122 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001123{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001125 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1126
Jason Ekstranda7af7042013-10-12 22:38:11 -05001127 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001128
1129 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001130 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001131 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001132 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 *vx = 0;
1134 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001135 return;
1136 }
1137
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 *vx = v.f[0] / v.f[3];
1139 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001140 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 *vx = x - view->geometry.x;
1142 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001143 }
1144}
1145
1146WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001147weston_view_from_global_fixed(struct weston_view *view,
1148 wl_fixed_t x, wl_fixed_t y,
1149 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001150{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001152
Jason Ekstranda7af7042013-10-12 22:38:11 -05001153 weston_view_from_global_float(view,
1154 wl_fixed_to_double(x),
1155 wl_fixed_to_double(y),
1156 &vxf, &vyf);
1157 *vx = wl_fixed_from_double(vxf);
1158 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001159}
1160
1161WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162weston_view_from_global(struct weston_view *view,
1163 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001164{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001166
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1168 *vx = floorf(vxf);
1169 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001170}
1171
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001172WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001173weston_surface_schedule_repaint(struct weston_surface *surface)
1174{
1175 struct weston_output *output;
1176
1177 wl_list_for_each(output, &surface->compositor->output_list, link)
1178 if (surface->output_mask & (1 << output->id))
1179 weston_output_schedule_repaint(output);
1180}
1181
1182WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001183weston_view_schedule_repaint(struct weston_view *view)
1184{
1185 struct weston_output *output;
1186
1187 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1188 if (view->output_mask & (1 << output->id))
1189 weston_output_schedule_repaint(output);
1190}
1191
1192WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001193weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001194{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001195 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 0, 0, surface->width,
1197 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001198
Kristian Høgsberg98238702012-08-03 16:29:12 -04001199 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001200}
1201
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001202WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001204{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001205 if (view->geometry.x == x && view->geometry.y == y)
1206 return;
1207
Jason Ekstranda7af7042013-10-12 22:38:11 -05001208 view->geometry.x = x;
1209 view->geometry.y = y;
1210 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001211}
1212
Pekka Paalanen483243f2013-03-08 14:56:50 +02001213static void
1214transform_parent_handle_parent_destroy(struct wl_listener *listener,
1215 void *data)
1216{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217 struct weston_view *view =
1218 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001219 geometry.parent_destroy_listener);
1220
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001222}
1223
1224WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001225weston_view_set_transform_parent(struct weston_view *view,
1226 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001227{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 if (view->geometry.parent) {
1229 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1230 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001231 }
1232
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001234
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001236 transform_parent_handle_parent_destroy;
1237 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001238 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001240 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001241 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001242 }
1243
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 weston_view_geometry_dirty(view);
1245}
1246
1247WL_EXPORT int
1248weston_view_is_mapped(struct weston_view *view)
1249{
1250 if (view->output)
1251 return 1;
1252 else
1253 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001254}
1255
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001256WL_EXPORT int
1257weston_surface_is_mapped(struct weston_surface *surface)
1258{
1259 if (surface->output)
1260 return 1;
1261 else
1262 return 0;
1263}
1264
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001265static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001266surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001267{
1268 struct weston_view *view;
1269
1270 if (surface->width == width && surface->height == height)
1271 return;
1272
1273 surface->width = width;
1274 surface->height = height;
1275
1276 wl_list_for_each(view, &surface->views, surface_link)
1277 weston_view_geometry_dirty(view);
1278}
1279
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001280WL_EXPORT void
1281weston_surface_set_size(struct weston_surface *surface,
1282 int32_t width, int32_t height)
1283{
1284 assert(!surface->resource);
1285 surface_set_size(surface, width, height);
1286}
1287
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001288static void
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001289weston_surface_set_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001290{
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001291 int32_t width, height;
1292
1293 if (!surface->buffer_ref.buffer) {
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001294 surface_set_size(surface, 0, 0);
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001295 return;
1296 }
1297
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02001298 if (surface->buffer_viewport.viewport_set) {
Jonny Lamb74130762013-11-26 18:19:46 +01001299 surface->width = surface->buffer_viewport.dst_width;
1300 surface->height = surface->buffer_viewport.dst_height;
1301 return;
1302 }
1303
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001304 switch (surface->buffer_viewport.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001305 case WL_OUTPUT_TRANSFORM_90:
1306 case WL_OUTPUT_TRANSFORM_270:
1307 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1308 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001309 width = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001310 height = surface->buffer_ref.buffer->width;
1311 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001312 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001313 width = surface->buffer_ref.buffer->width;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001314 height = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001315 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001316 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001317
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001318 width = width / surface->buffer_viewport.scale;
1319 height = height / surface->buffer_viewport.scale;
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001320 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001321}
1322
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001323WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001324weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001325{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001326 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001327
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001328 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001329
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001330 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001331}
1332
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333WL_EXPORT struct weston_view *
1334weston_compositor_pick_view(struct weston_compositor *compositor,
1335 wl_fixed_t x, wl_fixed_t y,
1336 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001337{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001339
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 wl_list_for_each(view, &compositor->view_list, link) {
1341 weston_view_from_global_fixed(view, x, y, vx, vy);
1342 if (pixman_region32_contains_point(&view->surface->input,
1343 wl_fixed_to_int(*vx),
1344 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001345 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001347 }
1348
1349 return NULL;
1350}
1351
1352static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001353weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001354{
Daniel Stone37816df2012-05-16 18:45:18 +01001355 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001356
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001357 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001358 return;
1359
Daniel Stone37816df2012-05-16 18:45:18 +01001360 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001361 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001362}
1363
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001364WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001366{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001367 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001368
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 if (!weston_view_is_mapped(view))
1370 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001371
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 weston_view_damage_below(view);
1373 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001374 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 wl_list_remove(&view->layer_link);
1376 wl_list_init(&view->layer_link);
1377 wl_list_remove(&view->link);
1378 wl_list_init(&view->link);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001379 wl_list_remove(&view->output_move_listener.link);
1380 wl_list_init(&view->output_move_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +02001381 wl_list_remove(&view->output_destroy_listener.link);
1382 wl_list_init(&view->output_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383 view->output_mask = 0;
1384 weston_surface_assign_output(view->surface);
1385
1386 if (weston_surface_is_mapped(view->surface))
1387 return;
1388
1389 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1390 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001391 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001393 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001394 NULL,
1395 wl_fixed_from_int(0),
1396 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001397 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001398 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001399 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001400}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001401
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402WL_EXPORT void
1403weston_surface_unmap(struct weston_surface *surface)
1404{
1405 struct weston_view *view;
1406
1407 wl_list_for_each(view, &surface->views, surface_link)
1408 weston_view_unmap(view);
1409 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001410}
1411
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001412struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001413 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001414 struct wl_list link;
1415};
1416
Jason Ekstranda7af7042013-10-12 22:38:11 -05001417WL_EXPORT void
1418weston_view_destroy(struct weston_view *view)
1419{
1420 wl_signal_emit(&view->destroy_signal, view);
1421
1422 assert(wl_list_empty(&view->geometry.child_list));
1423
1424 if (weston_view_is_mapped(view)) {
1425 weston_view_unmap(view);
1426 weston_compositor_build_view_list(view->surface->compositor);
1427 }
1428
1429 wl_list_remove(&view->link);
1430 wl_list_remove(&view->layer_link);
1431
1432 pixman_region32_fini(&view->clip);
1433 pixman_region32_fini(&view->transform.boundingbox);
1434
1435 weston_view_set_transform_parent(view, NULL);
1436
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437 wl_list_remove(&view->surface_link);
1438
1439 free(view);
1440}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001441
1442WL_EXPORT void
1443weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001444{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001445 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001447
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001448 if (--surface->ref_count > 0)
1449 return;
1450
1451 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1452
Pekka Paalanene67858b2013-04-25 13:57:42 +03001453 assert(wl_list_empty(&surface->subsurface_list_pending));
1454 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001455
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1457 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001458
Pekka Paalanenbc106382012-10-10 12:49:31 +03001459 wl_list_for_each_safe(cb, next,
1460 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001461 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001462
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001463 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001464 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001465 pixman_region32_fini(&surface->pending.damage);
1466
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001467 if (surface->pending.buffer)
1468 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1469
Pekka Paalanende685b82012-12-04 15:58:12 +02001470 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001471
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001472 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001473 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001474 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001475
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001476 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001477 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001478
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001479 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001480}
1481
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001482static void
1483destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001484{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001485 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001486
Giulio Camuffo0d379742013-11-15 22:06:15 +01001487 /* Set the resource to NULL, since we don't want to leave a
1488 * dangling pointer if the surface was refcounted and survives
1489 * the weston_surface_destroy() call. */
1490 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001491 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001492}
1493
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001494static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001495weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1496{
1497 struct weston_buffer *buffer =
1498 container_of(listener, struct weston_buffer, destroy_listener);
1499
1500 wl_signal_emit(&buffer->destroy_signal, buffer);
1501 free(buffer);
1502}
1503
1504struct weston_buffer *
1505weston_buffer_from_resource(struct wl_resource *resource)
1506{
1507 struct weston_buffer *buffer;
1508 struct wl_listener *listener;
1509
1510 listener = wl_resource_get_destroy_listener(resource,
1511 weston_buffer_destroy_handler);
1512
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001513 if (listener)
1514 return container_of(listener, struct weston_buffer,
1515 destroy_listener);
1516
1517 buffer = zalloc(sizeof *buffer);
1518 if (buffer == NULL)
1519 return NULL;
1520
1521 buffer->resource = resource;
1522 wl_signal_init(&buffer->destroy_signal);
1523 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001524 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001525 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001526
1527 return buffer;
1528}
1529
1530static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001531weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1532 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001533{
Pekka Paalanende685b82012-12-04 15:58:12 +02001534 struct weston_buffer_reference *ref =
1535 container_of(listener, struct weston_buffer_reference,
1536 destroy_listener);
1537
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001538 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001539 ref->buffer = NULL;
1540}
1541
1542WL_EXPORT void
1543weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001544 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001545{
1546 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001547 ref->buffer->busy_count--;
1548 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001549 assert(wl_resource_get_client(ref->buffer->resource));
1550 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001551 WL_BUFFER_RELEASE);
1552 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001553 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001554 }
1555
Pekka Paalanende685b82012-12-04 15:58:12 +02001556 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001557 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001558 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001559 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001560 }
1561
Pekka Paalanende685b82012-12-04 15:58:12 +02001562 ref->buffer = buffer;
1563 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1564}
1565
1566static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001567weston_surface_attach(struct weston_surface *surface,
1568 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001569{
1570 weston_buffer_reference(&surface->buffer_ref, buffer);
1571
Pekka Paalanena6421c42012-12-04 15:58:10 +02001572 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001573 if (weston_surface_is_mapped(surface))
1574 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001575 }
1576
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001577 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001578}
1579
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001580WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001581weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001582{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001583 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001584
1585 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001586 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001587}
1588
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001589WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001590weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001591{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001592 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001593
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001594 pixman_region32_union(&compositor->primary_plane.damage,
1595 &compositor->primary_plane.damage,
1596 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001597 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001598}
1599
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001600static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001601surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001602{
Pekka Paalanende685b82012-12-04 15:58:12 +02001603 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001604 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001605 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001606
Jason Ekstranda7af7042013-10-12 22:38:11 -05001607 empty_region(&surface->damage);
1608}
1609
1610static void
1611view_accumulate_damage(struct weston_view *view,
1612 pixman_region32_t *opaque)
1613{
1614 pixman_region32_t damage;
1615
1616 pixman_region32_init(&damage);
1617 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001618 pixman_box32_t *extents;
1619
Jason Ekstranda7af7042013-10-12 22:38:11 -05001620 extents = pixman_region32_extents(&view->surface->damage);
1621 view_compute_bbox(view, extents->x1, extents->y1,
1622 extents->x2 - extents->x1,
1623 extents->y2 - extents->y1,
1624 &damage);
1625 pixman_region32_translate(&damage,
1626 -view->plane->x,
1627 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001628 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001629 pixman_region32_copy(&damage, &view->surface->damage);
1630 pixman_region32_translate(&damage,
1631 view->geometry.x - view->plane->x,
1632 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001633 }
1634
Jason Ekstranda7af7042013-10-12 22:38:11 -05001635 pixman_region32_subtract(&damage, &damage, opaque);
1636 pixman_region32_union(&view->plane->damage,
1637 &view->plane->damage, &damage);
1638 pixman_region32_fini(&damage);
1639 pixman_region32_copy(&view->clip, opaque);
1640 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001641}
1642
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001643static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001644compositor_accumulate_damage(struct weston_compositor *ec)
1645{
1646 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001647 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001648 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001649
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001650 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001651
1652 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001653 pixman_region32_copy(&plane->clip, &clip);
1654
1655 pixman_region32_init(&opaque);
1656
Jason Ekstranda7af7042013-10-12 22:38:11 -05001657 wl_list_for_each(ev, &ec->view_list, link) {
1658 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001659 continue;
1660
Jason Ekstranda7af7042013-10-12 22:38:11 -05001661 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001662 }
1663
1664 pixman_region32_union(&clip, &clip, &opaque);
1665 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001666 }
1667
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001668 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001669
Jason Ekstranda7af7042013-10-12 22:38:11 -05001670 wl_list_for_each(ev, &ec->view_list, link)
1671 ev->surface->touched = 0;
1672
1673 wl_list_for_each(ev, &ec->view_list, link) {
1674 if (ev->surface->touched)
1675 continue;
1676 ev->surface->touched = 1;
1677
1678 surface_flush_damage(ev->surface);
1679
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001680 /* Both the renderer and the backend have seen the buffer
1681 * by now. If renderer needs the buffer, it has its own
1682 * reference set. If the backend wants to keep the buffer
1683 * around for migrating the surface into a non-primary plane
1684 * later, keep_buffer is true. Otherwise, drop the core
1685 * reference now, and allow early buffer release. This enables
1686 * clients to use single-buffering.
1687 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688 if (!ev->surface->keep_buffer)
1689 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001690 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001691}
1692
1693static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001694surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001695{
1696 struct weston_subsurface *sub;
1697
Pekka Paalanene67858b2013-04-25 13:57:42 +03001698 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001699 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001700 continue;
1701
Jason Ekstranda7af7042013-10-12 22:38:11 -05001702 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1703 wl_list_init(&sub->surface->views);
1704
1705 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001706 }
1707}
1708
1709static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001710surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001711{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001712 struct weston_subsurface *sub;
1713 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001714
Jason Ekstranda7af7042013-10-12 22:38:11 -05001715 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1716 if (sub->surface == surface)
1717 continue;
1718
1719 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1720 weston_view_destroy(view);
1721
1722 surface_free_unused_subsurface_views(sub->surface);
1723 }
1724}
1725
1726static void
1727view_list_add_subsurface_view(struct weston_compositor *compositor,
1728 struct weston_subsurface *sub,
1729 struct weston_view *parent)
1730{
1731 struct weston_subsurface *child;
1732 struct weston_view *view = NULL, *iv;
1733
1734 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1735 if (iv->geometry.parent == parent) {
1736 view = iv;
1737 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001738 }
1739 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001740
1741 if (view) {
1742 /* Put it back in the surface's list of views */
1743 wl_list_remove(&view->surface_link);
1744 wl_list_insert(&sub->surface->views, &view->surface_link);
1745 } else {
1746 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001747 weston_view_set_position(view,
1748 sub->position.x,
1749 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750 weston_view_set_transform_parent(view, parent);
1751 }
1752
1753 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001754
Pekka Paalanenb188e912013-11-19 14:03:35 +02001755 if (wl_list_empty(&sub->surface->subsurface_list)) {
1756 wl_list_insert(compositor->view_list.prev, &view->link);
1757 return;
1758 }
1759
1760 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1761 if (child->surface == sub->surface)
1762 wl_list_insert(compositor->view_list.prev, &view->link);
1763 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001764 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001765 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766}
1767
1768static void
1769view_list_add(struct weston_compositor *compositor,
1770 struct weston_view *view)
1771{
1772 struct weston_subsurface *sub;
1773
1774 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001775
Pekka Paalanenb188e912013-11-19 14:03:35 +02001776 if (wl_list_empty(&view->surface->subsurface_list)) {
1777 wl_list_insert(compositor->view_list.prev, &view->link);
1778 return;
1779 }
1780
1781 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1782 if (sub->surface == view->surface)
1783 wl_list_insert(compositor->view_list.prev, &view->link);
1784 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001785 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001786 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001787}
1788
1789static void
1790weston_compositor_build_view_list(struct weston_compositor *compositor)
1791{
1792 struct weston_view *view;
1793 struct weston_layer *layer;
1794
1795 wl_list_for_each(layer, &compositor->layer_list, link)
1796 wl_list_for_each(view, &layer->view_list, layer_link)
1797 surface_stash_subsurface_views(view->surface);
1798
1799 wl_list_init(&compositor->view_list);
1800 wl_list_for_each(layer, &compositor->layer_list, link) {
1801 wl_list_for_each(view, &layer->view_list, layer_link) {
1802 view_list_add(compositor, view);
1803 }
1804 }
1805
1806 wl_list_for_each(layer, &compositor->layer_list, link)
1807 wl_list_for_each(view, &layer->view_list, layer_link)
1808 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001809}
1810
David Herrmann1edf44c2013-10-22 17:11:26 +02001811static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001812weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001813{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001814 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001816 struct weston_animation *animation, *next;
1817 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001818 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001819 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001820 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001821
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001822 if (output->destroying)
1823 return 0;
1824
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001825 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001827
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001828 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001829 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001830 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001831 wl_list_for_each(ev, &ec->view_list, link)
1832 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001833
Pekka Paalanene67858b2013-04-25 13:57:42 +03001834 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 wl_list_for_each(ev, &ec->view_list, link) {
1836 /* Note: This operation is safe to do multiple times on the
1837 * same surface.
1838 */
1839 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001840 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001841 &ev->surface->frame_callback_list);
1842 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001843 }
1844 }
1845
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001846 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001847
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001848 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001849 pixman_region32_intersect(&output_damage,
1850 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001851 pixman_region32_subtract(&output_damage,
1852 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001853
Scott Moreauccbf29d2012-02-22 14:21:41 -07001854 if (output->dirty)
1855 weston_output_update_matrix(output);
1856
David Herrmann1edf44c2013-10-22 17:11:26 +02001857 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001858
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001859 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001860
Kristian Høgsbergef044142011-06-21 15:02:12 -04001861 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001862
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001863 weston_compositor_repick(ec);
1864 wl_event_loop_dispatch(ec->input_loop, 0);
1865
Jonas Ådahldb773762012-06-13 00:01:21 +02001866 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001867 wl_callback_send_done(cb->resource, msecs);
1868 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001869 }
1870
Scott Moreaud64cf212012-06-08 19:40:54 -06001871 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001872 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001873 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001874 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001875
1876 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001877}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001878
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001879static int
1880weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001881{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001882 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001883
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001884 wl_event_loop_dispatch(compositor->input_loop, 0);
1885
1886 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001887}
1888
1889WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001890weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001891{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001892 struct weston_compositor *compositor = output->compositor;
1893 struct wl_event_loop *loop =
1894 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001895 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001896
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001897 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001898
1899 if (output->repaint_needed &&
1900 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1901 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001902 r = weston_output_repaint(output, msecs);
1903 if (!r)
1904 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001905 }
1906
1907 output->repaint_scheduled = 0;
1908 if (compositor->input_loop_source)
1909 return;
1910
1911 fd = wl_event_loop_get_fd(compositor->input_loop);
1912 compositor->input_loop_source =
1913 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1914 weston_compositor_read_input, compositor);
1915}
1916
1917static void
1918idle_repaint(void *data)
1919{
1920 struct weston_output *output = data;
1921
Jonas Ådahle5a12252013-04-05 23:07:11 +02001922 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001923}
1924
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001925WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001926weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1927{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001928 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001929 if (below != NULL)
1930 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001931}
1932
1933WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001934weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001935{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001936 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001937 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001938
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001939 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1940 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001941 return;
1942
Kristian Høgsbergef044142011-06-21 15:02:12 -04001943 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001944 output->repaint_needed = 1;
1945 if (output->repaint_scheduled)
1946 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001947
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001948 wl_event_loop_add_idle(loop, idle_repaint, output);
1949 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001950
1951 if (compositor->input_loop_source) {
1952 wl_event_source_remove(compositor->input_loop_source);
1953 compositor->input_loop_source = NULL;
1954 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001955}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001956
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001957WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001958weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1959{
1960 struct weston_output *output;
1961
1962 wl_list_for_each(output, &compositor->output_list, link)
1963 weston_output_schedule_repaint(output);
1964}
1965
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001966static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001967surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001968{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001969 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001970}
1971
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001972static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001973surface_attach(struct wl_client *client,
1974 struct wl_resource *resource,
1975 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1976{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001977 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001978 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001979
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001980 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001981 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001982 if (buffer == NULL) {
1983 wl_client_post_no_memory(client);
1984 return;
1985 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001986 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001987
Pekka Paalanende685b82012-12-04 15:58:12 +02001988 /* Attach, attach, without commit in between does not send
1989 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001990 if (surface->pending.buffer)
1991 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001992
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001993 surface->pending.sx = sx;
1994 surface->pending.sy = sy;
1995 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001996 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001997 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001998 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001999 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002000 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002001}
2002
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002003static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002004surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002005 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002006 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002007{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002008 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002009
Pekka Paalanen8e159182012-10-10 12:49:25 +03002010 pixman_region32_union_rect(&surface->pending.damage,
2011 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002012 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002013}
2014
Kristian Høgsberg33418202011-08-16 23:01:28 -04002015static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002016destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002017{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002018 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002019
2020 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002021 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002022}
2023
2024static void
2025surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002026 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002027{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002028 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002029 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002030
2031 cb = malloc(sizeof *cb);
2032 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002033 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002034 return;
2035 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002036
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002037 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2038 callback);
2039 if (cb->resource == NULL) {
2040 free(cb);
2041 wl_resource_post_no_memory(resource);
2042 return;
2043 }
2044
Jason Ekstranda85118c2013-06-27 20:17:02 -05002045 wl_resource_set_implementation(cb->resource, NULL, cb,
2046 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002047
Pekka Paalanenbc106382012-10-10 12:49:31 +03002048 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002049}
2050
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002051static void
2052surface_set_opaque_region(struct wl_client *client,
2053 struct wl_resource *resource,
2054 struct wl_resource *region_resource)
2055{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002056 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002057 struct weston_region *region;
2058
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002059 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002060 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002061 pixman_region32_copy(&surface->pending.opaque,
2062 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002063 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03002064 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002065 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002066}
2067
2068static void
2069surface_set_input_region(struct wl_client *client,
2070 struct wl_resource *resource,
2071 struct wl_resource *region_resource)
2072{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002073 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002074 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002075
2076 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002077 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002078 pixman_region32_copy(&surface->pending.input,
2079 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002080 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002081 pixman_region32_fini(&surface->pending.input);
2082 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002083 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002084}
2085
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002086static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002087weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002088{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002089 struct weston_subsurface *sub;
2090
2091 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2092 parent_link_pending) {
2093 wl_list_remove(&sub->parent_link);
2094 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2095 }
2096}
2097
2098static void
2099weston_surface_commit(struct weston_surface *surface)
2100{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002101 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002102 pixman_region32_t opaque;
2103
Alexander Larsson4ea95522013-05-22 14:41:37 +02002104 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002105 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002106 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002107 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002108
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002109 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01002110 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002111 weston_surface_attach(surface, surface->pending.buffer);
2112
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002113 weston_surface_set_size_from_buffer(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01002114
2115 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02002116 surface->configure(surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002117 surface->pending.sx, surface->pending.sy);
Giulio Camuffo184df502013-02-21 11:29:21 +01002118
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05002119 if (surface->pending.buffer)
2120 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
2121 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11002122 surface->pending.sx = 0;
2123 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01002124 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002125
2126 /* wl_surface.damage */
2127 pixman_region32_union(&surface->damage, &surface->damage,
2128 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002129 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2130 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 surface->width,
2132 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002133 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002134
2135 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002136 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002137 surface->width,
2138 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002139 pixman_region32_intersect(&opaque,
2140 &opaque, &surface->pending.opaque);
2141
2142 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2143 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144 wl_list_for_each(view, &surface->views, surface_link)
2145 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002146 }
2147
2148 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002149
2150 /* wl_surface.set_input_region */
2151 pixman_region32_fini(&surface->input);
2152 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002153 surface->width,
2154 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002155 pixman_region32_intersect(&surface->input,
2156 &surface->input, &surface->pending.input);
2157
Pekka Paalanenbc106382012-10-10 12:49:31 +03002158 /* wl_surface.frame */
2159 wl_list_insert_list(&surface->frame_callback_list,
2160 &surface->pending.frame_callback_list);
2161 wl_list_init(&surface->pending.frame_callback_list);
2162
Pekka Paalanene67858b2013-04-25 13:57:42 +03002163 weston_surface_commit_subsurface_order(surface);
2164
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002165 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002166}
2167
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002168static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002169weston_subsurface_commit(struct weston_subsurface *sub);
2170
2171static void
2172weston_subsurface_parent_commit(struct weston_subsurface *sub,
2173 int parent_is_synchronized);
2174
2175static void
2176surface_commit(struct wl_client *client, struct wl_resource *resource)
2177{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002178 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002179 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2180
2181 if (sub) {
2182 weston_subsurface_commit(sub);
2183 return;
2184 }
2185
2186 weston_surface_commit(surface);
2187
2188 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2189 if (sub->surface != surface)
2190 weston_subsurface_parent_commit(sub, 0);
2191 }
2192}
2193
2194static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002195surface_set_buffer_transform(struct wl_client *client,
2196 struct wl_resource *resource, int transform)
2197{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002198 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002199
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002200 surface->pending.buffer_viewport.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002201}
2202
Alexander Larsson4ea95522013-05-22 14:41:37 +02002203static void
2204surface_set_buffer_scale(struct wl_client *client,
2205 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002206 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002207{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002208 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002209
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002210 surface->pending.buffer_viewport.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002211}
2212
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002213static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002214 surface_destroy,
2215 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002216 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002217 surface_frame,
2218 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002219 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002220 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002221 surface_set_buffer_transform,
2222 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002223};
2224
2225static void
2226compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002227 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002228{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002229 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002230 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002231
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002232 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002233 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002234 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002235 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002236 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002237
Jason Ekstranda85118c2013-06-27 20:17:02 -05002238 surface->resource =
2239 wl_resource_create(client, &wl_surface_interface,
2240 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002241 if (surface->resource == NULL) {
2242 weston_surface_destroy(surface);
2243 wl_resource_post_no_memory(resource);
2244 return;
2245 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002246 wl_resource_set_implementation(surface->resource, &surface_interface,
2247 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002248}
2249
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002250static void
2251destroy_region(struct wl_resource *resource)
2252{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002253 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002254
2255 pixman_region32_fini(&region->region);
2256 free(region);
2257}
2258
2259static void
2260region_destroy(struct wl_client *client, struct wl_resource *resource)
2261{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002262 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002263}
2264
2265static void
2266region_add(struct wl_client *client, struct wl_resource *resource,
2267 int32_t x, int32_t y, int32_t width, int32_t height)
2268{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002269 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002270
2271 pixman_region32_union_rect(&region->region, &region->region,
2272 x, y, width, height);
2273}
2274
2275static void
2276region_subtract(struct wl_client *client, struct wl_resource *resource,
2277 int32_t x, int32_t y, int32_t width, int32_t height)
2278{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002279 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002280 pixman_region32_t rect;
2281
2282 pixman_region32_init_rect(&rect, x, y, width, height);
2283 pixman_region32_subtract(&region->region, &region->region, &rect);
2284 pixman_region32_fini(&rect);
2285}
2286
2287static const struct wl_region_interface region_interface = {
2288 region_destroy,
2289 region_add,
2290 region_subtract
2291};
2292
2293static void
2294compositor_create_region(struct wl_client *client,
2295 struct wl_resource *resource, uint32_t id)
2296{
2297 struct weston_region *region;
2298
2299 region = malloc(sizeof *region);
2300 if (region == NULL) {
2301 wl_resource_post_no_memory(resource);
2302 return;
2303 }
2304
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002305 pixman_region32_init(&region->region);
2306
Jason Ekstranda85118c2013-06-27 20:17:02 -05002307 region->resource =
2308 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002309 if (region->resource == NULL) {
2310 free(region);
2311 wl_resource_post_no_memory(resource);
2312 return;
2313 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002314 wl_resource_set_implementation(region->resource, &region_interface,
2315 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002316}
2317
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002318static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002319 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002320 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002321};
2322
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002323static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002324weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2325{
2326 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002327 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002328 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002329
Alexander Larsson4ea95522013-05-22 14:41:37 +02002330 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002331 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002332 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002333 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002334
Pekka Paalanene67858b2013-04-25 13:57:42 +03002335 /* wl_surface.attach */
2336 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2337 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2338 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2339
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002340 weston_surface_set_size_from_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002341
2342 if (surface->configure && sub->cached.newly_attached)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002343 surface->configure(surface, sub->cached.sx, sub->cached.sy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002344 sub->cached.sx = 0;
2345 sub->cached.sy = 0;
2346 sub->cached.newly_attached = 0;
2347
2348 /* wl_surface.damage */
2349 pixman_region32_union(&surface->damage, &surface->damage,
2350 &sub->cached.damage);
2351 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2352 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002353 surface->width,
2354 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002355 empty_region(&sub->cached.damage);
2356
2357 /* wl_surface.set_opaque_region */
2358 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002359 surface->width,
2360 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002361 pixman_region32_intersect(&opaque,
2362 &opaque, &sub->cached.opaque);
2363
2364 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2365 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002366 wl_list_for_each(view, &surface->views, surface_link)
2367 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002368 }
2369
2370 pixman_region32_fini(&opaque);
2371
2372 /* wl_surface.set_input_region */
2373 pixman_region32_fini(&surface->input);
2374 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002375 surface->width,
2376 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002377 pixman_region32_intersect(&surface->input,
2378 &surface->input, &sub->cached.input);
2379
2380 /* wl_surface.frame */
2381 wl_list_insert_list(&surface->frame_callback_list,
2382 &sub->cached.frame_callback_list);
2383 wl_list_init(&sub->cached.frame_callback_list);
2384
2385 weston_surface_commit_subsurface_order(surface);
2386
2387 weston_surface_schedule_repaint(surface);
2388
2389 sub->cached.has_data = 0;
2390}
2391
2392static void
2393weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2394{
2395 struct weston_surface *surface = sub->surface;
2396
2397 /*
2398 * If this commit would cause the surface to move by the
2399 * attach(dx, dy) parameters, the old damage region must be
2400 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002401 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002402 */
2403 pixman_region32_translate(&sub->cached.damage,
2404 -surface->pending.sx, -surface->pending.sy);
2405 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2406 &surface->pending.damage);
2407 empty_region(&surface->pending.damage);
2408
2409 if (surface->pending.newly_attached) {
2410 sub->cached.newly_attached = 1;
2411 weston_buffer_reference(&sub->cached.buffer_ref,
2412 surface->pending.buffer);
2413 }
2414 sub->cached.sx += surface->pending.sx;
2415 sub->cached.sy += surface->pending.sy;
2416 surface->pending.sx = 0;
2417 surface->pending.sy = 0;
2418 surface->pending.newly_attached = 0;
2419
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002420 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002421
2422 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2423
2424 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2425
2426 wl_list_insert_list(&sub->cached.frame_callback_list,
2427 &surface->pending.frame_callback_list);
2428 wl_list_init(&surface->pending.frame_callback_list);
2429
2430 sub->cached.has_data = 1;
2431}
2432
2433static int
2434weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2435{
2436 while (sub) {
2437 if (sub->synchronized)
2438 return 1;
2439
2440 if (!sub->parent)
2441 return 0;
2442
2443 sub = weston_surface_to_subsurface(sub->parent);
2444 }
2445
2446 return 0;
2447}
2448
2449static void
2450weston_subsurface_commit(struct weston_subsurface *sub)
2451{
2452 struct weston_surface *surface = sub->surface;
2453 struct weston_subsurface *tmp;
2454
2455 /* Recursive check for effectively synchronized. */
2456 if (weston_subsurface_is_synchronized(sub)) {
2457 weston_subsurface_commit_to_cache(sub);
2458 } else {
2459 if (sub->cached.has_data) {
2460 /* flush accumulated state from cache */
2461 weston_subsurface_commit_to_cache(sub);
2462 weston_subsurface_commit_from_cache(sub);
2463 } else {
2464 weston_surface_commit(surface);
2465 }
2466
2467 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2468 if (tmp->surface != surface)
2469 weston_subsurface_parent_commit(tmp, 0);
2470 }
2471 }
2472}
2473
2474static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002475weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002476{
2477 struct weston_surface *surface = sub->surface;
2478 struct weston_subsurface *tmp;
2479
Pekka Paalanene67858b2013-04-25 13:57:42 +03002480 /* From now on, commit_from_cache the whole sub-tree, regardless of
2481 * the synchronized mode of each child. This sub-surface or some
2482 * of its ancestors were synchronized, so we are synchronized
2483 * all the way down.
2484 */
2485
2486 if (sub->cached.has_data)
2487 weston_subsurface_commit_from_cache(sub);
2488
2489 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2490 if (tmp->surface != surface)
2491 weston_subsurface_parent_commit(tmp, 1);
2492 }
2493}
2494
2495static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002496weston_subsurface_parent_commit(struct weston_subsurface *sub,
2497 int parent_is_synchronized)
2498{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002499 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002500 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002501 wl_list_for_each(view, &sub->surface->views, surface_link)
2502 weston_view_set_position(view,
2503 sub->position.x,
2504 sub->position.y);
2505
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002506 sub->position.set = 0;
2507 }
2508
2509 if (parent_is_synchronized || sub->synchronized)
2510 weston_subsurface_synchronized_commit(sub);
2511}
2512
2513static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002514subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002515{
2516 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002517 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002518
Jason Ekstranda7af7042013-10-12 22:38:11 -05002519 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002520 weston_view_set_position(view,
2521 view->geometry.x + dx,
2522 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002523
2524 /* No need to check parent mappedness, because if parent is not
2525 * mapped, parent is not in a visible layer, so this sub-surface
2526 * will not be drawn either.
2527 */
2528 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002529 /* Cannot call weston_surface_update_transform(),
2530 * because that would call it also for the parent surface,
2531 * which might not be mapped yet. That would lead to
2532 * inconsistent state, where the window could never be
2533 * mapped.
2534 *
2535 * Instead just assing any output, to make
2536 * weston_surface_is_mapped() return true, so that when the
2537 * parent surface does get mapped, this one will get
2538 * included, too. See surface_list_add().
2539 */
2540 assert(!wl_list_empty(&compositor->output_list));
2541 surface->output = container_of(compositor->output_list.next,
2542 struct weston_output, link);
2543 }
2544}
2545
2546static struct weston_subsurface *
2547weston_surface_to_subsurface(struct weston_surface *surface)
2548{
2549 if (surface->configure == subsurface_configure)
2550 return surface->configure_private;
2551
2552 return NULL;
2553}
2554
Pekka Paalanen01388e22013-04-25 13:57:44 +03002555WL_EXPORT struct weston_surface *
2556weston_surface_get_main_surface(struct weston_surface *surface)
2557{
2558 struct weston_subsurface *sub;
2559
2560 while (surface && (sub = weston_surface_to_subsurface(surface)))
2561 surface = sub->parent;
2562
2563 return surface;
2564}
2565
Pekka Paalanene67858b2013-04-25 13:57:42 +03002566static void
2567subsurface_set_position(struct wl_client *client,
2568 struct wl_resource *resource, int32_t x, int32_t y)
2569{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002570 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002571
2572 if (!sub)
2573 return;
2574
2575 sub->position.x = x;
2576 sub->position.y = y;
2577 sub->position.set = 1;
2578}
2579
2580static struct weston_subsurface *
2581subsurface_from_surface(struct weston_surface *surface)
2582{
2583 struct weston_subsurface *sub;
2584
2585 sub = weston_surface_to_subsurface(surface);
2586 if (sub)
2587 return sub;
2588
2589 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2590 if (sub->surface == surface)
2591 return sub;
2592
2593 return NULL;
2594}
2595
2596static struct weston_subsurface *
2597subsurface_sibling_check(struct weston_subsurface *sub,
2598 struct weston_surface *surface,
2599 const char *request)
2600{
2601 struct weston_subsurface *sibling;
2602
2603 sibling = subsurface_from_surface(surface);
2604
2605 if (!sibling) {
2606 wl_resource_post_error(sub->resource,
2607 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2608 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002609 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002610 return NULL;
2611 }
2612
2613 if (sibling->parent != sub->parent) {
2614 wl_resource_post_error(sub->resource,
2615 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2616 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002617 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002618 return NULL;
2619 }
2620
2621 return sibling;
2622}
2623
2624static void
2625subsurface_place_above(struct wl_client *client,
2626 struct wl_resource *resource,
2627 struct wl_resource *sibling_resource)
2628{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002629 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002630 struct weston_surface *surface =
2631 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002632 struct weston_subsurface *sibling;
2633
2634 if (!sub)
2635 return;
2636
2637 sibling = subsurface_sibling_check(sub, surface, "place_above");
2638 if (!sibling)
2639 return;
2640
2641 wl_list_remove(&sub->parent_link_pending);
2642 wl_list_insert(sibling->parent_link_pending.prev,
2643 &sub->parent_link_pending);
2644}
2645
2646static void
2647subsurface_place_below(struct wl_client *client,
2648 struct wl_resource *resource,
2649 struct wl_resource *sibling_resource)
2650{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002651 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002652 struct weston_surface *surface =
2653 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002654 struct weston_subsurface *sibling;
2655
2656 if (!sub)
2657 return;
2658
2659 sibling = subsurface_sibling_check(sub, surface, "place_below");
2660 if (!sibling)
2661 return;
2662
2663 wl_list_remove(&sub->parent_link_pending);
2664 wl_list_insert(&sibling->parent_link_pending,
2665 &sub->parent_link_pending);
2666}
2667
2668static void
2669subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2670{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002671 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002672
2673 if (sub)
2674 sub->synchronized = 1;
2675}
2676
2677static void
2678subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2679{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002680 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002681
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002682 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002683 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002684
2685 /* If sub became effectively desynchronized, flush. */
2686 if (!weston_subsurface_is_synchronized(sub))
2687 weston_subsurface_synchronized_commit(sub);
2688 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002689}
2690
2691static void
2692weston_subsurface_cache_init(struct weston_subsurface *sub)
2693{
2694 pixman_region32_init(&sub->cached.damage);
2695 pixman_region32_init(&sub->cached.opaque);
2696 pixman_region32_init(&sub->cached.input);
2697 wl_list_init(&sub->cached.frame_callback_list);
2698 sub->cached.buffer_ref.buffer = NULL;
2699}
2700
2701static void
2702weston_subsurface_cache_fini(struct weston_subsurface *sub)
2703{
2704 struct weston_frame_callback *cb, *tmp;
2705
2706 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002707 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002708
2709 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2710 pixman_region32_fini(&sub->cached.damage);
2711 pixman_region32_fini(&sub->cached.opaque);
2712 pixman_region32_fini(&sub->cached.input);
2713}
2714
2715static void
2716weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2717{
2718 wl_list_remove(&sub->parent_link);
2719 wl_list_remove(&sub->parent_link_pending);
2720 wl_list_remove(&sub->parent_destroy_listener.link);
2721 sub->parent = NULL;
2722}
2723
2724static void
2725weston_subsurface_destroy(struct weston_subsurface *sub);
2726
2727static void
2728subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2729{
2730 struct weston_subsurface *sub =
2731 container_of(listener, struct weston_subsurface,
2732 surface_destroy_listener);
2733 assert(data == &sub->surface->resource);
2734
2735 /* The protocol object (wl_resource) is left inert. */
2736 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002737 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002738
2739 weston_subsurface_destroy(sub);
2740}
2741
2742static void
2743subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2744{
2745 struct weston_subsurface *sub =
2746 container_of(listener, struct weston_subsurface,
2747 parent_destroy_listener);
2748 assert(data == &sub->parent->resource);
2749 assert(sub->surface != sub->parent);
2750
2751 if (weston_surface_is_mapped(sub->surface))
2752 weston_surface_unmap(sub->surface);
2753
2754 weston_subsurface_unlink_parent(sub);
2755}
2756
2757static void
2758subsurface_resource_destroy(struct wl_resource *resource)
2759{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002760 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002761
2762 if (sub)
2763 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002764}
2765
2766static void
2767subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2768{
2769 wl_resource_destroy(resource);
2770}
2771
2772static void
2773weston_subsurface_link_parent(struct weston_subsurface *sub,
2774 struct weston_surface *parent)
2775{
2776 sub->parent = parent;
2777 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002778 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002779 &sub->parent_destroy_listener);
2780
2781 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2782 wl_list_insert(&parent->subsurface_list_pending,
2783 &sub->parent_link_pending);
2784}
2785
2786static void
2787weston_subsurface_link_surface(struct weston_subsurface *sub,
2788 struct weston_surface *surface)
2789{
2790 sub->surface = surface;
2791 sub->surface_destroy_listener.notify =
2792 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002793 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002794 &sub->surface_destroy_listener);
2795}
2796
2797static void
2798weston_subsurface_destroy(struct weston_subsurface *sub)
2799{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002800 struct weston_view *view, *next;
2801
Pekka Paalanene67858b2013-04-25 13:57:42 +03002802 assert(sub->surface);
2803
2804 if (sub->resource) {
2805 assert(weston_surface_to_subsurface(sub->surface) == sub);
2806 assert(sub->parent_destroy_listener.notify ==
2807 subsurface_handle_parent_destroy);
2808
Jason Ekstranda7af7042013-10-12 22:38:11 -05002809 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2810 weston_view_destroy(view);
2811
Pekka Paalanene67858b2013-04-25 13:57:42 +03002812 if (sub->parent)
2813 weston_subsurface_unlink_parent(sub);
2814
2815 weston_subsurface_cache_fini(sub);
2816
2817 sub->surface->configure = NULL;
2818 sub->surface->configure_private = NULL;
2819 } else {
2820 /* the dummy weston_subsurface for the parent itself */
2821 assert(sub->parent_destroy_listener.notify == NULL);
2822 wl_list_remove(&sub->parent_link);
2823 wl_list_remove(&sub->parent_link_pending);
2824 }
2825
2826 wl_list_remove(&sub->surface_destroy_listener.link);
2827 free(sub);
2828}
2829
2830static const struct wl_subsurface_interface subsurface_implementation = {
2831 subsurface_destroy,
2832 subsurface_set_position,
2833 subsurface_place_above,
2834 subsurface_place_below,
2835 subsurface_set_sync,
2836 subsurface_set_desync
2837};
2838
2839static struct weston_subsurface *
2840weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2841 struct weston_surface *parent)
2842{
2843 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002844 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002845
2846 sub = calloc(1, sizeof *sub);
2847 if (!sub)
2848 return NULL;
2849
Jason Ekstranda7af7042013-10-12 22:38:11 -05002850 wl_list_init(&sub->unused_views);
2851
Jason Ekstranda85118c2013-06-27 20:17:02 -05002852 sub->resource =
2853 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002854 if (!sub->resource) {
2855 free(sub);
2856 return NULL;
2857 }
2858
Jason Ekstranda85118c2013-06-27 20:17:02 -05002859 wl_resource_set_implementation(sub->resource,
2860 &subsurface_implementation,
2861 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002862 weston_subsurface_link_surface(sub, surface);
2863 weston_subsurface_link_parent(sub, parent);
2864 weston_subsurface_cache_init(sub);
2865 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002866
2867 return sub;
2868}
2869
2870/* Create a dummy subsurface for having the parent itself in its
2871 * sub-surface lists. Makes stacking order manipulation easy.
2872 */
2873static struct weston_subsurface *
2874weston_subsurface_create_for_parent(struct weston_surface *parent)
2875{
2876 struct weston_subsurface *sub;
2877
2878 sub = calloc(1, sizeof *sub);
2879 if (!sub)
2880 return NULL;
2881
2882 weston_subsurface_link_surface(sub, parent);
2883 sub->parent = parent;
2884 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2885 wl_list_insert(&parent->subsurface_list_pending,
2886 &sub->parent_link_pending);
2887
2888 return sub;
2889}
2890
2891static void
2892subcompositor_get_subsurface(struct wl_client *client,
2893 struct wl_resource *resource,
2894 uint32_t id,
2895 struct wl_resource *surface_resource,
2896 struct wl_resource *parent_resource)
2897{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002898 struct weston_surface *surface =
2899 wl_resource_get_user_data(surface_resource);
2900 struct weston_surface *parent =
2901 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002902 struct weston_subsurface *sub;
2903 static const char where[] = "get_subsurface: wl_subsurface@";
2904
2905 if (surface == parent) {
2906 wl_resource_post_error(resource,
2907 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2908 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002909 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002910 return;
2911 }
2912
2913 if (weston_surface_to_subsurface(surface)) {
2914 wl_resource_post_error(resource,
2915 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2916 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002917 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002918 return;
2919 }
2920
2921 if (surface->configure) {
2922 wl_resource_post_error(resource,
2923 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2924 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002925 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002926 return;
2927 }
2928
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002929 if (weston_surface_get_main_surface(parent) == surface) {
2930 wl_resource_post_error(resource,
2931 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2932 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002933 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002934 return;
2935 }
2936
Pekka Paalanene67858b2013-04-25 13:57:42 +03002937 /* make sure the parent is in its own list */
2938 if (wl_list_empty(&parent->subsurface_list)) {
2939 if (!weston_subsurface_create_for_parent(parent)) {
2940 wl_resource_post_no_memory(resource);
2941 return;
2942 }
2943 }
2944
2945 sub = weston_subsurface_create(id, surface, parent);
2946 if (!sub) {
2947 wl_resource_post_no_memory(resource);
2948 return;
2949 }
2950
2951 surface->configure = subsurface_configure;
2952 surface->configure_private = sub;
2953}
2954
2955static void
2956subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2957{
2958 wl_resource_destroy(resource);
2959}
2960
2961static const struct wl_subcompositor_interface subcompositor_interface = {
2962 subcompositor_destroy,
2963 subcompositor_get_subsurface
2964};
2965
2966static void
2967bind_subcompositor(struct wl_client *client,
2968 void *data, uint32_t version, uint32_t id)
2969{
2970 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002971 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002972
Jason Ekstranda85118c2013-06-27 20:17:02 -05002973 resource =
2974 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002975 if (resource == NULL) {
2976 wl_client_post_no_memory(client);
2977 return;
2978 }
2979 wl_resource_set_implementation(resource, &subcompositor_interface,
2980 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002981}
2982
2983static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002984weston_compositor_dpms(struct weston_compositor *compositor,
2985 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002986{
2987 struct weston_output *output;
2988
2989 wl_list_for_each(output, &compositor->output_list, link)
2990 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002991 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002992}
2993
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002994WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002995weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002996{
Neil Roberts8b62e202013-09-30 13:14:47 +01002997 uint32_t old_state = compositor->state;
2998
2999 /* The state needs to be changed before emitting the wake
3000 * signal because that may try to schedule a repaint which
3001 * will not work if the compositor is still sleeping */
3002 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3003
3004 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003005 case WESTON_COMPOSITOR_SLEEPING:
3006 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3007 /* fall through */
3008 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003009 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003010 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003011 /* fall through */
3012 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003013 wl_event_source_timer_update(compositor->idle_source,
3014 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003015 }
3016}
3017
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003018WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003019weston_compositor_offscreen(struct weston_compositor *compositor)
3020{
3021 switch (compositor->state) {
3022 case WESTON_COMPOSITOR_OFFSCREEN:
3023 return;
3024 case WESTON_COMPOSITOR_SLEEPING:
3025 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3026 /* fall through */
3027 default:
3028 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3029 wl_event_source_timer_update(compositor->idle_source, 0);
3030 }
3031}
3032
3033WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003034weston_compositor_sleep(struct weston_compositor *compositor)
3035{
3036 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3037 return;
3038
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003039 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003040 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3041 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3042}
3043
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003044static int
3045idle_handler(void *data)
3046{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003047 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003048
3049 if (compositor->idle_inhibit)
3050 return 1;
3051
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003052 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003053 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003054
3055 return 1;
3056}
3057
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003058WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003059weston_plane_init(struct weston_plane *plane,
3060 struct weston_compositor *ec,
3061 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003062{
3063 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003064 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003065 plane->x = x;
3066 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003067 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003068
3069 /* Init the link so that the call to wl_list_remove() when releasing
3070 * the plane without ever stacking doesn't lead to a crash */
3071 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003072}
3073
3074WL_EXPORT void
3075weston_plane_release(struct weston_plane *plane)
3076{
Xiong Zhang97116532013-10-23 13:58:31 +08003077 struct weston_view *view;
3078
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003079 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003080 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003081
Xiong Zhang97116532013-10-23 13:58:31 +08003082 wl_list_for_each(view, &plane->compositor->view_list, link) {
3083 if (view->plane == plane)
3084 view->plane = NULL;
3085 }
3086
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003087 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003088}
3089
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003090WL_EXPORT void
3091weston_compositor_stack_plane(struct weston_compositor *ec,
3092 struct weston_plane *plane,
3093 struct weston_plane *above)
3094{
3095 if (above)
3096 wl_list_insert(above->link.prev, &plane->link);
3097 else
3098 wl_list_insert(&ec->plane_list, &plane->link);
3099}
3100
Casey Dahlin9074db52012-04-19 22:50:09 -04003101static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003102{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003103 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003104}
3105
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003106static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003107bind_output(struct wl_client *client,
3108 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003109{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003110 struct weston_output *output = data;
3111 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003112 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003113
Jason Ekstranda85118c2013-06-27 20:17:02 -05003114 resource = wl_resource_create(client, &wl_output_interface,
3115 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003116 if (resource == NULL) {
3117 wl_client_post_no_memory(client);
3118 return;
3119 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003120
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003121 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003122 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003123
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003124 wl_output_send_geometry(resource,
3125 output->x,
3126 output->y,
3127 output->mm_width,
3128 output->mm_height,
3129 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003130 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003131 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003132 if (version >= 2)
3133 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003134 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003135
3136 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003137 wl_output_send_mode(resource,
3138 mode->flags,
3139 mode->width,
3140 mode->height,
3141 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003142 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003143
3144 if (version >= 2)
3145 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003146}
3147
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003148/* Move other outputs when one is removed so the space remains contiguos. */
3149static void
3150weston_compositor_remove_output(struct weston_compositor *compositor,
3151 struct weston_output *remove_output)
3152{
3153 struct weston_output *output;
3154 int offset = 0;
3155
3156 wl_list_for_each(output, &compositor->output_list, link) {
3157 if (output == remove_output) {
3158 offset = output->width;
3159 continue;
3160 }
3161
3162 if (offset > 0) {
3163 weston_output_move(output,
3164 output->x - offset, output->y);
3165 output->dirty = 1;
3166 }
3167 }
3168}
3169
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02003170static void
3171weston_compositor_verify_pointers(struct weston_compositor *ec)
3172{
3173 struct weston_seat *seat;
3174
3175 wl_list_for_each(seat, &ec->seat_list, link) {
3176 if (!seat->pointer)
3177 continue;
3178
3179 weston_pointer_verify(seat->pointer);
3180 }
3181}
3182
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003183WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003184weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003185{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003186 output->destroying = 1;
3187
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003188 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003189 wl_list_remove(&output->link);
3190
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02003191 weston_compositor_verify_pointers(output->compositor);
3192
Richard Hughes64ddde12013-05-01 21:52:10 +01003193 wl_signal_emit(&output->destroy_signal, output);
3194
Richard Hughesafe690c2013-05-02 10:10:04 +01003195 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003196 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003197 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003198 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003199
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003200 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003201}
3202
Scott Moreau1bad5db2012-08-18 01:04:05 -06003203static void
3204weston_output_compute_transform(struct weston_output *output)
3205{
3206 struct weston_matrix transform;
3207 int flip;
3208
3209 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003210 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003211
3212 switch(output->transform) {
3213 case WL_OUTPUT_TRANSFORM_FLIPPED:
3214 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3215 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3216 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003217 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003218 flip = -1;
3219 break;
3220 default:
3221 flip = 1;
3222 break;
3223 }
3224
3225 switch(output->transform) {
3226 case WL_OUTPUT_TRANSFORM_NORMAL:
3227 case WL_OUTPUT_TRANSFORM_FLIPPED:
3228 transform.d[0] = flip;
3229 transform.d[1] = 0;
3230 transform.d[4] = 0;
3231 transform.d[5] = 1;
3232 break;
3233 case WL_OUTPUT_TRANSFORM_90:
3234 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3235 transform.d[0] = 0;
3236 transform.d[1] = -flip;
3237 transform.d[4] = 1;
3238 transform.d[5] = 0;
3239 break;
3240 case WL_OUTPUT_TRANSFORM_180:
3241 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3242 transform.d[0] = -flip;
3243 transform.d[1] = 0;
3244 transform.d[4] = 0;
3245 transform.d[5] = -1;
3246 break;
3247 case WL_OUTPUT_TRANSFORM_270:
3248 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3249 transform.d[0] = 0;
3250 transform.d[1] = flip;
3251 transform.d[4] = -1;
3252 transform.d[5] = 0;
3253 break;
3254 default:
3255 break;
3256 }
3257
3258 weston_matrix_multiply(&output->matrix, &transform);
3259}
3260
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003261WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003262weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003263{
Scott Moreau850ca422012-05-21 15:21:25 -06003264 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003265 struct weston_matrix camera;
3266 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003267
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003268 weston_matrix_init(&output->matrix);
3269 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003270 -(output->x + output->width / 2.0),
3271 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003272
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003273 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003274 2.0 / output->width,
3275 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003276
3277 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003278
Scott Moreauccbf29d2012-02-22 14:21:41 -07003279 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003280 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003281 weston_matrix_init(&camera);
3282 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003283 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003284 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003285 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003286 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003287 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003288 weston_matrix_multiply(&output->matrix, &modelview);
3289 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003290
Scott Moreauccbf29d2012-02-22 14:21:41 -07003291 output->dirty = 0;
3292}
3293
Scott Moreau1bad5db2012-08-18 01:04:05 -06003294static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003295weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003296{
3297 output->transform = transform;
3298
3299 switch (transform) {
3300 case WL_OUTPUT_TRANSFORM_90:
3301 case WL_OUTPUT_TRANSFORM_270:
3302 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3303 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3304 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003305 output->width = output->current_mode->height;
3306 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003307 break;
3308 case WL_OUTPUT_TRANSFORM_NORMAL:
3309 case WL_OUTPUT_TRANSFORM_180:
3310 case WL_OUTPUT_TRANSFORM_FLIPPED:
3311 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003312 output->width = output->current_mode->width;
3313 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003314 break;
3315 default:
3316 break;
3317 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003318
Hardening57388e42013-09-18 23:56:36 +02003319 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003320 output->width /= scale;
3321 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003322}
3323
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003324static void
3325weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003326{
3327 output->x = x;
3328 output->y = y;
3329
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003330 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003331 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003332 output->width,
3333 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003334}
3335
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003336WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003337weston_output_move(struct weston_output *output, int x, int y)
3338{
3339 pixman_region32_t old_region;
3340 struct wl_resource *resource;
3341
3342 output->move_x = x - output->x;
3343 output->move_y = y - output->y;
3344
3345 if (output->move_x == 0 && output->move_y == 0)
3346 return;
3347
3348 pixman_region32_init(&old_region);
3349 pixman_region32_copy(&old_region, &output->region);
3350
3351 weston_output_init_geometry(output, x, y);
3352
3353 output->dirty = 1;
3354
3355 /* Move views on this output. */
3356 wl_signal_emit(&output->move_signal, output);
3357
3358 /* Notify clients of the change for output position. */
3359 wl_resource_for_each(resource, &output->resource_list)
3360 wl_output_send_geometry(resource,
3361 output->x,
3362 output->y,
3363 output->mm_width,
3364 output->mm_height,
3365 output->subpixel,
3366 output->make,
3367 output->model,
3368 output->transform);
3369}
3370
3371WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003372weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003373 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003374 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003375{
3376 output->compositor = c;
3377 output->x = x;
3378 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003379 output->mm_width = mm_width;
3380 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003381 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003382 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003383
Alexander Larsson0b135062013-05-28 16:23:36 +02003384 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003385 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003386
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003387 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003388 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003389
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003390 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003391 wl_signal_init(&output->destroy_signal);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003392 wl_signal_init(&output->move_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003393 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003394 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003395
Casey Dahlin58ba1372012-04-19 22:50:08 -04003396 output->id = ffs(~output->compositor->output_id_pool) - 1;
3397 output->compositor->output_id_pool |= 1 << output->id;
3398
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003399 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003400 wl_global_create(c->wl_display, &wl_output_interface, 2,
3401 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003402 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003403}
3404
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003405WL_EXPORT void
3406weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003407 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003408 wl_fixed_t *x, wl_fixed_t *y)
3409{
3410 wl_fixed_t tx, ty;
3411 wl_fixed_t width, height;
3412
Hardeningff39efa2013-09-18 23:56:35 +02003413 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3414 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003415
3416 switch(output->transform) {
3417 case WL_OUTPUT_TRANSFORM_NORMAL:
3418 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003419 tx = device_x;
3420 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003421 break;
3422 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003423 tx = device_y;
3424 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003425 break;
3426 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003427 tx = width - device_x;
3428 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003429 break;
3430 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003431 tx = width - device_y;
3432 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003433 break;
3434 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003435 tx = width - device_x;
3436 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003437 break;
3438 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003439 tx = width - device_y;
3440 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003441 break;
3442 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003443 tx = device_x;
3444 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003445 break;
3446 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003447 tx = device_y;
3448 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003449 break;
3450 }
3451
Hardeningff39efa2013-09-18 23:56:35 +02003452 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3453 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003454}
3455
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003456static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003457destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003458{
Jonny Lamb74130762013-11-26 18:19:46 +01003459 struct weston_surface *surface =
3460 wl_resource_get_user_data(resource);
3461
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003462 surface->viewport_resource = NULL;
3463 surface->pending.buffer_viewport.viewport_set = 0;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003464}
3465
3466static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003467viewport_destroy(struct wl_client *client,
3468 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003469{
3470 wl_resource_destroy(resource);
3471}
3472
3473static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003474viewport_set(struct wl_client *client,
3475 struct wl_resource *resource,
3476 wl_fixed_t src_x,
3477 wl_fixed_t src_y,
3478 wl_fixed_t src_width,
3479 wl_fixed_t src_height,
3480 int32_t dst_width,
3481 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003482{
Jonny Lamb74130762013-11-26 18:19:46 +01003483 struct weston_surface *surface =
3484 wl_resource_get_user_data(resource);
3485
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003486 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003487
Jonny Lamb8ae35902013-11-26 18:19:45 +01003488 if (wl_fixed_to_double(src_width) < 0 ||
3489 wl_fixed_to_double(src_height) < 0) {
3490 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003491 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003492 "source dimensions must be non-negative (%fx%f)",
3493 wl_fixed_to_double(src_width),
3494 wl_fixed_to_double(src_height));
3495 return;
3496 }
3497
3498 if (dst_width <= 0 || dst_height <= 0) {
3499 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003500 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003501 "destination dimensions must be positive (%dx%d)",
3502 dst_width, dst_height);
3503 return;
3504 }
3505
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003506 surface->pending.buffer_viewport.viewport_set = 1;
Jonny Lamb74130762013-11-26 18:19:46 +01003507
3508 surface->pending.buffer_viewport.src_x = src_x;
3509 surface->pending.buffer_viewport.src_y = src_y;
3510 surface->pending.buffer_viewport.src_width = src_width;
3511 surface->pending.buffer_viewport.src_height = src_height;
3512 surface->pending.buffer_viewport.dst_width = dst_width;
3513 surface->pending.buffer_viewport.dst_height = dst_height;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003514}
3515
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003516static const struct wl_viewport_interface viewport_interface = {
3517 viewport_destroy,
3518 viewport_set
Jonny Lamb8ae35902013-11-26 18:19:45 +01003519};
3520
3521static void
3522scaler_destroy(struct wl_client *client,
3523 struct wl_resource *resource)
3524{
3525 wl_resource_destroy(resource);
3526}
3527
3528static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003529scaler_get_viewport(struct wl_client *client,
3530 struct wl_resource *scaler,
3531 uint32_t id,
3532 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003533{
3534 struct weston_surface *surface = wl_resource_get_user_data(surface_resource);
3535 struct wl_resource *resource;
3536
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003537 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003538 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003539 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3540 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003541 return;
3542 }
3543
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003544 resource = wl_resource_create(client, &wl_viewport_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003545 1, id);
3546 if (resource == NULL) {
3547 wl_client_post_no_memory(client);
3548 return;
3549 }
3550
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003551 wl_resource_set_implementation(resource, &viewport_interface,
3552 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003553
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003554 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003555}
3556
3557static const struct wl_scaler_interface scaler_interface = {
3558 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003559 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003560};
3561
3562static void
3563bind_scaler(struct wl_client *client,
3564 void *data, uint32_t version, uint32_t id)
3565{
3566 struct wl_resource *resource;
3567
3568 resource = wl_resource_create(client, &wl_scaler_interface,
3569 1, id);
3570 if (resource == NULL) {
3571 wl_client_post_no_memory(client);
3572 return;
3573 }
3574
3575 wl_resource_set_implementation(resource, &scaler_interface,
3576 NULL, NULL);
3577}
3578
3579static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003580compositor_bind(struct wl_client *client,
3581 void *data, uint32_t version, uint32_t id)
3582{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003583 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003584 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003585
Jason Ekstranda85118c2013-06-27 20:17:02 -05003586 resource = wl_resource_create(client, &wl_compositor_interface,
3587 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003588 if (resource == NULL) {
3589 wl_client_post_no_memory(client);
3590 return;
3591 }
3592
3593 wl_resource_set_implementation(resource, &compositor_interface,
3594 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003595}
3596
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003597static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003598log_uname(void)
3599{
3600 struct utsname usys;
3601
3602 uname(&usys);
3603
3604 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3605 usys.version, usys.machine);
3606}
3607
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003608WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003609weston_environment_get_fd(const char *env)
3610{
3611 char *e, *end;
3612 int fd, flags;
3613
3614 e = getenv(env);
3615 if (!e)
3616 return -1;
3617 fd = strtol(e, &end, 0);
3618 if (*end != '\0')
3619 return -1;
3620
3621 flags = fcntl(fd, F_GETFD);
3622 if (flags == -1)
3623 return -1;
3624
3625 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3626 unsetenv(env);
3627
3628 return fd;
3629}
3630
3631WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003632weston_compositor_init(struct weston_compositor *ec,
3633 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003634 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003635 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003636{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003637 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003638 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003639 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003640
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003641 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003642 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003643 wl_signal_init(&ec->destroy_signal);
3644 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003645 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003646 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003647 wl_signal_init(&ec->idle_signal);
3648 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003649 wl_signal_init(&ec->show_input_panel_signal);
3650 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003651 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003652 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003653 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003654 wl_signal_init(&ec->session_signal);
3655 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003656
Casey Dahlin58ba1372012-04-19 22:50:08 -04003657 ec->output_id_pool = 0;
3658
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003659 if (!wl_global_create(display, &wl_compositor_interface, 3,
3660 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003661 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003662
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003663 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3664 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003665 return -1;
3666
Jonny Lamb8ae35902013-11-26 18:19:45 +01003667 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 1,
3668 ec, bind_scaler))
3669 return -1;
3670
Jason Ekstranda7af7042013-10-12 22:38:11 -05003671 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003672 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003673 wl_list_init(&ec->layer_list);
3674 wl_list_init(&ec->seat_list);
3675 wl_list_init(&ec->output_list);
3676 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003677 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003678 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003679 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003680 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003681 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003682
Xiong Zhang97116532013-10-23 13:58:31 +08003683 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003684 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003685
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003686 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3687 weston_config_section_get_string(s, "keymap_rules",
3688 (char **) &xkb_names.rules, NULL);
3689 weston_config_section_get_string(s, "keymap_model",
3690 (char **) &xkb_names.model, NULL);
3691 weston_config_section_get_string(s, "keymap_layout",
3692 (char **) &xkb_names.layout, NULL);
3693 weston_config_section_get_string(s, "keymap_variant",
3694 (char **) &xkb_names.variant, NULL);
3695 weston_config_section_get_string(s, "keymap_options",
3696 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003697
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003698 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3699 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003700
3701 ec->ping_handler = NULL;
3702
3703 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003704 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003705
3706 wl_data_device_manager_init(ec->wl_display);
3707
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003708 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003709
Daniel Stone725c2c32012-06-22 14:04:36 +01003710 loop = wl_display_get_event_loop(ec->wl_display);
3711 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3712 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3713
3714 ec->input_loop = wl_event_loop_create();
3715
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003716 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3717 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3718
3719 weston_compositor_schedule_repaint(ec);
3720
Daniel Stone725c2c32012-06-22 14:04:36 +01003721 return 0;
3722}
3723
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003724WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003725weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003726{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003727 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003728
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003729 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003730 if (ec->input_loop_source)
3731 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003732
Matt Roper361d2ad2011-08-29 13:52:23 -07003733 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003734 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003735 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003736
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003737 if (ec->renderer)
3738 ec->renderer->destroy(ec);
3739
Daniel Stone325fc2d2012-05-30 16:31:58 +01003740 weston_binding_list_destroy_all(&ec->key_binding_list);
3741 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003742 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003743 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003744 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003745
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003746 weston_plane_release(&ec->primary_plane);
3747
Jonas Ådahlc97af922012-03-28 22:36:09 +02003748 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003749
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003750 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003751}
3752
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003753WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003754weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3755 const struct weston_pointer_grab_interface *interface)
3756{
3757 struct weston_seat *seat;
3758
3759 ec->default_pointer_grab = interface;
3760 wl_list_for_each(seat, &ec->seat_list, link) {
3761 if (seat->pointer) {
3762 weston_pointer_set_default_grab(seat->pointer,
3763 interface);
3764 }
3765 }
3766}
3767
3768WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003769weston_version(int *major, int *minor, int *micro)
3770{
3771 *major = WESTON_VERSION_MAJOR;
3772 *minor = WESTON_VERSION_MINOR;
3773 *micro = WESTON_VERSION_MICRO;
3774}
3775
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003776static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003777 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003778 const char *desc;
3779} capability_strings[] = {
3780 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003781 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003782};
3783
3784static void
3785weston_compositor_log_capabilities(struct weston_compositor *compositor)
3786{
3787 unsigned i;
3788 int yes;
3789
3790 weston_log("Compositor capabilities:\n");
3791 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3792 yes = compositor->capabilities & capability_strings[i].bit;
3793 weston_log_continue(STAMP_SPACE "%s %s\n",
3794 capability_strings[i].desc,
3795 yes ? "yes" : "no");
3796 }
3797}
3798
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003799static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003800{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003801 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003802
Martin Minarik6d118362012-06-07 18:01:59 +02003803 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003804 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003805
3806 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003807}
3808
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003809#ifdef HAVE_LIBUNWIND
3810
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003811static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003812print_backtrace(void)
3813{
3814 unw_cursor_t cursor;
3815 unw_context_t context;
3816 unw_word_t off;
3817 unw_proc_info_t pip;
3818 int ret, i = 0;
3819 char procname[256];
3820 const char *filename;
3821 Dl_info dlinfo;
3822
3823 pip.unwind_info = NULL;
3824 ret = unw_getcontext(&context);
3825 if (ret) {
3826 weston_log("unw_getcontext: %d\n", ret);
3827 return;
3828 }
3829
3830 ret = unw_init_local(&cursor, &context);
3831 if (ret) {
3832 weston_log("unw_init_local: %d\n", ret);
3833 return;
3834 }
3835
3836 ret = unw_step(&cursor);
3837 while (ret > 0) {
3838 ret = unw_get_proc_info(&cursor, &pip);
3839 if (ret) {
3840 weston_log("unw_get_proc_info: %d\n", ret);
3841 break;
3842 }
3843
3844 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3845 if (ret && ret != -UNW_ENOMEM) {
3846 if (ret != -UNW_EUNSPEC)
3847 weston_log("unw_get_proc_name: %d\n", ret);
3848 procname[0] = '?';
3849 procname[1] = 0;
3850 }
3851
3852 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3853 *dlinfo.dli_fname)
3854 filename = dlinfo.dli_fname;
3855 else
3856 filename = "?";
3857
3858 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3859 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3860
3861 ret = unw_step(&cursor);
3862 if (ret < 0)
3863 weston_log("unw_step: %d\n", ret);
3864 }
3865}
3866
3867#else
3868
3869static void
3870print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003871{
3872 void *buffer[32];
3873 int i, count;
3874 Dl_info info;
3875
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003876 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3877 for (i = 0; i < count; i++) {
3878 dladdr(buffer[i], &info);
3879 weston_log(" [%016lx] %s (%s)\n",
3880 (long) buffer[i],
3881 info.dli_sname ? info.dli_sname : "--",
3882 info.dli_fname);
3883 }
3884}
3885
3886#endif
3887
3888static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003889on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003890{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003891 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003892 * then call the backend restore function, which will switch
3893 * back to the vt we launched from or ungrab X etc and then
3894 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003895 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003896 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003897 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003898
Peter Maatmane5b42e42013-03-27 22:38:53 +01003899 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003900
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003901 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003902
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003903 segv_compositor->restore(segv_compositor);
3904
3905 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003906}
3907
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003908WL_EXPORT void *
3909weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003910{
3911 char path[PATH_MAX];
3912 void *module, *init;
3913
3914 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003915 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003916 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003917 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003918
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003919 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3920 if (module) {
3921 weston_log("Module '%s' already loaded\n", path);
3922 dlclose(module);
3923 return NULL;
3924 }
3925
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003926 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003927 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003928 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003929 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003930 return NULL;
3931 }
3932
3933 init = dlsym(module, entrypoint);
3934 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003935 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003936 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003937 return NULL;
3938 }
3939
3940 return init;
3941}
3942
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003943static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003944load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003945 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003946{
3947 const char *p, *end;
3948 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003949 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003950 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003951
3952 if (modules == NULL)
3953 return 0;
3954
3955 p = modules;
3956 while (*p) {
3957 end = strchrnul(p, ',');
3958 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003959 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003960 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003961 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003962 p = end;
3963 while (*p == ',')
3964 p++;
3965
3966 }
3967
3968 return 0;
3969}
3970
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003971static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003972 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3973
3974static const char xdg_wrong_message[] =
3975 "fatal: environment variable XDG_RUNTIME_DIR\n"
3976 "is set to \"%s\", which is not a directory.\n";
3977
3978static const char xdg_wrong_mode_message[] =
3979 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3980 "correctly. Unix access mode must be 0700 but is %o,\n"
3981 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003982 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003983
3984static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003985 "Refer to your distribution on how to get it, or\n"
3986 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3987 "on how to implement it.\n";
3988
Martin Minarik37032f82012-06-18 20:15:18 +02003989static void
3990verify_xdg_runtime_dir(void)
3991{
3992 char *dir = getenv("XDG_RUNTIME_DIR");
3993 struct stat s;
3994
3995 if (!dir) {
3996 weston_log(xdg_error_message);
3997 weston_log_continue(xdg_detail_message);
3998 exit(EXIT_FAILURE);
3999 }
4000
4001 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4002 weston_log(xdg_wrong_message, dir);
4003 weston_log_continue(xdg_detail_message);
4004 exit(EXIT_FAILURE);
4005 }
4006
4007 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4008 weston_log(xdg_wrong_mode_message,
4009 dir, s.st_mode & 0777, s.st_uid);
4010 weston_log_continue(xdg_detail_message);
4011 }
4012}
4013
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004014static int
4015usage(int error_code)
4016{
4017 fprintf(stderr,
4018 "Usage: weston [OPTIONS]\n\n"
4019 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4020 "Weston supports multiple backends, and depending on which backend is in use\n"
4021 "different options will be accepted.\n\n"
4022
4023
4024 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004025 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004026 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004027 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4028 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004029 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004030 " -S, --socket=NAME\tName of socket to listen on\n"
4031 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004032 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004033 " --log==FILE\t\tLog to the given file\n"
4034 " -h, --help\t\tThis help message\n\n");
4035
4036 fprintf(stderr,
4037 "Options for drm-backend.so:\n\n"
4038 " --connector=ID\tBring up only this connector\n"
4039 " --seat=SEAT\t\tThe seat that weston should run on\n"
4040 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004041 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004042 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4043
4044 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004045 "Options for fbdev-backend.so:\n\n"
4046 " --tty=TTY\t\tThe tty to use\n"
4047 " --device=DEVICE\tThe framebuffer device to use\n\n");
4048
4049 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004050 "Options for x11-backend.so:\n\n"
4051 " --width=WIDTH\t\tWidth of X window\n"
4052 " --height=HEIGHT\tHeight of X window\n"
4053 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004054 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004055 " --output-count=COUNT\tCreate multiple outputs\n"
4056 " --no-input\t\tDont create input devices\n\n");
4057
4058 fprintf(stderr,
4059 "Options for wayland-backend.so:\n\n"
4060 " --width=WIDTH\t\tWidth of Wayland surface\n"
4061 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004062 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004063 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004064 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004065 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004066 " --display=DISPLAY\tWayland display to connect to\n\n");
4067
Pekka Paalanene31e0532013-05-22 18:03:07 +03004068#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4069 fprintf(stderr,
4070 "Options for rpi-backend.so:\n\n"
4071 " --tty=TTY\t\tThe tty to use\n"
4072 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4073 " --transform=TR\tThe output transformation, TR is one of:\n"
4074 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004075 " --opaque-regions\tEnable support for opaque regions, can be "
4076 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004077 "\n");
4078#endif
4079
Hardeningc077a842013-07-08 00:51:35 +02004080#if defined(BUILD_RDP_COMPOSITOR)
4081 fprintf(stderr,
4082 "Options for rdp-backend.so:\n\n"
4083 " --width=WIDTH\t\tWidth of desktop\n"
4084 " --height=HEIGHT\tHeight of desktop\n"
4085 " --extra-modes=MODES\t\n"
4086 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4087 " --address=ADDR\tThe address to bind\n"
4088 " --port=PORT\tThe port to listen on\n"
4089 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4090 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4091 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4092 "\n");
4093#endif
4094
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004095 exit(error_code);
4096}
4097
Peter Maatmane5b42e42013-03-27 22:38:53 +01004098static void
4099catch_signals(void)
4100{
4101 struct sigaction action;
4102
4103 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4104 action.sa_sigaction = on_caught_signal;
4105 sigemptyset(&action.sa_mask);
4106 sigaction(SIGSEGV, &action, NULL);
4107 sigaction(SIGABRT, &action, NULL);
4108}
4109
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004110int main(int argc, char *argv[])
4111{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004112 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004113 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004114 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004115 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004116 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004117 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004118 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004119 int *argc, char *argv[],
4120 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07004121 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004122 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004123 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004124 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004125 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004126 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004127 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004128 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004129 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06004130 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004131 struct weston_config *config;
4132 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004133
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004134 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004135 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05004136 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004137 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4138 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004139 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004140 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004141 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004142 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004143 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004144
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004145 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004146
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004147 if (help)
4148 usage(EXIT_SUCCESS);
4149
Scott Moreau12245142012-08-29 15:15:58 -06004150 if (version) {
4151 printf(PACKAGE_STRING "\n");
4152 return EXIT_SUCCESS;
4153 }
4154
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004155 weston_log_file_open(log);
4156
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004157 weston_log("%s\n"
4158 STAMP_SPACE "%s\n"
4159 STAMP_SPACE "Bug reports to: %s\n"
4160 STAMP_SPACE "Build: %s\n",
4161 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004162 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004163 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004164
Martin Minarik37032f82012-06-18 20:15:18 +02004165 verify_xdg_runtime_dir();
4166
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004167 display = wl_display_create();
4168
Tiago Vignatti2116b892011-08-08 05:52:59 -07004169 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004170 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4171 display);
4172 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4173 display);
4174 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4175 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004176
4177 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004178 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4179 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004180
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004181 config = weston_config_parse("weston.ini");
4182 if (config != NULL) {
4183 weston_log("Using config file '%s'\n",
4184 weston_config_get_full_path(config));
4185 } else {
4186 weston_log("Starting with no config file.\n");
4187 }
4188 section = weston_config_get_section(config, "core", NULL, NULL);
4189
4190 weston_config_section_get_string(section, "backend", &backend, NULL);
4191 if (option_backend) {
4192 backend = option_backend;
4193 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004194 if (!backend) {
4195 if (getenv("WAYLAND_DISPLAY"))
4196 backend = "wayland-backend.so";
4197 else if (getenv("DISPLAY"))
4198 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004199 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02004200 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004201 }
4202
Jason Ekstranda985da42013-08-22 17:28:03 -05004203 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004204
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004205 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004206 if (!backend_init)
4207 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004208
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004209 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004210 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004211 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004212 exit(EXIT_FAILURE);
4213 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004214
Peter Maatmane5b42e42013-03-27 22:38:53 +01004215 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004216 segv_compositor = ec;
4217
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004218 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004219 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004220
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004221 setenv("WAYLAND_DISPLAY", socket_name, 1);
4222
Jason Ekstranda985da42013-08-22 17:28:03 -05004223 if (!shell)
4224 weston_config_section_get_string(section, "shell", &shell,
4225 "desktop-shell.so");
4226 if (load_modules(ec, shell, &argc, argv) < 0)
4227 goto out;
4228
Ossama Othmana50e6e42013-05-14 09:48:26 -07004229 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004230 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004231 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004232 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004233
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004234 for (i = 1; i < argc; i++)
4235 weston_log("fatal: unhandled option: %s\n", argv[i]);
4236 if (argc > 1) {
4237 ret = EXIT_FAILURE;
4238 goto out;
4239 }
4240
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004241 weston_compositor_log_capabilities(ec);
4242
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004243 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004244 weston_log("fatal: failed to add socket: %m\n");
4245 ret = EXIT_FAILURE;
4246 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004247 }
4248
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004249 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004250
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004251 wl_display_run(display);
4252
4253 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004254 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004255 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004256
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004257 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004258
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004259 for (i = ARRAY_LENGTH(signals); i;)
4260 wl_event_source_remove(signals[--i]);
4261
Daniel Stone855028f2012-05-01 20:37:10 +01004262 weston_compositor_xkb_destroy(ec);
4263
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004264 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004265 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004266
Martin Minarik19e6f262012-06-07 13:08:46 +02004267 weston_log_file_close();
4268
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004269 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004270}