blob: 632bbe7008ecb9c311c9926255409cad886b61c7 [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
361 if (surface->compositor->renderer->create_view &&
362 surface->compositor->renderer->create_view(view) < 0) {
363 free(view);
364 return NULL;
365 }
366
367 /* Assign to surface */
368 wl_list_insert(&surface->views, &view->surface_link);
369
370 wl_signal_init(&view->destroy_signal);
371 wl_list_init(&view->link);
372 wl_list_init(&view->layer_link);
373
Xiong Zhang97116532013-10-23 13:58:31 +0800374 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500375
376 pixman_region32_init(&view->clip);
377
378 view->alpha = 1.0;
379 pixman_region32_init(&view->transform.opaque);
380
381 wl_list_init(&view->geometry.transformation_list);
382 wl_list_insert(&view->geometry.transformation_list,
383 &view->transform.position.link);
384 weston_matrix_init(&view->transform.position.matrix);
385 wl_list_init(&view->geometry.child_list);
386 pixman_region32_init(&view->transform.boundingbox);
387 view->transform.dirty = 1;
388
389 view->output = NULL;
390
391 return view;
392}
393
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500394WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500395weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500396{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500397 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400398
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200399 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400400 if (surface == NULL)
401 return NULL;
402
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500403 wl_signal_init(&surface->destroy_signal);
404
405 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500406
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500407 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200408 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400409
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100410 if (compositor->renderer->create_surface(surface) < 0) {
411 free(surface);
412 return NULL;
413 }
414
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200415 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200416 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200417 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200418 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400419 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100420 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200421
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400422 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500423 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300424 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400425
Jason Ekstranda7af7042013-10-12 22:38:11 -0500426 wl_list_init(&surface->views);
427
428 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500429
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300430 surface->pending.buffer_destroy_listener.notify =
431 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300432 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300433 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300434 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300435 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300436
Pekka Paalanene67858b2013-04-25 13:57:42 +0300437 wl_list_init(&surface->subsurface_list);
438 wl_list_init(&surface->subsurface_list_pending);
439
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400440 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500441}
442
Alex Wu8811bf92012-02-28 18:07:54 +0800443WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500444weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200445 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500446{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100447 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500448}
449
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400450WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500451weston_view_to_global_float(struct weston_view *view,
452 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200453{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500454 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200455 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
456
Jason Ekstranda7af7042013-10-12 22:38:11 -0500457 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200458
459 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200460 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700461 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200462 v.f[3]);
463 *x = 0;
464 *y = 0;
465 return;
466 }
467
468 *x = v.f[0] / v.f[3];
469 *y = v.f[1] / v.f[3];
470 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500471 *x = sx + view->geometry.x;
472 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200473 }
474}
475
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500476WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200477weston_transformed_coord(int width, int height,
478 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200479 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200480 float sx, float sy, float *bx, float *by)
481{
482 switch (transform) {
483 case WL_OUTPUT_TRANSFORM_NORMAL:
484 default:
485 *bx = sx;
486 *by = sy;
487 break;
488 case WL_OUTPUT_TRANSFORM_FLIPPED:
489 *bx = width - sx;
490 *by = sy;
491 break;
492 case WL_OUTPUT_TRANSFORM_90:
493 *bx = height - sy;
494 *by = sx;
495 break;
496 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
497 *bx = height - sy;
498 *by = width - sx;
499 break;
500 case WL_OUTPUT_TRANSFORM_180:
501 *bx = width - sx;
502 *by = height - sy;
503 break;
504 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
505 *bx = sx;
506 *by = height - sy;
507 break;
508 case WL_OUTPUT_TRANSFORM_270:
509 *bx = sy;
510 *by = width - sx;
511 break;
512 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
513 *bx = sy;
514 *by = sx;
515 break;
516 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200517
518 *bx *= scale;
519 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200520}
521
522WL_EXPORT pixman_box32_t
523weston_transformed_rect(int width, int height,
524 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200525 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200526 pixman_box32_t rect)
527{
528 float x1, x2, y1, y2;
529
530 pixman_box32_t ret;
531
Alexander Larsson4ea95522013-05-22 14:41:37 +0200532 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200533 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200534 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200535 rect.x2, rect.y2, &x2, &y2);
536
537 if (x1 <= x2) {
538 ret.x1 = x1;
539 ret.x2 = x2;
540 } else {
541 ret.x1 = x2;
542 ret.x2 = x1;
543 }
544
545 if (y1 <= y2) {
546 ret.y1 = y1;
547 ret.y2 = y2;
548 } else {
549 ret.y1 = y2;
550 ret.y2 = y1;
551 }
552
553 return ret;
554}
555
556WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200557weston_surface_to_buffer_float(struct weston_surface *surface,
558 float sx, float sy, float *bx, float *by)
559{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500560 weston_transformed_coord(surface->width,
561 surface->height,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200562 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200563 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200564 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200565}
566
Alexander Larsson4ea95522013-05-22 14:41:37 +0200567WL_EXPORT void
568weston_surface_to_buffer(struct weston_surface *surface,
569 int sx, int sy, int *bx, int *by)
570{
571 float bxf, byf;
572
Jason Ekstranda7af7042013-10-12 22:38:11 -0500573 weston_transformed_coord(surface->width,
574 surface->height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200575 surface->buffer_transform,
576 surface->buffer_scale,
577 sx, sy, &bxf, &byf);
578 *bx = floorf(bxf);
579 *by = floorf(byf);
580}
581
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200582WL_EXPORT pixman_box32_t
583weston_surface_to_buffer_rect(struct weston_surface *surface,
584 pixman_box32_t rect)
585{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586 return weston_transformed_rect(surface->width,
587 surface->height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200588 surface->buffer_transform,
589 surface->buffer_scale,
590 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200591}
592
593WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500594weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400595 struct weston_plane *plane)
596{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500597 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400598 return;
599
Jason Ekstranda7af7042013-10-12 22:38:11 -0500600 weston_view_damage_below(view);
601 view->plane = plane;
602 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400603}
604
605WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500606weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200607{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500608 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200609
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500610 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500611 pixman_region32_subtract(&damage, &view->transform.boundingbox,
612 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800613 if (view->plane)
614 pixman_region32_union(&view->plane->damage,
615 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500616 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200617}
618
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400619static void
620weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
621{
622 uint32_t different = es->output_mask ^ mask;
623 uint32_t entered = mask & different;
624 uint32_t left = es->output_mask & different;
625 struct weston_output *output;
626 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500627 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400628
629 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500630 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400631 return;
632 if (different == 0)
633 return;
634
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500635 client = wl_resource_get_client(es->resource);
636
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400637 wl_list_for_each(output, &es->compositor->output_list, link) {
638 if (1 << output->id & different)
639 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500640 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400641 client);
642 if (resource == NULL)
643 continue;
644 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500645 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400646 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500647 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400648 }
649}
650
651static void
652weston_surface_assign_output(struct weston_surface *es)
653{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500654 struct weston_output *new_output;
655 struct weston_view *view;
656 pixman_region32_t region;
657 uint32_t max, area, mask;
658 pixman_box32_t *e;
659
660 new_output = NULL;
661 max = 0;
662 mask = 0;
663 pixman_region32_init(&region);
664 wl_list_for_each(view, &es->views, surface_link) {
665 if (!view->output)
666 continue;
667
668 pixman_region32_intersect(&region, &view->transform.boundingbox,
669 &view->output->region);
670
671 e = pixman_region32_extents(&region);
672 area = (e->x2 - e->x1) * (e->y2 - e->y1);
673
674 mask |= view->output_mask;
675
676 if (area >= max) {
677 new_output = view->output;
678 max = area;
679 }
680 }
681 pixman_region32_fini(&region);
682
683 es->output = new_output;
684 weston_surface_update_output_mask(es, mask);
685}
686
687static void
688weston_view_assign_output(struct weston_view *ev)
689{
690 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400691 struct weston_output *output, *new_output;
692 pixman_region32_t region;
693 uint32_t max, area, mask;
694 pixman_box32_t *e;
695
696 new_output = NULL;
697 max = 0;
698 mask = 0;
699 pixman_region32_init(&region);
700 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500701 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400702 &output->region);
703
704 e = pixman_region32_extents(&region);
705 area = (e->x2 - e->x1) * (e->y2 - e->y1);
706
707 if (area > 0)
708 mask |= 1 << output->id;
709
710 if (area >= max) {
711 new_output = output;
712 max = area;
713 }
714 }
715 pixman_region32_fini(&region);
716
Jason Ekstranda7af7042013-10-12 22:38:11 -0500717 ev->output = new_output;
718 ev->output_mask = mask;
719
720 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400721}
722
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200723static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500724view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
725 int32_t width, int32_t height,
726 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200727{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200728 float min_x = HUGE_VALF, min_y = HUGE_VALF;
729 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200730 int32_t s[4][2] = {
731 { sx, sy },
732 { sx, sy + height },
733 { sx + width, sy },
734 { sx + width, sy + height }
735 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200736 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200737 int i;
738
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300739 if (width == 0 || height == 0) {
740 /* avoid rounding empty bbox to 1x1 */
741 pixman_region32_init(bbox);
742 return;
743 }
744
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200745 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200746 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500747 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200748 if (x < min_x)
749 min_x = x;
750 if (x > max_x)
751 max_x = x;
752 if (y < min_y)
753 min_y = y;
754 if (y > max_y)
755 max_y = y;
756 }
757
Pekka Paalanen219b9822012-02-08 15:38:37 +0200758 int_x = floorf(min_x);
759 int_y = floorf(min_y);
760 pixman_region32_init_rect(bbox, int_x, int_y,
761 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200762}
763
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200764static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500765weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200766{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500767 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200768
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200769 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500770 view->geometry.x = roundf(view->geometry.x);
771 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200772
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500773 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500774 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
775 view->transform.position.matrix.d[12] = view->geometry.x;
776 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500777
Jason Ekstranda7af7042013-10-12 22:38:11 -0500778 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500779
Jason Ekstranda7af7042013-10-12 22:38:11 -0500780 view->transform.inverse = view->transform.position.matrix;
781 view->transform.inverse.d[12] = -view->geometry.x;
782 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500783
Jason Ekstranda7af7042013-10-12 22:38:11 -0500784 pixman_region32_init_rect(&view->transform.boundingbox,
785 view->geometry.x,
786 view->geometry.y,
787 view->geometry.width,
788 view->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500789
Jason Ekstranda7af7042013-10-12 22:38:11 -0500790 if (view->alpha == 1.0) {
791 pixman_region32_copy(&view->transform.opaque,
792 &view->surface->opaque);
793 pixman_region32_translate(&view->transform.opaque,
794 view->geometry.x,
795 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500796 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200797}
798
799static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500800weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200801{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500802 struct weston_view *parent = view->geometry.parent;
803 struct weston_matrix *matrix = &view->transform.matrix;
804 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200805 struct weston_transform *tform;
806
Jason Ekstranda7af7042013-10-12 22:38:11 -0500807 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200808
809 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500810 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
811 view->transform.position.matrix.d[12] = view->geometry.x;
812 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200813
814 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500815 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200816 weston_matrix_multiply(matrix, &tform->matrix);
817
Pekka Paalanen483243f2013-03-08 14:56:50 +0200818 if (parent)
819 weston_matrix_multiply(matrix, &parent->transform.matrix);
820
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200821 if (weston_matrix_invert(inverse, matrix) < 0) {
822 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500823 weston_log("error: weston_view %p"
824 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200825 return -1;
826 }
827
Jason Ekstranda7af7042013-10-12 22:38:11 -0500828 view_compute_bbox(view, 0, 0, view->geometry.width,
829 view->geometry.height,
830 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500831
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200832 return 0;
833}
834
835WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500836weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200837{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500838 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200839
Jason Ekstranda7af7042013-10-12 22:38:11 -0500840 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200841 return;
842
Pekka Paalanen483243f2013-03-08 14:56:50 +0200843 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500844 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200845
Jason Ekstranda7af7042013-10-12 22:38:11 -0500846 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200847
Jason Ekstranda7af7042013-10-12 22:38:11 -0500848 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200849
Jason Ekstranda7af7042013-10-12 22:38:11 -0500850 pixman_region32_fini(&view->transform.boundingbox);
851 pixman_region32_fini(&view->transform.opaque);
852 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500853
Pekka Paalanencd403622012-01-25 13:37:39 +0200854 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500855 if (view->geometry.transformation_list.next ==
856 &view->transform.position.link &&
857 view->geometry.transformation_list.prev ==
858 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200859 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500860 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200861 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500862 if (weston_view_update_transform_enable(view) < 0)
863 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200864 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200865
Jason Ekstranda7af7042013-10-12 22:38:11 -0500866 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200867
Jason Ekstranda7af7042013-10-12 22:38:11 -0500868 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300869
Jason Ekstranda7af7042013-10-12 22:38:11 -0500870 wl_signal_emit(&view->surface->compositor->transform_signal,
871 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200872}
873
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200874WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500875weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200876{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500877 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200878
879 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -0500880 * The invariant: if view->geometry.dirty, then all views
881 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +0200882 * Corollary: if not parent->geometry.dirty, then all ancestors
883 * are not dirty.
884 */
885
Jason Ekstranda7af7042013-10-12 22:38:11 -0500886 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +0200887 return;
888
Jason Ekstranda7af7042013-10-12 22:38:11 -0500889 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200890
Jason Ekstranda7af7042013-10-12 22:38:11 -0500891 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200892 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500893 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200894}
895
896WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500897weston_view_to_global_fixed(struct weston_view *view,
898 wl_fixed_t vx, wl_fixed_t vy,
899 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +0100900{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200901 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100902
Jason Ekstranda7af7042013-10-12 22:38:11 -0500903 weston_view_to_global_float(view,
904 wl_fixed_to_double(vx),
905 wl_fixed_to_double(vy),
906 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100907 *x = wl_fixed_from_double(xf);
908 *y = wl_fixed_from_double(yf);
909}
910
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400911WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500912weston_view_from_global_float(struct weston_view *view,
913 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200914{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500915 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200916 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
917
Jason Ekstranda7af7042013-10-12 22:38:11 -0500918 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200919
920 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200921 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -0500922 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200923 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500924 *vx = 0;
925 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200926 return;
927 }
928
Jason Ekstranda7af7042013-10-12 22:38:11 -0500929 *vx = v.f[0] / v.f[3];
930 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200931 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932 *vx = x - view->geometry.x;
933 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200934 }
935}
936
937WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938weston_view_from_global_fixed(struct weston_view *view,
939 wl_fixed_t x, wl_fixed_t y,
940 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +0100941{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500942 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100943
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944 weston_view_from_global_float(view,
945 wl_fixed_to_double(x),
946 wl_fixed_to_double(y),
947 &vxf, &vyf);
948 *vx = wl_fixed_from_double(vxf);
949 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100950}
951
952WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500953weston_view_from_global(struct weston_view *view,
954 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200955{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500956 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200957
Jason Ekstranda7af7042013-10-12 22:38:11 -0500958 weston_view_from_global_float(view, x, y, &vxf, &vyf);
959 *vx = floorf(vxf);
960 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200961}
962
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400963WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400964weston_surface_schedule_repaint(struct weston_surface *surface)
965{
966 struct weston_output *output;
967
968 wl_list_for_each(output, &surface->compositor->output_list, link)
969 if (surface->output_mask & (1 << output->id))
970 weston_output_schedule_repaint(output);
971}
972
973WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500974weston_view_schedule_repaint(struct weston_view *view)
975{
976 struct weston_output *output;
977
978 wl_list_for_each(output, &view->surface->compositor->output_list, link)
979 if (view->output_mask & (1 << output->id))
980 weston_output_schedule_repaint(output);
981}
982
983WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500984weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500985{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400986 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 0, 0, surface->width,
988 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200989
Kristian Høgsberg98238702012-08-03 16:29:12 -0400990 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500991}
992
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400993WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994weston_view_configure(struct weston_view *view,
995 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400996{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 view->geometry.x = x;
998 view->geometry.y = y;
999 view->geometry.width = width;
1000 view->geometry.height = height;
1001 weston_view_geometry_dirty(view);
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001002}
1003
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001004WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001006{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007 view->geometry.x = x;
1008 view->geometry.y = y;
1009 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001010}
1011
Pekka Paalanen483243f2013-03-08 14:56:50 +02001012static void
1013transform_parent_handle_parent_destroy(struct wl_listener *listener,
1014 void *data)
1015{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016 struct weston_view *view =
1017 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001018 geometry.parent_destroy_listener);
1019
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001021}
1022
1023WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001024weston_view_set_transform_parent(struct weston_view *view,
1025 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001026{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 if (view->geometry.parent) {
1028 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1029 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001030 }
1031
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001033
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001035 transform_parent_handle_parent_destroy;
1036 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001037 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001039 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001041 }
1042
Jason Ekstranda7af7042013-10-12 22:38:11 -05001043 weston_view_geometry_dirty(view);
1044}
1045
1046WL_EXPORT int
1047weston_view_is_mapped(struct weston_view *view)
1048{
1049 if (view->output)
1050 return 1;
1051 else
1052 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001053}
1054
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001055WL_EXPORT int
1056weston_surface_is_mapped(struct weston_surface *surface)
1057{
1058 if (surface->output)
1059 return 1;
1060 else
1061 return 0;
1062}
1063
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001064WL_EXPORT int32_t
1065weston_surface_buffer_width(struct weston_surface *surface)
1066{
Alexander Larsson4ea95522013-05-22 14:41:37 +02001067 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001068 switch (surface->buffer_transform) {
1069 case WL_OUTPUT_TRANSFORM_90:
1070 case WL_OUTPUT_TRANSFORM_270:
1071 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1072 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001073 width = surface->buffer_ref.buffer->height;
1074 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001075 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001076 width = surface->buffer_ref.buffer->width;
1077 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001078 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001079 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001080}
1081
1082WL_EXPORT int32_t
1083weston_surface_buffer_height(struct weston_surface *surface)
1084{
Alexander Larsson4ea95522013-05-22 14:41:37 +02001085 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001086 switch (surface->buffer_transform) {
1087 case WL_OUTPUT_TRANSFORM_90:
1088 case WL_OUTPUT_TRANSFORM_270:
1089 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1090 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001091 height = surface->buffer_ref.buffer->width;
1092 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001093 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001094 height = surface->buffer_ref.buffer->height;
1095 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001096 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001097 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001098}
1099
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001100WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001101weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001102{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001103 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001104
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001105 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001106
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001107 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001108}
1109
Jason Ekstranda7af7042013-10-12 22:38:11 -05001110WL_EXPORT struct weston_view *
1111weston_compositor_pick_view(struct weston_compositor *compositor,
1112 wl_fixed_t x, wl_fixed_t y,
1113 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001114{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001116
Jason Ekstranda7af7042013-10-12 22:38:11 -05001117 wl_list_for_each(view, &compositor->view_list, link) {
1118 weston_view_from_global_fixed(view, x, y, vx, vy);
1119 if (pixman_region32_contains_point(&view->surface->input,
1120 wl_fixed_to_int(*vx),
1121 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001122 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001124 }
1125
1126 return NULL;
1127}
1128
1129static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001130weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001131{
Daniel Stone37816df2012-05-16 18:45:18 +01001132 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001133
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001134 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001135 return;
1136
Daniel Stone37816df2012-05-16 18:45:18 +01001137 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001138 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001139}
1140
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001141WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001142weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001143{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001144 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001145
Jason Ekstranda7af7042013-10-12 22:38:11 -05001146 if (!weston_view_is_mapped(view))
1147 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001148
Jason Ekstranda7af7042013-10-12 22:38:11 -05001149 weston_view_damage_below(view);
1150 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001151 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 wl_list_remove(&view->layer_link);
1153 wl_list_init(&view->layer_link);
1154 wl_list_remove(&view->link);
1155 wl_list_init(&view->link);
1156 /* We need to do this before torching the output mask */
1157 weston_view_schedule_repaint(view);
1158 view->output_mask = 0;
1159 weston_surface_assign_output(view->surface);
1160
1161 if (weston_surface_is_mapped(view->surface))
1162 return;
1163
1164 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1165 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001166 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001168 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001169 NULL,
1170 wl_fixed_from_int(0),
1171 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001173 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001174 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001176
Jason Ekstranda7af7042013-10-12 22:38:11 -05001177WL_EXPORT void
1178weston_surface_unmap(struct weston_surface *surface)
1179{
1180 struct weston_view *view;
1181
1182 wl_list_for_each(view, &surface->views, surface_link)
1183 weston_view_unmap(view);
1184 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001185}
1186
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001187struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001188 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001189 struct wl_list link;
1190};
1191
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192WL_EXPORT void
1193weston_view_destroy(struct weston_view *view)
1194{
1195 wl_signal_emit(&view->destroy_signal, view);
1196
1197 assert(wl_list_empty(&view->geometry.child_list));
1198
1199 if (weston_view_is_mapped(view)) {
1200 weston_view_unmap(view);
1201 weston_compositor_build_view_list(view->surface->compositor);
1202 }
1203
1204 wl_list_remove(&view->link);
1205 wl_list_remove(&view->layer_link);
1206
1207 pixman_region32_fini(&view->clip);
1208 pixman_region32_fini(&view->transform.boundingbox);
1209
1210 weston_view_set_transform_parent(view, NULL);
1211
1212 if (view->surface->compositor->renderer->destroy_view)
1213 view->surface->compositor->renderer->destroy_view(view);
1214
1215 wl_list_remove(&view->surface_link);
1216
1217 free(view);
1218}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001219
1220WL_EXPORT void
1221weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001222{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001223 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001224 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001225 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001226
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001227 if (--surface->ref_count > 0)
1228 return;
1229
1230 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1231
Pekka Paalanene67858b2013-04-25 13:57:42 +03001232 assert(wl_list_empty(&surface->subsurface_list_pending));
1233 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001234
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1236 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001237
Pekka Paalanenbc106382012-10-10 12:49:31 +03001238 wl_list_for_each_safe(cb, next,
1239 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001240 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001241
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001242 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001243 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001244 pixman_region32_fini(&surface->pending.damage);
1245
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001246 if (surface->pending.buffer)
1247 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1248
Pekka Paalanende685b82012-12-04 15:58:12 +02001249 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001250
Kristian Høgsberg42263852012-09-06 21:59:29 -04001251 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001252
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001253 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001254 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001255 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001256
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001257 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001258 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001259
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001260 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001261}
1262
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001263static void
1264destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001265{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001266 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001267
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001268 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001269}
1270
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001271static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001272weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1273{
1274 struct weston_buffer *buffer =
1275 container_of(listener, struct weston_buffer, destroy_listener);
1276
1277 wl_signal_emit(&buffer->destroy_signal, buffer);
1278 free(buffer);
1279}
1280
1281struct weston_buffer *
1282weston_buffer_from_resource(struct wl_resource *resource)
1283{
1284 struct weston_buffer *buffer;
1285 struct wl_listener *listener;
1286
1287 listener = wl_resource_get_destroy_listener(resource,
1288 weston_buffer_destroy_handler);
1289
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001290 if (listener)
1291 return container_of(listener, struct weston_buffer,
1292 destroy_listener);
1293
1294 buffer = zalloc(sizeof *buffer);
1295 if (buffer == NULL)
1296 return NULL;
1297
1298 buffer->resource = resource;
1299 wl_signal_init(&buffer->destroy_signal);
1300 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001301 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001302 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001303
1304 return buffer;
1305}
1306
1307static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001308weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1309 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001310{
Pekka Paalanende685b82012-12-04 15:58:12 +02001311 struct weston_buffer_reference *ref =
1312 container_of(listener, struct weston_buffer_reference,
1313 destroy_listener);
1314
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001315 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001316 ref->buffer = NULL;
1317}
1318
1319WL_EXPORT void
1320weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001321 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001322{
1323 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001324 ref->buffer->busy_count--;
1325 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001326 assert(wl_resource_get_client(ref->buffer->resource));
1327 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001328 WL_BUFFER_RELEASE);
1329 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001330 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001331 }
1332
Pekka Paalanende685b82012-12-04 15:58:12 +02001333 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001334 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001335 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001336 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001337 }
1338
Pekka Paalanende685b82012-12-04 15:58:12 +02001339 ref->buffer = buffer;
1340 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1341}
1342
1343static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001344weston_surface_attach(struct weston_surface *surface,
1345 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001346{
1347 weston_buffer_reference(&surface->buffer_ref, buffer);
1348
Pekka Paalanena6421c42012-12-04 15:58:10 +02001349 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001350 if (weston_surface_is_mapped(surface))
1351 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001352 }
1353
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001354 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001355}
1356
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001357WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001358weston_view_restack(struct weston_view *view, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001359{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 wl_list_remove(&view->layer_link);
1361 wl_list_insert(below, &view->layer_link);
1362 weston_view_damage_below(view);
1363 weston_surface_damage(view->surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001364}
1365
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001366WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001367weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001368{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001369 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001370
1371 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001372 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001373}
1374
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001375WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001376weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001377{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001378 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001379
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001380 pixman_region32_union(&compositor->primary_plane.damage,
1381 &compositor->primary_plane.damage,
1382 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001383 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001384}
1385
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001386static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001388{
Pekka Paalanende685b82012-12-04 15:58:12 +02001389 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001390 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001391 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001392
Jason Ekstranda7af7042013-10-12 22:38:11 -05001393 empty_region(&surface->damage);
1394}
1395
1396static void
1397view_accumulate_damage(struct weston_view *view,
1398 pixman_region32_t *opaque)
1399{
1400 pixman_region32_t damage;
1401
1402 pixman_region32_init(&damage);
1403 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001404 pixman_box32_t *extents;
1405
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 extents = pixman_region32_extents(&view->surface->damage);
1407 view_compute_bbox(view, extents->x1, extents->y1,
1408 extents->x2 - extents->x1,
1409 extents->y2 - extents->y1,
1410 &damage);
1411 pixman_region32_translate(&damage,
1412 -view->plane->x,
1413 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001414 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415 pixman_region32_copy(&damage, &view->surface->damage);
1416 pixman_region32_translate(&damage,
1417 view->geometry.x - view->plane->x,
1418 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001419 }
1420
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 pixman_region32_subtract(&damage, &damage, opaque);
1422 pixman_region32_union(&view->plane->damage,
1423 &view->plane->damage, &damage);
1424 pixman_region32_fini(&damage);
1425 pixman_region32_copy(&view->clip, opaque);
1426 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001427}
1428
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001429static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001430compositor_accumulate_damage(struct weston_compositor *ec)
1431{
1432 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001434 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001435
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001436 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001437
1438 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001439 pixman_region32_copy(&plane->clip, &clip);
1440
1441 pixman_region32_init(&opaque);
1442
Jason Ekstranda7af7042013-10-12 22:38:11 -05001443 wl_list_for_each(ev, &ec->view_list, link) {
1444 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001445 continue;
1446
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001448 }
1449
1450 pixman_region32_union(&clip, &clip, &opaque);
1451 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001452 }
1453
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001454 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001455
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 wl_list_for_each(ev, &ec->view_list, link)
1457 ev->surface->touched = 0;
1458
1459 wl_list_for_each(ev, &ec->view_list, link) {
1460 if (ev->surface->touched)
1461 continue;
1462 ev->surface->touched = 1;
1463
1464 surface_flush_damage(ev->surface);
1465
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001466 /* Both the renderer and the backend have seen the buffer
1467 * by now. If renderer needs the buffer, it has its own
1468 * reference set. If the backend wants to keep the buffer
1469 * around for migrating the surface into a non-primary plane
1470 * later, keep_buffer is true. Otherwise, drop the core
1471 * reference now, and allow early buffer release. This enables
1472 * clients to use single-buffering.
1473 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474 if (!ev->surface->keep_buffer)
1475 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001476 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001477}
1478
1479static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001481{
1482 struct weston_subsurface *sub;
1483
Pekka Paalanene67858b2013-04-25 13:57:42 +03001484 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001485 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001486 continue;
1487
Jason Ekstranda7af7042013-10-12 22:38:11 -05001488 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1489 wl_list_init(&sub->surface->views);
1490
1491 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001492 }
1493}
1494
1495static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001497{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 struct weston_subsurface *sub;
1499 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001500
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1502 if (sub->surface == surface)
1503 continue;
1504
1505 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1506 weston_view_destroy(view);
1507
1508 surface_free_unused_subsurface_views(sub->surface);
1509 }
1510}
1511
1512static void
1513view_list_add_subsurface_view(struct weston_compositor *compositor,
1514 struct weston_subsurface *sub,
1515 struct weston_view *parent)
1516{
1517 struct weston_subsurface *child;
1518 struct weston_view *view = NULL, *iv;
1519
1520 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1521 if (iv->geometry.parent == parent) {
1522 view = iv;
1523 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001524 }
1525 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001526
1527 if (view) {
1528 /* Put it back in the surface's list of views */
1529 wl_list_remove(&view->surface_link);
1530 wl_list_insert(&sub->surface->views, &view->surface_link);
1531 } else {
1532 view = weston_view_create(sub->surface);
1533 weston_view_configure(view,
1534 sub->position.x,
1535 sub->position.y,
1536 sub->surface->width,
1537 sub->surface->height);
1538 weston_view_set_transform_parent(view, parent);
1539 }
1540
1541 weston_view_update_transform(view);
1542 wl_list_insert(compositor->view_list.next, &view->link);
1543
1544 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
1545 if (child->surface != sub->surface)
1546 view_list_add_subsurface_view(compositor, child, view);
1547}
1548
1549static void
1550view_list_add(struct weston_compositor *compositor,
1551 struct weston_view *view)
1552{
1553 struct weston_subsurface *sub;
1554
1555 weston_view_update_transform(view);
1556 wl_list_insert(compositor->view_list.prev, &view->link);
1557
1558 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link)
1559 if (sub->surface != view->surface)
1560 view_list_add_subsurface_view(compositor, sub, view);
1561}
1562
1563static void
1564weston_compositor_build_view_list(struct weston_compositor *compositor)
1565{
1566 struct weston_view *view;
1567 struct weston_layer *layer;
1568
1569 wl_list_for_each(layer, &compositor->layer_list, link)
1570 wl_list_for_each(view, &layer->view_list, layer_link)
1571 surface_stash_subsurface_views(view->surface);
1572
1573 wl_list_init(&compositor->view_list);
1574 wl_list_for_each(layer, &compositor->layer_list, link) {
1575 wl_list_for_each(view, &layer->view_list, layer_link) {
1576 view_list_add(compositor, view);
1577 }
1578 }
1579
1580 wl_list_for_each(layer, &compositor->layer_list, link)
1581 wl_list_for_each(view, &layer->view_list, layer_link)
1582 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001583}
1584
David Herrmann1edf44c2013-10-22 17:11:26 +02001585static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001586weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001587{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001588 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001590 struct weston_animation *animation, *next;
1591 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001592 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001593 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001594 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001595
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001596 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001598
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001599 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001600 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001601 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001602 wl_list_for_each(ev, &ec->view_list, link)
1603 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001604
Pekka Paalanene67858b2013-04-25 13:57:42 +03001605 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001606 wl_list_for_each(ev, &ec->view_list, link) {
1607 /* Note: This operation is safe to do multiple times on the
1608 * same surface.
1609 */
1610 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001611 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001612 &ev->surface->frame_callback_list);
1613 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001614 }
1615 }
1616
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001617 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001618
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001619 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001620 pixman_region32_intersect(&output_damage,
1621 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001622 pixman_region32_subtract(&output_damage,
1623 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001624
Scott Moreauccbf29d2012-02-22 14:21:41 -07001625 if (output->dirty)
1626 weston_output_update_matrix(output);
1627
David Herrmann1edf44c2013-10-22 17:11:26 +02001628 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001629
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001630 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001631
Kristian Høgsbergef044142011-06-21 15:02:12 -04001632 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001633
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001634 weston_compositor_repick(ec);
1635 wl_event_loop_dispatch(ec->input_loop, 0);
1636
Jonas Ådahldb773762012-06-13 00:01:21 +02001637 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001638 wl_callback_send_done(cb->resource, msecs);
1639 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001640 }
1641
Scott Moreaud64cf212012-06-08 19:40:54 -06001642 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001643 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001644 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001645 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001646
1647 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001648}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001649
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001650static int
1651weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001652{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001653 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001654
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001655 wl_event_loop_dispatch(compositor->input_loop, 0);
1656
1657 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001658}
1659
1660WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001661weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001662{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001663 struct weston_compositor *compositor = output->compositor;
1664 struct wl_event_loop *loop =
1665 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001666 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001667
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001668 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001669
1670 if (output->repaint_needed &&
1671 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1672 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001673 r = weston_output_repaint(output, msecs);
1674 if (!r)
1675 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001676 }
1677
1678 output->repaint_scheduled = 0;
1679 if (compositor->input_loop_source)
1680 return;
1681
1682 fd = wl_event_loop_get_fd(compositor->input_loop);
1683 compositor->input_loop_source =
1684 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1685 weston_compositor_read_input, compositor);
1686}
1687
1688static void
1689idle_repaint(void *data)
1690{
1691 struct weston_output *output = data;
1692
Jonas Ådahle5a12252013-04-05 23:07:11 +02001693 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001694}
1695
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001696WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001697weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1698{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001699 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001700 if (below != NULL)
1701 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001702}
1703
1704WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001705weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001706{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001707 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001708 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001709
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001710 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1711 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001712 return;
1713
Kristian Høgsbergef044142011-06-21 15:02:12 -04001714 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001715 output->repaint_needed = 1;
1716 if (output->repaint_scheduled)
1717 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001718
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001719 wl_event_loop_add_idle(loop, idle_repaint, output);
1720 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001721
1722 if (compositor->input_loop_source) {
1723 wl_event_source_remove(compositor->input_loop_source);
1724 compositor->input_loop_source = NULL;
1725 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001726}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001727
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001728WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001729weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1730{
1731 struct weston_output *output;
1732
1733 wl_list_for_each(output, &compositor->output_list, link)
1734 weston_output_schedule_repaint(output);
1735}
1736
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001737static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001738surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001739{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001740 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001741}
1742
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001743static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001744surface_attach(struct wl_client *client,
1745 struct wl_resource *resource,
1746 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1747{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001748 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001749 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001750
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001751 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001752 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001753 if (buffer == NULL) {
1754 wl_client_post_no_memory(client);
1755 return;
1756 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001757 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001758
Pekka Paalanende685b82012-12-04 15:58:12 +02001759 /* Attach, attach, without commit in between does not send
1760 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001761 if (surface->pending.buffer)
1762 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001763
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001764 surface->pending.sx = sx;
1765 surface->pending.sy = sy;
1766 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001767 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001768 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001769 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001770 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001771 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001772}
1773
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001774static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001775surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001776 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001777 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001778{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001779 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001780
Pekka Paalanen8e159182012-10-10 12:49:25 +03001781 pixman_region32_union_rect(&surface->pending.damage,
1782 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001783 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001784}
1785
Kristian Høgsberg33418202011-08-16 23:01:28 -04001786static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001787destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001788{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001789 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001790
1791 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001792 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001793}
1794
1795static void
1796surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001797 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001798{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001799 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001800 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001801
1802 cb = malloc(sizeof *cb);
1803 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001804 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001805 return;
1806 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001807
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001808 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1809 callback);
1810 if (cb->resource == NULL) {
1811 free(cb);
1812 wl_resource_post_no_memory(resource);
1813 return;
1814 }
1815
Jason Ekstranda85118c2013-06-27 20:17:02 -05001816 wl_resource_set_implementation(cb->resource, NULL, cb,
1817 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001818
Pekka Paalanenbc106382012-10-10 12:49:31 +03001819 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001820}
1821
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001822static void
1823surface_set_opaque_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øgsberg5e7e6f22012-02-23 16:11:59 -05001828 struct weston_region *region;
1829
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001830 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001831 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001832 pixman_region32_copy(&surface->pending.opaque,
1833 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001834 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001835 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001836 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001837}
1838
1839static void
1840surface_set_input_region(struct wl_client *client,
1841 struct wl_resource *resource,
1842 struct wl_resource *region_resource)
1843{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001844 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001845 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001846
1847 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001848 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001849 pixman_region32_copy(&surface->pending.input,
1850 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001851 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001852 pixman_region32_fini(&surface->pending.input);
1853 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001854 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001855}
1856
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001857static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001858weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001859{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001860 struct weston_subsurface *sub;
1861
1862 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1863 parent_link_pending) {
1864 wl_list_remove(&sub->parent_link);
1865 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1866 }
1867}
1868
1869static void
1870weston_surface_commit(struct weston_surface *surface)
1871{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001872 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001873 pixman_region32_t opaque;
1874
Alexander Larsson4ea95522013-05-22 14:41:37 +02001875 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001876 surface->buffer_transform = surface->pending.buffer_transform;
1877
Alexander Larsson4ea95522013-05-22 14:41:37 +02001878 /* wl_surface.set_buffer_scale */
1879 surface->buffer_scale = surface->pending.buffer_scale;
1880
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001881 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001882 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001883 weston_surface_attach(surface, surface->pending.buffer);
1884
Jason Ekstranda7af7042013-10-12 22:38:11 -05001885 surface->width = 0;
1886 surface->height = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001887 if (surface->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001888 /* This already includes the buffer scale */
1889 surface->width = weston_surface_buffer_width(surface);
1890 surface->height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001891 }
1892
1893 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001894 surface->configure(surface,
1895 surface->pending.sx, surface->pending.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001896 surface->width, surface->height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001897
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001898 if (surface->pending.buffer)
1899 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1900 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001901 surface->pending.sx = 0;
1902 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001903 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001904
1905 /* wl_surface.damage */
1906 pixman_region32_union(&surface->damage, &surface->damage,
1907 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001908 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1909 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001910 surface->width,
1911 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001912 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001913
1914 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001915 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001916 surface->width,
1917 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001918 pixman_region32_intersect(&opaque,
1919 &opaque, &surface->pending.opaque);
1920
1921 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1922 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001923 wl_list_for_each(view, &surface->views, surface_link)
1924 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001925 }
1926
1927 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001928
1929 /* wl_surface.set_input_region */
1930 pixman_region32_fini(&surface->input);
1931 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001932 surface->width,
1933 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001934 pixman_region32_intersect(&surface->input,
1935 &surface->input, &surface->pending.input);
1936
Pekka Paalanenbc106382012-10-10 12:49:31 +03001937 /* wl_surface.frame */
1938 wl_list_insert_list(&surface->frame_callback_list,
1939 &surface->pending.frame_callback_list);
1940 wl_list_init(&surface->pending.frame_callback_list);
1941
Pekka Paalanene67858b2013-04-25 13:57:42 +03001942 weston_surface_commit_subsurface_order(surface);
1943
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001944 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001945}
1946
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001947static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001948weston_subsurface_commit(struct weston_subsurface *sub);
1949
1950static void
1951weston_subsurface_parent_commit(struct weston_subsurface *sub,
1952 int parent_is_synchronized);
1953
1954static void
1955surface_commit(struct wl_client *client, struct wl_resource *resource)
1956{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001957 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001958 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1959
1960 if (sub) {
1961 weston_subsurface_commit(sub);
1962 return;
1963 }
1964
1965 weston_surface_commit(surface);
1966
1967 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1968 if (sub->surface != surface)
1969 weston_subsurface_parent_commit(sub, 0);
1970 }
1971}
1972
1973static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001974surface_set_buffer_transform(struct wl_client *client,
1975 struct wl_resource *resource, int transform)
1976{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001977 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001978
1979 surface->pending.buffer_transform = transform;
1980}
1981
Alexander Larsson4ea95522013-05-22 14:41:37 +02001982static void
1983surface_set_buffer_scale(struct wl_client *client,
1984 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001985 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001986{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001987 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001988
1989 surface->pending.buffer_scale = scale;
1990}
1991
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001992static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001993 surface_destroy,
1994 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001995 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001996 surface_frame,
1997 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001998 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001999 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002000 surface_set_buffer_transform,
2001 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002002};
2003
2004static void
2005compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002006 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002007{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002008 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002009 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002010
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002011 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002012 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002013 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002014 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002015 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002016
Jason Ekstranda85118c2013-06-27 20:17:02 -05002017 surface->resource =
2018 wl_resource_create(client, &wl_surface_interface,
2019 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002020 if (surface->resource == NULL) {
2021 weston_surface_destroy(surface);
2022 wl_resource_post_no_memory(resource);
2023 return;
2024 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002025 wl_resource_set_implementation(surface->resource, &surface_interface,
2026 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002027}
2028
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002029static void
2030destroy_region(struct wl_resource *resource)
2031{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002032 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002033
2034 pixman_region32_fini(&region->region);
2035 free(region);
2036}
2037
2038static void
2039region_destroy(struct wl_client *client, struct wl_resource *resource)
2040{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002041 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002042}
2043
2044static void
2045region_add(struct wl_client *client, struct wl_resource *resource,
2046 int32_t x, int32_t y, int32_t width, int32_t height)
2047{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002048 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002049
2050 pixman_region32_union_rect(&region->region, &region->region,
2051 x, y, width, height);
2052}
2053
2054static void
2055region_subtract(struct wl_client *client, struct wl_resource *resource,
2056 int32_t x, int32_t y, int32_t width, int32_t height)
2057{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002058 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002059 pixman_region32_t rect;
2060
2061 pixman_region32_init_rect(&rect, x, y, width, height);
2062 pixman_region32_subtract(&region->region, &region->region, &rect);
2063 pixman_region32_fini(&rect);
2064}
2065
2066static const struct wl_region_interface region_interface = {
2067 region_destroy,
2068 region_add,
2069 region_subtract
2070};
2071
2072static void
2073compositor_create_region(struct wl_client *client,
2074 struct wl_resource *resource, uint32_t id)
2075{
2076 struct weston_region *region;
2077
2078 region = malloc(sizeof *region);
2079 if (region == NULL) {
2080 wl_resource_post_no_memory(resource);
2081 return;
2082 }
2083
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002084 pixman_region32_init(&region->region);
2085
Jason Ekstranda85118c2013-06-27 20:17:02 -05002086 region->resource =
2087 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002088 if (region->resource == NULL) {
2089 free(region);
2090 wl_resource_post_no_memory(resource);
2091 return;
2092 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002093 wl_resource_set_implementation(region->resource, &region_interface,
2094 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002095}
2096
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002097static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002098 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002099 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002100};
2101
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002102static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002103weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2104{
2105 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002106 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002107 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002108
Alexander Larsson4ea95522013-05-22 14:41:37 +02002109 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03002110 surface->buffer_transform = sub->cached.buffer_transform;
2111
Alexander Larsson4ea95522013-05-22 14:41:37 +02002112 /* wl_surface.set_buffer_scale */
2113 surface->buffer_scale = sub->cached.buffer_scale;
2114
Pekka Paalanene67858b2013-04-25 13:57:42 +03002115 /* wl_surface.attach */
2116 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2117 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2118 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2119
Jason Ekstranda7af7042013-10-12 22:38:11 -05002120 surface->width = 0;
2121 surface->height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002122 if (surface->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002123 surface->width = weston_surface_buffer_width(surface);
2124 surface->height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002125 }
2126
2127 if (surface->configure && sub->cached.newly_attached)
2128 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002129 surface->width, surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002130 sub->cached.sx = 0;
2131 sub->cached.sy = 0;
2132 sub->cached.newly_attached = 0;
2133
2134 /* wl_surface.damage */
2135 pixman_region32_union(&surface->damage, &surface->damage,
2136 &sub->cached.damage);
2137 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2138 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002139 surface->width,
2140 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002141 empty_region(&sub->cached.damage);
2142
2143 /* wl_surface.set_opaque_region */
2144 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002145 surface->width,
2146 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002147 pixman_region32_intersect(&opaque,
2148 &opaque, &sub->cached.opaque);
2149
2150 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2151 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002152 wl_list_for_each(view, &surface->views, surface_link)
2153 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002154 }
2155
2156 pixman_region32_fini(&opaque);
2157
2158 /* wl_surface.set_input_region */
2159 pixman_region32_fini(&surface->input);
2160 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161 surface->width,
2162 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002163 pixman_region32_intersect(&surface->input,
2164 &surface->input, &sub->cached.input);
2165
2166 /* wl_surface.frame */
2167 wl_list_insert_list(&surface->frame_callback_list,
2168 &sub->cached.frame_callback_list);
2169 wl_list_init(&sub->cached.frame_callback_list);
2170
2171 weston_surface_commit_subsurface_order(surface);
2172
2173 weston_surface_schedule_repaint(surface);
2174
2175 sub->cached.has_data = 0;
2176}
2177
2178static void
2179weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2180{
2181 struct weston_surface *surface = sub->surface;
2182
2183 /*
2184 * If this commit would cause the surface to move by the
2185 * attach(dx, dy) parameters, the old damage region must be
2186 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002187 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002188 */
2189 pixman_region32_translate(&sub->cached.damage,
2190 -surface->pending.sx, -surface->pending.sy);
2191 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2192 &surface->pending.damage);
2193 empty_region(&surface->pending.damage);
2194
2195 if (surface->pending.newly_attached) {
2196 sub->cached.newly_attached = 1;
2197 weston_buffer_reference(&sub->cached.buffer_ref,
2198 surface->pending.buffer);
2199 }
2200 sub->cached.sx += surface->pending.sx;
2201 sub->cached.sy += surface->pending.sy;
2202 surface->pending.sx = 0;
2203 surface->pending.sy = 0;
2204 surface->pending.newly_attached = 0;
2205
2206 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002207 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002208
2209 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2210
2211 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2212
2213 wl_list_insert_list(&sub->cached.frame_callback_list,
2214 &surface->pending.frame_callback_list);
2215 wl_list_init(&surface->pending.frame_callback_list);
2216
2217 sub->cached.has_data = 1;
2218}
2219
2220static int
2221weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2222{
2223 while (sub) {
2224 if (sub->synchronized)
2225 return 1;
2226
2227 if (!sub->parent)
2228 return 0;
2229
2230 sub = weston_surface_to_subsurface(sub->parent);
2231 }
2232
2233 return 0;
2234}
2235
2236static void
2237weston_subsurface_commit(struct weston_subsurface *sub)
2238{
2239 struct weston_surface *surface = sub->surface;
2240 struct weston_subsurface *tmp;
2241
2242 /* Recursive check for effectively synchronized. */
2243 if (weston_subsurface_is_synchronized(sub)) {
2244 weston_subsurface_commit_to_cache(sub);
2245 } else {
2246 if (sub->cached.has_data) {
2247 /* flush accumulated state from cache */
2248 weston_subsurface_commit_to_cache(sub);
2249 weston_subsurface_commit_from_cache(sub);
2250 } else {
2251 weston_surface_commit(surface);
2252 }
2253
2254 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2255 if (tmp->surface != surface)
2256 weston_subsurface_parent_commit(tmp, 0);
2257 }
2258 }
2259}
2260
2261static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002262weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002263{
2264 struct weston_surface *surface = sub->surface;
2265 struct weston_subsurface *tmp;
2266
Pekka Paalanene67858b2013-04-25 13:57:42 +03002267 /* From now on, commit_from_cache the whole sub-tree, regardless of
2268 * the synchronized mode of each child. This sub-surface or some
2269 * of its ancestors were synchronized, so we are synchronized
2270 * all the way down.
2271 */
2272
2273 if (sub->cached.has_data)
2274 weston_subsurface_commit_from_cache(sub);
2275
2276 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2277 if (tmp->surface != surface)
2278 weston_subsurface_parent_commit(tmp, 1);
2279 }
2280}
2281
2282static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002283weston_subsurface_parent_commit(struct weston_subsurface *sub,
2284 int parent_is_synchronized)
2285{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002286 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002287 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288 wl_list_for_each(view, &sub->surface->views, surface_link)
2289 weston_view_set_position(view,
2290 sub->position.x,
2291 sub->position.y);
2292
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002293 sub->position.set = 0;
2294 }
2295
2296 if (parent_is_synchronized || sub->synchronized)
2297 weston_subsurface_synchronized_commit(sub);
2298}
2299
2300static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002301subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
2302 int32_t width, int32_t height)
2303{
2304 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002306
Jason Ekstranda7af7042013-10-12 22:38:11 -05002307 wl_list_for_each(view, &surface->views, surface_link)
2308 weston_view_configure(view,
2309 view->geometry.x + dx,
2310 view->geometry.y + dy,
2311 width, height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002312
2313 /* No need to check parent mappedness, because if parent is not
2314 * mapped, parent is not in a visible layer, so this sub-surface
2315 * will not be drawn either.
2316 */
2317 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002318 /* Cannot call weston_surface_update_transform(),
2319 * because that would call it also for the parent surface,
2320 * which might not be mapped yet. That would lead to
2321 * inconsistent state, where the window could never be
2322 * mapped.
2323 *
2324 * Instead just assing any output, to make
2325 * weston_surface_is_mapped() return true, so that when the
2326 * parent surface does get mapped, this one will get
2327 * included, too. See surface_list_add().
2328 */
2329 assert(!wl_list_empty(&compositor->output_list));
2330 surface->output = container_of(compositor->output_list.next,
2331 struct weston_output, link);
2332 }
2333}
2334
2335static struct weston_subsurface *
2336weston_surface_to_subsurface(struct weston_surface *surface)
2337{
2338 if (surface->configure == subsurface_configure)
2339 return surface->configure_private;
2340
2341 return NULL;
2342}
2343
Pekka Paalanen01388e22013-04-25 13:57:44 +03002344WL_EXPORT struct weston_surface *
2345weston_surface_get_main_surface(struct weston_surface *surface)
2346{
2347 struct weston_subsurface *sub;
2348
2349 while (surface && (sub = weston_surface_to_subsurface(surface)))
2350 surface = sub->parent;
2351
2352 return surface;
2353}
2354
Pekka Paalanene67858b2013-04-25 13:57:42 +03002355static void
2356subsurface_set_position(struct wl_client *client,
2357 struct wl_resource *resource, int32_t x, int32_t y)
2358{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002359 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002360
2361 if (!sub)
2362 return;
2363
2364 sub->position.x = x;
2365 sub->position.y = y;
2366 sub->position.set = 1;
2367}
2368
2369static struct weston_subsurface *
2370subsurface_from_surface(struct weston_surface *surface)
2371{
2372 struct weston_subsurface *sub;
2373
2374 sub = weston_surface_to_subsurface(surface);
2375 if (sub)
2376 return sub;
2377
2378 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2379 if (sub->surface == surface)
2380 return sub;
2381
2382 return NULL;
2383}
2384
2385static struct weston_subsurface *
2386subsurface_sibling_check(struct weston_subsurface *sub,
2387 struct weston_surface *surface,
2388 const char *request)
2389{
2390 struct weston_subsurface *sibling;
2391
2392 sibling = subsurface_from_surface(surface);
2393
2394 if (!sibling) {
2395 wl_resource_post_error(sub->resource,
2396 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2397 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002398 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002399 return NULL;
2400 }
2401
2402 if (sibling->parent != sub->parent) {
2403 wl_resource_post_error(sub->resource,
2404 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2405 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002406 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002407 return NULL;
2408 }
2409
2410 return sibling;
2411}
2412
2413static void
2414subsurface_place_above(struct wl_client *client,
2415 struct wl_resource *resource,
2416 struct wl_resource *sibling_resource)
2417{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002418 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002419 struct weston_surface *surface =
2420 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002421 struct weston_subsurface *sibling;
2422
2423 if (!sub)
2424 return;
2425
2426 sibling = subsurface_sibling_check(sub, surface, "place_above");
2427 if (!sibling)
2428 return;
2429
2430 wl_list_remove(&sub->parent_link_pending);
2431 wl_list_insert(sibling->parent_link_pending.prev,
2432 &sub->parent_link_pending);
2433}
2434
2435static void
2436subsurface_place_below(struct wl_client *client,
2437 struct wl_resource *resource,
2438 struct wl_resource *sibling_resource)
2439{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002440 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002441 struct weston_surface *surface =
2442 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002443 struct weston_subsurface *sibling;
2444
2445 if (!sub)
2446 return;
2447
2448 sibling = subsurface_sibling_check(sub, surface, "place_below");
2449 if (!sibling)
2450 return;
2451
2452 wl_list_remove(&sub->parent_link_pending);
2453 wl_list_insert(&sibling->parent_link_pending,
2454 &sub->parent_link_pending);
2455}
2456
2457static void
2458subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2459{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002460 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002461
2462 if (sub)
2463 sub->synchronized = 1;
2464}
2465
2466static void
2467subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2468{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002469 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002470
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002471 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002472 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002473
2474 /* If sub became effectively desynchronized, flush. */
2475 if (!weston_subsurface_is_synchronized(sub))
2476 weston_subsurface_synchronized_commit(sub);
2477 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002478}
2479
2480static void
2481weston_subsurface_cache_init(struct weston_subsurface *sub)
2482{
2483 pixman_region32_init(&sub->cached.damage);
2484 pixman_region32_init(&sub->cached.opaque);
2485 pixman_region32_init(&sub->cached.input);
2486 wl_list_init(&sub->cached.frame_callback_list);
2487 sub->cached.buffer_ref.buffer = NULL;
2488}
2489
2490static void
2491weston_subsurface_cache_fini(struct weston_subsurface *sub)
2492{
2493 struct weston_frame_callback *cb, *tmp;
2494
2495 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002496 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002497
2498 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2499 pixman_region32_fini(&sub->cached.damage);
2500 pixman_region32_fini(&sub->cached.opaque);
2501 pixman_region32_fini(&sub->cached.input);
2502}
2503
2504static void
2505weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2506{
2507 wl_list_remove(&sub->parent_link);
2508 wl_list_remove(&sub->parent_link_pending);
2509 wl_list_remove(&sub->parent_destroy_listener.link);
2510 sub->parent = NULL;
2511}
2512
2513static void
2514weston_subsurface_destroy(struct weston_subsurface *sub);
2515
2516static void
2517subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2518{
2519 struct weston_subsurface *sub =
2520 container_of(listener, struct weston_subsurface,
2521 surface_destroy_listener);
2522 assert(data == &sub->surface->resource);
2523
2524 /* The protocol object (wl_resource) is left inert. */
2525 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002526 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002527
2528 weston_subsurface_destroy(sub);
2529}
2530
2531static void
2532subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2533{
2534 struct weston_subsurface *sub =
2535 container_of(listener, struct weston_subsurface,
2536 parent_destroy_listener);
2537 assert(data == &sub->parent->resource);
2538 assert(sub->surface != sub->parent);
2539
2540 if (weston_surface_is_mapped(sub->surface))
2541 weston_surface_unmap(sub->surface);
2542
2543 weston_subsurface_unlink_parent(sub);
2544}
2545
2546static void
2547subsurface_resource_destroy(struct wl_resource *resource)
2548{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002549 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002550
2551 if (sub)
2552 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002553}
2554
2555static void
2556subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2557{
2558 wl_resource_destroy(resource);
2559}
2560
2561static void
2562weston_subsurface_link_parent(struct weston_subsurface *sub,
2563 struct weston_surface *parent)
2564{
2565 sub->parent = parent;
2566 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002567 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002568 &sub->parent_destroy_listener);
2569
2570 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2571 wl_list_insert(&parent->subsurface_list_pending,
2572 &sub->parent_link_pending);
2573}
2574
2575static void
2576weston_subsurface_link_surface(struct weston_subsurface *sub,
2577 struct weston_surface *surface)
2578{
2579 sub->surface = surface;
2580 sub->surface_destroy_listener.notify =
2581 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002582 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002583 &sub->surface_destroy_listener);
2584}
2585
2586static void
2587weston_subsurface_destroy(struct weston_subsurface *sub)
2588{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002589 struct weston_view *view, *next;
2590
Pekka Paalanene67858b2013-04-25 13:57:42 +03002591 assert(sub->surface);
2592
2593 if (sub->resource) {
2594 assert(weston_surface_to_subsurface(sub->surface) == sub);
2595 assert(sub->parent_destroy_listener.notify ==
2596 subsurface_handle_parent_destroy);
2597
Jason Ekstranda7af7042013-10-12 22:38:11 -05002598 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2599 weston_view_destroy(view);
2600
Pekka Paalanene67858b2013-04-25 13:57:42 +03002601 if (sub->parent)
2602 weston_subsurface_unlink_parent(sub);
2603
2604 weston_subsurface_cache_fini(sub);
2605
2606 sub->surface->configure = NULL;
2607 sub->surface->configure_private = NULL;
2608 } else {
2609 /* the dummy weston_subsurface for the parent itself */
2610 assert(sub->parent_destroy_listener.notify == NULL);
2611 wl_list_remove(&sub->parent_link);
2612 wl_list_remove(&sub->parent_link_pending);
2613 }
2614
2615 wl_list_remove(&sub->surface_destroy_listener.link);
2616 free(sub);
2617}
2618
2619static const struct wl_subsurface_interface subsurface_implementation = {
2620 subsurface_destroy,
2621 subsurface_set_position,
2622 subsurface_place_above,
2623 subsurface_place_below,
2624 subsurface_set_sync,
2625 subsurface_set_desync
2626};
2627
2628static struct weston_subsurface *
2629weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2630 struct weston_surface *parent)
2631{
2632 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002633 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002634
2635 sub = calloc(1, sizeof *sub);
2636 if (!sub)
2637 return NULL;
2638
Jason Ekstranda7af7042013-10-12 22:38:11 -05002639 wl_list_init(&sub->unused_views);
2640
Jason Ekstranda85118c2013-06-27 20:17:02 -05002641 sub->resource =
2642 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002643 if (!sub->resource) {
2644 free(sub);
2645 return NULL;
2646 }
2647
Jason Ekstranda85118c2013-06-27 20:17:02 -05002648 wl_resource_set_implementation(sub->resource,
2649 &subsurface_implementation,
2650 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002651 weston_subsurface_link_surface(sub, surface);
2652 weston_subsurface_link_parent(sub, parent);
2653 weston_subsurface_cache_init(sub);
2654 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002655
2656 return sub;
2657}
2658
2659/* Create a dummy subsurface for having the parent itself in its
2660 * sub-surface lists. Makes stacking order manipulation easy.
2661 */
2662static struct weston_subsurface *
2663weston_subsurface_create_for_parent(struct weston_surface *parent)
2664{
2665 struct weston_subsurface *sub;
2666
2667 sub = calloc(1, sizeof *sub);
2668 if (!sub)
2669 return NULL;
2670
2671 weston_subsurface_link_surface(sub, parent);
2672 sub->parent = parent;
2673 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2674 wl_list_insert(&parent->subsurface_list_pending,
2675 &sub->parent_link_pending);
2676
2677 return sub;
2678}
2679
2680static void
2681subcompositor_get_subsurface(struct wl_client *client,
2682 struct wl_resource *resource,
2683 uint32_t id,
2684 struct wl_resource *surface_resource,
2685 struct wl_resource *parent_resource)
2686{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002687 struct weston_surface *surface =
2688 wl_resource_get_user_data(surface_resource);
2689 struct weston_surface *parent =
2690 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002691 struct weston_subsurface *sub;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002692 struct weston_view *view, *subview;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002693 static const char where[] = "get_subsurface: wl_subsurface@";
2694
2695 if (surface == parent) {
2696 wl_resource_post_error(resource,
2697 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2698 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002699 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002700 return;
2701 }
2702
2703 if (weston_surface_to_subsurface(surface)) {
2704 wl_resource_post_error(resource,
2705 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2706 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002707 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002708 return;
2709 }
2710
2711 if (surface->configure) {
2712 wl_resource_post_error(resource,
2713 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2714 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002715 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002716 return;
2717 }
2718
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002719 if (weston_surface_get_main_surface(parent) == surface) {
2720 wl_resource_post_error(resource,
2721 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2722 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002723 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002724 return;
2725 }
2726
Pekka Paalanene67858b2013-04-25 13:57:42 +03002727 /* make sure the parent is in its own list */
2728 if (wl_list_empty(&parent->subsurface_list)) {
2729 if (!weston_subsurface_create_for_parent(parent)) {
2730 wl_resource_post_no_memory(resource);
2731 return;
2732 }
2733 }
2734
2735 sub = weston_subsurface_create(id, surface, parent);
2736 if (!sub) {
2737 wl_resource_post_no_memory(resource);
2738 return;
2739 }
2740
2741 surface->configure = subsurface_configure;
2742 surface->configure_private = sub;
2743}
2744
2745static void
2746subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2747{
2748 wl_resource_destroy(resource);
2749}
2750
2751static const struct wl_subcompositor_interface subcompositor_interface = {
2752 subcompositor_destroy,
2753 subcompositor_get_subsurface
2754};
2755
2756static void
2757bind_subcompositor(struct wl_client *client,
2758 void *data, uint32_t version, uint32_t id)
2759{
2760 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002761 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002762
Jason Ekstranda85118c2013-06-27 20:17:02 -05002763 resource =
2764 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002765 if (resource == NULL) {
2766 wl_client_post_no_memory(client);
2767 return;
2768 }
2769 wl_resource_set_implementation(resource, &subcompositor_interface,
2770 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002771}
2772
2773static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002774weston_compositor_dpms(struct weston_compositor *compositor,
2775 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002776{
2777 struct weston_output *output;
2778
2779 wl_list_for_each(output, &compositor->output_list, link)
2780 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002781 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002782}
2783
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002784WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002785weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002786{
Neil Roberts8b62e202013-09-30 13:14:47 +01002787 uint32_t old_state = compositor->state;
2788
2789 /* The state needs to be changed before emitting the wake
2790 * signal because that may try to schedule a repaint which
2791 * will not work if the compositor is still sleeping */
2792 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2793
2794 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002795 case WESTON_COMPOSITOR_SLEEPING:
2796 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2797 /* fall through */
2798 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002799 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002800 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002801 /* fall through */
2802 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002803 wl_event_source_timer_update(compositor->idle_source,
2804 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002805 }
2806}
2807
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002808WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002809weston_compositor_offscreen(struct weston_compositor *compositor)
2810{
2811 switch (compositor->state) {
2812 case WESTON_COMPOSITOR_OFFSCREEN:
2813 return;
2814 case WESTON_COMPOSITOR_SLEEPING:
2815 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2816 /* fall through */
2817 default:
2818 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2819 wl_event_source_timer_update(compositor->idle_source, 0);
2820 }
2821}
2822
2823WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002824weston_compositor_sleep(struct weston_compositor *compositor)
2825{
2826 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2827 return;
2828
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002829 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002830 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2831 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2832}
2833
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002834static int
2835idle_handler(void *data)
2836{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002837 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002838
2839 if (compositor->idle_inhibit)
2840 return 1;
2841
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002842 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002843 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002844
2845 return 1;
2846}
2847
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002848WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08002849weston_plane_init(struct weston_plane *plane,
2850 struct weston_compositor *ec,
2851 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002852{
2853 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002854 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002855 plane->x = x;
2856 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08002857 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002858
2859 /* Init the link so that the call to wl_list_remove() when releasing
2860 * the plane without ever stacking doesn't lead to a crash */
2861 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002862}
2863
2864WL_EXPORT void
2865weston_plane_release(struct weston_plane *plane)
2866{
Xiong Zhang97116532013-10-23 13:58:31 +08002867 struct weston_view *view;
2868
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002869 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002870 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002871
Xiong Zhang97116532013-10-23 13:58:31 +08002872 wl_list_for_each(view, &plane->compositor->view_list, link) {
2873 if (view->plane == plane)
2874 view->plane = NULL;
2875 }
2876
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002877 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002878}
2879
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002880WL_EXPORT void
2881weston_compositor_stack_plane(struct weston_compositor *ec,
2882 struct weston_plane *plane,
2883 struct weston_plane *above)
2884{
2885 if (above)
2886 wl_list_insert(above->link.prev, &plane->link);
2887 else
2888 wl_list_insert(&ec->plane_list, &plane->link);
2889}
2890
Casey Dahlin9074db52012-04-19 22:50:09 -04002891static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002892{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002893 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002894}
2895
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002896static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002897bind_output(struct wl_client *client,
2898 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002899{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002900 struct weston_output *output = data;
2901 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002902 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002903
Jason Ekstranda85118c2013-06-27 20:17:02 -05002904 resource = wl_resource_create(client, &wl_output_interface,
2905 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002906 if (resource == NULL) {
2907 wl_client_post_no_memory(client);
2908 return;
2909 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002910
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002911 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002912 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002913
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002914 wl_output_send_geometry(resource,
2915 output->x,
2916 output->y,
2917 output->mm_width,
2918 output->mm_height,
2919 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002920 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002921 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002922 if (version >= 2)
2923 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02002924 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002925
2926 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002927 wl_output_send_mode(resource,
2928 mode->flags,
2929 mode->width,
2930 mode->height,
2931 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002932 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002933
2934 if (version >= 2)
2935 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002936}
2937
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002938WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002939weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002940{
Richard Hughes64ddde12013-05-01 21:52:10 +01002941 wl_signal_emit(&output->destroy_signal, output);
2942
Richard Hughesafe690c2013-05-02 10:10:04 +01002943 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002944 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002945 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002946 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002947
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002948 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002949}
2950
Scott Moreau1bad5db2012-08-18 01:04:05 -06002951static void
2952weston_output_compute_transform(struct weston_output *output)
2953{
2954 struct weston_matrix transform;
2955 int flip;
2956
2957 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002958 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002959
2960 switch(output->transform) {
2961 case WL_OUTPUT_TRANSFORM_FLIPPED:
2962 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2963 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2964 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002965 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002966 flip = -1;
2967 break;
2968 default:
2969 flip = 1;
2970 break;
2971 }
2972
2973 switch(output->transform) {
2974 case WL_OUTPUT_TRANSFORM_NORMAL:
2975 case WL_OUTPUT_TRANSFORM_FLIPPED:
2976 transform.d[0] = flip;
2977 transform.d[1] = 0;
2978 transform.d[4] = 0;
2979 transform.d[5] = 1;
2980 break;
2981 case WL_OUTPUT_TRANSFORM_90:
2982 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2983 transform.d[0] = 0;
2984 transform.d[1] = -flip;
2985 transform.d[4] = 1;
2986 transform.d[5] = 0;
2987 break;
2988 case WL_OUTPUT_TRANSFORM_180:
2989 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2990 transform.d[0] = -flip;
2991 transform.d[1] = 0;
2992 transform.d[4] = 0;
2993 transform.d[5] = -1;
2994 break;
2995 case WL_OUTPUT_TRANSFORM_270:
2996 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2997 transform.d[0] = 0;
2998 transform.d[1] = flip;
2999 transform.d[4] = -1;
3000 transform.d[5] = 0;
3001 break;
3002 default:
3003 break;
3004 }
3005
3006 weston_matrix_multiply(&output->matrix, &transform);
3007}
3008
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003009WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003010weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003011{
Scott Moreau850ca422012-05-21 15:21:25 -06003012 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003013 struct weston_matrix camera;
3014 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003015
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003016 weston_matrix_init(&output->matrix);
3017 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003018 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
3019 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003020
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003021 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003022 2.0 / (output->width + output->border.left + output->border.right),
3023 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
3024
3025 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003026
Scott Moreauccbf29d2012-02-22 14:21:41 -07003027 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003028 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003029 weston_matrix_init(&camera);
3030 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003031 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003032 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003033 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003034 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003035 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003036 weston_matrix_multiply(&output->matrix, &modelview);
3037 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003038
Scott Moreauccbf29d2012-02-22 14:21:41 -07003039 output->dirty = 0;
3040}
3041
Scott Moreau1bad5db2012-08-18 01:04:05 -06003042static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003043weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003044{
3045 output->transform = transform;
3046
3047 switch (transform) {
3048 case WL_OUTPUT_TRANSFORM_90:
3049 case WL_OUTPUT_TRANSFORM_270:
3050 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3051 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3052 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003053 output->width = output->current_mode->height;
3054 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003055 break;
3056 case WL_OUTPUT_TRANSFORM_NORMAL:
3057 case WL_OUTPUT_TRANSFORM_180:
3058 case WL_OUTPUT_TRANSFORM_FLIPPED:
3059 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003060 output->width = output->current_mode->width;
3061 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003062 break;
3063 default:
3064 break;
3065 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003066
Hardening57388e42013-09-18 23:56:36 +02003067 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003068 output->width /= scale;
3069 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003070}
3071
Scott Moreauccbf29d2012-02-22 14:21:41 -07003072WL_EXPORT void
3073weston_output_move(struct weston_output *output, int x, int y)
3074{
3075 output->x = x;
3076 output->y = y;
3077
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003078 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003079 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003080 output->width,
3081 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003082}
3083
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003084WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003085weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003086 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003087 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003088{
3089 output->compositor = c;
3090 output->x = x;
3091 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05003092 output->border.top = 0;
3093 output->border.bottom = 0;
3094 output->border.left = 0;
3095 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02003096 output->mm_width = mm_width;
3097 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003098 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003099 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003100
Alexander Larsson0b135062013-05-28 16:23:36 +02003101 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003102 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003103
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003104 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003105 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003106
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003107 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003108 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003109 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003110 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003111
Casey Dahlin58ba1372012-04-19 22:50:08 -04003112 output->id = ffs(~output->compositor->output_id_pool) - 1;
3113 output->compositor->output_id_pool |= 1 << output->id;
3114
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003115 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003116 wl_global_create(c->wl_display, &wl_output_interface, 2,
3117 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003118 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003119}
3120
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003121WL_EXPORT void
3122weston_output_transform_coordinate(struct weston_output *output,
3123 int device_x, int device_y,
3124 wl_fixed_t *x, wl_fixed_t *y)
3125{
3126 wl_fixed_t tx, ty;
3127 wl_fixed_t width, height;
3128
Hardeningff39efa2013-09-18 23:56:35 +02003129 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3130 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003131
3132 switch(output->transform) {
3133 case WL_OUTPUT_TRANSFORM_NORMAL:
3134 default:
3135 tx = wl_fixed_from_int(device_x);
3136 ty = wl_fixed_from_int(device_y);
3137 break;
3138 case WL_OUTPUT_TRANSFORM_90:
3139 tx = wl_fixed_from_int(device_y);
3140 ty = height - wl_fixed_from_int(device_x);
3141 break;
3142 case WL_OUTPUT_TRANSFORM_180:
3143 tx = width - wl_fixed_from_int(device_x);
3144 ty = height - wl_fixed_from_int(device_y);
3145 break;
3146 case WL_OUTPUT_TRANSFORM_270:
3147 tx = width - wl_fixed_from_int(device_y);
3148 ty = wl_fixed_from_int(device_x);
3149 break;
3150 case WL_OUTPUT_TRANSFORM_FLIPPED:
3151 tx = width - wl_fixed_from_int(device_x);
3152 ty = wl_fixed_from_int(device_y);
3153 break;
3154 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3155 tx = width - wl_fixed_from_int(device_y);
3156 ty = height - wl_fixed_from_int(device_x);
3157 break;
3158 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3159 tx = wl_fixed_from_int(device_x);
3160 ty = height - wl_fixed_from_int(device_y);
3161 break;
3162 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3163 tx = wl_fixed_from_int(device_y);
3164 ty = wl_fixed_from_int(device_x);
3165 break;
3166 }
3167
Hardeningff39efa2013-09-18 23:56:35 +02003168 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3169 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003170}
3171
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003172static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003173compositor_bind(struct wl_client *client,
3174 void *data, uint32_t version, uint32_t id)
3175{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003176 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003177 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003178
Jason Ekstranda85118c2013-06-27 20:17:02 -05003179 resource = wl_resource_create(client, &wl_compositor_interface,
3180 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003181 if (resource == NULL) {
3182 wl_client_post_no_memory(client);
3183 return;
3184 }
3185
3186 wl_resource_set_implementation(resource, &compositor_interface,
3187 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003188}
3189
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003190static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003191log_uname(void)
3192{
3193 struct utsname usys;
3194
3195 uname(&usys);
3196
3197 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3198 usys.version, usys.machine);
3199}
3200
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003201WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003202weston_environment_get_fd(const char *env)
3203{
3204 char *e, *end;
3205 int fd, flags;
3206
3207 e = getenv(env);
3208 if (!e)
3209 return -1;
3210 fd = strtol(e, &end, 0);
3211 if (*end != '\0')
3212 return -1;
3213
3214 flags = fcntl(fd, F_GETFD);
3215 if (flags == -1)
3216 return -1;
3217
3218 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3219 unsetenv(env);
3220
3221 return fd;
3222}
3223
3224WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003225weston_compositor_init(struct weston_compositor *ec,
3226 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003227 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003228 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003229{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003230 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003231 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003232 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003233
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003234 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003235 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003236 wl_signal_init(&ec->destroy_signal);
3237 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003238 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003239 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003240 wl_signal_init(&ec->idle_signal);
3241 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003242 wl_signal_init(&ec->show_input_panel_signal);
3243 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003244 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003245 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003246 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003247 wl_signal_init(&ec->session_signal);
3248 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003249
Casey Dahlin58ba1372012-04-19 22:50:08 -04003250 ec->output_id_pool = 0;
3251
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003252 if (!wl_global_create(display, &wl_compositor_interface, 3,
3253 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003254 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003255
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003256 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3257 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003258 return -1;
3259
Jason Ekstranda7af7042013-10-12 22:38:11 -05003260 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003261 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003262 wl_list_init(&ec->layer_list);
3263 wl_list_init(&ec->seat_list);
3264 wl_list_init(&ec->output_list);
3265 wl_list_init(&ec->key_binding_list);
3266 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003267 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003268 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003269 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003270
Xiong Zhang97116532013-10-23 13:58:31 +08003271 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003272 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003273
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003274 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3275 weston_config_section_get_string(s, "keymap_rules",
3276 (char **) &xkb_names.rules, NULL);
3277 weston_config_section_get_string(s, "keymap_model",
3278 (char **) &xkb_names.model, NULL);
3279 weston_config_section_get_string(s, "keymap_layout",
3280 (char **) &xkb_names.layout, NULL);
3281 weston_config_section_get_string(s, "keymap_variant",
3282 (char **) &xkb_names.variant, NULL);
3283 weston_config_section_get_string(s, "keymap_options",
3284 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003285
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003286 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3287 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003288
3289 ec->ping_handler = NULL;
3290
3291 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003292 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003293
3294 wl_data_device_manager_init(ec->wl_display);
3295
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003296 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003297
Daniel Stone725c2c32012-06-22 14:04:36 +01003298 loop = wl_display_get_event_loop(ec->wl_display);
3299 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3300 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3301
3302 ec->input_loop = wl_event_loop_create();
3303
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003304 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3305 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3306
3307 weston_compositor_schedule_repaint(ec);
3308
Daniel Stone725c2c32012-06-22 14:04:36 +01003309 return 0;
3310}
3311
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003312WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003313weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003314{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003315 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003316
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003317 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003318 if (ec->input_loop_source)
3319 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003320
Matt Roper361d2ad2011-08-29 13:52:23 -07003321 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003322 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003323 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003324
Daniel Stone325fc2d2012-05-30 16:31:58 +01003325 weston_binding_list_destroy_all(&ec->key_binding_list);
3326 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003327 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003328 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003329 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003330
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003331 weston_plane_release(&ec->primary_plane);
3332
Jonas Ådahlc97af922012-03-28 22:36:09 +02003333 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003334
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003335 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003336}
3337
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003338WL_EXPORT void
3339weston_version(int *major, int *minor, int *micro)
3340{
3341 *major = WESTON_VERSION_MAJOR;
3342 *minor = WESTON_VERSION_MINOR;
3343 *micro = WESTON_VERSION_MICRO;
3344}
3345
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003346static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003347 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003348 const char *desc;
3349} capability_strings[] = {
3350 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003351 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003352};
3353
3354static void
3355weston_compositor_log_capabilities(struct weston_compositor *compositor)
3356{
3357 unsigned i;
3358 int yes;
3359
3360 weston_log("Compositor capabilities:\n");
3361 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3362 yes = compositor->capabilities & capability_strings[i].bit;
3363 weston_log_continue(STAMP_SPACE "%s %s\n",
3364 capability_strings[i].desc,
3365 yes ? "yes" : "no");
3366 }
3367}
3368
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003369static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003370{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003371 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003372
Martin Minarik6d118362012-06-07 18:01:59 +02003373 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003374 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003375
3376 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003377}
3378
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003379#ifdef HAVE_LIBUNWIND
3380
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003381static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003382print_backtrace(void)
3383{
3384 unw_cursor_t cursor;
3385 unw_context_t context;
3386 unw_word_t off;
3387 unw_proc_info_t pip;
3388 int ret, i = 0;
3389 char procname[256];
3390 const char *filename;
3391 Dl_info dlinfo;
3392
3393 pip.unwind_info = NULL;
3394 ret = unw_getcontext(&context);
3395 if (ret) {
3396 weston_log("unw_getcontext: %d\n", ret);
3397 return;
3398 }
3399
3400 ret = unw_init_local(&cursor, &context);
3401 if (ret) {
3402 weston_log("unw_init_local: %d\n", ret);
3403 return;
3404 }
3405
3406 ret = unw_step(&cursor);
3407 while (ret > 0) {
3408 ret = unw_get_proc_info(&cursor, &pip);
3409 if (ret) {
3410 weston_log("unw_get_proc_info: %d\n", ret);
3411 break;
3412 }
3413
3414 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3415 if (ret && ret != -UNW_ENOMEM) {
3416 if (ret != -UNW_EUNSPEC)
3417 weston_log("unw_get_proc_name: %d\n", ret);
3418 procname[0] = '?';
3419 procname[1] = 0;
3420 }
3421
3422 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3423 *dlinfo.dli_fname)
3424 filename = dlinfo.dli_fname;
3425 else
3426 filename = "?";
3427
3428 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3429 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3430
3431 ret = unw_step(&cursor);
3432 if (ret < 0)
3433 weston_log("unw_step: %d\n", ret);
3434 }
3435}
3436
3437#else
3438
3439static void
3440print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003441{
3442 void *buffer[32];
3443 int i, count;
3444 Dl_info info;
3445
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003446 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3447 for (i = 0; i < count; i++) {
3448 dladdr(buffer[i], &info);
3449 weston_log(" [%016lx] %s (%s)\n",
3450 (long) buffer[i],
3451 info.dli_sname ? info.dli_sname : "--",
3452 info.dli_fname);
3453 }
3454}
3455
3456#endif
3457
3458static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003459on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003460{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003461 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003462 * then call the backend restore function, which will switch
3463 * back to the vt we launched from or ungrab X etc and then
3464 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003465 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003466 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003467 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003468
Peter Maatmane5b42e42013-03-27 22:38:53 +01003469 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003470
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003471 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003472
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003473 segv_compositor->restore(segv_compositor);
3474
3475 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003476}
3477
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003478WL_EXPORT void *
3479weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003480{
3481 char path[PATH_MAX];
3482 void *module, *init;
3483
3484 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003485 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003486 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003487 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003488
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003489 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3490 if (module) {
3491 weston_log("Module '%s' already loaded\n", path);
3492 dlclose(module);
3493 return NULL;
3494 }
3495
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003496 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003497 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003498 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003499 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003500 return NULL;
3501 }
3502
3503 init = dlsym(module, entrypoint);
3504 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003505 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003506 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003507 return NULL;
3508 }
3509
3510 return init;
3511}
3512
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003513static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003514load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003515 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003516{
3517 const char *p, *end;
3518 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003519 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003520 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003521
3522 if (modules == NULL)
3523 return 0;
3524
3525 p = modules;
3526 while (*p) {
3527 end = strchrnul(p, ',');
3528 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003529 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003530 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003531 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003532 p = end;
3533 while (*p == ',')
3534 p++;
3535
3536 }
3537
3538 return 0;
3539}
3540
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003541static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003542 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3543
3544static const char xdg_wrong_message[] =
3545 "fatal: environment variable XDG_RUNTIME_DIR\n"
3546 "is set to \"%s\", which is not a directory.\n";
3547
3548static const char xdg_wrong_mode_message[] =
3549 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3550 "correctly. Unix access mode must be 0700 but is %o,\n"
3551 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003552 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003553
3554static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003555 "Refer to your distribution on how to get it, or\n"
3556 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3557 "on how to implement it.\n";
3558
Martin Minarik37032f82012-06-18 20:15:18 +02003559static void
3560verify_xdg_runtime_dir(void)
3561{
3562 char *dir = getenv("XDG_RUNTIME_DIR");
3563 struct stat s;
3564
3565 if (!dir) {
3566 weston_log(xdg_error_message);
3567 weston_log_continue(xdg_detail_message);
3568 exit(EXIT_FAILURE);
3569 }
3570
3571 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3572 weston_log(xdg_wrong_message, dir);
3573 weston_log_continue(xdg_detail_message);
3574 exit(EXIT_FAILURE);
3575 }
3576
3577 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3578 weston_log(xdg_wrong_mode_message,
3579 dir, s.st_mode & 0777, s.st_uid);
3580 weston_log_continue(xdg_detail_message);
3581 }
3582}
3583
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003584static int
3585usage(int error_code)
3586{
3587 fprintf(stderr,
3588 "Usage: weston [OPTIONS]\n\n"
3589 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3590 "Weston supports multiple backends, and depending on which backend is in use\n"
3591 "different options will be accepted.\n\n"
3592
3593
3594 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003595 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003596 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003597 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3598 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003599 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003600 " -S, --socket=NAME\tName of socket to listen on\n"
3601 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003602 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003603 " --log==FILE\t\tLog to the given file\n"
3604 " -h, --help\t\tThis help message\n\n");
3605
3606 fprintf(stderr,
3607 "Options for drm-backend.so:\n\n"
3608 " --connector=ID\tBring up only this connector\n"
3609 " --seat=SEAT\t\tThe seat that weston should run on\n"
3610 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003611 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003612 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3613
3614 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003615 "Options for fbdev-backend.so:\n\n"
3616 " --tty=TTY\t\tThe tty to use\n"
3617 " --device=DEVICE\tThe framebuffer device to use\n\n");
3618
3619 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003620 "Options for x11-backend.so:\n\n"
3621 " --width=WIDTH\t\tWidth of X window\n"
3622 " --height=HEIGHT\tHeight of X window\n"
3623 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003624 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003625 " --output-count=COUNT\tCreate multiple outputs\n"
3626 " --no-input\t\tDont create input devices\n\n");
3627
3628 fprintf(stderr,
3629 "Options for wayland-backend.so:\n\n"
3630 " --width=WIDTH\t\tWidth of Wayland surface\n"
3631 " --height=HEIGHT\tHeight of Wayland surface\n"
3632 " --display=DISPLAY\tWayland display to connect to\n\n");
3633
Pekka Paalanene31e0532013-05-22 18:03:07 +03003634#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3635 fprintf(stderr,
3636 "Options for rpi-backend.so:\n\n"
3637 " --tty=TTY\t\tThe tty to use\n"
3638 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3639 " --transform=TR\tThe output transformation, TR is one of:\n"
3640 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3641 "\n");
3642#endif
3643
Hardeningc077a842013-07-08 00:51:35 +02003644#if defined(BUILD_RDP_COMPOSITOR)
3645 fprintf(stderr,
3646 "Options for rdp-backend.so:\n\n"
3647 " --width=WIDTH\t\tWidth of desktop\n"
3648 " --height=HEIGHT\tHeight of desktop\n"
3649 " --extra-modes=MODES\t\n"
3650 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3651 " --address=ADDR\tThe address to bind\n"
3652 " --port=PORT\tThe port to listen on\n"
3653 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3654 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3655 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3656 "\n");
3657#endif
3658
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003659 exit(error_code);
3660}
3661
Peter Maatmane5b42e42013-03-27 22:38:53 +01003662static void
3663catch_signals(void)
3664{
3665 struct sigaction action;
3666
3667 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3668 action.sa_sigaction = on_caught_signal;
3669 sigemptyset(&action.sa_mask);
3670 sigaction(SIGSEGV, &action, NULL);
3671 sigaction(SIGABRT, &action, NULL);
3672}
3673
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003674int main(int argc, char *argv[])
3675{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003676 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003677 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003678 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003679 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003680 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003681 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003682 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003683 int *argc, char *argv[],
3684 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003685 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003686 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003687 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003688 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003689 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003690 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003691 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003692 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003693 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003694 struct weston_config *config;
3695 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003696
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003697 const struct weston_option core_options[] = {
3698 { WESTON_OPTION_STRING, "backend", 'B', &backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003699 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003700 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3701 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003702 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003703 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003704 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003705 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003706 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003707
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003708 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003709
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003710 if (help)
3711 usage(EXIT_SUCCESS);
3712
Scott Moreau12245142012-08-29 15:15:58 -06003713 if (version) {
3714 printf(PACKAGE_STRING "\n");
3715 return EXIT_SUCCESS;
3716 }
3717
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003718 weston_log_file_open(log);
3719
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003720 weston_log("%s\n"
3721 STAMP_SPACE "%s\n"
3722 STAMP_SPACE "Bug reports to: %s\n"
3723 STAMP_SPACE "Build: %s\n",
3724 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003725 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003726 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003727
Martin Minarik37032f82012-06-18 20:15:18 +02003728 verify_xdg_runtime_dir();
3729
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003730 display = wl_display_create();
3731
Tiago Vignatti2116b892011-08-08 05:52:59 -07003732 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003733 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3734 display);
3735 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3736 display);
3737 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3738 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003739
3740 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003741 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3742 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003743
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003744 if (!backend) {
3745 if (getenv("WAYLAND_DISPLAY"))
3746 backend = "wayland-backend.so";
3747 else if (getenv("DISPLAY"))
3748 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003749 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003750 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003751 }
3752
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003753 config = weston_config_parse("weston.ini");
Alexandru DAMIAN5f429302013-09-26 10:27:16 +01003754 if (config != NULL) {
3755 weston_log("Using config file '%s'\n",
3756 weston_config_get_full_path(config));
3757 } else {
3758 weston_log("Starting with no config file.\n");
3759 }
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003760 section = weston_config_get_section(config, "core", NULL, NULL);
Jason Ekstranda985da42013-08-22 17:28:03 -05003761 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003762
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003763 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003764 if (!backend_init)
3765 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003766
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003767 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003768 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003769 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003770 exit(EXIT_FAILURE);
3771 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003772
Peter Maatmane5b42e42013-03-27 22:38:53 +01003773 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003774 segv_compositor = ec;
3775
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003776 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003777
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003778 setenv("WAYLAND_DISPLAY", socket_name, 1);
3779
Jason Ekstranda985da42013-08-22 17:28:03 -05003780 if (!shell)
3781 weston_config_section_get_string(section, "shell", &shell,
3782 "desktop-shell.so");
3783 if (load_modules(ec, shell, &argc, argv) < 0)
3784 goto out;
3785
Ossama Othmana50e6e42013-05-14 09:48:26 -07003786 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003787 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003788 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003789 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003790
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003791 for (i = 1; i < argc; i++)
3792 weston_log("fatal: unhandled option: %s\n", argv[i]);
3793 if (argc > 1) {
3794 ret = EXIT_FAILURE;
3795 goto out;
3796 }
3797
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003798 weston_compositor_log_capabilities(ec);
3799
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003800 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003801 weston_log("fatal: failed to add socket: %m\n");
3802 ret = EXIT_FAILURE;
3803 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003804 }
3805
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003806 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003807
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003808 wl_display_run(display);
3809
3810 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003811 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003812 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003813
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003814 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003815
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003816 for (i = ARRAY_LENGTH(signals); i;)
3817 wl_event_source_remove(signals[--i]);
3818
Daniel Stone855028f2012-05-01 20:37:10 +01003819 weston_compositor_xkb_destroy(ec);
3820
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003821 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003822 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003823
Martin Minarik19e6f262012-06-07 13:08:46 +02003824 weston_log_file_close();
3825
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003826 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003827}