blob: 1968c7bcc6b693272d9a1631b1b50ab36978259e [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
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200350weston_view_output_destroy_handler(struct wl_listener *listener,
351 void *data)
352{
353 struct weston_view *ev;
354 struct weston_compositor *ec;
355 struct weston_output *output, *first_output;
356 float x, y;
357 int visible;
358
359 ev = container_of(listener, struct weston_view,
360 output_destroy_listener);
361
362 ec = ev->surface->compositor;
363
364 /* the child window's view->geometry is a relative coordinate to
365 * parent view, no need to move child_view. */
366 if (ev->geometry.parent)
367 return;
368
369 x = ev->geometry.x;
370 y = ev->geometry.y;
371
372 /* At this point the destroyed output is not in the list anymore.
373 * If the view is still visible somewhere, we leave where it is,
374 * otherwise, move it to the first output. */
375 visible = 0;
376 wl_list_for_each(output, &ec->output_list, link) {
377 if (pixman_region32_contains_point(&output->region,
378 x, y, NULL)) {
379 visible = 1;
380 break;
381 }
382 }
383
384 if (!visible) {
385 first_output = container_of(ec->output_list.next,
386 struct weston_output, link);
387
388 x = first_output->x + first_output->width / 4;
389 y = first_output->y + first_output->height / 4;
390 }
391
392 weston_view_set_position(ev, x, y);
Zhang, Xiong Y31236932013-12-13 22:10:57 +0200393
394 if (ev->surface->output_destroyed)
395 ev->surface->output_destroyed(ev->surface);
Ander Conselvan de Oliveira24dff2b2013-12-19 18:34:28 +0200396
Ander Conselvan de Oliveira24dff2b2013-12-19 18:34:28 +0200397 wl_list_remove(&ev->output_destroy_listener.link);
398
Ander Conselvan de Oliveira24dff2b2013-12-19 18:34:28 +0200399 wl_list_init(&ev->output_destroy_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200400}
401
Jason Ekstranda7af7042013-10-12 22:38:11 -0500402WL_EXPORT struct weston_view *
403weston_view_create(struct weston_surface *surface)
404{
405 struct weston_view *view;
406
407 view = calloc(1, sizeof *view);
408 if (view == NULL)
409 return NULL;
410
411 view->surface = surface;
412
Jason Ekstranda7af7042013-10-12 22:38:11 -0500413 /* Assign to surface */
414 wl_list_insert(&surface->views, &view->surface_link);
415
416 wl_signal_init(&view->destroy_signal);
417 wl_list_init(&view->link);
418 wl_list_init(&view->layer_link);
419
Xiong Zhang97116532013-10-23 13:58:31 +0800420 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500421
422 pixman_region32_init(&view->clip);
423
424 view->alpha = 1.0;
425 pixman_region32_init(&view->transform.opaque);
426
427 wl_list_init(&view->geometry.transformation_list);
428 wl_list_insert(&view->geometry.transformation_list,
429 &view->transform.position.link);
430 weston_matrix_init(&view->transform.position.matrix);
431 wl_list_init(&view->geometry.child_list);
432 pixman_region32_init(&view->transform.boundingbox);
433 view->transform.dirty = 1;
434
435 view->output = NULL;
436
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200437 view->output_destroy_listener.notify =
438 weston_view_output_destroy_handler;
Kristian Høgsbergdf42a802013-12-17 14:58:19 -0800439 wl_list_init(&view->output_destroy_listener.link);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200440
Jason Ekstranda7af7042013-10-12 22:38:11 -0500441 return view;
442}
443
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500444WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500445weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500446{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500447 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400448
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200449 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400450 if (surface == NULL)
451 return NULL;
452
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500453 wl_signal_init(&surface->destroy_signal);
454
455 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500456
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500457 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200458 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400459
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100460 surface->buffer_viewport.transform = WL_OUTPUT_TRANSFORM_NORMAL;
461 surface->buffer_viewport.scale = 1;
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200462 surface->buffer_viewport.viewport_set = 0;
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100463 surface->pending.buffer_viewport = surface->buffer_viewport;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400464 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100465 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200466
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200467 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100468
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400469 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500470 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300471 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400472
Jason Ekstranda7af7042013-10-12 22:38:11 -0500473 wl_list_init(&surface->views);
474
475 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500476
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300477 surface->pending.buffer_destroy_listener.notify =
478 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300479 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300480 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300481 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300482 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300483
Pekka Paalanene67858b2013-04-25 13:57:42 +0300484 wl_list_init(&surface->subsurface_list);
485 wl_list_init(&surface->subsurface_list_pending);
486
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400487 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500488}
489
Alex Wu8811bf92012-02-28 18:07:54 +0800490WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500491weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200492 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500493{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100494 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500495}
496
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400497WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500498weston_view_to_global_float(struct weston_view *view,
499 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200500{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500501 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200502 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
503
Jason Ekstranda7af7042013-10-12 22:38:11 -0500504 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200505
506 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200507 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700508 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200509 v.f[3]);
510 *x = 0;
511 *y = 0;
512 return;
513 }
514
515 *x = v.f[0] / v.f[3];
516 *y = v.f[1] / v.f[3];
517 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500518 *x = sx + view->geometry.x;
519 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200520 }
521}
522
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500523WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200524weston_transformed_coord(int width, int height,
525 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200526 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200527 float sx, float sy, float *bx, float *by)
528{
529 switch (transform) {
530 case WL_OUTPUT_TRANSFORM_NORMAL:
531 default:
532 *bx = sx;
533 *by = sy;
534 break;
535 case WL_OUTPUT_TRANSFORM_FLIPPED:
536 *bx = width - sx;
537 *by = sy;
538 break;
539 case WL_OUTPUT_TRANSFORM_90:
540 *bx = height - sy;
541 *by = sx;
542 break;
543 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
544 *bx = height - sy;
545 *by = width - sx;
546 break;
547 case WL_OUTPUT_TRANSFORM_180:
548 *bx = width - sx;
549 *by = height - sy;
550 break;
551 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
552 *bx = sx;
553 *by = height - sy;
554 break;
555 case WL_OUTPUT_TRANSFORM_270:
556 *bx = sy;
557 *by = width - sx;
558 break;
559 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
560 *bx = sy;
561 *by = sx;
562 break;
563 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200564
565 *bx *= scale;
566 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200567}
568
569WL_EXPORT pixman_box32_t
570weston_transformed_rect(int width, int height,
571 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200572 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200573 pixman_box32_t rect)
574{
575 float x1, x2, y1, y2;
576
577 pixman_box32_t ret;
578
Alexander Larsson4ea95522013-05-22 14:41:37 +0200579 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200580 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200581 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200582 rect.x2, rect.y2, &x2, &y2);
583
584 if (x1 <= x2) {
585 ret.x1 = x1;
586 ret.x2 = x2;
587 } else {
588 ret.x1 = x2;
589 ret.x2 = x1;
590 }
591
592 if (y1 <= y2) {
593 ret.y1 = y1;
594 ret.y2 = y2;
595 } else {
596 ret.y1 = y2;
597 ret.y2 = y1;
598 }
599
600 return ret;
601}
602
603WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500604weston_transformed_region(int width, int height,
605 enum wl_output_transform transform,
606 int32_t scale,
607 pixman_region32_t *src, pixman_region32_t *dest)
608{
609 pixman_box32_t *src_rects, *dest_rects;
610 int nrects, i;
611
612 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
613 if (src != dest)
614 pixman_region32_copy(dest, src);
615 return;
616 }
617
618 src_rects = pixman_region32_rectangles(src, &nrects);
619 dest_rects = malloc(nrects * sizeof(*dest_rects));
620 if (!dest_rects)
621 return;
622
623 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
624 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
625 } else {
626 for (i = 0; i < nrects; i++) {
627 switch (transform) {
628 default:
629 case WL_OUTPUT_TRANSFORM_NORMAL:
630 dest_rects[i].x1 = src_rects[i].x1;
631 dest_rects[i].y1 = src_rects[i].y1;
632 dest_rects[i].x2 = src_rects[i].x2;
633 dest_rects[i].y2 = src_rects[i].y2;
634 break;
635 case WL_OUTPUT_TRANSFORM_90:
636 dest_rects[i].x1 = height - src_rects[i].y2;
637 dest_rects[i].y1 = src_rects[i].x1;
638 dest_rects[i].x2 = height - src_rects[i].y1;
639 dest_rects[i].y2 = src_rects[i].x2;
640 break;
641 case WL_OUTPUT_TRANSFORM_180:
642 dest_rects[i].x1 = width - src_rects[i].x2;
643 dest_rects[i].y1 = height - src_rects[i].y2;
644 dest_rects[i].x2 = width - src_rects[i].x1;
645 dest_rects[i].y2 = height - src_rects[i].y1;
646 break;
647 case WL_OUTPUT_TRANSFORM_270:
648 dest_rects[i].x1 = src_rects[i].y1;
649 dest_rects[i].y1 = width - src_rects[i].x2;
650 dest_rects[i].x2 = src_rects[i].y2;
651 dest_rects[i].y2 = width - src_rects[i].x1;
652 break;
653 case WL_OUTPUT_TRANSFORM_FLIPPED:
654 dest_rects[i].x1 = width - src_rects[i].x2;
655 dest_rects[i].y1 = src_rects[i].y1;
656 dest_rects[i].x2 = width - src_rects[i].x1;
657 dest_rects[i].y2 = src_rects[i].y2;
658 break;
659 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
660 dest_rects[i].x1 = height - src_rects[i].y2;
661 dest_rects[i].y1 = width - src_rects[i].x2;
662 dest_rects[i].x2 = height - src_rects[i].y1;
663 dest_rects[i].y2 = width - src_rects[i].x1;
664 break;
665 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
666 dest_rects[i].x1 = src_rects[i].x1;
667 dest_rects[i].y1 = height - src_rects[i].y2;
668 dest_rects[i].x2 = src_rects[i].x2;
669 dest_rects[i].y2 = height - src_rects[i].y1;
670 break;
671 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
672 dest_rects[i].x1 = src_rects[i].y1;
673 dest_rects[i].y1 = src_rects[i].x1;
674 dest_rects[i].x2 = src_rects[i].y2;
675 dest_rects[i].y2 = src_rects[i].x2;
676 break;
677 }
678 }
679 }
680
681 if (scale != 1) {
682 for (i = 0; i < nrects; i++) {
683 dest_rects[i].x1 *= scale;
684 dest_rects[i].x2 *= scale;
685 dest_rects[i].y1 *= scale;
686 dest_rects[i].y2 *= scale;
687 }
688 }
689
690 pixman_region32_clear(dest);
691 pixman_region32_init_rects(dest, dest_rects, nrects);
692 free(dest_rects);
693}
694
Jonny Lamb74130762013-11-26 18:19:46 +0100695static void
696scaler_surface_to_buffer(struct weston_surface *surface,
697 float sx, float sy, float *bx, float *by)
698{
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200699 if (surface->buffer_viewport.viewport_set) {
Jonny Lamb74130762013-11-26 18:19:46 +0100700 double a, b;
701
702 a = sx / surface->buffer_viewport.dst_width;
703 b = a * wl_fixed_to_double(surface->buffer_viewport.src_width);
704 *bx = b + wl_fixed_to_double(surface->buffer_viewport.src_x);
705
706 a = sy / surface->buffer_viewport.dst_height;
707 b = a * wl_fixed_to_double(surface->buffer_viewport.src_height);
708 *by = b + wl_fixed_to_double(surface->buffer_viewport.src_y);
709 } else {
710 *bx = sx;
711 *by = sy;
712 }
713}
714
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500715WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200716weston_surface_to_buffer_float(struct weston_surface *surface,
717 float sx, float sy, float *bx, float *by)
718{
Jonny Lamb74130762013-11-26 18:19:46 +0100719 /* first transform coordinates if the scaler is set */
720 scaler_surface_to_buffer(surface, sx, sy, bx, by);
721
Jason Ekstranda7af7042013-10-12 22:38:11 -0500722 weston_transformed_coord(surface->width,
723 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100724 surface->buffer_viewport.transform,
725 surface->buffer_viewport.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100726 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200727}
728
Alexander Larsson4ea95522013-05-22 14:41:37 +0200729WL_EXPORT void
730weston_surface_to_buffer(struct weston_surface *surface,
731 int sx, int sy, int *bx, int *by)
732{
733 float bxf, byf;
734
Jonny Lamb74130762013-11-26 18:19:46 +0100735 weston_surface_to_buffer_float(surface,
736 sx, sy, &bxf, &byf);
737
Alexander Larsson4ea95522013-05-22 14:41:37 +0200738 *bx = floorf(bxf);
739 *by = floorf(byf);
740}
741
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200742WL_EXPORT pixman_box32_t
743weston_surface_to_buffer_rect(struct weston_surface *surface,
744 pixman_box32_t rect)
745{
Jonny Lamb74130762013-11-26 18:19:46 +0100746 float xf, yf;
747
748 /* first transform box coordinates if the scaler is set */
749 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
750 rect.x1 = floorf(xf);
751 rect.y1 = floorf(yf);
752
753 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
754 rect.x2 = floorf(xf);
755 rect.y2 = floorf(yf);
756
Jason Ekstranda7af7042013-10-12 22:38:11 -0500757 return weston_transformed_rect(surface->width,
758 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100759 surface->buffer_viewport.transform,
760 surface->buffer_viewport.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200761 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200762}
763
764WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500765weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400766 struct weston_plane *plane)
767{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500768 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400769 return;
770
Jason Ekstranda7af7042013-10-12 22:38:11 -0500771 weston_view_damage_below(view);
772 view->plane = plane;
773 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400774}
775
776WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500777weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200778{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500779 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200780
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500781 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500782 pixman_region32_subtract(&damage, &view->transform.boundingbox,
783 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800784 if (view->plane)
785 pixman_region32_union(&view->plane->damage,
786 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500787 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800788 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200789}
790
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400791static void
792weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
793{
794 uint32_t different = es->output_mask ^ mask;
795 uint32_t entered = mask & different;
796 uint32_t left = es->output_mask & different;
797 struct weston_output *output;
798 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500799 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400800
801 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500802 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400803 return;
804 if (different == 0)
805 return;
806
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500807 client = wl_resource_get_client(es->resource);
808
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400809 wl_list_for_each(output, &es->compositor->output_list, link) {
810 if (1 << output->id & different)
811 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500812 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400813 client);
814 if (resource == NULL)
815 continue;
816 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500817 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400818 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500819 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400820 }
821}
822
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200823
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400824static void
825weston_surface_assign_output(struct weston_surface *es)
826{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500827 struct weston_output *new_output;
828 struct weston_view *view;
829 pixman_region32_t region;
830 uint32_t max, area, mask;
831 pixman_box32_t *e;
832
833 new_output = NULL;
834 max = 0;
835 mask = 0;
836 pixman_region32_init(&region);
837 wl_list_for_each(view, &es->views, surface_link) {
838 if (!view->output)
839 continue;
840
841 pixman_region32_intersect(&region, &view->transform.boundingbox,
842 &view->output->region);
843
844 e = pixman_region32_extents(&region);
845 area = (e->x2 - e->x1) * (e->y2 - e->y1);
846
847 mask |= view->output_mask;
848
849 if (area >= max) {
850 new_output = view->output;
851 max = area;
852 }
853 }
854 pixman_region32_fini(&region);
855
856 es->output = new_output;
857 weston_surface_update_output_mask(es, mask);
858}
859
860static void
861weston_view_assign_output(struct weston_view *ev)
862{
863 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400864 struct weston_output *output, *new_output;
865 pixman_region32_t region;
866 uint32_t max, area, mask;
867 pixman_box32_t *e;
868
869 new_output = NULL;
870 max = 0;
871 mask = 0;
872 pixman_region32_init(&region);
873 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500874 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400875 &output->region);
876
877 e = pixman_region32_extents(&region);
878 area = (e->x2 - e->x1) * (e->y2 - e->y1);
879
880 if (area > 0)
881 mask |= 1 << output->id;
882
883 if (area >= max) {
884 new_output = output;
885 max = area;
886 }
887 }
888 pixman_region32_fini(&region);
889
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +0200890 if (ev->output_mask != 0)
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200891 wl_list_remove(&ev->output_destroy_listener.link);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200892
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +0200893 if (mask != 0)
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200894 wl_signal_add(&new_output->destroy_signal,
895 &ev->output_destroy_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +0200896 else
Kristian Høgsbergb0fb25d2014-01-09 22:41:48 -0800897 wl_list_init(&ev->output_destroy_listener.link);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200898
Jason Ekstranda7af7042013-10-12 22:38:11 -0500899 ev->output = new_output;
900 ev->output_mask = mask;
901
902 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400903}
904
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200905static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500906view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
907 int32_t width, int32_t height,
908 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200909{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200910 float min_x = HUGE_VALF, min_y = HUGE_VALF;
911 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200912 int32_t s[4][2] = {
913 { sx, sy },
914 { sx, sy + height },
915 { sx + width, sy },
916 { sx + width, sy + height }
917 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200918 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200919 int i;
920
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300921 if (width == 0 || height == 0) {
922 /* avoid rounding empty bbox to 1x1 */
923 pixman_region32_init(bbox);
924 return;
925 }
926
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200927 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200928 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500929 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200930 if (x < min_x)
931 min_x = x;
932 if (x > max_x)
933 max_x = x;
934 if (y < min_y)
935 min_y = y;
936 if (y > max_y)
937 max_y = y;
938 }
939
Pekka Paalanen219b9822012-02-08 15:38:37 +0200940 int_x = floorf(min_x);
941 int_y = floorf(min_y);
942 pixman_region32_init_rect(bbox, int_x, int_y,
943 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200944}
945
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200946static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500947weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200948{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200950
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200951 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 view->geometry.x = roundf(view->geometry.x);
953 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200954
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500955 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500956 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
957 view->transform.position.matrix.d[12] = view->geometry.x;
958 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500959
Jason Ekstranda7af7042013-10-12 22:38:11 -0500960 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500961
Jason Ekstranda7af7042013-10-12 22:38:11 -0500962 view->transform.inverse = view->transform.position.matrix;
963 view->transform.inverse.d[12] = -view->geometry.x;
964 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500965
Jason Ekstranda7af7042013-10-12 22:38:11 -0500966 pixman_region32_init_rect(&view->transform.boundingbox,
967 view->geometry.x,
968 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600969 view->surface->width,
970 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500971
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 if (view->alpha == 1.0) {
973 pixman_region32_copy(&view->transform.opaque,
974 &view->surface->opaque);
975 pixman_region32_translate(&view->transform.opaque,
976 view->geometry.x,
977 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500978 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200979}
980
981static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200983{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500984 struct weston_view *parent = view->geometry.parent;
985 struct weston_matrix *matrix = &view->transform.matrix;
986 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200987 struct weston_transform *tform;
988
Jason Ekstranda7af7042013-10-12 22:38:11 -0500989 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200990
991 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
993 view->transform.position.matrix.d[12] = view->geometry.x;
994 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200995
996 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200998 weston_matrix_multiply(matrix, &tform->matrix);
999
Pekka Paalanen483243f2013-03-08 14:56:50 +02001000 if (parent)
1001 weston_matrix_multiply(matrix, &parent->transform.matrix);
1002
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001003 if (weston_matrix_invert(inverse, matrix) < 0) {
1004 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 weston_log("error: weston_view %p"
1006 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001007 return -1;
1008 }
1009
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001010 view_compute_bbox(view, 0, 0,
1011 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001013
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001014 return 0;
1015}
1016
1017WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001018weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001019{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001021
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001023 return;
1024
Pekka Paalanen483243f2013-03-08 14:56:50 +02001025 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001026 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001027
Jason Ekstranda7af7042013-10-12 22:38:11 -05001028 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001029
Jason Ekstranda7af7042013-10-12 22:38:11 -05001030 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001031
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 pixman_region32_fini(&view->transform.boundingbox);
1033 pixman_region32_fini(&view->transform.opaque);
1034 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001035
Pekka Paalanencd403622012-01-25 13:37:39 +02001036 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 if (view->geometry.transformation_list.next ==
1038 &view->transform.position.link &&
1039 view->geometry.transformation_list.prev ==
1040 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001041 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001042 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001043 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001044 if (weston_view_update_transform_enable(view) < 0)
1045 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001046 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001047
Jason Ekstranda7af7042013-10-12 22:38:11 -05001048 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001049
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001051
Jason Ekstranda7af7042013-10-12 22:38:11 -05001052 wl_signal_emit(&view->surface->compositor->transform_signal,
1053 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001054}
1055
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001056WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001057weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001058{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001059 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001060
1061 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001062 * The invariant: if view->geometry.dirty, then all views
1063 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001064 * Corollary: if not parent->geometry.dirty, then all ancestors
1065 * are not dirty.
1066 */
1067
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001069 return;
1070
Jason Ekstranda7af7042013-10-12 22:38:11 -05001071 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001072
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001074 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001076}
1077
1078WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079weston_view_to_global_fixed(struct weston_view *view,
1080 wl_fixed_t vx, wl_fixed_t vy,
1081 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001082{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001083 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001084
Jason Ekstranda7af7042013-10-12 22:38:11 -05001085 weston_view_to_global_float(view,
1086 wl_fixed_to_double(vx),
1087 wl_fixed_to_double(vy),
1088 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001089 *x = wl_fixed_from_double(xf);
1090 *y = wl_fixed_from_double(yf);
1091}
1092
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001093WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001094weston_view_from_global_float(struct weston_view *view,
1095 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001096{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001097 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001098 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1099
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001101
1102 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001103 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001104 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001105 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001106 *vx = 0;
1107 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001108 return;
1109 }
1110
Jason Ekstranda7af7042013-10-12 22:38:11 -05001111 *vx = v.f[0] / v.f[3];
1112 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001113 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001114 *vx = x - view->geometry.x;
1115 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001116 }
1117}
1118
1119WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001120weston_view_from_global_fixed(struct weston_view *view,
1121 wl_fixed_t x, wl_fixed_t y,
1122 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001123{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001125
Jason Ekstranda7af7042013-10-12 22:38:11 -05001126 weston_view_from_global_float(view,
1127 wl_fixed_to_double(x),
1128 wl_fixed_to_double(y),
1129 &vxf, &vyf);
1130 *vx = wl_fixed_from_double(vxf);
1131 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001132}
1133
1134WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135weston_view_from_global(struct weston_view *view,
1136 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001137{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001139
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1141 *vx = floorf(vxf);
1142 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001143}
1144
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001145WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001146weston_surface_schedule_repaint(struct weston_surface *surface)
1147{
1148 struct weston_output *output;
1149
1150 wl_list_for_each(output, &surface->compositor->output_list, link)
1151 if (surface->output_mask & (1 << output->id))
1152 weston_output_schedule_repaint(output);
1153}
1154
1155WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156weston_view_schedule_repaint(struct weston_view *view)
1157{
1158 struct weston_output *output;
1159
1160 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1161 if (view->output_mask & (1 << output->id))
1162 weston_output_schedule_repaint(output);
1163}
1164
1165WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001166weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001167{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001168 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 0, 0, surface->width,
1170 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001171
Kristian Høgsberg98238702012-08-03 16:29:12 -04001172 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001173}
1174
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001175WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001176weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001177{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001178 if (view->geometry.x == x && view->geometry.y == y)
1179 return;
1180
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 view->geometry.x = x;
1182 view->geometry.y = y;
1183 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001184}
1185
Pekka Paalanen483243f2013-03-08 14:56:50 +02001186static void
1187transform_parent_handle_parent_destroy(struct wl_listener *listener,
1188 void *data)
1189{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190 struct weston_view *view =
1191 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001192 geometry.parent_destroy_listener);
1193
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001195}
1196
1197WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198weston_view_set_transform_parent(struct weston_view *view,
1199 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001200{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001201 if (view->geometry.parent) {
1202 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1203 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001204 }
1205
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001207
Jason Ekstranda7af7042013-10-12 22:38:11 -05001208 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001209 transform_parent_handle_parent_destroy;
1210 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001211 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001213 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001215 }
1216
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217 weston_view_geometry_dirty(view);
1218}
1219
1220WL_EXPORT int
1221weston_view_is_mapped(struct weston_view *view)
1222{
1223 if (view->output)
1224 return 1;
1225 else
1226 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001227}
1228
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001229WL_EXPORT int
1230weston_surface_is_mapped(struct weston_surface *surface)
1231{
1232 if (surface->output)
1233 return 1;
1234 else
1235 return 0;
1236}
1237
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001238static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001239surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001240{
1241 struct weston_view *view;
1242
1243 if (surface->width == width && surface->height == height)
1244 return;
1245
1246 surface->width = width;
1247 surface->height = height;
1248
1249 wl_list_for_each(view, &surface->views, surface_link)
1250 weston_view_geometry_dirty(view);
1251}
1252
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001253WL_EXPORT void
1254weston_surface_set_size(struct weston_surface *surface,
1255 int32_t width, int32_t height)
1256{
1257 assert(!surface->resource);
1258 surface_set_size(surface, width, height);
1259}
1260
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001261static void
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001262weston_surface_set_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001263{
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001264 int32_t width, height;
1265
1266 if (!surface->buffer_ref.buffer) {
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001267 surface_set_size(surface, 0, 0);
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001268 return;
1269 }
1270
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02001271 if (surface->buffer_viewport.viewport_set) {
Jonny Lamb74130762013-11-26 18:19:46 +01001272 surface->width = surface->buffer_viewport.dst_width;
1273 surface->height = surface->buffer_viewport.dst_height;
1274 return;
1275 }
1276
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001277 switch (surface->buffer_viewport.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001278 case WL_OUTPUT_TRANSFORM_90:
1279 case WL_OUTPUT_TRANSFORM_270:
1280 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1281 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001282 width = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001283 height = surface->buffer_ref.buffer->width;
1284 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001285 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001286 width = surface->buffer_ref.buffer->width;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001287 height = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001288 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001289 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001290
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001291 width = width / surface->buffer_viewport.scale;
1292 height = height / surface->buffer_viewport.scale;
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001293 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001294}
1295
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001296WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001297weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001298{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001299 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001300
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001301 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001302
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001303 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001304}
1305
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306WL_EXPORT struct weston_view *
1307weston_compositor_pick_view(struct weston_compositor *compositor,
1308 wl_fixed_t x, wl_fixed_t y,
1309 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001310{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001312
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313 wl_list_for_each(view, &compositor->view_list, link) {
1314 weston_view_from_global_fixed(view, x, y, vx, vy);
1315 if (pixman_region32_contains_point(&view->surface->input,
1316 wl_fixed_to_int(*vx),
1317 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001318 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001320 }
1321
1322 return NULL;
1323}
1324
1325static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001326weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001327{
Daniel Stone37816df2012-05-16 18:45:18 +01001328 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001329
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001330 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001331 return;
1332
Daniel Stone37816df2012-05-16 18:45:18 +01001333 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001334 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001335}
1336
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001337WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001339{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001340 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001341
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342 if (!weston_view_is_mapped(view))
1343 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001344
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 weston_view_damage_below(view);
1346 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001347 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348 wl_list_remove(&view->layer_link);
1349 wl_list_init(&view->layer_link);
1350 wl_list_remove(&view->link);
1351 wl_list_init(&view->link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +02001352 wl_list_remove(&view->output_destroy_listener.link);
1353 wl_list_init(&view->output_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354 view->output_mask = 0;
1355 weston_surface_assign_output(view->surface);
1356
1357 if (weston_surface_is_mapped(view->surface))
1358 return;
1359
1360 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1361 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001362 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001364 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001365 NULL,
1366 wl_fixed_from_int(0),
1367 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001368 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001369 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001370 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001372
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373WL_EXPORT void
1374weston_surface_unmap(struct weston_surface *surface)
1375{
1376 struct weston_view *view;
1377
1378 wl_list_for_each(view, &surface->views, surface_link)
1379 weston_view_unmap(view);
1380 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001381}
1382
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001383struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001384 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001385 struct wl_list link;
1386};
1387
Jason Ekstranda7af7042013-10-12 22:38:11 -05001388WL_EXPORT void
1389weston_view_destroy(struct weston_view *view)
1390{
1391 wl_signal_emit(&view->destroy_signal, view);
1392
1393 assert(wl_list_empty(&view->geometry.child_list));
1394
1395 if (weston_view_is_mapped(view)) {
1396 weston_view_unmap(view);
1397 weston_compositor_build_view_list(view->surface->compositor);
1398 }
1399
1400 wl_list_remove(&view->link);
1401 wl_list_remove(&view->layer_link);
1402
1403 pixman_region32_fini(&view->clip);
1404 pixman_region32_fini(&view->transform.boundingbox);
1405
1406 weston_view_set_transform_parent(view, NULL);
1407
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408 wl_list_remove(&view->surface_link);
1409
1410 free(view);
1411}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001412
1413WL_EXPORT void
1414weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001415{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001416 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001417 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001418
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001419 if (--surface->ref_count > 0)
1420 return;
1421
1422 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1423
Pekka Paalanene67858b2013-04-25 13:57:42 +03001424 assert(wl_list_empty(&surface->subsurface_list_pending));
1425 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001426
Jason Ekstranda7af7042013-10-12 22:38:11 -05001427 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1428 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001429
Pekka Paalanenbc106382012-10-10 12:49:31 +03001430 wl_list_for_each_safe(cb, next,
1431 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001432 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001433
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001434 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001435 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001436 pixman_region32_fini(&surface->pending.damage);
1437
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001438 if (surface->pending.buffer)
1439 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1440
Pekka Paalanende685b82012-12-04 15:58:12 +02001441 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001442
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001443 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001444 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001445 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001446
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001447 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001448 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001449
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001450 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001451}
1452
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001453static void
1454destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001455{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001456 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001457
Giulio Camuffo0d379742013-11-15 22:06:15 +01001458 /* Set the resource to NULL, since we don't want to leave a
1459 * dangling pointer if the surface was refcounted and survives
1460 * the weston_surface_destroy() call. */
1461 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001462 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001463}
1464
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001465static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001466weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1467{
1468 struct weston_buffer *buffer =
1469 container_of(listener, struct weston_buffer, destroy_listener);
1470
1471 wl_signal_emit(&buffer->destroy_signal, buffer);
1472 free(buffer);
1473}
1474
1475struct weston_buffer *
1476weston_buffer_from_resource(struct wl_resource *resource)
1477{
1478 struct weston_buffer *buffer;
1479 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001480
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001481 listener = wl_resource_get_destroy_listener(resource,
1482 weston_buffer_destroy_handler);
1483
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001484 if (listener)
1485 return container_of(listener, struct weston_buffer,
1486 destroy_listener);
1487
1488 buffer = zalloc(sizeof *buffer);
1489 if (buffer == NULL)
1490 return NULL;
1491
1492 buffer->resource = resource;
1493 wl_signal_init(&buffer->destroy_signal);
1494 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001495 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001496 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001497
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001498 return buffer;
1499}
1500
1501static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001502weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1503 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001504{
Pekka Paalanende685b82012-12-04 15:58:12 +02001505 struct weston_buffer_reference *ref =
1506 container_of(listener, struct weston_buffer_reference,
1507 destroy_listener);
1508
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001509 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001510 ref->buffer = NULL;
1511}
1512
1513WL_EXPORT void
1514weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001515 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001516{
1517 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001518 ref->buffer->busy_count--;
1519 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001520 assert(wl_resource_get_client(ref->buffer->resource));
1521 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001522 WL_BUFFER_RELEASE);
1523 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001524 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001525 }
1526
Pekka Paalanende685b82012-12-04 15:58:12 +02001527 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001528 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001529 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001530 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001531 }
1532
Pekka Paalanende685b82012-12-04 15:58:12 +02001533 ref->buffer = buffer;
1534 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1535}
1536
1537static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001538weston_surface_attach(struct weston_surface *surface,
1539 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001540{
1541 weston_buffer_reference(&surface->buffer_ref, buffer);
1542
Pekka Paalanena6421c42012-12-04 15:58:10 +02001543 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001544 if (weston_surface_is_mapped(surface))
1545 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001546 }
1547
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001548 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001549}
1550
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001551WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001552weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001553{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001554 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001555
1556 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001557 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001558}
1559
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001560WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001561weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001562{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001563 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001564
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001565 pixman_region32_union(&compositor->primary_plane.damage,
1566 &compositor->primary_plane.damage,
1567 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001568 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001569}
1570
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001571static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001573{
Pekka Paalanende685b82012-12-04 15:58:12 +02001574 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001575 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001576 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001577
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 empty_region(&surface->damage);
1579}
1580
1581static void
1582view_accumulate_damage(struct weston_view *view,
1583 pixman_region32_t *opaque)
1584{
1585 pixman_region32_t damage;
1586
1587 pixman_region32_init(&damage);
1588 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001589 pixman_box32_t *extents;
1590
Jason Ekstranda7af7042013-10-12 22:38:11 -05001591 extents = pixman_region32_extents(&view->surface->damage);
1592 view_compute_bbox(view, extents->x1, extents->y1,
1593 extents->x2 - extents->x1,
1594 extents->y2 - extents->y1,
1595 &damage);
1596 pixman_region32_translate(&damage,
1597 -view->plane->x,
1598 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001599 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001600 pixman_region32_copy(&damage, &view->surface->damage);
1601 pixman_region32_translate(&damage,
1602 view->geometry.x - view->plane->x,
1603 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001604 }
1605
Jason Ekstranda7af7042013-10-12 22:38:11 -05001606 pixman_region32_subtract(&damage, &damage, opaque);
1607 pixman_region32_union(&view->plane->damage,
1608 &view->plane->damage, &damage);
1609 pixman_region32_fini(&damage);
1610 pixman_region32_copy(&view->clip, opaque);
1611 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001612}
1613
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001614static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001615compositor_accumulate_damage(struct weston_compositor *ec)
1616{
1617 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001618 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001619 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001620
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001621 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001622
1623 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001624 pixman_region32_copy(&plane->clip, &clip);
1625
1626 pixman_region32_init(&opaque);
1627
Jason Ekstranda7af7042013-10-12 22:38:11 -05001628 wl_list_for_each(ev, &ec->view_list, link) {
1629 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001630 continue;
1631
Jason Ekstranda7af7042013-10-12 22:38:11 -05001632 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001633 }
1634
1635 pixman_region32_union(&clip, &clip, &opaque);
1636 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001637 }
1638
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001639 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001640
Jason Ekstranda7af7042013-10-12 22:38:11 -05001641 wl_list_for_each(ev, &ec->view_list, link)
1642 ev->surface->touched = 0;
1643
1644 wl_list_for_each(ev, &ec->view_list, link) {
1645 if (ev->surface->touched)
1646 continue;
1647 ev->surface->touched = 1;
1648
1649 surface_flush_damage(ev->surface);
1650
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001651 /* Both the renderer and the backend have seen the buffer
1652 * by now. If renderer needs the buffer, it has its own
1653 * reference set. If the backend wants to keep the buffer
1654 * around for migrating the surface into a non-primary plane
1655 * later, keep_buffer is true. Otherwise, drop the core
1656 * reference now, and allow early buffer release. This enables
1657 * clients to use single-buffering.
1658 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001659 if (!ev->surface->keep_buffer)
1660 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001661 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001662}
1663
1664static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001665surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001666{
1667 struct weston_subsurface *sub;
1668
Pekka Paalanene67858b2013-04-25 13:57:42 +03001669 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001670 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001671 continue;
1672
Jason Ekstranda7af7042013-10-12 22:38:11 -05001673 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1674 wl_list_init(&sub->surface->views);
1675
1676 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001677 }
1678}
1679
1680static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001681surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001682{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001683 struct weston_subsurface *sub;
1684 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001685
Jason Ekstranda7af7042013-10-12 22:38:11 -05001686 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1687 if (sub->surface == surface)
1688 continue;
1689
1690 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1691 weston_view_destroy(view);
1692
1693 surface_free_unused_subsurface_views(sub->surface);
1694 }
1695}
1696
1697static void
1698view_list_add_subsurface_view(struct weston_compositor *compositor,
1699 struct weston_subsurface *sub,
1700 struct weston_view *parent)
1701{
1702 struct weston_subsurface *child;
1703 struct weston_view *view = NULL, *iv;
1704
1705 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1706 if (iv->geometry.parent == parent) {
1707 view = iv;
1708 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001709 }
1710 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001711
1712 if (view) {
1713 /* Put it back in the surface's list of views */
1714 wl_list_remove(&view->surface_link);
1715 wl_list_insert(&sub->surface->views, &view->surface_link);
1716 } else {
1717 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001718 weston_view_set_position(view,
1719 sub->position.x,
1720 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001721 weston_view_set_transform_parent(view, parent);
1722 }
1723
1724 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001725
Pekka Paalanenb188e912013-11-19 14:03:35 +02001726 if (wl_list_empty(&sub->surface->subsurface_list)) {
1727 wl_list_insert(compositor->view_list.prev, &view->link);
1728 return;
1729 }
1730
1731 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1732 if (child->surface == sub->surface)
1733 wl_list_insert(compositor->view_list.prev, &view->link);
1734 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001736 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001737}
1738
1739static void
1740view_list_add(struct weston_compositor *compositor,
1741 struct weston_view *view)
1742{
1743 struct weston_subsurface *sub;
1744
1745 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746
Pekka Paalanenb188e912013-11-19 14:03:35 +02001747 if (wl_list_empty(&view->surface->subsurface_list)) {
1748 wl_list_insert(compositor->view_list.prev, &view->link);
1749 return;
1750 }
1751
1752 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1753 if (sub->surface == view->surface)
1754 wl_list_insert(compositor->view_list.prev, &view->link);
1755 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001756 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001757 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001758}
1759
1760static void
1761weston_compositor_build_view_list(struct weston_compositor *compositor)
1762{
1763 struct weston_view *view;
1764 struct weston_layer *layer;
1765
1766 wl_list_for_each(layer, &compositor->layer_list, link)
1767 wl_list_for_each(view, &layer->view_list, layer_link)
1768 surface_stash_subsurface_views(view->surface);
1769
1770 wl_list_init(&compositor->view_list);
1771 wl_list_for_each(layer, &compositor->layer_list, link) {
1772 wl_list_for_each(view, &layer->view_list, layer_link) {
1773 view_list_add(compositor, view);
1774 }
1775 }
1776
1777 wl_list_for_each(layer, &compositor->layer_list, link)
1778 wl_list_for_each(view, &layer->view_list, layer_link)
1779 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001780}
1781
David Herrmann1edf44c2013-10-22 17:11:26 +02001782static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001783weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001784{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001785 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001787 struct weston_animation *animation, *next;
1788 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001789 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001790 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001791 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001792
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001793 if (output->destroying)
1794 return 0;
1795
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001796 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001797 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001798
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001799 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001800 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001801 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001802 wl_list_for_each(ev, &ec->view_list, link)
1803 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001804
Pekka Paalanene67858b2013-04-25 13:57:42 +03001805 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001806 wl_list_for_each(ev, &ec->view_list, link) {
1807 /* Note: This operation is safe to do multiple times on the
1808 * same surface.
1809 */
1810 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001811 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001812 &ev->surface->frame_callback_list);
1813 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001814 }
1815 }
1816
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001817 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001818
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001819 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001820 pixman_region32_intersect(&output_damage,
1821 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001822 pixman_region32_subtract(&output_damage,
1823 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001824
Scott Moreauccbf29d2012-02-22 14:21:41 -07001825 if (output->dirty)
1826 weston_output_update_matrix(output);
1827
David Herrmann1edf44c2013-10-22 17:11:26 +02001828 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001829
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001830 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001831
Kristian Høgsbergef044142011-06-21 15:02:12 -04001832 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001833
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001834 weston_compositor_repick(ec);
1835 wl_event_loop_dispatch(ec->input_loop, 0);
1836
Jonas Ådahldb773762012-06-13 00:01:21 +02001837 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001838 wl_callback_send_done(cb->resource, msecs);
1839 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001840 }
1841
Scott Moreaud64cf212012-06-08 19:40:54 -06001842 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001843 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001844 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001845 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001846
1847 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001848}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001849
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001850static int
1851weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001852{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001853 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001854
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001855 wl_event_loop_dispatch(compositor->input_loop, 0);
1856
1857 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001858}
1859
1860WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001861weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001862{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001863 struct weston_compositor *compositor = output->compositor;
1864 struct wl_event_loop *loop =
1865 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001866 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001867
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001868 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001869
1870 if (output->repaint_needed &&
1871 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1872 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001873 r = weston_output_repaint(output, msecs);
1874 if (!r)
1875 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001876 }
1877
1878 output->repaint_scheduled = 0;
1879 if (compositor->input_loop_source)
1880 return;
1881
1882 fd = wl_event_loop_get_fd(compositor->input_loop);
1883 compositor->input_loop_source =
1884 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1885 weston_compositor_read_input, compositor);
1886}
1887
1888static void
1889idle_repaint(void *data)
1890{
1891 struct weston_output *output = data;
1892
Jonas Ådahle5a12252013-04-05 23:07:11 +02001893 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001894}
1895
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001896WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001897weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1898{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001899 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001900 if (below != NULL)
1901 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001902}
1903
1904WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001905weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001906{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001907 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001908 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001909
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001910 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1911 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001912 return;
1913
Kristian Høgsbergef044142011-06-21 15:02:12 -04001914 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001915 output->repaint_needed = 1;
1916 if (output->repaint_scheduled)
1917 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001918
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001919 wl_event_loop_add_idle(loop, idle_repaint, output);
1920 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001921
1922 if (compositor->input_loop_source) {
1923 wl_event_source_remove(compositor->input_loop_source);
1924 compositor->input_loop_source = NULL;
1925 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001926}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001927
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001928WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001929weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1930{
1931 struct weston_output *output;
1932
1933 wl_list_for_each(output, &compositor->output_list, link)
1934 weston_output_schedule_repaint(output);
1935}
1936
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001937static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001938surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001939{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001940 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001941}
1942
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001943static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001944surface_attach(struct wl_client *client,
1945 struct wl_resource *resource,
1946 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1947{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001948 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001949 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001950
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001951 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001952 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001953 if (buffer == NULL) {
1954 wl_client_post_no_memory(client);
1955 return;
1956 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001957 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001958
Pekka Paalanende685b82012-12-04 15:58:12 +02001959 /* Attach, attach, without commit in between does not send
1960 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001961 if (surface->pending.buffer)
1962 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001963
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001964 surface->pending.sx = sx;
1965 surface->pending.sy = sy;
1966 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001967 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001968 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001969 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001970 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001971 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001972}
1973
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001974static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001975surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001976 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001977 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001978{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001979 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001980
Pekka Paalanen8e159182012-10-10 12:49:25 +03001981 pixman_region32_union_rect(&surface->pending.damage,
1982 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001983 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001984}
1985
Kristian Høgsberg33418202011-08-16 23:01:28 -04001986static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001987destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001988{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001989 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001990
1991 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001992 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001993}
1994
1995static void
1996surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001997 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001998{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001999 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002000 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002001
2002 cb = malloc(sizeof *cb);
2003 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002004 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002005 return;
2006 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002007
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002008 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2009 callback);
2010 if (cb->resource == NULL) {
2011 free(cb);
2012 wl_resource_post_no_memory(resource);
2013 return;
2014 }
2015
Jason Ekstranda85118c2013-06-27 20:17:02 -05002016 wl_resource_set_implementation(cb->resource, NULL, cb,
2017 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002018
Pekka Paalanenbc106382012-10-10 12:49:31 +03002019 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002020}
2021
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002022static void
2023surface_set_opaque_region(struct wl_client *client,
2024 struct wl_resource *resource,
2025 struct wl_resource *region_resource)
2026{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002027 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002028 struct weston_region *region;
2029
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002030 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002031 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002032 pixman_region32_copy(&surface->pending.opaque,
2033 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002034 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03002035 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002036 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002037}
2038
2039static void
2040surface_set_input_region(struct wl_client *client,
2041 struct wl_resource *resource,
2042 struct wl_resource *region_resource)
2043{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002044 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002045 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002046
2047 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002048 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002049 pixman_region32_copy(&surface->pending.input,
2050 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002051 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002052 pixman_region32_fini(&surface->pending.input);
2053 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002054 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002055}
2056
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002057static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002058weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002059{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002060 struct weston_subsurface *sub;
2061
2062 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2063 parent_link_pending) {
2064 wl_list_remove(&sub->parent_link);
2065 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2066 }
2067}
2068
2069static void
2070weston_surface_commit(struct weston_surface *surface)
2071{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002072 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002073 pixman_region32_t opaque;
2074
Alexander Larsson4ea95522013-05-22 14:41:37 +02002075 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002076 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002077 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002078 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002079
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002080 /* wl_surface.attach */
Jason Ekstrand81038fb2014-01-11 12:12:19 -06002081 if (surface->pending.buffer || surface->pending.newly_attached) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002082 weston_surface_attach(surface, surface->pending.buffer);
Jason Ekstrand81038fb2014-01-11 12:12:19 -06002083 weston_surface_set_size_from_buffer(surface);
2084 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002085
2086 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02002087 surface->configure(surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002088 surface->pending.sx, surface->pending.sy);
Giulio Camuffo184df502013-02-21 11:29:21 +01002089
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05002090 if (surface->pending.buffer)
2091 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
2092 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11002093 surface->pending.sx = 0;
2094 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01002095 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002096
2097 /* wl_surface.damage */
2098 pixman_region32_union(&surface->damage, &surface->damage,
2099 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002100 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2101 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002102 surface->width,
2103 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002104 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002105
2106 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002107 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002108 surface->width,
2109 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002110 pixman_region32_intersect(&opaque,
2111 &opaque, &surface->pending.opaque);
2112
2113 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2114 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002115 wl_list_for_each(view, &surface->views, surface_link)
2116 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002117 }
2118
2119 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002120
2121 /* wl_surface.set_input_region */
2122 pixman_region32_fini(&surface->input);
2123 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124 surface->width,
2125 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002126 pixman_region32_intersect(&surface->input,
2127 &surface->input, &surface->pending.input);
2128
Pekka Paalanenbc106382012-10-10 12:49:31 +03002129 /* wl_surface.frame */
2130 wl_list_insert_list(&surface->frame_callback_list,
2131 &surface->pending.frame_callback_list);
2132 wl_list_init(&surface->pending.frame_callback_list);
2133
Pekka Paalanene67858b2013-04-25 13:57:42 +03002134 weston_surface_commit_subsurface_order(surface);
2135
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002136 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002137}
2138
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002139static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002140weston_subsurface_commit(struct weston_subsurface *sub);
2141
2142static void
2143weston_subsurface_parent_commit(struct weston_subsurface *sub,
2144 int parent_is_synchronized);
2145
2146static void
2147surface_commit(struct wl_client *client, struct wl_resource *resource)
2148{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002149 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002150 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2151
2152 if (sub) {
2153 weston_subsurface_commit(sub);
2154 return;
2155 }
2156
2157 weston_surface_commit(surface);
2158
2159 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2160 if (sub->surface != surface)
2161 weston_subsurface_parent_commit(sub, 0);
2162 }
2163}
2164
2165static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002166surface_set_buffer_transform(struct wl_client *client,
2167 struct wl_resource *resource, int transform)
2168{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002169 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002170
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002171 surface->pending.buffer_viewport.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002172}
2173
Alexander Larsson4ea95522013-05-22 14:41:37 +02002174static void
2175surface_set_buffer_scale(struct wl_client *client,
2176 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002177 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002178{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002179 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002180
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002181 surface->pending.buffer_viewport.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002182}
2183
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002184static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002185 surface_destroy,
2186 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002187 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002188 surface_frame,
2189 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002190 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002191 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002192 surface_set_buffer_transform,
2193 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002194};
2195
2196static void
2197compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002198 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002199{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002200 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002201 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002202
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002203 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002204 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002205 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002206 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002207 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002208
Jason Ekstranda85118c2013-06-27 20:17:02 -05002209 surface->resource =
2210 wl_resource_create(client, &wl_surface_interface,
2211 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002212 if (surface->resource == NULL) {
2213 weston_surface_destroy(surface);
2214 wl_resource_post_no_memory(resource);
2215 return;
2216 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002217 wl_resource_set_implementation(surface->resource, &surface_interface,
2218 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002219}
2220
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002221static void
2222destroy_region(struct wl_resource *resource)
2223{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002224 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002225
2226 pixman_region32_fini(&region->region);
2227 free(region);
2228}
2229
2230static void
2231region_destroy(struct wl_client *client, struct wl_resource *resource)
2232{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002233 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002234}
2235
2236static void
2237region_add(struct wl_client *client, struct wl_resource *resource,
2238 int32_t x, int32_t y, int32_t width, int32_t height)
2239{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002240 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002241
2242 pixman_region32_union_rect(&region->region, &region->region,
2243 x, y, width, height);
2244}
2245
2246static void
2247region_subtract(struct wl_client *client, struct wl_resource *resource,
2248 int32_t x, int32_t y, int32_t width, int32_t height)
2249{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002250 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002251 pixman_region32_t rect;
2252
2253 pixman_region32_init_rect(&rect, x, y, width, height);
2254 pixman_region32_subtract(&region->region, &region->region, &rect);
2255 pixman_region32_fini(&rect);
2256}
2257
2258static const struct wl_region_interface region_interface = {
2259 region_destroy,
2260 region_add,
2261 region_subtract
2262};
2263
2264static void
2265compositor_create_region(struct wl_client *client,
2266 struct wl_resource *resource, uint32_t id)
2267{
2268 struct weston_region *region;
2269
2270 region = malloc(sizeof *region);
2271 if (region == NULL) {
2272 wl_resource_post_no_memory(resource);
2273 return;
2274 }
2275
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002276 pixman_region32_init(&region->region);
2277
Jason Ekstranda85118c2013-06-27 20:17:02 -05002278 region->resource =
2279 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002280 if (region->resource == NULL) {
2281 free(region);
2282 wl_resource_post_no_memory(resource);
2283 return;
2284 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002285 wl_resource_set_implementation(region->resource, &region_interface,
2286 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002287}
2288
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002289static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002290 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002291 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002292};
2293
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002294static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002295weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2296{
2297 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002298 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002299 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002300
Alexander Larsson4ea95522013-05-22 14:41:37 +02002301 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002302 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002303 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002304 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002305
Pekka Paalanene67858b2013-04-25 13:57:42 +03002306 /* wl_surface.attach */
Jason Ekstrand81038fb2014-01-11 12:12:19 -06002307 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002308 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
Jason Ekstrand81038fb2014-01-11 12:12:19 -06002309 weston_surface_set_size_from_buffer(surface);
2310 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002311 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2312
Pekka Paalanene67858b2013-04-25 13:57:42 +03002313 if (surface->configure && sub->cached.newly_attached)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002314 surface->configure(surface, sub->cached.sx, sub->cached.sy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002315 sub->cached.sx = 0;
2316 sub->cached.sy = 0;
2317 sub->cached.newly_attached = 0;
2318
2319 /* wl_surface.damage */
2320 pixman_region32_union(&surface->damage, &surface->damage,
2321 &sub->cached.damage);
2322 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2323 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002324 surface->width,
2325 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002326 empty_region(&sub->cached.damage);
2327
2328 /* wl_surface.set_opaque_region */
2329 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002330 surface->width,
2331 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002332 pixman_region32_intersect(&opaque,
2333 &opaque, &sub->cached.opaque);
2334
2335 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2336 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002337 wl_list_for_each(view, &surface->views, surface_link)
2338 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002339 }
2340
2341 pixman_region32_fini(&opaque);
2342
2343 /* wl_surface.set_input_region */
2344 pixman_region32_fini(&surface->input);
2345 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002346 surface->width,
2347 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002348 pixman_region32_intersect(&surface->input,
2349 &surface->input, &sub->cached.input);
2350
2351 /* wl_surface.frame */
2352 wl_list_insert_list(&surface->frame_callback_list,
2353 &sub->cached.frame_callback_list);
2354 wl_list_init(&sub->cached.frame_callback_list);
2355
2356 weston_surface_commit_subsurface_order(surface);
2357
2358 weston_surface_schedule_repaint(surface);
2359
2360 sub->cached.has_data = 0;
2361}
2362
2363static void
2364weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2365{
2366 struct weston_surface *surface = sub->surface;
2367
2368 /*
2369 * If this commit would cause the surface to move by the
2370 * attach(dx, dy) parameters, the old damage region must be
2371 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002372 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002373 */
2374 pixman_region32_translate(&sub->cached.damage,
2375 -surface->pending.sx, -surface->pending.sy);
2376 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2377 &surface->pending.damage);
2378 empty_region(&surface->pending.damage);
2379
2380 if (surface->pending.newly_attached) {
2381 sub->cached.newly_attached = 1;
2382 weston_buffer_reference(&sub->cached.buffer_ref,
2383 surface->pending.buffer);
2384 }
2385 sub->cached.sx += surface->pending.sx;
2386 sub->cached.sy += surface->pending.sy;
2387 surface->pending.sx = 0;
2388 surface->pending.sy = 0;
2389 surface->pending.newly_attached = 0;
2390
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002391 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002392
2393 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2394
2395 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2396
2397 wl_list_insert_list(&sub->cached.frame_callback_list,
2398 &surface->pending.frame_callback_list);
2399 wl_list_init(&surface->pending.frame_callback_list);
2400
2401 sub->cached.has_data = 1;
2402}
2403
2404static int
2405weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2406{
2407 while (sub) {
2408 if (sub->synchronized)
2409 return 1;
2410
2411 if (!sub->parent)
2412 return 0;
2413
2414 sub = weston_surface_to_subsurface(sub->parent);
2415 }
2416
2417 return 0;
2418}
2419
2420static void
2421weston_subsurface_commit(struct weston_subsurface *sub)
2422{
2423 struct weston_surface *surface = sub->surface;
2424 struct weston_subsurface *tmp;
2425
2426 /* Recursive check for effectively synchronized. */
2427 if (weston_subsurface_is_synchronized(sub)) {
2428 weston_subsurface_commit_to_cache(sub);
2429 } else {
2430 if (sub->cached.has_data) {
2431 /* flush accumulated state from cache */
2432 weston_subsurface_commit_to_cache(sub);
2433 weston_subsurface_commit_from_cache(sub);
2434 } else {
2435 weston_surface_commit(surface);
2436 }
2437
2438 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2439 if (tmp->surface != surface)
2440 weston_subsurface_parent_commit(tmp, 0);
2441 }
2442 }
2443}
2444
2445static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002446weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002447{
2448 struct weston_surface *surface = sub->surface;
2449 struct weston_subsurface *tmp;
2450
Pekka Paalanene67858b2013-04-25 13:57:42 +03002451 /* From now on, commit_from_cache the whole sub-tree, regardless of
2452 * the synchronized mode of each child. This sub-surface or some
2453 * of its ancestors were synchronized, so we are synchronized
2454 * all the way down.
2455 */
2456
2457 if (sub->cached.has_data)
2458 weston_subsurface_commit_from_cache(sub);
2459
2460 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2461 if (tmp->surface != surface)
2462 weston_subsurface_parent_commit(tmp, 1);
2463 }
2464}
2465
2466static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002467weston_subsurface_parent_commit(struct weston_subsurface *sub,
2468 int parent_is_synchronized)
2469{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002470 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002471 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002472 wl_list_for_each(view, &sub->surface->views, surface_link)
2473 weston_view_set_position(view,
2474 sub->position.x,
2475 sub->position.y);
2476
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002477 sub->position.set = 0;
2478 }
2479
2480 if (parent_is_synchronized || sub->synchronized)
2481 weston_subsurface_synchronized_commit(sub);
2482}
2483
2484static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002485subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002486{
2487 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002488 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002489
Jason Ekstranda7af7042013-10-12 22:38:11 -05002490 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002491 weston_view_set_position(view,
2492 view->geometry.x + dx,
2493 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002494
2495 /* No need to check parent mappedness, because if parent is not
2496 * mapped, parent is not in a visible layer, so this sub-surface
2497 * will not be drawn either.
2498 */
2499 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002500 /* Cannot call weston_surface_update_transform(),
2501 * because that would call it also for the parent surface,
2502 * which might not be mapped yet. That would lead to
2503 * inconsistent state, where the window could never be
2504 * mapped.
2505 *
2506 * Instead just assing any output, to make
2507 * weston_surface_is_mapped() return true, so that when the
2508 * parent surface does get mapped, this one will get
2509 * included, too. See surface_list_add().
2510 */
2511 assert(!wl_list_empty(&compositor->output_list));
2512 surface->output = container_of(compositor->output_list.next,
2513 struct weston_output, link);
2514 }
2515}
2516
2517static struct weston_subsurface *
2518weston_surface_to_subsurface(struct weston_surface *surface)
2519{
2520 if (surface->configure == subsurface_configure)
2521 return surface->configure_private;
2522
2523 return NULL;
2524}
2525
Pekka Paalanen01388e22013-04-25 13:57:44 +03002526WL_EXPORT struct weston_surface *
2527weston_surface_get_main_surface(struct weston_surface *surface)
2528{
2529 struct weston_subsurface *sub;
2530
2531 while (surface && (sub = weston_surface_to_subsurface(surface)))
2532 surface = sub->parent;
2533
2534 return surface;
2535}
2536
Pekka Paalanene67858b2013-04-25 13:57:42 +03002537static void
2538subsurface_set_position(struct wl_client *client,
2539 struct wl_resource *resource, int32_t x, int32_t y)
2540{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002541 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002542
2543 if (!sub)
2544 return;
2545
2546 sub->position.x = x;
2547 sub->position.y = y;
2548 sub->position.set = 1;
2549}
2550
2551static struct weston_subsurface *
2552subsurface_from_surface(struct weston_surface *surface)
2553{
2554 struct weston_subsurface *sub;
2555
2556 sub = weston_surface_to_subsurface(surface);
2557 if (sub)
2558 return sub;
2559
2560 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2561 if (sub->surface == surface)
2562 return sub;
2563
2564 return NULL;
2565}
2566
2567static struct weston_subsurface *
2568subsurface_sibling_check(struct weston_subsurface *sub,
2569 struct weston_surface *surface,
2570 const char *request)
2571{
2572 struct weston_subsurface *sibling;
2573
2574 sibling = subsurface_from_surface(surface);
2575
2576 if (!sibling) {
2577 wl_resource_post_error(sub->resource,
2578 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2579 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002580 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002581 return NULL;
2582 }
2583
2584 if (sibling->parent != sub->parent) {
2585 wl_resource_post_error(sub->resource,
2586 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2587 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002588 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002589 return NULL;
2590 }
2591
2592 return sibling;
2593}
2594
2595static void
2596subsurface_place_above(struct wl_client *client,
2597 struct wl_resource *resource,
2598 struct wl_resource *sibling_resource)
2599{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002600 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002601 struct weston_surface *surface =
2602 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002603 struct weston_subsurface *sibling;
2604
2605 if (!sub)
2606 return;
2607
2608 sibling = subsurface_sibling_check(sub, surface, "place_above");
2609 if (!sibling)
2610 return;
2611
2612 wl_list_remove(&sub->parent_link_pending);
2613 wl_list_insert(sibling->parent_link_pending.prev,
2614 &sub->parent_link_pending);
2615}
2616
2617static void
2618subsurface_place_below(struct wl_client *client,
2619 struct wl_resource *resource,
2620 struct wl_resource *sibling_resource)
2621{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002622 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002623 struct weston_surface *surface =
2624 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002625 struct weston_subsurface *sibling;
2626
2627 if (!sub)
2628 return;
2629
2630 sibling = subsurface_sibling_check(sub, surface, "place_below");
2631 if (!sibling)
2632 return;
2633
2634 wl_list_remove(&sub->parent_link_pending);
2635 wl_list_insert(&sibling->parent_link_pending,
2636 &sub->parent_link_pending);
2637}
2638
2639static void
2640subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2641{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002642 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002643
2644 if (sub)
2645 sub->synchronized = 1;
2646}
2647
2648static void
2649subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2650{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002651 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002652
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002653 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002654 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002655
2656 /* If sub became effectively desynchronized, flush. */
2657 if (!weston_subsurface_is_synchronized(sub))
2658 weston_subsurface_synchronized_commit(sub);
2659 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002660}
2661
2662static void
2663weston_subsurface_cache_init(struct weston_subsurface *sub)
2664{
2665 pixman_region32_init(&sub->cached.damage);
2666 pixman_region32_init(&sub->cached.opaque);
2667 pixman_region32_init(&sub->cached.input);
2668 wl_list_init(&sub->cached.frame_callback_list);
2669 sub->cached.buffer_ref.buffer = NULL;
2670}
2671
2672static void
2673weston_subsurface_cache_fini(struct weston_subsurface *sub)
2674{
2675 struct weston_frame_callback *cb, *tmp;
2676
2677 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002678 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002679
2680 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2681 pixman_region32_fini(&sub->cached.damage);
2682 pixman_region32_fini(&sub->cached.opaque);
2683 pixman_region32_fini(&sub->cached.input);
2684}
2685
2686static void
2687weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2688{
2689 wl_list_remove(&sub->parent_link);
2690 wl_list_remove(&sub->parent_link_pending);
2691 wl_list_remove(&sub->parent_destroy_listener.link);
2692 sub->parent = NULL;
2693}
2694
2695static void
2696weston_subsurface_destroy(struct weston_subsurface *sub);
2697
2698static void
2699subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2700{
2701 struct weston_subsurface *sub =
2702 container_of(listener, struct weston_subsurface,
2703 surface_destroy_listener);
2704 assert(data == &sub->surface->resource);
2705
2706 /* The protocol object (wl_resource) is left inert. */
2707 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002708 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002709
2710 weston_subsurface_destroy(sub);
2711}
2712
2713static void
2714subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2715{
2716 struct weston_subsurface *sub =
2717 container_of(listener, struct weston_subsurface,
2718 parent_destroy_listener);
2719 assert(data == &sub->parent->resource);
2720 assert(sub->surface != sub->parent);
2721
2722 if (weston_surface_is_mapped(sub->surface))
2723 weston_surface_unmap(sub->surface);
2724
2725 weston_subsurface_unlink_parent(sub);
2726}
2727
2728static void
2729subsurface_resource_destroy(struct wl_resource *resource)
2730{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002731 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002732
2733 if (sub)
2734 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002735}
2736
2737static void
2738subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2739{
2740 wl_resource_destroy(resource);
2741}
2742
2743static void
2744weston_subsurface_link_parent(struct weston_subsurface *sub,
2745 struct weston_surface *parent)
2746{
2747 sub->parent = parent;
2748 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002749 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002750 &sub->parent_destroy_listener);
2751
2752 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2753 wl_list_insert(&parent->subsurface_list_pending,
2754 &sub->parent_link_pending);
2755}
2756
2757static void
2758weston_subsurface_link_surface(struct weston_subsurface *sub,
2759 struct weston_surface *surface)
2760{
2761 sub->surface = surface;
2762 sub->surface_destroy_listener.notify =
2763 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002764 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002765 &sub->surface_destroy_listener);
2766}
2767
2768static void
2769weston_subsurface_destroy(struct weston_subsurface *sub)
2770{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002771 struct weston_view *view, *next;
2772
Pekka Paalanene67858b2013-04-25 13:57:42 +03002773 assert(sub->surface);
2774
2775 if (sub->resource) {
2776 assert(weston_surface_to_subsurface(sub->surface) == sub);
2777 assert(sub->parent_destroy_listener.notify ==
2778 subsurface_handle_parent_destroy);
2779
Jason Ekstranda7af7042013-10-12 22:38:11 -05002780 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2781 weston_view_destroy(view);
2782
Pekka Paalanene67858b2013-04-25 13:57:42 +03002783 if (sub->parent)
2784 weston_subsurface_unlink_parent(sub);
2785
2786 weston_subsurface_cache_fini(sub);
2787
2788 sub->surface->configure = NULL;
2789 sub->surface->configure_private = NULL;
2790 } else {
2791 /* the dummy weston_subsurface for the parent itself */
2792 assert(sub->parent_destroy_listener.notify == NULL);
2793 wl_list_remove(&sub->parent_link);
2794 wl_list_remove(&sub->parent_link_pending);
2795 }
2796
2797 wl_list_remove(&sub->surface_destroy_listener.link);
2798 free(sub);
2799}
2800
2801static const struct wl_subsurface_interface subsurface_implementation = {
2802 subsurface_destroy,
2803 subsurface_set_position,
2804 subsurface_place_above,
2805 subsurface_place_below,
2806 subsurface_set_sync,
2807 subsurface_set_desync
2808};
2809
2810static struct weston_subsurface *
2811weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2812 struct weston_surface *parent)
2813{
2814 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002815 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002816
2817 sub = calloc(1, sizeof *sub);
2818 if (!sub)
2819 return NULL;
2820
Jason Ekstranda7af7042013-10-12 22:38:11 -05002821 wl_list_init(&sub->unused_views);
2822
Jason Ekstranda85118c2013-06-27 20:17:02 -05002823 sub->resource =
2824 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002825 if (!sub->resource) {
2826 free(sub);
2827 return NULL;
2828 }
2829
Jason Ekstranda85118c2013-06-27 20:17:02 -05002830 wl_resource_set_implementation(sub->resource,
2831 &subsurface_implementation,
2832 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002833 weston_subsurface_link_surface(sub, surface);
2834 weston_subsurface_link_parent(sub, parent);
2835 weston_subsurface_cache_init(sub);
2836 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002837
2838 return sub;
2839}
2840
2841/* Create a dummy subsurface for having the parent itself in its
2842 * sub-surface lists. Makes stacking order manipulation easy.
2843 */
2844static struct weston_subsurface *
2845weston_subsurface_create_for_parent(struct weston_surface *parent)
2846{
2847 struct weston_subsurface *sub;
2848
2849 sub = calloc(1, sizeof *sub);
2850 if (!sub)
2851 return NULL;
2852
2853 weston_subsurface_link_surface(sub, parent);
2854 sub->parent = parent;
2855 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2856 wl_list_insert(&parent->subsurface_list_pending,
2857 &sub->parent_link_pending);
2858
2859 return sub;
2860}
2861
2862static void
2863subcompositor_get_subsurface(struct wl_client *client,
2864 struct wl_resource *resource,
2865 uint32_t id,
2866 struct wl_resource *surface_resource,
2867 struct wl_resource *parent_resource)
2868{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002869 struct weston_surface *surface =
2870 wl_resource_get_user_data(surface_resource);
2871 struct weston_surface *parent =
2872 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002873 struct weston_subsurface *sub;
2874 static const char where[] = "get_subsurface: wl_subsurface@";
2875
2876 if (surface == parent) {
2877 wl_resource_post_error(resource,
2878 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2879 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002880 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002881 return;
2882 }
2883
2884 if (weston_surface_to_subsurface(surface)) {
2885 wl_resource_post_error(resource,
2886 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2887 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002888 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002889 return;
2890 }
2891
2892 if (surface->configure) {
2893 wl_resource_post_error(resource,
2894 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2895 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002896 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002897 return;
2898 }
2899
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002900 if (weston_surface_get_main_surface(parent) == surface) {
2901 wl_resource_post_error(resource,
2902 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2903 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002904 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002905 return;
2906 }
2907
Pekka Paalanene67858b2013-04-25 13:57:42 +03002908 /* make sure the parent is in its own list */
2909 if (wl_list_empty(&parent->subsurface_list)) {
2910 if (!weston_subsurface_create_for_parent(parent)) {
2911 wl_resource_post_no_memory(resource);
2912 return;
2913 }
2914 }
2915
2916 sub = weston_subsurface_create(id, surface, parent);
2917 if (!sub) {
2918 wl_resource_post_no_memory(resource);
2919 return;
2920 }
2921
2922 surface->configure = subsurface_configure;
2923 surface->configure_private = sub;
2924}
2925
2926static void
2927subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2928{
2929 wl_resource_destroy(resource);
2930}
2931
2932static const struct wl_subcompositor_interface subcompositor_interface = {
2933 subcompositor_destroy,
2934 subcompositor_get_subsurface
2935};
2936
2937static void
2938bind_subcompositor(struct wl_client *client,
2939 void *data, uint32_t version, uint32_t id)
2940{
2941 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002942 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002943
Jason Ekstranda85118c2013-06-27 20:17:02 -05002944 resource =
2945 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002946 if (resource == NULL) {
2947 wl_client_post_no_memory(client);
2948 return;
2949 }
2950 wl_resource_set_implementation(resource, &subcompositor_interface,
2951 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002952}
2953
2954static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002955weston_compositor_dpms(struct weston_compositor *compositor,
2956 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002957{
2958 struct weston_output *output;
2959
2960 wl_list_for_each(output, &compositor->output_list, link)
2961 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002962 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002963}
2964
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002965WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002966weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002967{
Neil Roberts8b62e202013-09-30 13:14:47 +01002968 uint32_t old_state = compositor->state;
2969
2970 /* The state needs to be changed before emitting the wake
2971 * signal because that may try to schedule a repaint which
2972 * will not work if the compositor is still sleeping */
2973 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2974
2975 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002976 case WESTON_COMPOSITOR_SLEEPING:
2977 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2978 /* fall through */
2979 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002980 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002981 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002982 /* fall through */
2983 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002984 wl_event_source_timer_update(compositor->idle_source,
2985 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002986 }
2987}
2988
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002989WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002990weston_compositor_offscreen(struct weston_compositor *compositor)
2991{
2992 switch (compositor->state) {
2993 case WESTON_COMPOSITOR_OFFSCREEN:
2994 return;
2995 case WESTON_COMPOSITOR_SLEEPING:
2996 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2997 /* fall through */
2998 default:
2999 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3000 wl_event_source_timer_update(compositor->idle_source, 0);
3001 }
3002}
3003
3004WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003005weston_compositor_sleep(struct weston_compositor *compositor)
3006{
3007 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3008 return;
3009
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003010 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003011 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3012 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3013}
3014
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003015static int
3016idle_handler(void *data)
3017{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003018 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003019
3020 if (compositor->idle_inhibit)
3021 return 1;
3022
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003023 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003024 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003025
3026 return 1;
3027}
3028
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003029WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003030weston_plane_init(struct weston_plane *plane,
3031 struct weston_compositor *ec,
3032 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003033{
3034 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003035 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003036 plane->x = x;
3037 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003038 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003039
3040 /* Init the link so that the call to wl_list_remove() when releasing
3041 * the plane without ever stacking doesn't lead to a crash */
3042 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003043}
3044
3045WL_EXPORT void
3046weston_plane_release(struct weston_plane *plane)
3047{
Xiong Zhang97116532013-10-23 13:58:31 +08003048 struct weston_view *view;
3049
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003050 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003051 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003052
Xiong Zhang97116532013-10-23 13:58:31 +08003053 wl_list_for_each(view, &plane->compositor->view_list, link) {
3054 if (view->plane == plane)
3055 view->plane = NULL;
3056 }
3057
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003058 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003059}
3060
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003061WL_EXPORT void
3062weston_compositor_stack_plane(struct weston_compositor *ec,
3063 struct weston_plane *plane,
3064 struct weston_plane *above)
3065{
3066 if (above)
3067 wl_list_insert(above->link.prev, &plane->link);
3068 else
3069 wl_list_insert(&ec->plane_list, &plane->link);
3070}
3071
Casey Dahlin9074db52012-04-19 22:50:09 -04003072static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003073{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003074 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003075}
3076
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003077static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003078bind_output(struct wl_client *client,
3079 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003080{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003081 struct weston_output *output = data;
3082 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003083 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003084
Jason Ekstranda85118c2013-06-27 20:17:02 -05003085 resource = wl_resource_create(client, &wl_output_interface,
3086 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003087 if (resource == NULL) {
3088 wl_client_post_no_memory(client);
3089 return;
3090 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003091
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003092 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003093 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003094
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003095 wl_output_send_geometry(resource,
3096 output->x,
3097 output->y,
3098 output->mm_width,
3099 output->mm_height,
3100 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003101 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003102 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003103 if (version >= 2)
3104 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003105 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003106
3107 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003108 wl_output_send_mode(resource,
3109 mode->flags,
3110 mode->width,
3111 mode->height,
3112 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003113 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003114
3115 if (version >= 2)
3116 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003117}
3118
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003119/* Move other outputs when one is removed so the space remains contiguos. */
3120static void
3121weston_compositor_remove_output(struct weston_compositor *compositor,
3122 struct weston_output *remove_output)
3123{
3124 struct weston_output *output;
3125 int offset = 0;
3126
3127 wl_list_for_each(output, &compositor->output_list, link) {
3128 if (output == remove_output) {
3129 offset = output->width;
3130 continue;
3131 }
3132
3133 if (offset > 0) {
3134 weston_output_move(output,
3135 output->x - offset, output->y);
3136 output->dirty = 1;
3137 }
3138 }
3139}
3140
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003141WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003142weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003143{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003144 output->destroying = 1;
3145
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003146 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003147 wl_list_remove(&output->link);
3148
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003149 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003150 wl_signal_emit(&output->destroy_signal, output);
3151
Richard Hughesafe690c2013-05-02 10:10:04 +01003152 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003153 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003154 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003155 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003156
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003157 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003158}
3159
Scott Moreau1bad5db2012-08-18 01:04:05 -06003160static void
3161weston_output_compute_transform(struct weston_output *output)
3162{
3163 struct weston_matrix transform;
3164 int flip;
3165
3166 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003167 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003168
3169 switch(output->transform) {
3170 case WL_OUTPUT_TRANSFORM_FLIPPED:
3171 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3172 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3173 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003174 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003175 flip = -1;
3176 break;
3177 default:
3178 flip = 1;
3179 break;
3180 }
3181
3182 switch(output->transform) {
3183 case WL_OUTPUT_TRANSFORM_NORMAL:
3184 case WL_OUTPUT_TRANSFORM_FLIPPED:
3185 transform.d[0] = flip;
3186 transform.d[1] = 0;
3187 transform.d[4] = 0;
3188 transform.d[5] = 1;
3189 break;
3190 case WL_OUTPUT_TRANSFORM_90:
3191 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3192 transform.d[0] = 0;
3193 transform.d[1] = -flip;
3194 transform.d[4] = 1;
3195 transform.d[5] = 0;
3196 break;
3197 case WL_OUTPUT_TRANSFORM_180:
3198 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3199 transform.d[0] = -flip;
3200 transform.d[1] = 0;
3201 transform.d[4] = 0;
3202 transform.d[5] = -1;
3203 break;
3204 case WL_OUTPUT_TRANSFORM_270:
3205 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3206 transform.d[0] = 0;
3207 transform.d[1] = flip;
3208 transform.d[4] = -1;
3209 transform.d[5] = 0;
3210 break;
3211 default:
3212 break;
3213 }
3214
3215 weston_matrix_multiply(&output->matrix, &transform);
3216}
3217
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003218WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003219weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003220{
Scott Moreau850ca422012-05-21 15:21:25 -06003221 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003222 struct weston_matrix camera;
3223 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003224
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003225 weston_matrix_init(&output->matrix);
3226 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003227 -(output->x + output->width / 2.0),
3228 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003229
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003230 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003231 2.0 / output->width,
3232 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003233
3234 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003235
Scott Moreauccbf29d2012-02-22 14:21:41 -07003236 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003237 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003238 weston_matrix_init(&camera);
3239 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003240 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003241 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003242 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003243 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003244 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003245 weston_matrix_multiply(&output->matrix, &modelview);
3246 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003247
Scott Moreauccbf29d2012-02-22 14:21:41 -07003248 output->dirty = 0;
3249}
3250
Scott Moreau1bad5db2012-08-18 01:04:05 -06003251static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003252weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003253{
3254 output->transform = transform;
3255
3256 switch (transform) {
3257 case WL_OUTPUT_TRANSFORM_90:
3258 case WL_OUTPUT_TRANSFORM_270:
3259 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3260 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3261 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003262 output->width = output->current_mode->height;
3263 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003264 break;
3265 case WL_OUTPUT_TRANSFORM_NORMAL:
3266 case WL_OUTPUT_TRANSFORM_180:
3267 case WL_OUTPUT_TRANSFORM_FLIPPED:
3268 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003269 output->width = output->current_mode->width;
3270 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003271 break;
3272 default:
3273 break;
3274 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003275
Hardening57388e42013-09-18 23:56:36 +02003276 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003277 output->width /= scale;
3278 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003279}
3280
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003281static void
3282weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003283{
3284 output->x = x;
3285 output->y = y;
3286
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003287 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003288 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003289 output->width,
3290 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003291}
3292
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003293WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003294weston_output_move(struct weston_output *output, int x, int y)
3295{
3296 pixman_region32_t old_region;
3297 struct wl_resource *resource;
3298
3299 output->move_x = x - output->x;
3300 output->move_y = y - output->y;
3301
3302 if (output->move_x == 0 && output->move_y == 0)
3303 return;
3304
3305 pixman_region32_init(&old_region);
3306 pixman_region32_copy(&old_region, &output->region);
3307
3308 weston_output_init_geometry(output, x, y);
3309
3310 output->dirty = 1;
3311
3312 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003313 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003314
3315 /* Notify clients of the change for output position. */
3316 wl_resource_for_each(resource, &output->resource_list)
3317 wl_output_send_geometry(resource,
3318 output->x,
3319 output->y,
3320 output->mm_width,
3321 output->mm_height,
3322 output->subpixel,
3323 output->make,
3324 output->model,
3325 output->transform);
3326}
3327
3328WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003329weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003330 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003331 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003332{
3333 output->compositor = c;
3334 output->x = x;
3335 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003336 output->mm_width = mm_width;
3337 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003338 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003339 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003340
Alexander Larsson0b135062013-05-28 16:23:36 +02003341 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003342 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003343
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003344 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003345 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003346
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003347 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003348 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003349 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003350 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003351
Casey Dahlin58ba1372012-04-19 22:50:08 -04003352 output->id = ffs(~output->compositor->output_id_pool) - 1;
3353 output->compositor->output_id_pool |= 1 << output->id;
3354
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003355 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003356 wl_global_create(c->wl_display, &wl_output_interface, 2,
3357 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003358 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003359}
3360
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003361WL_EXPORT void
3362weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003363 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003364 wl_fixed_t *x, wl_fixed_t *y)
3365{
3366 wl_fixed_t tx, ty;
3367 wl_fixed_t width, height;
3368
Hardeningff39efa2013-09-18 23:56:35 +02003369 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3370 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003371
3372 switch(output->transform) {
3373 case WL_OUTPUT_TRANSFORM_NORMAL:
3374 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003375 tx = device_x;
3376 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003377 break;
3378 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003379 tx = device_y;
3380 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003381 break;
3382 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003383 tx = width - device_x;
3384 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003385 break;
3386 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003387 tx = width - device_y;
3388 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003389 break;
3390 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003391 tx = width - device_x;
3392 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003393 break;
3394 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003395 tx = width - device_y;
3396 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003397 break;
3398 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003399 tx = device_x;
3400 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003401 break;
3402 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003403 tx = device_y;
3404 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003405 break;
3406 }
3407
Hardeningff39efa2013-09-18 23:56:35 +02003408 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3409 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003410}
3411
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003412static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003413destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003414{
Jonny Lamb74130762013-11-26 18:19:46 +01003415 struct weston_surface *surface =
3416 wl_resource_get_user_data(resource);
3417
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003418 surface->viewport_resource = NULL;
3419 surface->pending.buffer_viewport.viewport_set = 0;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003420}
3421
3422static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003423viewport_destroy(struct wl_client *client,
3424 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003425{
3426 wl_resource_destroy(resource);
3427}
3428
3429static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003430viewport_set(struct wl_client *client,
3431 struct wl_resource *resource,
3432 wl_fixed_t src_x,
3433 wl_fixed_t src_y,
3434 wl_fixed_t src_width,
3435 wl_fixed_t src_height,
3436 int32_t dst_width,
3437 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003438{
Jonny Lamb74130762013-11-26 18:19:46 +01003439 struct weston_surface *surface =
3440 wl_resource_get_user_data(resource);
3441
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003442 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003443
Jonny Lamb8ae35902013-11-26 18:19:45 +01003444 if (wl_fixed_to_double(src_width) < 0 ||
3445 wl_fixed_to_double(src_height) < 0) {
3446 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003447 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003448 "source dimensions must be non-negative (%fx%f)",
3449 wl_fixed_to_double(src_width),
3450 wl_fixed_to_double(src_height));
3451 return;
3452 }
3453
3454 if (dst_width <= 0 || dst_height <= 0) {
3455 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003456 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003457 "destination dimensions must be positive (%dx%d)",
3458 dst_width, dst_height);
3459 return;
3460 }
3461
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003462 surface->pending.buffer_viewport.viewport_set = 1;
Jonny Lamb74130762013-11-26 18:19:46 +01003463
3464 surface->pending.buffer_viewport.src_x = src_x;
3465 surface->pending.buffer_viewport.src_y = src_y;
3466 surface->pending.buffer_viewport.src_width = src_width;
3467 surface->pending.buffer_viewport.src_height = src_height;
3468 surface->pending.buffer_viewport.dst_width = dst_width;
3469 surface->pending.buffer_viewport.dst_height = dst_height;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003470}
3471
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003472static const struct wl_viewport_interface viewport_interface = {
3473 viewport_destroy,
3474 viewport_set
Jonny Lamb8ae35902013-11-26 18:19:45 +01003475};
3476
3477static void
3478scaler_destroy(struct wl_client *client,
3479 struct wl_resource *resource)
3480{
3481 wl_resource_destroy(resource);
3482}
3483
3484static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003485scaler_get_viewport(struct wl_client *client,
3486 struct wl_resource *scaler,
3487 uint32_t id,
3488 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003489{
3490 struct weston_surface *surface = wl_resource_get_user_data(surface_resource);
3491 struct wl_resource *resource;
3492
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003493 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003494 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003495 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3496 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003497 return;
3498 }
3499
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003500 resource = wl_resource_create(client, &wl_viewport_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003501 1, id);
3502 if (resource == NULL) {
3503 wl_client_post_no_memory(client);
3504 return;
3505 }
3506
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003507 wl_resource_set_implementation(resource, &viewport_interface,
3508 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003509
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003510 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003511}
3512
3513static const struct wl_scaler_interface scaler_interface = {
3514 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003515 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003516};
3517
3518static void
3519bind_scaler(struct wl_client *client,
3520 void *data, uint32_t version, uint32_t id)
3521{
3522 struct wl_resource *resource;
3523
3524 resource = wl_resource_create(client, &wl_scaler_interface,
3525 1, id);
3526 if (resource == NULL) {
3527 wl_client_post_no_memory(client);
3528 return;
3529 }
3530
3531 wl_resource_set_implementation(resource, &scaler_interface,
3532 NULL, NULL);
3533}
3534
3535static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003536compositor_bind(struct wl_client *client,
3537 void *data, uint32_t version, uint32_t id)
3538{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003539 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003540 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003541
Jason Ekstranda85118c2013-06-27 20:17:02 -05003542 resource = wl_resource_create(client, &wl_compositor_interface,
3543 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003544 if (resource == NULL) {
3545 wl_client_post_no_memory(client);
3546 return;
3547 }
3548
3549 wl_resource_set_implementation(resource, &compositor_interface,
3550 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003551}
3552
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003553static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003554log_uname(void)
3555{
3556 struct utsname usys;
3557
3558 uname(&usys);
3559
3560 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3561 usys.version, usys.machine);
3562}
3563
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003564WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003565weston_environment_get_fd(const char *env)
3566{
3567 char *e, *end;
3568 int fd, flags;
3569
3570 e = getenv(env);
3571 if (!e)
3572 return -1;
3573 fd = strtol(e, &end, 0);
3574 if (*end != '\0')
3575 return -1;
3576
3577 flags = fcntl(fd, F_GETFD);
3578 if (flags == -1)
3579 return -1;
3580
3581 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3582 unsetenv(env);
3583
3584 return fd;
3585}
3586
3587WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003588weston_compositor_init(struct weston_compositor *ec,
3589 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003590 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003591 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003592{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003593 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003594 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003595 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003596
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003597 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003598 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003599 wl_signal_init(&ec->destroy_signal);
3600 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003601 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003602 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003603 wl_signal_init(&ec->idle_signal);
3604 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003605 wl_signal_init(&ec->show_input_panel_signal);
3606 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003607 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003608 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003609 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003610 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003611 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003612 wl_signal_init(&ec->session_signal);
3613 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003614
Casey Dahlin58ba1372012-04-19 22:50:08 -04003615 ec->output_id_pool = 0;
3616
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003617 if (!wl_global_create(display, &wl_compositor_interface, 3,
3618 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003619 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003620
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003621 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3622 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003623 return -1;
3624
Jonny Lamb8ae35902013-11-26 18:19:45 +01003625 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 1,
3626 ec, bind_scaler))
3627 return -1;
3628
Jason Ekstranda7af7042013-10-12 22:38:11 -05003629 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003630 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003631 wl_list_init(&ec->layer_list);
3632 wl_list_init(&ec->seat_list);
3633 wl_list_init(&ec->output_list);
3634 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003635 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003636 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003637 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003638 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003639 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003640
Xiong Zhang97116532013-10-23 13:58:31 +08003641 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003642 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003643
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003644 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3645 weston_config_section_get_string(s, "keymap_rules",
3646 (char **) &xkb_names.rules, NULL);
3647 weston_config_section_get_string(s, "keymap_model",
3648 (char **) &xkb_names.model, NULL);
3649 weston_config_section_get_string(s, "keymap_layout",
3650 (char **) &xkb_names.layout, NULL);
3651 weston_config_section_get_string(s, "keymap_variant",
3652 (char **) &xkb_names.variant, NULL);
3653 weston_config_section_get_string(s, "keymap_options",
3654 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003655
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003656 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3657 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003658
3659 ec->ping_handler = NULL;
3660
3661 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003662 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003663
3664 wl_data_device_manager_init(ec->wl_display);
3665
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003666 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003667
Daniel Stone725c2c32012-06-22 14:04:36 +01003668 loop = wl_display_get_event_loop(ec->wl_display);
3669 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3670 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3671
3672 ec->input_loop = wl_event_loop_create();
3673
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003674 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3675 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3676
3677 weston_compositor_schedule_repaint(ec);
3678
Daniel Stone725c2c32012-06-22 14:04:36 +01003679 return 0;
3680}
3681
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003682WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003683weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003684{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003685 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003686
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003687 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003688 if (ec->input_loop_source)
3689 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003690
Matt Roper361d2ad2011-08-29 13:52:23 -07003691 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003692 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003693 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003694
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003695 if (ec->renderer)
3696 ec->renderer->destroy(ec);
3697
Daniel Stone325fc2d2012-05-30 16:31:58 +01003698 weston_binding_list_destroy_all(&ec->key_binding_list);
3699 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003700 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003701 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003702 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003703
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003704 weston_plane_release(&ec->primary_plane);
3705
Jonas Ådahlc97af922012-03-28 22:36:09 +02003706 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003707
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003708 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003709}
3710
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003711WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003712weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3713 const struct weston_pointer_grab_interface *interface)
3714{
3715 struct weston_seat *seat;
3716
3717 ec->default_pointer_grab = interface;
3718 wl_list_for_each(seat, &ec->seat_list, link) {
3719 if (seat->pointer) {
3720 weston_pointer_set_default_grab(seat->pointer,
3721 interface);
3722 }
3723 }
3724}
3725
3726WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003727weston_version(int *major, int *minor, int *micro)
3728{
3729 *major = WESTON_VERSION_MAJOR;
3730 *minor = WESTON_VERSION_MINOR;
3731 *micro = WESTON_VERSION_MICRO;
3732}
3733
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003734static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003735 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003736 const char *desc;
3737} capability_strings[] = {
3738 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003739 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003740};
3741
3742static void
3743weston_compositor_log_capabilities(struct weston_compositor *compositor)
3744{
3745 unsigned i;
3746 int yes;
3747
3748 weston_log("Compositor capabilities:\n");
3749 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3750 yes = compositor->capabilities & capability_strings[i].bit;
3751 weston_log_continue(STAMP_SPACE "%s %s\n",
3752 capability_strings[i].desc,
3753 yes ? "yes" : "no");
3754 }
3755}
3756
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003757static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003758{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003759 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003760
Martin Minarik6d118362012-06-07 18:01:59 +02003761 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003762 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003763
3764 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003765}
3766
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003767#ifdef HAVE_LIBUNWIND
3768
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003769static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003770print_backtrace(void)
3771{
3772 unw_cursor_t cursor;
3773 unw_context_t context;
3774 unw_word_t off;
3775 unw_proc_info_t pip;
3776 int ret, i = 0;
3777 char procname[256];
3778 const char *filename;
3779 Dl_info dlinfo;
3780
3781 pip.unwind_info = NULL;
3782 ret = unw_getcontext(&context);
3783 if (ret) {
3784 weston_log("unw_getcontext: %d\n", ret);
3785 return;
3786 }
3787
3788 ret = unw_init_local(&cursor, &context);
3789 if (ret) {
3790 weston_log("unw_init_local: %d\n", ret);
3791 return;
3792 }
3793
3794 ret = unw_step(&cursor);
3795 while (ret > 0) {
3796 ret = unw_get_proc_info(&cursor, &pip);
3797 if (ret) {
3798 weston_log("unw_get_proc_info: %d\n", ret);
3799 break;
3800 }
3801
3802 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3803 if (ret && ret != -UNW_ENOMEM) {
3804 if (ret != -UNW_EUNSPEC)
3805 weston_log("unw_get_proc_name: %d\n", ret);
3806 procname[0] = '?';
3807 procname[1] = 0;
3808 }
3809
3810 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3811 *dlinfo.dli_fname)
3812 filename = dlinfo.dli_fname;
3813 else
3814 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003815
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003816 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3817 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3818
3819 ret = unw_step(&cursor);
3820 if (ret < 0)
3821 weston_log("unw_step: %d\n", ret);
3822 }
3823}
3824
3825#else
3826
3827static void
3828print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003829{
3830 void *buffer[32];
3831 int i, count;
3832 Dl_info info;
3833
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003834 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3835 for (i = 0; i < count; i++) {
3836 dladdr(buffer[i], &info);
3837 weston_log(" [%016lx] %s (%s)\n",
3838 (long) buffer[i],
3839 info.dli_sname ? info.dli_sname : "--",
3840 info.dli_fname);
3841 }
3842}
3843
3844#endif
3845
3846static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003847on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003848{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003849 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003850 * then call the backend restore function, which will switch
3851 * back to the vt we launched from or ungrab X etc and then
3852 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003853 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003854 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003855 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003856
Peter Maatmane5b42e42013-03-27 22:38:53 +01003857 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003858
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003859 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003860
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003861 segv_compositor->restore(segv_compositor);
3862
3863 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003864}
3865
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003866WL_EXPORT void *
3867weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003868{
3869 char path[PATH_MAX];
3870 void *module, *init;
3871
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003872 if (name == NULL)
3873 return NULL;
3874
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003875 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003876 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003877 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003878 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003879
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003880 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3881 if (module) {
3882 weston_log("Module '%s' already loaded\n", path);
3883 dlclose(module);
3884 return NULL;
3885 }
3886
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003887 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003888 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003889 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003890 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003891 return NULL;
3892 }
3893
3894 init = dlsym(module, entrypoint);
3895 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003896 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003897 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003898 return NULL;
3899 }
3900
3901 return init;
3902}
3903
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003904static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003905load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003906 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003907{
3908 const char *p, *end;
3909 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003910 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003911 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003912
3913 if (modules == NULL)
3914 return 0;
3915
3916 p = modules;
3917 while (*p) {
3918 end = strchrnul(p, ',');
3919 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003920 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003921 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003922 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003923 p = end;
3924 while (*p == ',')
3925 p++;
3926
3927 }
3928
3929 return 0;
3930}
3931
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003932static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003933 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3934
3935static const char xdg_wrong_message[] =
3936 "fatal: environment variable XDG_RUNTIME_DIR\n"
3937 "is set to \"%s\", which is not a directory.\n";
3938
3939static const char xdg_wrong_mode_message[] =
3940 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01003941 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
3942 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003943
3944static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003945 "Refer to your distribution on how to get it, or\n"
3946 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3947 "on how to implement it.\n";
3948
Martin Minarik37032f82012-06-18 20:15:18 +02003949static void
3950verify_xdg_runtime_dir(void)
3951{
3952 char *dir = getenv("XDG_RUNTIME_DIR");
3953 struct stat s;
3954
3955 if (!dir) {
3956 weston_log(xdg_error_message);
3957 weston_log_continue(xdg_detail_message);
3958 exit(EXIT_FAILURE);
3959 }
3960
3961 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3962 weston_log(xdg_wrong_message, dir);
3963 weston_log_continue(xdg_detail_message);
3964 exit(EXIT_FAILURE);
3965 }
3966
3967 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3968 weston_log(xdg_wrong_mode_message,
3969 dir, s.st_mode & 0777, s.st_uid);
3970 weston_log_continue(xdg_detail_message);
3971 }
3972}
3973
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003974static int
3975usage(int error_code)
3976{
3977 fprintf(stderr,
3978 "Usage: weston [OPTIONS]\n\n"
3979 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3980 "Weston supports multiple backends, and depending on which backend is in use\n"
3981 "different options will be accepted.\n\n"
3982
3983
3984 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003985 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003986 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003987 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3988 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003989 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003990 " -S, --socket=NAME\tName of socket to listen on\n"
3991 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003992 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003993 " --log==FILE\t\tLog to the given file\n"
3994 " -h, --help\t\tThis help message\n\n");
3995
3996 fprintf(stderr,
3997 "Options for drm-backend.so:\n\n"
3998 " --connector=ID\tBring up only this connector\n"
3999 " --seat=SEAT\t\tThe seat that weston should run on\n"
4000 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004001 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004002 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4003
4004 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004005 "Options for fbdev-backend.so:\n\n"
4006 " --tty=TTY\t\tThe tty to use\n"
4007 " --device=DEVICE\tThe framebuffer device to use\n\n");
4008
4009 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004010 "Options for x11-backend.so:\n\n"
4011 " --width=WIDTH\t\tWidth of X window\n"
4012 " --height=HEIGHT\tHeight of X window\n"
4013 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004014 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004015 " --output-count=COUNT\tCreate multiple outputs\n"
4016 " --no-input\t\tDont create input devices\n\n");
4017
4018 fprintf(stderr,
4019 "Options for wayland-backend.so:\n\n"
4020 " --width=WIDTH\t\tWidth of Wayland surface\n"
4021 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004022 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004023 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004024 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004025 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004026 " --display=DISPLAY\tWayland display to connect to\n\n");
4027
Pekka Paalanene31e0532013-05-22 18:03:07 +03004028#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4029 fprintf(stderr,
4030 "Options for rpi-backend.so:\n\n"
4031 " --tty=TTY\t\tThe tty to use\n"
4032 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4033 " --transform=TR\tThe output transformation, TR is one of:\n"
4034 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004035 " --opaque-regions\tEnable support for opaque regions, can be "
4036 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004037 "\n");
4038#endif
4039
Hardeningc077a842013-07-08 00:51:35 +02004040#if defined(BUILD_RDP_COMPOSITOR)
4041 fprintf(stderr,
4042 "Options for rdp-backend.so:\n\n"
4043 " --width=WIDTH\t\tWidth of desktop\n"
4044 " --height=HEIGHT\tHeight of desktop\n"
4045 " --extra-modes=MODES\t\n"
4046 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4047 " --address=ADDR\tThe address to bind\n"
4048 " --port=PORT\tThe port to listen on\n"
4049 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4050 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4051 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4052 "\n");
4053#endif
4054
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004055 exit(error_code);
4056}
4057
Peter Maatmane5b42e42013-03-27 22:38:53 +01004058static void
4059catch_signals(void)
4060{
4061 struct sigaction action;
4062
4063 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4064 action.sa_sigaction = on_caught_signal;
4065 sigemptyset(&action.sa_mask);
4066 sigaction(SIGSEGV, &action, NULL);
4067 sigaction(SIGABRT, &action, NULL);
4068}
4069
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004070int main(int argc, char *argv[])
4071{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004072 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004073 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004074 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004075 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004076 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004077 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004078 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004079 int *argc, char *argv[],
4080 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07004081 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004082 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004083 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004084 char *shell = NULL;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004085 char *option_shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004086 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004087 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004088 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004089 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004090 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06004091 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004092 struct weston_config *config;
4093 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004094
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004095 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004096 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004097 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004098 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4099 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004100 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004101 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004102 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004103 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004104 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004105
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004106 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004107
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004108 if (help)
4109 usage(EXIT_SUCCESS);
4110
Scott Moreau12245142012-08-29 15:15:58 -06004111 if (version) {
4112 printf(PACKAGE_STRING "\n");
4113 return EXIT_SUCCESS;
4114 }
4115
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004116 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004117
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004118 weston_log("%s\n"
4119 STAMP_SPACE "%s\n"
4120 STAMP_SPACE "Bug reports to: %s\n"
4121 STAMP_SPACE "Build: %s\n",
4122 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004123 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004124 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004125
Martin Minarik37032f82012-06-18 20:15:18 +02004126 verify_xdg_runtime_dir();
4127
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004128 display = wl_display_create();
4129
Tiago Vignatti2116b892011-08-08 05:52:59 -07004130 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004131 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4132 display);
4133 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4134 display);
4135 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4136 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004137
4138 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004139 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4140 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004141
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004142 config = weston_config_parse("weston.ini");
4143 if (config != NULL) {
4144 weston_log("Using config file '%s'\n",
4145 weston_config_get_full_path(config));
4146 } else {
4147 weston_log("Starting with no config file.\n");
4148 }
4149 section = weston_config_get_section(config, "core", NULL, NULL);
4150
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004151 if (option_backend)
4152 backend = strdup(option_backend);
4153 else
4154 weston_config_section_get_string(section, "backend", &backend,
4155 NULL);
4156
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004157 if (!backend) {
4158 if (getenv("WAYLAND_DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004159 backend = strdup("wayland-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004160 else if (getenv("DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004161 backend = strdup("x11-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004162 else
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004163 backend = strdup(WESTON_NATIVE_BACKEND);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004164 }
4165
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004166 backend_init = weston_load_module(backend, "backend_init");
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004167 free(backend);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004168 if (!backend_init)
4169 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004170
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004171 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004172 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004173 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004174 exit(EXIT_FAILURE);
4175 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004176
Peter Maatmane5b42e42013-03-27 22:38:53 +01004177 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004178 segv_compositor = ec;
4179
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004180 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004181 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004182
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004183 setenv("WAYLAND_DISPLAY", socket_name, 1);
4184
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004185 if (option_shell)
4186 shell = strdup(option_shell);
4187 else
Jason Ekstranda985da42013-08-22 17:28:03 -05004188 weston_config_section_get_string(section, "shell", &shell,
4189 "desktop-shell.so");
Jason Ekstranda985da42013-08-22 17:28:03 -05004190
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004191 if (load_modules(ec, shell, &argc, argv) < 0) {
4192 free(shell);
Pekka Paalanen33616392012-07-30 16:56:57 +03004193 goto out;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004194 }
4195 free(shell);
4196
4197 weston_config_section_get_string(section, "modules", &modules, "");
4198 if (load_modules(ec, modules, &argc, argv) < 0) {
4199 free(modules);
4200 goto out;
4201 }
4202 free(modules);
4203
Ossama Othmana50e6e42013-05-14 09:48:26 -07004204 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004205 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004206
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004207 for (i = 1; i < argc; i++)
4208 weston_log("fatal: unhandled option: %s\n", argv[i]);
4209 if (argc > 1) {
4210 ret = EXIT_FAILURE;
4211 goto out;
4212 }
4213
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004214 weston_compositor_log_capabilities(ec);
4215
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004216 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004217 weston_log("fatal: failed to add socket: %m\n");
4218 ret = EXIT_FAILURE;
4219 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004220 }
4221
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004222 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004223
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004224 wl_display_run(display);
4225
4226 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004227 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004228 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004229
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004230 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004231
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004232 for (i = ARRAY_LENGTH(signals); i;)
4233 wl_event_source_remove(signals[--i]);
4234
Daniel Stone855028f2012-05-01 20:37:10 +01004235 weston_compositor_xkb_destroy(ec);
4236
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004237 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004238 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004239
Martin Minarik19e6f262012-06-07 13:08:46 +02004240 weston_log_file_close();
4241
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004242 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004243}