blob: adc219ac43b156ce1ae70d7d9bde5eea29b0e236 [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"
U. Artie Eoffec08f332013-05-13 15:55:47 -070056#include "subsurface-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
141 output->current_mode = mode;
142 output->current_scale = scale;
143 break;
144 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
145 if (!temporary_mode) {
146 weston_log("already in the native mode\n");
147 return -1;
148 }
149
150 notify_mode_changed = (output->original_mode != output->native_mode);
151
152 ret = output->switch_mode(output, mode);
153 if (ret < 0)
154 return ret;
155
156 if (output->original_scale != output->native_scale) {
157 notify_scale_changed = 1;
158 scale = output->native_scale;
159 output->original_scale = scale;
160 }
161 output->original_mode = 0;
162
163 output->current_scale = output->native_scale;
164 break;
165 default:
166 weston_log("unknown weston_switch_mode_op %d\n", op);
167 break;
168 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200169
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200170 pixman_region32_init(&old_output_region);
171 pixman_region32_copy(&old_output_region, &output->region);
172
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200173 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200174 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200175
176 pixman_region32_init(&output->previous_damage);
177 pixman_region32_init_rect(&output->region, output->x, output->y,
178 output->width, output->height);
179
180 weston_output_update_matrix(output);
181
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200182 /* If a pointer falls outside the outputs new geometry, move it to its
183 * lower-right corner */
184 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400185 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200186 int32_t x, y;
187
188 if (!pointer)
189 continue;
190
191 x = wl_fixed_to_int(pointer->x);
192 y = wl_fixed_to_int(pointer->y);
193
194 if (!pixman_region32_contains_point(&old_output_region,
195 x, y, NULL) ||
196 pixman_region32_contains_point(&output->region,
197 x, y, NULL))
198 continue;
199
200 if (x >= output->x + output->width)
201 x = output->x + output->width - 1;
202 if (y >= output->y + output->height)
203 y = output->y + output->height - 1;
204
205 pointer->x = wl_fixed_from_int(x);
206 pointer->y = wl_fixed_from_int(y);
207 }
208
209 pixman_region32_fini(&old_output_region);
210
Hardening57388e42013-09-18 23:56:36 +0200211 /* notify clients of the changes */
212 if (notify_mode_changed || notify_scale_changed) {
213 wl_resource_for_each(resource, &output->resource_list) {
214 if(notify_mode_changed) {
215 wl_output_send_mode(resource,
216 mode->flags | WL_OUTPUT_MODE_CURRENT,
217 mode->width,
218 mode->height,
219 mode->refresh);
220 }
221
222 if (notify_scale_changed)
223 wl_output_send_scale(resource, scale);
224
225 if (wl_resource_get_version(resource) >= 2)
226 wl_output_send_done(resource);
227 }
228 }
229
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200230 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800231}
232
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400233WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500234weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400235{
236 wl_list_insert(&child_process_list, &process->link);
237}
238
Benjamin Franzke06286262011-05-06 19:12:33 +0200239static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200240child_client_exec(int sockfd, const char *path)
241{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500242 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200243 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200244 sigset_t allsigs;
245
246 /* do not give our signal mask to the new process */
247 sigfillset(&allsigs);
248 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200249
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100250 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
251 if (seteuid(getuid()) == -1) {
252 weston_log("compositor: failed seteuid\n");
253 return;
254 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500255
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500256 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
257 * non-CLOEXEC fd to pass through exec. */
258 clientfd = dup(sockfd);
259 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200260 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500261 return;
262 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200263
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500264 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200265 setenv("WAYLAND_SOCKET", s, 1);
266
267 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200268 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200269 path);
270}
271
272WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500273weston_client_launch(struct weston_compositor *compositor,
274 struct weston_process *proc,
275 const char *path,
276 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200277{
278 int sv[2];
279 pid_t pid;
280 struct wl_client *client;
281
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300282 weston_log("launching '%s'\n", path);
283
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300284 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200285 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200286 "socketpair failed while launching '%s': %m\n",
287 path);
288 return NULL;
289 }
290
291 pid = fork();
292 if (pid == -1) {
293 close(sv[0]);
294 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200295 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200296 "fork failed while launching '%s': %m\n", path);
297 return NULL;
298 }
299
300 if (pid == 0) {
301 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700302 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200303 }
304
305 close(sv[1]);
306
307 client = wl_client_create(compositor->wl_display, sv[0]);
308 if (!client) {
309 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200310 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200311 "wl_client_create failed while launching '%s'.\n",
312 path);
313 return NULL;
314 }
315
316 proc->pid = pid;
317 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500318 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200319
320 return client;
321}
322
323static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300324surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
325{
326 struct weston_surface *surface =
327 container_of(listener, struct weston_surface,
328 pending.buffer_destroy_listener);
329
330 surface->pending.buffer = NULL;
331}
332
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200333static void
334empty_region(pixman_region32_t *region)
335{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300336 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200337 pixman_region32_init(region);
338}
339
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300340static void
341region_init_infinite(pixman_region32_t *region)
342{
343 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
344 UINT32_MAX, UINT32_MAX);
345}
346
Pekka Paalanene67858b2013-04-25 13:57:42 +0300347static struct weston_subsurface *
348weston_surface_to_subsurface(struct weston_surface *surface);
349
Jason Ekstranda7af7042013-10-12 22:38:11 -0500350WL_EXPORT struct weston_view *
351weston_view_create(struct weston_surface *surface)
352{
353 struct weston_view *view;
354
355 view = calloc(1, sizeof *view);
356 if (view == NULL)
357 return NULL;
358
359 view->surface = surface;
360
Jason Ekstranda7af7042013-10-12 22:38:11 -0500361 /* Assign to surface */
362 wl_list_insert(&surface->views, &view->surface_link);
363
364 wl_signal_init(&view->destroy_signal);
365 wl_list_init(&view->link);
366 wl_list_init(&view->layer_link);
367
Xiong Zhang97116532013-10-23 13:58:31 +0800368 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500369
370 pixman_region32_init(&view->clip);
371
372 view->alpha = 1.0;
373 pixman_region32_init(&view->transform.opaque);
374
375 wl_list_init(&view->geometry.transformation_list);
376 wl_list_insert(&view->geometry.transformation_list,
377 &view->transform.position.link);
378 weston_matrix_init(&view->transform.position.matrix);
379 wl_list_init(&view->geometry.child_list);
380 pixman_region32_init(&view->transform.boundingbox);
381 view->transform.dirty = 1;
382
383 view->output = NULL;
384
385 return view;
386}
387
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500388WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500389weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500390{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500391 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400392
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200393 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400394 if (surface == NULL)
395 return NULL;
396
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500397 wl_signal_init(&surface->destroy_signal);
398
399 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500400
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500401 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200402 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400403
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200404 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200405 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200406 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200407 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400408 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100409 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200410
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400411 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500412 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300413 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400414
Jason Ekstranda7af7042013-10-12 22:38:11 -0500415 wl_list_init(&surface->views);
416
417 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500418
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300419 surface->pending.buffer_destroy_listener.notify =
420 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300421 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300422 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300423 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300424 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300425
Pekka Paalanene67858b2013-04-25 13:57:42 +0300426 wl_list_init(&surface->subsurface_list);
427 wl_list_init(&surface->subsurface_list_pending);
428
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400429 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500430}
431
Alex Wu8811bf92012-02-28 18:07:54 +0800432WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500433weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200434 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500435{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100436 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500437}
438
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400439WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500440weston_view_to_global_float(struct weston_view *view,
441 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200442{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500443 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200444 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
445
Jason Ekstranda7af7042013-10-12 22:38:11 -0500446 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200447
448 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200449 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700450 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200451 v.f[3]);
452 *x = 0;
453 *y = 0;
454 return;
455 }
456
457 *x = v.f[0] / v.f[3];
458 *y = v.f[1] / v.f[3];
459 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500460 *x = sx + view->geometry.x;
461 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200462 }
463}
464
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500465WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200466weston_transformed_coord(int width, int height,
467 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200468 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200469 float sx, float sy, float *bx, float *by)
470{
471 switch (transform) {
472 case WL_OUTPUT_TRANSFORM_NORMAL:
473 default:
474 *bx = sx;
475 *by = sy;
476 break;
477 case WL_OUTPUT_TRANSFORM_FLIPPED:
478 *bx = width - sx;
479 *by = sy;
480 break;
481 case WL_OUTPUT_TRANSFORM_90:
482 *bx = height - sy;
483 *by = sx;
484 break;
485 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
486 *bx = height - sy;
487 *by = width - sx;
488 break;
489 case WL_OUTPUT_TRANSFORM_180:
490 *bx = width - sx;
491 *by = height - sy;
492 break;
493 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
494 *bx = sx;
495 *by = height - sy;
496 break;
497 case WL_OUTPUT_TRANSFORM_270:
498 *bx = sy;
499 *by = width - sx;
500 break;
501 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
502 *bx = sy;
503 *by = sx;
504 break;
505 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200506
507 *bx *= scale;
508 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200509}
510
511WL_EXPORT pixman_box32_t
512weston_transformed_rect(int width, int height,
513 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200514 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200515 pixman_box32_t rect)
516{
517 float x1, x2, y1, y2;
518
519 pixman_box32_t ret;
520
Alexander Larsson4ea95522013-05-22 14:41:37 +0200521 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200522 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200523 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200524 rect.x2, rect.y2, &x2, &y2);
525
526 if (x1 <= x2) {
527 ret.x1 = x1;
528 ret.x2 = x2;
529 } else {
530 ret.x1 = x2;
531 ret.x2 = x1;
532 }
533
534 if (y1 <= y2) {
535 ret.y1 = y1;
536 ret.y2 = y2;
537 } else {
538 ret.y1 = y2;
539 ret.y2 = y1;
540 }
541
542 return ret;
543}
544
545WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200546weston_surface_to_buffer_float(struct weston_surface *surface,
547 float sx, float sy, float *bx, float *by)
548{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500549 weston_transformed_coord(surface->width,
550 surface->height,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200551 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200552 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200553 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200554}
555
Alexander Larsson4ea95522013-05-22 14:41:37 +0200556WL_EXPORT void
557weston_surface_to_buffer(struct weston_surface *surface,
558 int sx, int sy, int *bx, int *by)
559{
560 float bxf, byf;
561
Jason Ekstranda7af7042013-10-12 22:38:11 -0500562 weston_transformed_coord(surface->width,
563 surface->height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200564 surface->buffer_transform,
565 surface->buffer_scale,
566 sx, sy, &bxf, &byf);
567 *bx = floorf(bxf);
568 *by = floorf(byf);
569}
570
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200571WL_EXPORT pixman_box32_t
572weston_surface_to_buffer_rect(struct weston_surface *surface,
573 pixman_box32_t rect)
574{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500575 return weston_transformed_rect(surface->width,
576 surface->height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200577 surface->buffer_transform,
578 surface->buffer_scale,
579 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200580}
581
582WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500583weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400584 struct weston_plane *plane)
585{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400587 return;
588
Jason Ekstranda7af7042013-10-12 22:38:11 -0500589 weston_view_damage_below(view);
590 view->plane = plane;
591 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400592}
593
594WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500595weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200596{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500597 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200598
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500599 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500600 pixman_region32_subtract(&damage, &view->transform.boundingbox,
601 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800602 if (view->plane)
603 pixman_region32_union(&view->plane->damage,
604 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500605 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200606}
607
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400608static void
609weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
610{
611 uint32_t different = es->output_mask ^ mask;
612 uint32_t entered = mask & different;
613 uint32_t left = es->output_mask & different;
614 struct weston_output *output;
615 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500616 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400617
618 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500619 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400620 return;
621 if (different == 0)
622 return;
623
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500624 client = wl_resource_get_client(es->resource);
625
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400626 wl_list_for_each(output, &es->compositor->output_list, link) {
627 if (1 << output->id & different)
628 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500629 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400630 client);
631 if (resource == NULL)
632 continue;
633 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500634 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400635 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500636 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400637 }
638}
639
640static void
641weston_surface_assign_output(struct weston_surface *es)
642{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500643 struct weston_output *new_output;
644 struct weston_view *view;
645 pixman_region32_t region;
646 uint32_t max, area, mask;
647 pixman_box32_t *e;
648
649 new_output = NULL;
650 max = 0;
651 mask = 0;
652 pixman_region32_init(&region);
653 wl_list_for_each(view, &es->views, surface_link) {
654 if (!view->output)
655 continue;
656
657 pixman_region32_intersect(&region, &view->transform.boundingbox,
658 &view->output->region);
659
660 e = pixman_region32_extents(&region);
661 area = (e->x2 - e->x1) * (e->y2 - e->y1);
662
663 mask |= view->output_mask;
664
665 if (area >= max) {
666 new_output = view->output;
667 max = area;
668 }
669 }
670 pixman_region32_fini(&region);
671
672 es->output = new_output;
673 weston_surface_update_output_mask(es, mask);
674}
675
676static void
677weston_view_assign_output(struct weston_view *ev)
678{
679 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400680 struct weston_output *output, *new_output;
681 pixman_region32_t region;
682 uint32_t max, area, mask;
683 pixman_box32_t *e;
684
685 new_output = NULL;
686 max = 0;
687 mask = 0;
688 pixman_region32_init(&region);
689 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500690 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400691 &output->region);
692
693 e = pixman_region32_extents(&region);
694 area = (e->x2 - e->x1) * (e->y2 - e->y1);
695
696 if (area > 0)
697 mask |= 1 << output->id;
698
699 if (area >= max) {
700 new_output = output;
701 max = area;
702 }
703 }
704 pixman_region32_fini(&region);
705
Jason Ekstranda7af7042013-10-12 22:38:11 -0500706 ev->output = new_output;
707 ev->output_mask = mask;
708
709 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400710}
711
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200712static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500713view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
714 int32_t width, int32_t height,
715 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200716{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200717 float min_x = HUGE_VALF, min_y = HUGE_VALF;
718 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200719 int32_t s[4][2] = {
720 { sx, sy },
721 { sx, sy + height },
722 { sx + width, sy },
723 { sx + width, sy + height }
724 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200725 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200726 int i;
727
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300728 if (width == 0 || height == 0) {
729 /* avoid rounding empty bbox to 1x1 */
730 pixman_region32_init(bbox);
731 return;
732 }
733
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200734 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200735 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500736 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200737 if (x < min_x)
738 min_x = x;
739 if (x > max_x)
740 max_x = x;
741 if (y < min_y)
742 min_y = y;
743 if (y > max_y)
744 max_y = y;
745 }
746
Pekka Paalanen219b9822012-02-08 15:38:37 +0200747 int_x = floorf(min_x);
748 int_y = floorf(min_y);
749 pixman_region32_init_rect(bbox, int_x, int_y,
750 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200751}
752
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200753static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500754weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200755{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500756 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200757
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200758 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500759 view->geometry.x = roundf(view->geometry.x);
760 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200761
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500762 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500763 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
764 view->transform.position.matrix.d[12] = view->geometry.x;
765 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500766
Jason Ekstranda7af7042013-10-12 22:38:11 -0500767 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500768
Jason Ekstranda7af7042013-10-12 22:38:11 -0500769 view->transform.inverse = view->transform.position.matrix;
770 view->transform.inverse.d[12] = -view->geometry.x;
771 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500772
Jason Ekstranda7af7042013-10-12 22:38:11 -0500773 pixman_region32_init_rect(&view->transform.boundingbox,
774 view->geometry.x,
775 view->geometry.y,
776 view->geometry.width,
777 view->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500778
Jason Ekstranda7af7042013-10-12 22:38:11 -0500779 if (view->alpha == 1.0) {
780 pixman_region32_copy(&view->transform.opaque,
781 &view->surface->opaque);
782 pixman_region32_translate(&view->transform.opaque,
783 view->geometry.x,
784 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500785 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200786}
787
788static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500789weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200790{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500791 struct weston_view *parent = view->geometry.parent;
792 struct weston_matrix *matrix = &view->transform.matrix;
793 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200794 struct weston_transform *tform;
795
Jason Ekstranda7af7042013-10-12 22:38:11 -0500796 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200797
798 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500799 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
800 view->transform.position.matrix.d[12] = view->geometry.x;
801 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200802
803 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500804 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200805 weston_matrix_multiply(matrix, &tform->matrix);
806
Pekka Paalanen483243f2013-03-08 14:56:50 +0200807 if (parent)
808 weston_matrix_multiply(matrix, &parent->transform.matrix);
809
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200810 if (weston_matrix_invert(inverse, matrix) < 0) {
811 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500812 weston_log("error: weston_view %p"
813 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200814 return -1;
815 }
816
Jason Ekstranda7af7042013-10-12 22:38:11 -0500817 view_compute_bbox(view, 0, 0, view->geometry.width,
818 view->geometry.height,
819 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500820
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200821 return 0;
822}
823
824WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500825weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200826{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500827 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200828
Jason Ekstranda7af7042013-10-12 22:38:11 -0500829 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200830 return;
831
Pekka Paalanen483243f2013-03-08 14:56:50 +0200832 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500833 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200834
Jason Ekstranda7af7042013-10-12 22:38:11 -0500835 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200836
Jason Ekstranda7af7042013-10-12 22:38:11 -0500837 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200838
Jason Ekstranda7af7042013-10-12 22:38:11 -0500839 pixman_region32_fini(&view->transform.boundingbox);
840 pixman_region32_fini(&view->transform.opaque);
841 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500842
Pekka Paalanencd403622012-01-25 13:37:39 +0200843 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500844 if (view->geometry.transformation_list.next ==
845 &view->transform.position.link &&
846 view->geometry.transformation_list.prev ==
847 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200848 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500849 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200850 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500851 if (weston_view_update_transform_enable(view) < 0)
852 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200853 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200854
Jason Ekstranda7af7042013-10-12 22:38:11 -0500855 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200856
Jason Ekstranda7af7042013-10-12 22:38:11 -0500857 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300858
Jason Ekstranda7af7042013-10-12 22:38:11 -0500859 wl_signal_emit(&view->surface->compositor->transform_signal,
860 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200861}
862
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200863WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500864weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200865{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500866 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200867
868 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -0500869 * The invariant: if view->geometry.dirty, then all views
870 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +0200871 * Corollary: if not parent->geometry.dirty, then all ancestors
872 * are not dirty.
873 */
874
Jason Ekstranda7af7042013-10-12 22:38:11 -0500875 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +0200876 return;
877
Jason Ekstranda7af7042013-10-12 22:38:11 -0500878 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200879
Jason Ekstranda7af7042013-10-12 22:38:11 -0500880 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200881 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500882 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200883}
884
885WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500886weston_view_to_global_fixed(struct weston_view *view,
887 wl_fixed_t vx, wl_fixed_t vy,
888 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +0100889{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200890 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100891
Jason Ekstranda7af7042013-10-12 22:38:11 -0500892 weston_view_to_global_float(view,
893 wl_fixed_to_double(vx),
894 wl_fixed_to_double(vy),
895 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100896 *x = wl_fixed_from_double(xf);
897 *y = wl_fixed_from_double(yf);
898}
899
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400900WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500901weston_view_from_global_float(struct weston_view *view,
902 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200903{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500904 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200905 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
906
Jason Ekstranda7af7042013-10-12 22:38:11 -0500907 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200908
909 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200910 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200912 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500913 *vx = 0;
914 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200915 return;
916 }
917
Jason Ekstranda7af7042013-10-12 22:38:11 -0500918 *vx = v.f[0] / v.f[3];
919 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200920 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500921 *vx = x - view->geometry.x;
922 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200923 }
924}
925
926WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500927weston_view_from_global_fixed(struct weston_view *view,
928 wl_fixed_t x, wl_fixed_t y,
929 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +0100930{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500931 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100932
Jason Ekstranda7af7042013-10-12 22:38:11 -0500933 weston_view_from_global_float(view,
934 wl_fixed_to_double(x),
935 wl_fixed_to_double(y),
936 &vxf, &vyf);
937 *vx = wl_fixed_from_double(vxf);
938 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100939}
940
941WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500942weston_view_from_global(struct weston_view *view,
943 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200944{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500945 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200946
Jason Ekstranda7af7042013-10-12 22:38:11 -0500947 weston_view_from_global_float(view, x, y, &vxf, &vyf);
948 *vx = floorf(vxf);
949 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200950}
951
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400952WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400953weston_surface_schedule_repaint(struct weston_surface *surface)
954{
955 struct weston_output *output;
956
957 wl_list_for_each(output, &surface->compositor->output_list, link)
958 if (surface->output_mask & (1 << output->id))
959 weston_output_schedule_repaint(output);
960}
961
962WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500963weston_view_schedule_repaint(struct weston_view *view)
964{
965 struct weston_output *output;
966
967 wl_list_for_each(output, &view->surface->compositor->output_list, link)
968 if (view->output_mask & (1 << output->id))
969 weston_output_schedule_repaint(output);
970}
971
972WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500973weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500974{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400975 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 0, 0, surface->width,
977 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200978
Kristian Høgsberg98238702012-08-03 16:29:12 -0400979 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500980}
981
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400982WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500983weston_view_configure(struct weston_view *view,
984 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400985{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500986 view->geometry.x = x;
987 view->geometry.y = y;
988 view->geometry.width = width;
989 view->geometry.height = height;
990 weston_view_geometry_dirty(view);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400991}
992
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200993WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200995{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500996 view->geometry.x = x;
997 view->geometry.y = y;
998 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200999}
1000
Pekka Paalanen483243f2013-03-08 14:56:50 +02001001static void
1002transform_parent_handle_parent_destroy(struct wl_listener *listener,
1003 void *data)
1004{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 struct weston_view *view =
1006 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001007 geometry.parent_destroy_listener);
1008
Jason Ekstranda7af7042013-10-12 22:38:11 -05001009 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001010}
1011
1012WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013weston_view_set_transform_parent(struct weston_view *view,
1014 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001015{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016 if (view->geometry.parent) {
1017 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1018 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001019 }
1020
Jason Ekstranda7af7042013-10-12 22:38:11 -05001021 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001022
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001024 transform_parent_handle_parent_destroy;
1025 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001026 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001028 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001029 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001030 }
1031
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 weston_view_geometry_dirty(view);
1033}
1034
1035WL_EXPORT int
1036weston_view_is_mapped(struct weston_view *view)
1037{
1038 if (view->output)
1039 return 1;
1040 else
1041 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001042}
1043
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001044WL_EXPORT int
1045weston_surface_is_mapped(struct weston_surface *surface)
1046{
1047 if (surface->output)
1048 return 1;
1049 else
1050 return 0;
1051}
1052
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001053WL_EXPORT int32_t
1054weston_surface_buffer_width(struct weston_surface *surface)
1055{
Alexander Larsson4ea95522013-05-22 14:41:37 +02001056 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001057 switch (surface->buffer_transform) {
1058 case WL_OUTPUT_TRANSFORM_90:
1059 case WL_OUTPUT_TRANSFORM_270:
1060 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1061 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001062 width = surface->buffer_ref.buffer->height;
1063 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001064 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001065 width = surface->buffer_ref.buffer->width;
1066 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001067 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001068 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001069}
1070
1071WL_EXPORT int32_t
1072weston_surface_buffer_height(struct weston_surface *surface)
1073{
Alexander Larsson4ea95522013-05-22 14:41:37 +02001074 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001075 switch (surface->buffer_transform) {
1076 case WL_OUTPUT_TRANSFORM_90:
1077 case WL_OUTPUT_TRANSFORM_270:
1078 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1079 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001080 height = surface->buffer_ref.buffer->width;
1081 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001082 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001083 height = surface->buffer_ref.buffer->height;
1084 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001085 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001086 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001087}
1088
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001089WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001090weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001091{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001092 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001093
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001094 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001095
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001096 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001097}
1098
Jason Ekstranda7af7042013-10-12 22:38:11 -05001099WL_EXPORT struct weston_view *
1100weston_compositor_pick_view(struct weston_compositor *compositor,
1101 wl_fixed_t x, wl_fixed_t y,
1102 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001103{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001104 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001105
Jason Ekstranda7af7042013-10-12 22:38:11 -05001106 wl_list_for_each(view, &compositor->view_list, link) {
1107 weston_view_from_global_fixed(view, x, y, vx, vy);
1108 if (pixman_region32_contains_point(&view->surface->input,
1109 wl_fixed_to_int(*vx),
1110 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001111 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001112 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001113 }
1114
1115 return NULL;
1116}
1117
1118static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001119weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001120{
Daniel Stone37816df2012-05-16 18:45:18 +01001121 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001122
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001123 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001124 return;
1125
Daniel Stone37816df2012-05-16 18:45:18 +01001126 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001127 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001128}
1129
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001130WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001131weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001132{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001133 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001134
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 if (!weston_view_is_mapped(view))
1136 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001137
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 weston_view_damage_below(view);
1139 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001140 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 wl_list_remove(&view->layer_link);
1142 wl_list_init(&view->layer_link);
1143 wl_list_remove(&view->link);
1144 wl_list_init(&view->link);
1145 /* We need to do this before torching the output mask */
1146 weston_view_schedule_repaint(view);
1147 view->output_mask = 0;
1148 weston_surface_assign_output(view->surface);
1149
1150 if (weston_surface_is_mapped(view->surface))
1151 return;
1152
1153 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1154 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001155 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001157 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001158 NULL,
1159 wl_fixed_from_int(0),
1160 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001162 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001163 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001165
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166WL_EXPORT void
1167weston_surface_unmap(struct weston_surface *surface)
1168{
1169 struct weston_view *view;
1170
1171 wl_list_for_each(view, &surface->views, surface_link)
1172 weston_view_unmap(view);
1173 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001174}
1175
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001176struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001177 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001178 struct wl_list link;
1179};
1180
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181WL_EXPORT void
1182weston_view_destroy(struct weston_view *view)
1183{
1184 wl_signal_emit(&view->destroy_signal, view);
1185
1186 assert(wl_list_empty(&view->geometry.child_list));
1187
1188 if (weston_view_is_mapped(view)) {
1189 weston_view_unmap(view);
1190 weston_compositor_build_view_list(view->surface->compositor);
1191 }
1192
1193 wl_list_remove(&view->link);
1194 wl_list_remove(&view->layer_link);
1195
1196 pixman_region32_fini(&view->clip);
1197 pixman_region32_fini(&view->transform.boundingbox);
1198
1199 weston_view_set_transform_parent(view, NULL);
1200
Jason Ekstranda7af7042013-10-12 22:38:11 -05001201 wl_list_remove(&view->surface_link);
1202
1203 free(view);
1204}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001205
1206WL_EXPORT void
1207weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001208{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001209 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001211
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001212 if (--surface->ref_count > 0)
1213 return;
1214
1215 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1216
Pekka Paalanene67858b2013-04-25 13:57:42 +03001217 assert(wl_list_empty(&surface->subsurface_list_pending));
1218 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001219
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1221 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001222
Pekka Paalanenbc106382012-10-10 12:49:31 +03001223 wl_list_for_each_safe(cb, next,
1224 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001225 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001226
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001227 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001228 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001229 pixman_region32_fini(&surface->pending.damage);
1230
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001231 if (surface->pending.buffer)
1232 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1233
Pekka Paalanende685b82012-12-04 15:58:12 +02001234 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001235
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001236 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001237 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001238 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001239
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001240 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001241 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001242
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001243 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001244}
1245
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001246static void
1247destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001248{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001249 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001250
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001251 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001252}
1253
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001254static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001255weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1256{
1257 struct weston_buffer *buffer =
1258 container_of(listener, struct weston_buffer, destroy_listener);
1259
1260 wl_signal_emit(&buffer->destroy_signal, buffer);
1261 free(buffer);
1262}
1263
1264struct weston_buffer *
1265weston_buffer_from_resource(struct wl_resource *resource)
1266{
1267 struct weston_buffer *buffer;
1268 struct wl_listener *listener;
1269
1270 listener = wl_resource_get_destroy_listener(resource,
1271 weston_buffer_destroy_handler);
1272
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001273 if (listener)
1274 return container_of(listener, struct weston_buffer,
1275 destroy_listener);
1276
1277 buffer = zalloc(sizeof *buffer);
1278 if (buffer == NULL)
1279 return NULL;
1280
1281 buffer->resource = resource;
1282 wl_signal_init(&buffer->destroy_signal);
1283 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001284 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001285 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001286
1287 return buffer;
1288}
1289
1290static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001291weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1292 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001293{
Pekka Paalanende685b82012-12-04 15:58:12 +02001294 struct weston_buffer_reference *ref =
1295 container_of(listener, struct weston_buffer_reference,
1296 destroy_listener);
1297
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001298 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001299 ref->buffer = NULL;
1300}
1301
1302WL_EXPORT void
1303weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001304 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001305{
1306 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001307 ref->buffer->busy_count--;
1308 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001309 assert(wl_resource_get_client(ref->buffer->resource));
1310 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001311 WL_BUFFER_RELEASE);
1312 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001313 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001314 }
1315
Pekka Paalanende685b82012-12-04 15:58:12 +02001316 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001317 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001318 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001319 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001320 }
1321
Pekka Paalanende685b82012-12-04 15:58:12 +02001322 ref->buffer = buffer;
1323 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1324}
1325
1326static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001327weston_surface_attach(struct weston_surface *surface,
1328 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001329{
1330 weston_buffer_reference(&surface->buffer_ref, buffer);
1331
Pekka Paalanena6421c42012-12-04 15:58:10 +02001332 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001333 if (weston_surface_is_mapped(surface))
1334 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001335 }
1336
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001337 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001338}
1339
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001340WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341weston_view_restack(struct weston_view *view, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001342{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343 wl_list_remove(&view->layer_link);
1344 wl_list_insert(below, &view->layer_link);
1345 weston_view_damage_below(view);
1346 weston_surface_damage(view->surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001347}
1348
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001349WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001350weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001351{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001352 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001353
1354 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001355 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001356}
1357
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001358WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001359weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001360{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001361 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001362
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001363 pixman_region32_union(&compositor->primary_plane.damage,
1364 &compositor->primary_plane.damage,
1365 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001366 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001367}
1368
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001369static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001370surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001371{
Pekka Paalanende685b82012-12-04 15:58:12 +02001372 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001373 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001374 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001375
Jason Ekstranda7af7042013-10-12 22:38:11 -05001376 empty_region(&surface->damage);
1377}
1378
1379static void
1380view_accumulate_damage(struct weston_view *view,
1381 pixman_region32_t *opaque)
1382{
1383 pixman_region32_t damage;
1384
1385 pixman_region32_init(&damage);
1386 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001387 pixman_box32_t *extents;
1388
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 extents = pixman_region32_extents(&view->surface->damage);
1390 view_compute_bbox(view, extents->x1, extents->y1,
1391 extents->x2 - extents->x1,
1392 extents->y2 - extents->y1,
1393 &damage);
1394 pixman_region32_translate(&damage,
1395 -view->plane->x,
1396 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001397 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 pixman_region32_copy(&damage, &view->surface->damage);
1399 pixman_region32_translate(&damage,
1400 view->geometry.x - view->plane->x,
1401 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001402 }
1403
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404 pixman_region32_subtract(&damage, &damage, opaque);
1405 pixman_region32_union(&view->plane->damage,
1406 &view->plane->damage, &damage);
1407 pixman_region32_fini(&damage);
1408 pixman_region32_copy(&view->clip, opaque);
1409 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001410}
1411
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001412static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001413compositor_accumulate_damage(struct weston_compositor *ec)
1414{
1415 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001417 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001418
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001419 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001420
1421 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001422 pixman_region32_copy(&plane->clip, &clip);
1423
1424 pixman_region32_init(&opaque);
1425
Jason Ekstranda7af7042013-10-12 22:38:11 -05001426 wl_list_for_each(ev, &ec->view_list, link) {
1427 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001428 continue;
1429
Jason Ekstranda7af7042013-10-12 22:38:11 -05001430 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001431 }
1432
1433 pixman_region32_union(&clip, &clip, &opaque);
1434 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001435 }
1436
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001437 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001438
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439 wl_list_for_each(ev, &ec->view_list, link)
1440 ev->surface->touched = 0;
1441
1442 wl_list_for_each(ev, &ec->view_list, link) {
1443 if (ev->surface->touched)
1444 continue;
1445 ev->surface->touched = 1;
1446
1447 surface_flush_damage(ev->surface);
1448
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001449 /* Both the renderer and the backend have seen the buffer
1450 * by now. If renderer needs the buffer, it has its own
1451 * reference set. If the backend wants to keep the buffer
1452 * around for migrating the surface into a non-primary plane
1453 * later, keep_buffer is true. Otherwise, drop the core
1454 * reference now, and allow early buffer release. This enables
1455 * clients to use single-buffering.
1456 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 if (!ev->surface->keep_buffer)
1458 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001459 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001460}
1461
1462static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001464{
1465 struct weston_subsurface *sub;
1466
Pekka Paalanene67858b2013-04-25 13:57:42 +03001467 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001468 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001469 continue;
1470
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1472 wl_list_init(&sub->surface->views);
1473
1474 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001475 }
1476}
1477
1478static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001479surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001480{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481 struct weston_subsurface *sub;
1482 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001483
Jason Ekstranda7af7042013-10-12 22:38:11 -05001484 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1485 if (sub->surface == surface)
1486 continue;
1487
1488 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1489 weston_view_destroy(view);
1490
1491 surface_free_unused_subsurface_views(sub->surface);
1492 }
1493}
1494
1495static void
1496view_list_add_subsurface_view(struct weston_compositor *compositor,
1497 struct weston_subsurface *sub,
1498 struct weston_view *parent)
1499{
1500 struct weston_subsurface *child;
1501 struct weston_view *view = NULL, *iv;
1502
1503 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1504 if (iv->geometry.parent == parent) {
1505 view = iv;
1506 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001507 }
1508 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001509
1510 if (view) {
1511 /* Put it back in the surface's list of views */
1512 wl_list_remove(&view->surface_link);
1513 wl_list_insert(&sub->surface->views, &view->surface_link);
1514 } else {
1515 view = weston_view_create(sub->surface);
1516 weston_view_configure(view,
1517 sub->position.x,
1518 sub->position.y,
1519 sub->surface->width,
1520 sub->surface->height);
1521 weston_view_set_transform_parent(view, parent);
1522 }
1523
1524 weston_view_update_transform(view);
1525 wl_list_insert(compositor->view_list.next, &view->link);
1526
1527 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
1528 if (child->surface != sub->surface)
1529 view_list_add_subsurface_view(compositor, child, view);
1530}
1531
1532static void
1533view_list_add(struct weston_compositor *compositor,
1534 struct weston_view *view)
1535{
1536 struct weston_subsurface *sub;
1537
1538 weston_view_update_transform(view);
1539 wl_list_insert(compositor->view_list.prev, &view->link);
1540
1541 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link)
1542 if (sub->surface != view->surface)
1543 view_list_add_subsurface_view(compositor, sub, view);
1544}
1545
1546static void
1547weston_compositor_build_view_list(struct weston_compositor *compositor)
1548{
1549 struct weston_view *view;
1550 struct weston_layer *layer;
1551
1552 wl_list_for_each(layer, &compositor->layer_list, link)
1553 wl_list_for_each(view, &layer->view_list, layer_link)
1554 surface_stash_subsurface_views(view->surface);
1555
1556 wl_list_init(&compositor->view_list);
1557 wl_list_for_each(layer, &compositor->layer_list, link) {
1558 wl_list_for_each(view, &layer->view_list, layer_link) {
1559 view_list_add(compositor, view);
1560 }
1561 }
1562
1563 wl_list_for_each(layer, &compositor->layer_list, link)
1564 wl_list_for_each(view, &layer->view_list, layer_link)
1565 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001566}
1567
David Herrmann1edf44c2013-10-22 17:11:26 +02001568static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001569weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001570{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001571 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001573 struct weston_animation *animation, *next;
1574 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001575 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001576 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001577 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001578
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001579 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001580 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001581
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001582 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001583 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001584 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001585 wl_list_for_each(ev, &ec->view_list, link)
1586 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001587
Pekka Paalanene67858b2013-04-25 13:57:42 +03001588 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589 wl_list_for_each(ev, &ec->view_list, link) {
1590 /* Note: This operation is safe to do multiple times on the
1591 * same surface.
1592 */
1593 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001594 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001595 &ev->surface->frame_callback_list);
1596 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001597 }
1598 }
1599
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001600 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001601
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001602 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001603 pixman_region32_intersect(&output_damage,
1604 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001605 pixman_region32_subtract(&output_damage,
1606 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001607
Scott Moreauccbf29d2012-02-22 14:21:41 -07001608 if (output->dirty)
1609 weston_output_update_matrix(output);
1610
David Herrmann1edf44c2013-10-22 17:11:26 +02001611 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001612
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001613 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001614
Kristian Høgsbergef044142011-06-21 15:02:12 -04001615 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001616
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001617 weston_compositor_repick(ec);
1618 wl_event_loop_dispatch(ec->input_loop, 0);
1619
Jonas Ådahldb773762012-06-13 00:01:21 +02001620 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001621 wl_callback_send_done(cb->resource, msecs);
1622 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001623 }
1624
Scott Moreaud64cf212012-06-08 19:40:54 -06001625 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001626 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001627 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001628 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001629
1630 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001631}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001632
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001633static int
1634weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001635{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001636 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001637
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001638 wl_event_loop_dispatch(compositor->input_loop, 0);
1639
1640 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001641}
1642
1643WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001644weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001645{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001646 struct weston_compositor *compositor = output->compositor;
1647 struct wl_event_loop *loop =
1648 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001649 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001650
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001651 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001652
1653 if (output->repaint_needed &&
1654 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1655 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001656 r = weston_output_repaint(output, msecs);
1657 if (!r)
1658 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001659 }
1660
1661 output->repaint_scheduled = 0;
1662 if (compositor->input_loop_source)
1663 return;
1664
1665 fd = wl_event_loop_get_fd(compositor->input_loop);
1666 compositor->input_loop_source =
1667 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1668 weston_compositor_read_input, compositor);
1669}
1670
1671static void
1672idle_repaint(void *data)
1673{
1674 struct weston_output *output = data;
1675
Jonas Ådahle5a12252013-04-05 23:07:11 +02001676 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001677}
1678
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001679WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001680weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1681{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001683 if (below != NULL)
1684 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001685}
1686
1687WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001688weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001689{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001690 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001691 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001692
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001693 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1694 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001695 return;
1696
Kristian Høgsbergef044142011-06-21 15:02:12 -04001697 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001698 output->repaint_needed = 1;
1699 if (output->repaint_scheduled)
1700 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001701
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001702 wl_event_loop_add_idle(loop, idle_repaint, output);
1703 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001704
1705 if (compositor->input_loop_source) {
1706 wl_event_source_remove(compositor->input_loop_source);
1707 compositor->input_loop_source = NULL;
1708 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001709}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001710
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001711WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001712weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1713{
1714 struct weston_output *output;
1715
1716 wl_list_for_each(output, &compositor->output_list, link)
1717 weston_output_schedule_repaint(output);
1718}
1719
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001720static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001721surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001722{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001723 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001724}
1725
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001726static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001727surface_attach(struct wl_client *client,
1728 struct wl_resource *resource,
1729 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1730{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001731 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001732 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001733
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001734 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001735 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001736 if (buffer == NULL) {
1737 wl_client_post_no_memory(client);
1738 return;
1739 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001740 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001741
Pekka Paalanende685b82012-12-04 15:58:12 +02001742 /* Attach, attach, without commit in between does not send
1743 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001744 if (surface->pending.buffer)
1745 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001746
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001747 surface->pending.sx = sx;
1748 surface->pending.sy = sy;
1749 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001750 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001751 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001752 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001753 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001754 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001755}
1756
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001757static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001758surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001759 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001760 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001761{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001762 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001763
Pekka Paalanen8e159182012-10-10 12:49:25 +03001764 pixman_region32_union_rect(&surface->pending.damage,
1765 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001766 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001767}
1768
Kristian Høgsberg33418202011-08-16 23:01:28 -04001769static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001770destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001771{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001772 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001773
1774 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001775 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001776}
1777
1778static void
1779surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001780 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001781{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001782 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001783 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001784
1785 cb = malloc(sizeof *cb);
1786 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001787 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001788 return;
1789 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001790
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001791 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1792 callback);
1793 if (cb->resource == NULL) {
1794 free(cb);
1795 wl_resource_post_no_memory(resource);
1796 return;
1797 }
1798
Jason Ekstranda85118c2013-06-27 20:17:02 -05001799 wl_resource_set_implementation(cb->resource, NULL, cb,
1800 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001801
Pekka Paalanenbc106382012-10-10 12:49:31 +03001802 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001803}
1804
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001805static void
1806surface_set_opaque_region(struct wl_client *client,
1807 struct wl_resource *resource,
1808 struct wl_resource *region_resource)
1809{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001810 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001811 struct weston_region *region;
1812
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001813 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001814 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001815 pixman_region32_copy(&surface->pending.opaque,
1816 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001817 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001818 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001819 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001820}
1821
1822static void
1823surface_set_input_region(struct wl_client *client,
1824 struct wl_resource *resource,
1825 struct wl_resource *region_resource)
1826{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001827 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001828 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001829
1830 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001831 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001832 pixman_region32_copy(&surface->pending.input,
1833 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001834 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001835 pixman_region32_fini(&surface->pending.input);
1836 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001837 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001838}
1839
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001840static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001841weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001842{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001843 struct weston_subsurface *sub;
1844
1845 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1846 parent_link_pending) {
1847 wl_list_remove(&sub->parent_link);
1848 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1849 }
1850}
1851
1852static void
1853weston_surface_commit(struct weston_surface *surface)
1854{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001855 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001856 pixman_region32_t opaque;
1857
Alexander Larsson4ea95522013-05-22 14:41:37 +02001858 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001859 surface->buffer_transform = surface->pending.buffer_transform;
1860
Alexander Larsson4ea95522013-05-22 14:41:37 +02001861 /* wl_surface.set_buffer_scale */
1862 surface->buffer_scale = surface->pending.buffer_scale;
1863
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001864 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001865 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001866 weston_surface_attach(surface, surface->pending.buffer);
1867
Jason Ekstranda7af7042013-10-12 22:38:11 -05001868 surface->width = 0;
1869 surface->height = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001870 if (surface->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001871 /* This already includes the buffer scale */
1872 surface->width = weston_surface_buffer_width(surface);
1873 surface->height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001874 }
1875
1876 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001877 surface->configure(surface,
1878 surface->pending.sx, surface->pending.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001879 surface->width, surface->height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001880
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001881 if (surface->pending.buffer)
1882 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1883 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001884 surface->pending.sx = 0;
1885 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001886 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001887
1888 /* wl_surface.damage */
1889 pixman_region32_union(&surface->damage, &surface->damage,
1890 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001891 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1892 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001893 surface->width,
1894 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001895 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001896
1897 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001898 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001899 surface->width,
1900 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001901 pixman_region32_intersect(&opaque,
1902 &opaque, &surface->pending.opaque);
1903
1904 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1905 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001906 wl_list_for_each(view, &surface->views, surface_link)
1907 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001908 }
1909
1910 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001911
1912 /* wl_surface.set_input_region */
1913 pixman_region32_fini(&surface->input);
1914 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001915 surface->width,
1916 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001917 pixman_region32_intersect(&surface->input,
1918 &surface->input, &surface->pending.input);
1919
Pekka Paalanenbc106382012-10-10 12:49:31 +03001920 /* wl_surface.frame */
1921 wl_list_insert_list(&surface->frame_callback_list,
1922 &surface->pending.frame_callback_list);
1923 wl_list_init(&surface->pending.frame_callback_list);
1924
Pekka Paalanene67858b2013-04-25 13:57:42 +03001925 weston_surface_commit_subsurface_order(surface);
1926
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001927 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001928}
1929
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001930static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001931weston_subsurface_commit(struct weston_subsurface *sub);
1932
1933static void
1934weston_subsurface_parent_commit(struct weston_subsurface *sub,
1935 int parent_is_synchronized);
1936
1937static void
1938surface_commit(struct wl_client *client, struct wl_resource *resource)
1939{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001940 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001941 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1942
1943 if (sub) {
1944 weston_subsurface_commit(sub);
1945 return;
1946 }
1947
1948 weston_surface_commit(surface);
1949
1950 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1951 if (sub->surface != surface)
1952 weston_subsurface_parent_commit(sub, 0);
1953 }
1954}
1955
1956static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001957surface_set_buffer_transform(struct wl_client *client,
1958 struct wl_resource *resource, int transform)
1959{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001960 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001961
1962 surface->pending.buffer_transform = transform;
1963}
1964
Alexander Larsson4ea95522013-05-22 14:41:37 +02001965static void
1966surface_set_buffer_scale(struct wl_client *client,
1967 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001968 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001969{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001970 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001971
1972 surface->pending.buffer_scale = scale;
1973}
1974
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001975static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001976 surface_destroy,
1977 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001978 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001979 surface_frame,
1980 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001981 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001982 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001983 surface_set_buffer_transform,
1984 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001985};
1986
1987static void
1988compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001989 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001990{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04001991 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001992 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001993
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001994 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001995 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001996 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001997 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001998 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001999
Jason Ekstranda85118c2013-06-27 20:17:02 -05002000 surface->resource =
2001 wl_resource_create(client, &wl_surface_interface,
2002 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002003 if (surface->resource == NULL) {
2004 weston_surface_destroy(surface);
2005 wl_resource_post_no_memory(resource);
2006 return;
2007 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002008 wl_resource_set_implementation(surface->resource, &surface_interface,
2009 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002010}
2011
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002012static void
2013destroy_region(struct wl_resource *resource)
2014{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002015 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002016
2017 pixman_region32_fini(&region->region);
2018 free(region);
2019}
2020
2021static void
2022region_destroy(struct wl_client *client, struct wl_resource *resource)
2023{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002024 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002025}
2026
2027static void
2028region_add(struct wl_client *client, struct wl_resource *resource,
2029 int32_t x, int32_t y, int32_t width, int32_t height)
2030{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002031 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002032
2033 pixman_region32_union_rect(&region->region, &region->region,
2034 x, y, width, height);
2035}
2036
2037static void
2038region_subtract(struct wl_client *client, struct wl_resource *resource,
2039 int32_t x, int32_t y, int32_t width, int32_t height)
2040{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002041 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002042 pixman_region32_t rect;
2043
2044 pixman_region32_init_rect(&rect, x, y, width, height);
2045 pixman_region32_subtract(&region->region, &region->region, &rect);
2046 pixman_region32_fini(&rect);
2047}
2048
2049static const struct wl_region_interface region_interface = {
2050 region_destroy,
2051 region_add,
2052 region_subtract
2053};
2054
2055static void
2056compositor_create_region(struct wl_client *client,
2057 struct wl_resource *resource, uint32_t id)
2058{
2059 struct weston_region *region;
2060
2061 region = malloc(sizeof *region);
2062 if (region == NULL) {
2063 wl_resource_post_no_memory(resource);
2064 return;
2065 }
2066
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002067 pixman_region32_init(&region->region);
2068
Jason Ekstranda85118c2013-06-27 20:17:02 -05002069 region->resource =
2070 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002071 if (region->resource == NULL) {
2072 free(region);
2073 wl_resource_post_no_memory(resource);
2074 return;
2075 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002076 wl_resource_set_implementation(region->resource, &region_interface,
2077 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002078}
2079
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002080static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002081 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002082 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002083};
2084
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002085static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002086weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2087{
2088 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002089 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002090 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002091
Alexander Larsson4ea95522013-05-22 14:41:37 +02002092 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03002093 surface->buffer_transform = sub->cached.buffer_transform;
2094
Alexander Larsson4ea95522013-05-22 14:41:37 +02002095 /* wl_surface.set_buffer_scale */
2096 surface->buffer_scale = sub->cached.buffer_scale;
2097
Pekka Paalanene67858b2013-04-25 13:57:42 +03002098 /* wl_surface.attach */
2099 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2100 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2101 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2102
Jason Ekstranda7af7042013-10-12 22:38:11 -05002103 surface->width = 0;
2104 surface->height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002105 if (surface->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002106 surface->width = weston_surface_buffer_width(surface);
2107 surface->height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002108 }
2109
2110 if (surface->configure && sub->cached.newly_attached)
2111 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112 surface->width, surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002113 sub->cached.sx = 0;
2114 sub->cached.sy = 0;
2115 sub->cached.newly_attached = 0;
2116
2117 /* wl_surface.damage */
2118 pixman_region32_union(&surface->damage, &surface->damage,
2119 &sub->cached.damage);
2120 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2121 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002122 surface->width,
2123 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002124 empty_region(&sub->cached.damage);
2125
2126 /* wl_surface.set_opaque_region */
2127 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002128 surface->width,
2129 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002130 pixman_region32_intersect(&opaque,
2131 &opaque, &sub->cached.opaque);
2132
2133 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2134 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002135 wl_list_for_each(view, &surface->views, surface_link)
2136 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002137 }
2138
2139 pixman_region32_fini(&opaque);
2140
2141 /* wl_surface.set_input_region */
2142 pixman_region32_fini(&surface->input);
2143 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144 surface->width,
2145 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002146 pixman_region32_intersect(&surface->input,
2147 &surface->input, &sub->cached.input);
2148
2149 /* wl_surface.frame */
2150 wl_list_insert_list(&surface->frame_callback_list,
2151 &sub->cached.frame_callback_list);
2152 wl_list_init(&sub->cached.frame_callback_list);
2153
2154 weston_surface_commit_subsurface_order(surface);
2155
2156 weston_surface_schedule_repaint(surface);
2157
2158 sub->cached.has_data = 0;
2159}
2160
2161static void
2162weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2163{
2164 struct weston_surface *surface = sub->surface;
2165
2166 /*
2167 * If this commit would cause the surface to move by the
2168 * attach(dx, dy) parameters, the old damage region must be
2169 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002170 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002171 */
2172 pixman_region32_translate(&sub->cached.damage,
2173 -surface->pending.sx, -surface->pending.sy);
2174 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2175 &surface->pending.damage);
2176 empty_region(&surface->pending.damage);
2177
2178 if (surface->pending.newly_attached) {
2179 sub->cached.newly_attached = 1;
2180 weston_buffer_reference(&sub->cached.buffer_ref,
2181 surface->pending.buffer);
2182 }
2183 sub->cached.sx += surface->pending.sx;
2184 sub->cached.sy += surface->pending.sy;
2185 surface->pending.sx = 0;
2186 surface->pending.sy = 0;
2187 surface->pending.newly_attached = 0;
2188
2189 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002190 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002191
2192 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2193
2194 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2195
2196 wl_list_insert_list(&sub->cached.frame_callback_list,
2197 &surface->pending.frame_callback_list);
2198 wl_list_init(&surface->pending.frame_callback_list);
2199
2200 sub->cached.has_data = 1;
2201}
2202
2203static int
2204weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2205{
2206 while (sub) {
2207 if (sub->synchronized)
2208 return 1;
2209
2210 if (!sub->parent)
2211 return 0;
2212
2213 sub = weston_surface_to_subsurface(sub->parent);
2214 }
2215
2216 return 0;
2217}
2218
2219static void
2220weston_subsurface_commit(struct weston_subsurface *sub)
2221{
2222 struct weston_surface *surface = sub->surface;
2223 struct weston_subsurface *tmp;
2224
2225 /* Recursive check for effectively synchronized. */
2226 if (weston_subsurface_is_synchronized(sub)) {
2227 weston_subsurface_commit_to_cache(sub);
2228 } else {
2229 if (sub->cached.has_data) {
2230 /* flush accumulated state from cache */
2231 weston_subsurface_commit_to_cache(sub);
2232 weston_subsurface_commit_from_cache(sub);
2233 } else {
2234 weston_surface_commit(surface);
2235 }
2236
2237 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2238 if (tmp->surface != surface)
2239 weston_subsurface_parent_commit(tmp, 0);
2240 }
2241 }
2242}
2243
2244static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002245weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002246{
2247 struct weston_surface *surface = sub->surface;
2248 struct weston_subsurface *tmp;
2249
Pekka Paalanene67858b2013-04-25 13:57:42 +03002250 /* From now on, commit_from_cache the whole sub-tree, regardless of
2251 * the synchronized mode of each child. This sub-surface or some
2252 * of its ancestors were synchronized, so we are synchronized
2253 * all the way down.
2254 */
2255
2256 if (sub->cached.has_data)
2257 weston_subsurface_commit_from_cache(sub);
2258
2259 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2260 if (tmp->surface != surface)
2261 weston_subsurface_parent_commit(tmp, 1);
2262 }
2263}
2264
2265static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002266weston_subsurface_parent_commit(struct weston_subsurface *sub,
2267 int parent_is_synchronized)
2268{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002269 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002270 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002271 wl_list_for_each(view, &sub->surface->views, surface_link)
2272 weston_view_set_position(view,
2273 sub->position.x,
2274 sub->position.y);
2275
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002276 sub->position.set = 0;
2277 }
2278
2279 if (parent_is_synchronized || sub->synchronized)
2280 weston_subsurface_synchronized_commit(sub);
2281}
2282
2283static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002284subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
2285 int32_t width, int32_t height)
2286{
2287 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002289
Jason Ekstranda7af7042013-10-12 22:38:11 -05002290 wl_list_for_each(view, &surface->views, surface_link)
2291 weston_view_configure(view,
2292 view->geometry.x + dx,
2293 view->geometry.y + dy,
2294 width, height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002295
2296 /* No need to check parent mappedness, because if parent is not
2297 * mapped, parent is not in a visible layer, so this sub-surface
2298 * will not be drawn either.
2299 */
2300 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002301 /* Cannot call weston_surface_update_transform(),
2302 * because that would call it also for the parent surface,
2303 * which might not be mapped yet. That would lead to
2304 * inconsistent state, where the window could never be
2305 * mapped.
2306 *
2307 * Instead just assing any output, to make
2308 * weston_surface_is_mapped() return true, so that when the
2309 * parent surface does get mapped, this one will get
2310 * included, too. See surface_list_add().
2311 */
2312 assert(!wl_list_empty(&compositor->output_list));
2313 surface->output = container_of(compositor->output_list.next,
2314 struct weston_output, link);
2315 }
2316}
2317
2318static struct weston_subsurface *
2319weston_surface_to_subsurface(struct weston_surface *surface)
2320{
2321 if (surface->configure == subsurface_configure)
2322 return surface->configure_private;
2323
2324 return NULL;
2325}
2326
Pekka Paalanen01388e22013-04-25 13:57:44 +03002327WL_EXPORT struct weston_surface *
2328weston_surface_get_main_surface(struct weston_surface *surface)
2329{
2330 struct weston_subsurface *sub;
2331
2332 while (surface && (sub = weston_surface_to_subsurface(surface)))
2333 surface = sub->parent;
2334
2335 return surface;
2336}
2337
Pekka Paalanene67858b2013-04-25 13:57:42 +03002338static void
2339subsurface_set_position(struct wl_client *client,
2340 struct wl_resource *resource, int32_t x, int32_t y)
2341{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002342 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002343
2344 if (!sub)
2345 return;
2346
2347 sub->position.x = x;
2348 sub->position.y = y;
2349 sub->position.set = 1;
2350}
2351
2352static struct weston_subsurface *
2353subsurface_from_surface(struct weston_surface *surface)
2354{
2355 struct weston_subsurface *sub;
2356
2357 sub = weston_surface_to_subsurface(surface);
2358 if (sub)
2359 return sub;
2360
2361 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2362 if (sub->surface == surface)
2363 return sub;
2364
2365 return NULL;
2366}
2367
2368static struct weston_subsurface *
2369subsurface_sibling_check(struct weston_subsurface *sub,
2370 struct weston_surface *surface,
2371 const char *request)
2372{
2373 struct weston_subsurface *sibling;
2374
2375 sibling = subsurface_from_surface(surface);
2376
2377 if (!sibling) {
2378 wl_resource_post_error(sub->resource,
2379 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2380 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002381 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002382 return NULL;
2383 }
2384
2385 if (sibling->parent != sub->parent) {
2386 wl_resource_post_error(sub->resource,
2387 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2388 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002389 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002390 return NULL;
2391 }
2392
2393 return sibling;
2394}
2395
2396static void
2397subsurface_place_above(struct wl_client *client,
2398 struct wl_resource *resource,
2399 struct wl_resource *sibling_resource)
2400{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002401 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002402 struct weston_surface *surface =
2403 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002404 struct weston_subsurface *sibling;
2405
2406 if (!sub)
2407 return;
2408
2409 sibling = subsurface_sibling_check(sub, surface, "place_above");
2410 if (!sibling)
2411 return;
2412
2413 wl_list_remove(&sub->parent_link_pending);
2414 wl_list_insert(sibling->parent_link_pending.prev,
2415 &sub->parent_link_pending);
2416}
2417
2418static void
2419subsurface_place_below(struct wl_client *client,
2420 struct wl_resource *resource,
2421 struct wl_resource *sibling_resource)
2422{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002423 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002424 struct weston_surface *surface =
2425 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002426 struct weston_subsurface *sibling;
2427
2428 if (!sub)
2429 return;
2430
2431 sibling = subsurface_sibling_check(sub, surface, "place_below");
2432 if (!sibling)
2433 return;
2434
2435 wl_list_remove(&sub->parent_link_pending);
2436 wl_list_insert(&sibling->parent_link_pending,
2437 &sub->parent_link_pending);
2438}
2439
2440static void
2441subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2442{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002443 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002444
2445 if (sub)
2446 sub->synchronized = 1;
2447}
2448
2449static void
2450subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2451{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002452 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002453
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002454 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002455 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002456
2457 /* If sub became effectively desynchronized, flush. */
2458 if (!weston_subsurface_is_synchronized(sub))
2459 weston_subsurface_synchronized_commit(sub);
2460 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002461}
2462
2463static void
2464weston_subsurface_cache_init(struct weston_subsurface *sub)
2465{
2466 pixman_region32_init(&sub->cached.damage);
2467 pixman_region32_init(&sub->cached.opaque);
2468 pixman_region32_init(&sub->cached.input);
2469 wl_list_init(&sub->cached.frame_callback_list);
2470 sub->cached.buffer_ref.buffer = NULL;
2471}
2472
2473static void
2474weston_subsurface_cache_fini(struct weston_subsurface *sub)
2475{
2476 struct weston_frame_callback *cb, *tmp;
2477
2478 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002479 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002480
2481 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2482 pixman_region32_fini(&sub->cached.damage);
2483 pixman_region32_fini(&sub->cached.opaque);
2484 pixman_region32_fini(&sub->cached.input);
2485}
2486
2487static void
2488weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2489{
2490 wl_list_remove(&sub->parent_link);
2491 wl_list_remove(&sub->parent_link_pending);
2492 wl_list_remove(&sub->parent_destroy_listener.link);
2493 sub->parent = NULL;
2494}
2495
2496static void
2497weston_subsurface_destroy(struct weston_subsurface *sub);
2498
2499static void
2500subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2501{
2502 struct weston_subsurface *sub =
2503 container_of(listener, struct weston_subsurface,
2504 surface_destroy_listener);
2505 assert(data == &sub->surface->resource);
2506
2507 /* The protocol object (wl_resource) is left inert. */
2508 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002509 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002510
2511 weston_subsurface_destroy(sub);
2512}
2513
2514static void
2515subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2516{
2517 struct weston_subsurface *sub =
2518 container_of(listener, struct weston_subsurface,
2519 parent_destroy_listener);
2520 assert(data == &sub->parent->resource);
2521 assert(sub->surface != sub->parent);
2522
2523 if (weston_surface_is_mapped(sub->surface))
2524 weston_surface_unmap(sub->surface);
2525
2526 weston_subsurface_unlink_parent(sub);
2527}
2528
2529static void
2530subsurface_resource_destroy(struct wl_resource *resource)
2531{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002532 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002533
2534 if (sub)
2535 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002536}
2537
2538static void
2539subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2540{
2541 wl_resource_destroy(resource);
2542}
2543
2544static void
2545weston_subsurface_link_parent(struct weston_subsurface *sub,
2546 struct weston_surface *parent)
2547{
2548 sub->parent = parent;
2549 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002550 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002551 &sub->parent_destroy_listener);
2552
2553 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2554 wl_list_insert(&parent->subsurface_list_pending,
2555 &sub->parent_link_pending);
2556}
2557
2558static void
2559weston_subsurface_link_surface(struct weston_subsurface *sub,
2560 struct weston_surface *surface)
2561{
2562 sub->surface = surface;
2563 sub->surface_destroy_listener.notify =
2564 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002565 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002566 &sub->surface_destroy_listener);
2567}
2568
2569static void
2570weston_subsurface_destroy(struct weston_subsurface *sub)
2571{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002572 struct weston_view *view, *next;
2573
Pekka Paalanene67858b2013-04-25 13:57:42 +03002574 assert(sub->surface);
2575
2576 if (sub->resource) {
2577 assert(weston_surface_to_subsurface(sub->surface) == sub);
2578 assert(sub->parent_destroy_listener.notify ==
2579 subsurface_handle_parent_destroy);
2580
Jason Ekstranda7af7042013-10-12 22:38:11 -05002581 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2582 weston_view_destroy(view);
2583
Pekka Paalanene67858b2013-04-25 13:57:42 +03002584 if (sub->parent)
2585 weston_subsurface_unlink_parent(sub);
2586
2587 weston_subsurface_cache_fini(sub);
2588
2589 sub->surface->configure = NULL;
2590 sub->surface->configure_private = NULL;
2591 } else {
2592 /* the dummy weston_subsurface for the parent itself */
2593 assert(sub->parent_destroy_listener.notify == NULL);
2594 wl_list_remove(&sub->parent_link);
2595 wl_list_remove(&sub->parent_link_pending);
2596 }
2597
2598 wl_list_remove(&sub->surface_destroy_listener.link);
2599 free(sub);
2600}
2601
2602static const struct wl_subsurface_interface subsurface_implementation = {
2603 subsurface_destroy,
2604 subsurface_set_position,
2605 subsurface_place_above,
2606 subsurface_place_below,
2607 subsurface_set_sync,
2608 subsurface_set_desync
2609};
2610
2611static struct weston_subsurface *
2612weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2613 struct weston_surface *parent)
2614{
2615 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002616 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002617
2618 sub = calloc(1, sizeof *sub);
2619 if (!sub)
2620 return NULL;
2621
Jason Ekstranda7af7042013-10-12 22:38:11 -05002622 wl_list_init(&sub->unused_views);
2623
Jason Ekstranda85118c2013-06-27 20:17:02 -05002624 sub->resource =
2625 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002626 if (!sub->resource) {
2627 free(sub);
2628 return NULL;
2629 }
2630
Jason Ekstranda85118c2013-06-27 20:17:02 -05002631 wl_resource_set_implementation(sub->resource,
2632 &subsurface_implementation,
2633 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002634 weston_subsurface_link_surface(sub, surface);
2635 weston_subsurface_link_parent(sub, parent);
2636 weston_subsurface_cache_init(sub);
2637 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002638
2639 return sub;
2640}
2641
2642/* Create a dummy subsurface for having the parent itself in its
2643 * sub-surface lists. Makes stacking order manipulation easy.
2644 */
2645static struct weston_subsurface *
2646weston_subsurface_create_for_parent(struct weston_surface *parent)
2647{
2648 struct weston_subsurface *sub;
2649
2650 sub = calloc(1, sizeof *sub);
2651 if (!sub)
2652 return NULL;
2653
2654 weston_subsurface_link_surface(sub, parent);
2655 sub->parent = parent;
2656 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2657 wl_list_insert(&parent->subsurface_list_pending,
2658 &sub->parent_link_pending);
2659
2660 return sub;
2661}
2662
2663static void
2664subcompositor_get_subsurface(struct wl_client *client,
2665 struct wl_resource *resource,
2666 uint32_t id,
2667 struct wl_resource *surface_resource,
2668 struct wl_resource *parent_resource)
2669{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002670 struct weston_surface *surface =
2671 wl_resource_get_user_data(surface_resource);
2672 struct weston_surface *parent =
2673 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002674 struct weston_subsurface *sub;
2675 static const char where[] = "get_subsurface: wl_subsurface@";
2676
2677 if (surface == parent) {
2678 wl_resource_post_error(resource,
2679 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2680 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002681 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002682 return;
2683 }
2684
2685 if (weston_surface_to_subsurface(surface)) {
2686 wl_resource_post_error(resource,
2687 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2688 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002689 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002690 return;
2691 }
2692
2693 if (surface->configure) {
2694 wl_resource_post_error(resource,
2695 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2696 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002697 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002698 return;
2699 }
2700
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002701 if (weston_surface_get_main_surface(parent) == surface) {
2702 wl_resource_post_error(resource,
2703 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2704 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002705 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002706 return;
2707 }
2708
Pekka Paalanene67858b2013-04-25 13:57:42 +03002709 /* make sure the parent is in its own list */
2710 if (wl_list_empty(&parent->subsurface_list)) {
2711 if (!weston_subsurface_create_for_parent(parent)) {
2712 wl_resource_post_no_memory(resource);
2713 return;
2714 }
2715 }
2716
2717 sub = weston_subsurface_create(id, surface, parent);
2718 if (!sub) {
2719 wl_resource_post_no_memory(resource);
2720 return;
2721 }
2722
2723 surface->configure = subsurface_configure;
2724 surface->configure_private = sub;
2725}
2726
2727static void
2728subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2729{
2730 wl_resource_destroy(resource);
2731}
2732
2733static const struct wl_subcompositor_interface subcompositor_interface = {
2734 subcompositor_destroy,
2735 subcompositor_get_subsurface
2736};
2737
2738static void
2739bind_subcompositor(struct wl_client *client,
2740 void *data, uint32_t version, uint32_t id)
2741{
2742 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002743 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002744
Jason Ekstranda85118c2013-06-27 20:17:02 -05002745 resource =
2746 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002747 if (resource == NULL) {
2748 wl_client_post_no_memory(client);
2749 return;
2750 }
2751 wl_resource_set_implementation(resource, &subcompositor_interface,
2752 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002753}
2754
2755static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002756weston_compositor_dpms(struct weston_compositor *compositor,
2757 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002758{
2759 struct weston_output *output;
2760
2761 wl_list_for_each(output, &compositor->output_list, link)
2762 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002763 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002764}
2765
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002766WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002767weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002768{
Neil Roberts8b62e202013-09-30 13:14:47 +01002769 uint32_t old_state = compositor->state;
2770
2771 /* The state needs to be changed before emitting the wake
2772 * signal because that may try to schedule a repaint which
2773 * will not work if the compositor is still sleeping */
2774 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2775
2776 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002777 case WESTON_COMPOSITOR_SLEEPING:
2778 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2779 /* fall through */
2780 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002781 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002782 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002783 /* fall through */
2784 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002785 wl_event_source_timer_update(compositor->idle_source,
2786 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002787 }
2788}
2789
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002790WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002791weston_compositor_offscreen(struct weston_compositor *compositor)
2792{
2793 switch (compositor->state) {
2794 case WESTON_COMPOSITOR_OFFSCREEN:
2795 return;
2796 case WESTON_COMPOSITOR_SLEEPING:
2797 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2798 /* fall through */
2799 default:
2800 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2801 wl_event_source_timer_update(compositor->idle_source, 0);
2802 }
2803}
2804
2805WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002806weston_compositor_sleep(struct weston_compositor *compositor)
2807{
2808 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2809 return;
2810
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002811 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002812 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2813 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2814}
2815
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002816static int
2817idle_handler(void *data)
2818{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002819 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002820
2821 if (compositor->idle_inhibit)
2822 return 1;
2823
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002824 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002825 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002826
2827 return 1;
2828}
2829
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002830WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08002831weston_plane_init(struct weston_plane *plane,
2832 struct weston_compositor *ec,
2833 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002834{
2835 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002836 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002837 plane->x = x;
2838 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08002839 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002840
2841 /* Init the link so that the call to wl_list_remove() when releasing
2842 * the plane without ever stacking doesn't lead to a crash */
2843 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002844}
2845
2846WL_EXPORT void
2847weston_plane_release(struct weston_plane *plane)
2848{
Xiong Zhang97116532013-10-23 13:58:31 +08002849 struct weston_view *view;
2850
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002851 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002852 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002853
Xiong Zhang97116532013-10-23 13:58:31 +08002854 wl_list_for_each(view, &plane->compositor->view_list, link) {
2855 if (view->plane == plane)
2856 view->plane = NULL;
2857 }
2858
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002859 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002860}
2861
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002862WL_EXPORT void
2863weston_compositor_stack_plane(struct weston_compositor *ec,
2864 struct weston_plane *plane,
2865 struct weston_plane *above)
2866{
2867 if (above)
2868 wl_list_insert(above->link.prev, &plane->link);
2869 else
2870 wl_list_insert(&ec->plane_list, &plane->link);
2871}
2872
Casey Dahlin9074db52012-04-19 22:50:09 -04002873static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002874{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002875 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002876}
2877
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002878static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002879bind_output(struct wl_client *client,
2880 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002881{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002882 struct weston_output *output = data;
2883 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002884 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002885
Jason Ekstranda85118c2013-06-27 20:17:02 -05002886 resource = wl_resource_create(client, &wl_output_interface,
2887 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002888 if (resource == NULL) {
2889 wl_client_post_no_memory(client);
2890 return;
2891 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002892
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002893 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002894 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002895
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002896 wl_output_send_geometry(resource,
2897 output->x,
2898 output->y,
2899 output->mm_width,
2900 output->mm_height,
2901 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002902 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002903 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002904 if (version >= 2)
2905 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02002906 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002907
2908 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002909 wl_output_send_mode(resource,
2910 mode->flags,
2911 mode->width,
2912 mode->height,
2913 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002914 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002915
2916 if (version >= 2)
2917 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002918}
2919
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002920WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002921weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002922{
Richard Hughes64ddde12013-05-01 21:52:10 +01002923 wl_signal_emit(&output->destroy_signal, output);
2924
Richard Hughesafe690c2013-05-02 10:10:04 +01002925 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002926 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002927 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002928 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002929
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002930 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002931}
2932
Scott Moreau1bad5db2012-08-18 01:04:05 -06002933static void
2934weston_output_compute_transform(struct weston_output *output)
2935{
2936 struct weston_matrix transform;
2937 int flip;
2938
2939 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002940 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002941
2942 switch(output->transform) {
2943 case WL_OUTPUT_TRANSFORM_FLIPPED:
2944 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2945 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2946 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002947 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002948 flip = -1;
2949 break;
2950 default:
2951 flip = 1;
2952 break;
2953 }
2954
2955 switch(output->transform) {
2956 case WL_OUTPUT_TRANSFORM_NORMAL:
2957 case WL_OUTPUT_TRANSFORM_FLIPPED:
2958 transform.d[0] = flip;
2959 transform.d[1] = 0;
2960 transform.d[4] = 0;
2961 transform.d[5] = 1;
2962 break;
2963 case WL_OUTPUT_TRANSFORM_90:
2964 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2965 transform.d[0] = 0;
2966 transform.d[1] = -flip;
2967 transform.d[4] = 1;
2968 transform.d[5] = 0;
2969 break;
2970 case WL_OUTPUT_TRANSFORM_180:
2971 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2972 transform.d[0] = -flip;
2973 transform.d[1] = 0;
2974 transform.d[4] = 0;
2975 transform.d[5] = -1;
2976 break;
2977 case WL_OUTPUT_TRANSFORM_270:
2978 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2979 transform.d[0] = 0;
2980 transform.d[1] = flip;
2981 transform.d[4] = -1;
2982 transform.d[5] = 0;
2983 break;
2984 default:
2985 break;
2986 }
2987
2988 weston_matrix_multiply(&output->matrix, &transform);
2989}
2990
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002991WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002992weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002993{
Scott Moreau850ca422012-05-21 15:21:25 -06002994 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002995 struct weston_matrix camera;
2996 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002997
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002998 weston_matrix_init(&output->matrix);
2999 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003000 -(output->x + output->width / 2.0),
3001 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003002
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003003 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003004 2.0 / output->width,
3005 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003006
3007 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003008
Scott Moreauccbf29d2012-02-22 14:21:41 -07003009 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003010 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003011 weston_matrix_init(&camera);
3012 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003013 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003014 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003015 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003016 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003017 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003018 weston_matrix_multiply(&output->matrix, &modelview);
3019 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003020
Scott Moreauccbf29d2012-02-22 14:21:41 -07003021 output->dirty = 0;
3022}
3023
Scott Moreau1bad5db2012-08-18 01:04:05 -06003024static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003025weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003026{
3027 output->transform = transform;
3028
3029 switch (transform) {
3030 case WL_OUTPUT_TRANSFORM_90:
3031 case WL_OUTPUT_TRANSFORM_270:
3032 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3033 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3034 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003035 output->width = output->current_mode->height;
3036 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003037 break;
3038 case WL_OUTPUT_TRANSFORM_NORMAL:
3039 case WL_OUTPUT_TRANSFORM_180:
3040 case WL_OUTPUT_TRANSFORM_FLIPPED:
3041 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003042 output->width = output->current_mode->width;
3043 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003044 break;
3045 default:
3046 break;
3047 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003048
Hardening57388e42013-09-18 23:56:36 +02003049 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003050 output->width /= scale;
3051 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003052}
3053
Scott Moreauccbf29d2012-02-22 14:21:41 -07003054WL_EXPORT void
3055weston_output_move(struct weston_output *output, int x, int y)
3056{
3057 output->x = x;
3058 output->y = y;
3059
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003060 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003061 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003062 output->width,
3063 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003064}
3065
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003066WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003067weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003068 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003069 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003070{
3071 output->compositor = c;
3072 output->x = x;
3073 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003074 output->mm_width = mm_width;
3075 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003076 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003077 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003078
Alexander Larsson0b135062013-05-28 16:23:36 +02003079 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003080 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003081
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003082 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003083 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003084
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003085 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003086 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003087 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003088 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003089
Casey Dahlin58ba1372012-04-19 22:50:08 -04003090 output->id = ffs(~output->compositor->output_id_pool) - 1;
3091 output->compositor->output_id_pool |= 1 << output->id;
3092
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003093 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003094 wl_global_create(c->wl_display, &wl_output_interface, 2,
3095 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003096 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003097}
3098
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003099WL_EXPORT void
3100weston_output_transform_coordinate(struct weston_output *output,
3101 int device_x, int device_y,
3102 wl_fixed_t *x, wl_fixed_t *y)
3103{
3104 wl_fixed_t tx, ty;
3105 wl_fixed_t width, height;
3106
Hardeningff39efa2013-09-18 23:56:35 +02003107 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3108 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003109
3110 switch(output->transform) {
3111 case WL_OUTPUT_TRANSFORM_NORMAL:
3112 default:
3113 tx = wl_fixed_from_int(device_x);
3114 ty = wl_fixed_from_int(device_y);
3115 break;
3116 case WL_OUTPUT_TRANSFORM_90:
3117 tx = wl_fixed_from_int(device_y);
3118 ty = height - wl_fixed_from_int(device_x);
3119 break;
3120 case WL_OUTPUT_TRANSFORM_180:
3121 tx = width - wl_fixed_from_int(device_x);
3122 ty = height - wl_fixed_from_int(device_y);
3123 break;
3124 case WL_OUTPUT_TRANSFORM_270:
3125 tx = width - wl_fixed_from_int(device_y);
3126 ty = wl_fixed_from_int(device_x);
3127 break;
3128 case WL_OUTPUT_TRANSFORM_FLIPPED:
3129 tx = width - wl_fixed_from_int(device_x);
3130 ty = wl_fixed_from_int(device_y);
3131 break;
3132 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3133 tx = width - wl_fixed_from_int(device_y);
3134 ty = height - wl_fixed_from_int(device_x);
3135 break;
3136 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3137 tx = wl_fixed_from_int(device_x);
3138 ty = height - wl_fixed_from_int(device_y);
3139 break;
3140 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3141 tx = wl_fixed_from_int(device_y);
3142 ty = wl_fixed_from_int(device_x);
3143 break;
3144 }
3145
Hardeningff39efa2013-09-18 23:56:35 +02003146 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3147 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003148}
3149
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003150static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003151compositor_bind(struct wl_client *client,
3152 void *data, uint32_t version, uint32_t id)
3153{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003154 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003155 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003156
Jason Ekstranda85118c2013-06-27 20:17:02 -05003157 resource = wl_resource_create(client, &wl_compositor_interface,
3158 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003159 if (resource == NULL) {
3160 wl_client_post_no_memory(client);
3161 return;
3162 }
3163
3164 wl_resource_set_implementation(resource, &compositor_interface,
3165 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003166}
3167
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003168static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003169log_uname(void)
3170{
3171 struct utsname usys;
3172
3173 uname(&usys);
3174
3175 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3176 usys.version, usys.machine);
3177}
3178
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003179WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003180weston_environment_get_fd(const char *env)
3181{
3182 char *e, *end;
3183 int fd, flags;
3184
3185 e = getenv(env);
3186 if (!e)
3187 return -1;
3188 fd = strtol(e, &end, 0);
3189 if (*end != '\0')
3190 return -1;
3191
3192 flags = fcntl(fd, F_GETFD);
3193 if (flags == -1)
3194 return -1;
3195
3196 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3197 unsetenv(env);
3198
3199 return fd;
3200}
3201
3202WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003203weston_compositor_init(struct weston_compositor *ec,
3204 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003205 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003206 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003207{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003208 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003209 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003210 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003211
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003212 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003213 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003214 wl_signal_init(&ec->destroy_signal);
3215 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003216 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003217 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003218 wl_signal_init(&ec->idle_signal);
3219 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003220 wl_signal_init(&ec->show_input_panel_signal);
3221 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003222 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003223 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003224 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003225 wl_signal_init(&ec->session_signal);
3226 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003227
Casey Dahlin58ba1372012-04-19 22:50:08 -04003228 ec->output_id_pool = 0;
3229
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003230 if (!wl_global_create(display, &wl_compositor_interface, 3,
3231 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003232 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003233
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003234 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3235 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003236 return -1;
3237
Jason Ekstranda7af7042013-10-12 22:38:11 -05003238 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003239 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003240 wl_list_init(&ec->layer_list);
3241 wl_list_init(&ec->seat_list);
3242 wl_list_init(&ec->output_list);
3243 wl_list_init(&ec->key_binding_list);
3244 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003245 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003246 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003247 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003248
Xiong Zhang97116532013-10-23 13:58:31 +08003249 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003250 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003251
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003252 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3253 weston_config_section_get_string(s, "keymap_rules",
3254 (char **) &xkb_names.rules, NULL);
3255 weston_config_section_get_string(s, "keymap_model",
3256 (char **) &xkb_names.model, NULL);
3257 weston_config_section_get_string(s, "keymap_layout",
3258 (char **) &xkb_names.layout, NULL);
3259 weston_config_section_get_string(s, "keymap_variant",
3260 (char **) &xkb_names.variant, NULL);
3261 weston_config_section_get_string(s, "keymap_options",
3262 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003263
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003264 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3265 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003266
3267 ec->ping_handler = NULL;
3268
3269 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003270 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003271
3272 wl_data_device_manager_init(ec->wl_display);
3273
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003274 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003275
Daniel Stone725c2c32012-06-22 14:04:36 +01003276 loop = wl_display_get_event_loop(ec->wl_display);
3277 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3278 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3279
3280 ec->input_loop = wl_event_loop_create();
3281
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003282 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3283 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3284
3285 weston_compositor_schedule_repaint(ec);
3286
Daniel Stone725c2c32012-06-22 14:04:36 +01003287 return 0;
3288}
3289
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003290WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003291weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003292{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003293 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003294
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003295 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003296 if (ec->input_loop_source)
3297 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003298
Matt Roper361d2ad2011-08-29 13:52:23 -07003299 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003300 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003301 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003302
Daniel Stone325fc2d2012-05-30 16:31:58 +01003303 weston_binding_list_destroy_all(&ec->key_binding_list);
3304 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003305 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003306 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003307 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003308
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003309 weston_plane_release(&ec->primary_plane);
3310
Jonas Ådahlc97af922012-03-28 22:36:09 +02003311 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003312
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003313 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003314}
3315
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003316WL_EXPORT void
3317weston_version(int *major, int *minor, int *micro)
3318{
3319 *major = WESTON_VERSION_MAJOR;
3320 *minor = WESTON_VERSION_MINOR;
3321 *micro = WESTON_VERSION_MICRO;
3322}
3323
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003324static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003325 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003326 const char *desc;
3327} capability_strings[] = {
3328 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003329 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003330};
3331
3332static void
3333weston_compositor_log_capabilities(struct weston_compositor *compositor)
3334{
3335 unsigned i;
3336 int yes;
3337
3338 weston_log("Compositor capabilities:\n");
3339 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3340 yes = compositor->capabilities & capability_strings[i].bit;
3341 weston_log_continue(STAMP_SPACE "%s %s\n",
3342 capability_strings[i].desc,
3343 yes ? "yes" : "no");
3344 }
3345}
3346
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003347static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003348{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003349 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003350
Martin Minarik6d118362012-06-07 18:01:59 +02003351 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003352 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003353
3354 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003355}
3356
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003357#ifdef HAVE_LIBUNWIND
3358
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003359static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003360print_backtrace(void)
3361{
3362 unw_cursor_t cursor;
3363 unw_context_t context;
3364 unw_word_t off;
3365 unw_proc_info_t pip;
3366 int ret, i = 0;
3367 char procname[256];
3368 const char *filename;
3369 Dl_info dlinfo;
3370
3371 pip.unwind_info = NULL;
3372 ret = unw_getcontext(&context);
3373 if (ret) {
3374 weston_log("unw_getcontext: %d\n", ret);
3375 return;
3376 }
3377
3378 ret = unw_init_local(&cursor, &context);
3379 if (ret) {
3380 weston_log("unw_init_local: %d\n", ret);
3381 return;
3382 }
3383
3384 ret = unw_step(&cursor);
3385 while (ret > 0) {
3386 ret = unw_get_proc_info(&cursor, &pip);
3387 if (ret) {
3388 weston_log("unw_get_proc_info: %d\n", ret);
3389 break;
3390 }
3391
3392 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3393 if (ret && ret != -UNW_ENOMEM) {
3394 if (ret != -UNW_EUNSPEC)
3395 weston_log("unw_get_proc_name: %d\n", ret);
3396 procname[0] = '?';
3397 procname[1] = 0;
3398 }
3399
3400 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3401 *dlinfo.dli_fname)
3402 filename = dlinfo.dli_fname;
3403 else
3404 filename = "?";
3405
3406 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3407 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3408
3409 ret = unw_step(&cursor);
3410 if (ret < 0)
3411 weston_log("unw_step: %d\n", ret);
3412 }
3413}
3414
3415#else
3416
3417static void
3418print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003419{
3420 void *buffer[32];
3421 int i, count;
3422 Dl_info info;
3423
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003424 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3425 for (i = 0; i < count; i++) {
3426 dladdr(buffer[i], &info);
3427 weston_log(" [%016lx] %s (%s)\n",
3428 (long) buffer[i],
3429 info.dli_sname ? info.dli_sname : "--",
3430 info.dli_fname);
3431 }
3432}
3433
3434#endif
3435
3436static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003437on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003438{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003439 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003440 * then call the backend restore function, which will switch
3441 * back to the vt we launched from or ungrab X etc and then
3442 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003443 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003444 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003445 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003446
Peter Maatmane5b42e42013-03-27 22:38:53 +01003447 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003448
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003449 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003450
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003451 segv_compositor->restore(segv_compositor);
3452
3453 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003454}
3455
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003456WL_EXPORT void *
3457weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003458{
3459 char path[PATH_MAX];
3460 void *module, *init;
3461
3462 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003463 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003464 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003465 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003466
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003467 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3468 if (module) {
3469 weston_log("Module '%s' already loaded\n", path);
3470 dlclose(module);
3471 return NULL;
3472 }
3473
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003474 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003475 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003476 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003477 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003478 return NULL;
3479 }
3480
3481 init = dlsym(module, entrypoint);
3482 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003483 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003484 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003485 return NULL;
3486 }
3487
3488 return init;
3489}
3490
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003491static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003492load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003493 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003494{
3495 const char *p, *end;
3496 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003497 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003498 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003499
3500 if (modules == NULL)
3501 return 0;
3502
3503 p = modules;
3504 while (*p) {
3505 end = strchrnul(p, ',');
3506 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003507 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003508 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003509 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003510 p = end;
3511 while (*p == ',')
3512 p++;
3513
3514 }
3515
3516 return 0;
3517}
3518
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003519static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003520 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3521
3522static const char xdg_wrong_message[] =
3523 "fatal: environment variable XDG_RUNTIME_DIR\n"
3524 "is set to \"%s\", which is not a directory.\n";
3525
3526static const char xdg_wrong_mode_message[] =
3527 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3528 "correctly. Unix access mode must be 0700 but is %o,\n"
3529 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003530 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003531
3532static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003533 "Refer to your distribution on how to get it, or\n"
3534 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3535 "on how to implement it.\n";
3536
Martin Minarik37032f82012-06-18 20:15:18 +02003537static void
3538verify_xdg_runtime_dir(void)
3539{
3540 char *dir = getenv("XDG_RUNTIME_DIR");
3541 struct stat s;
3542
3543 if (!dir) {
3544 weston_log(xdg_error_message);
3545 weston_log_continue(xdg_detail_message);
3546 exit(EXIT_FAILURE);
3547 }
3548
3549 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3550 weston_log(xdg_wrong_message, dir);
3551 weston_log_continue(xdg_detail_message);
3552 exit(EXIT_FAILURE);
3553 }
3554
3555 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3556 weston_log(xdg_wrong_mode_message,
3557 dir, s.st_mode & 0777, s.st_uid);
3558 weston_log_continue(xdg_detail_message);
3559 }
3560}
3561
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003562static int
3563usage(int error_code)
3564{
3565 fprintf(stderr,
3566 "Usage: weston [OPTIONS]\n\n"
3567 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3568 "Weston supports multiple backends, and depending on which backend is in use\n"
3569 "different options will be accepted.\n\n"
3570
3571
3572 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003573 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003574 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003575 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3576 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003577 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003578 " -S, --socket=NAME\tName of socket to listen on\n"
3579 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003580 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003581 " --log==FILE\t\tLog to the given file\n"
3582 " -h, --help\t\tThis help message\n\n");
3583
3584 fprintf(stderr,
3585 "Options for drm-backend.so:\n\n"
3586 " --connector=ID\tBring up only this connector\n"
3587 " --seat=SEAT\t\tThe seat that weston should run on\n"
3588 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003589 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003590 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3591
3592 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003593 "Options for fbdev-backend.so:\n\n"
3594 " --tty=TTY\t\tThe tty to use\n"
3595 " --device=DEVICE\tThe framebuffer device to use\n\n");
3596
3597 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003598 "Options for x11-backend.so:\n\n"
3599 " --width=WIDTH\t\tWidth of X window\n"
3600 " --height=HEIGHT\tHeight of X window\n"
3601 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003602 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003603 " --output-count=COUNT\tCreate multiple outputs\n"
3604 " --no-input\t\tDont create input devices\n\n");
3605
3606 fprintf(stderr,
3607 "Options for wayland-backend.so:\n\n"
3608 " --width=WIDTH\t\tWidth of Wayland surface\n"
3609 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003610 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003611 " --display=DISPLAY\tWayland display to connect to\n\n");
3612
Pekka Paalanene31e0532013-05-22 18:03:07 +03003613#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3614 fprintf(stderr,
3615 "Options for rpi-backend.so:\n\n"
3616 " --tty=TTY\t\tThe tty to use\n"
3617 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3618 " --transform=TR\tThe output transformation, TR is one of:\n"
3619 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3620 "\n");
3621#endif
3622
Hardeningc077a842013-07-08 00:51:35 +02003623#if defined(BUILD_RDP_COMPOSITOR)
3624 fprintf(stderr,
3625 "Options for rdp-backend.so:\n\n"
3626 " --width=WIDTH\t\tWidth of desktop\n"
3627 " --height=HEIGHT\tHeight of desktop\n"
3628 " --extra-modes=MODES\t\n"
3629 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3630 " --address=ADDR\tThe address to bind\n"
3631 " --port=PORT\tThe port to listen on\n"
3632 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3633 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3634 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3635 "\n");
3636#endif
3637
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003638 exit(error_code);
3639}
3640
Peter Maatmane5b42e42013-03-27 22:38:53 +01003641static void
3642catch_signals(void)
3643{
3644 struct sigaction action;
3645
3646 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3647 action.sa_sigaction = on_caught_signal;
3648 sigemptyset(&action.sa_mask);
3649 sigaction(SIGSEGV, &action, NULL);
3650 sigaction(SIGABRT, &action, NULL);
3651}
3652
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003653int main(int argc, char *argv[])
3654{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003655 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003656 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003657 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003658 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003659 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003660 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003661 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003662 int *argc, char *argv[],
3663 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003664 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003665 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003666 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003667 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003668 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003669 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003670 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003671 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003672 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003673 struct weston_config *config;
3674 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003675
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003676 const struct weston_option core_options[] = {
3677 { WESTON_OPTION_STRING, "backend", 'B', &backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003678 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003679 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3680 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003681 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003682 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003683 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003684 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003685 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003686
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003687 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003688
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003689 if (help)
3690 usage(EXIT_SUCCESS);
3691
Scott Moreau12245142012-08-29 15:15:58 -06003692 if (version) {
3693 printf(PACKAGE_STRING "\n");
3694 return EXIT_SUCCESS;
3695 }
3696
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003697 weston_log_file_open(log);
3698
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003699 weston_log("%s\n"
3700 STAMP_SPACE "%s\n"
3701 STAMP_SPACE "Bug reports to: %s\n"
3702 STAMP_SPACE "Build: %s\n",
3703 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003704 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003705 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003706
Martin Minarik37032f82012-06-18 20:15:18 +02003707 verify_xdg_runtime_dir();
3708
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003709 display = wl_display_create();
3710
Tiago Vignatti2116b892011-08-08 05:52:59 -07003711 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003712 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3713 display);
3714 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3715 display);
3716 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3717 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003718
3719 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003720 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3721 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003722
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003723 if (!backend) {
3724 if (getenv("WAYLAND_DISPLAY"))
3725 backend = "wayland-backend.so";
3726 else if (getenv("DISPLAY"))
3727 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003728 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003729 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003730 }
3731
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003732 config = weston_config_parse("weston.ini");
Alexandru DAMIAN5f429302013-09-26 10:27:16 +01003733 if (config != NULL) {
3734 weston_log("Using config file '%s'\n",
3735 weston_config_get_full_path(config));
3736 } else {
3737 weston_log("Starting with no config file.\n");
3738 }
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003739 section = weston_config_get_section(config, "core", NULL, NULL);
Jason Ekstranda985da42013-08-22 17:28:03 -05003740 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003741
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003742 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003743 if (!backend_init)
3744 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003745
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003746 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003747 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003748 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003749 exit(EXIT_FAILURE);
3750 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003751
Peter Maatmane5b42e42013-03-27 22:38:53 +01003752 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003753 segv_compositor = ec;
3754
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003755 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003756
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003757 setenv("WAYLAND_DISPLAY", socket_name, 1);
3758
Jason Ekstranda985da42013-08-22 17:28:03 -05003759 if (!shell)
3760 weston_config_section_get_string(section, "shell", &shell,
3761 "desktop-shell.so");
3762 if (load_modules(ec, shell, &argc, argv) < 0)
3763 goto out;
3764
Ossama Othmana50e6e42013-05-14 09:48:26 -07003765 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003766 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003767 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003768 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003769
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003770 for (i = 1; i < argc; i++)
3771 weston_log("fatal: unhandled option: %s\n", argv[i]);
3772 if (argc > 1) {
3773 ret = EXIT_FAILURE;
3774 goto out;
3775 }
3776
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003777 weston_compositor_log_capabilities(ec);
3778
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003779 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003780 weston_log("fatal: failed to add socket: %m\n");
3781 ret = EXIT_FAILURE;
3782 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003783 }
3784
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003785 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003786
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003787 wl_display_run(display);
3788
3789 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003790 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003791 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003792
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003793 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003794
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003795 for (i = ARRAY_LENGTH(signals); i;)
3796 wl_event_source_remove(signals[--i]);
3797
Daniel Stone855028f2012-05-01 20:37:10 +01003798 weston_compositor_xkb_destroy(ec);
3799
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003800 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003801 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003802
Martin Minarik19e6f262012-06-07 13:08:46 +02003803 weston_log_file_close();
3804
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003805 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003806}