blob: 16d04819c58d45782ec39c4f83dd39e7ff859062 [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
Pekka Paalanen50719bc2011-11-22 14:18:50 +020055#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
U. Artie Eoffec08f332013-05-13 15:55:47 -070057#include "subsurface-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030058#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040059#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050060#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061
Kristian Høgsberg27da5382011-06-21 17:32:25 -040062static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040063static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040064
65static int
66sigchld_handler(int signal_number, void *data)
67{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050068 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040069 int status;
70 pid_t pid;
71
Bill Spitzak027b9622012-03-17 15:22:03 -070072 pid = waitpid(-1, &status, WNOHANG);
73 if (!pid)
74 return 1;
75
Kristian Høgsberg27da5382011-06-21 17:32:25 -040076 wl_list_for_each(p, &child_process_list, link) {
77 if (p->pid == pid)
78 break;
79 }
80
81 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020082 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040083 return 1;
84 }
85
86 wl_list_remove(&p->link);
87 p->cleanup(p, status);
88
89 return 1;
90}
91
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020092static void
Alexander Larsson0b135062013-05-28 16:23:36 +020093weston_output_transform_scale_init(struct weston_output *output,
94 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020095
Rob Bradford27b17932013-06-26 18:08:46 +010096static void
97weston_compositor_build_surface_list(struct weston_compositor *compositor);
98
Alex Wu2dda6042012-04-17 17:20:47 +080099WL_EXPORT int
Alexander Larsson355748e2013-05-28 16:23:38 +0200100weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode, int32_t scale)
Alex Wu2dda6042012-04-17 17:20:47 +0800101{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200102 struct weston_seat *seat;
103 pixman_region32_t old_output_region;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200104 int ret;
105
Alex Wu2dda6042012-04-17 17:20:47 +0800106 if (!output->switch_mode)
107 return -1;
108
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200109 ret = output->switch_mode(output, mode);
110 if (ret < 0)
111 return ret;
112
Alexander Larsson355748e2013-05-28 16:23:38 +0200113 output->scale = scale;
114
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200115 pixman_region32_init(&old_output_region);
116 pixman_region32_copy(&old_output_region, &output->region);
117
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200118 /* Update output region and transformation matrix */
Alexander Larsson0b135062013-05-28 16:23:36 +0200119 weston_output_transform_scale_init(output, output->transform, output->scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200120
121 pixman_region32_init(&output->previous_damage);
122 pixman_region32_init_rect(&output->region, output->x, output->y,
123 output->width, output->height);
124
125 weston_output_update_matrix(output);
126
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200127 /* If a pointer falls outside the outputs new geometry, move it to its
128 * lower-right corner */
129 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400130 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200131 int32_t x, y;
132
133 if (!pointer)
134 continue;
135
136 x = wl_fixed_to_int(pointer->x);
137 y = wl_fixed_to_int(pointer->y);
138
139 if (!pixman_region32_contains_point(&old_output_region,
140 x, y, NULL) ||
141 pixman_region32_contains_point(&output->region,
142 x, y, NULL))
143 continue;
144
145 if (x >= output->x + output->width)
146 x = output->x + output->width - 1;
147 if (y >= output->y + output->height)
148 y = output->y + output->height - 1;
149
150 pointer->x = wl_fixed_from_int(x);
151 pointer->y = wl_fixed_from_int(y);
152 }
153
154 pixman_region32_fini(&old_output_region);
155
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200156 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800157}
158
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400159WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500160weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400161{
162 wl_list_insert(&child_process_list, &process->link);
163}
164
Benjamin Franzke06286262011-05-06 19:12:33 +0200165static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200166child_client_exec(int sockfd, const char *path)
167{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500168 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200169 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200170 sigset_t allsigs;
171
172 /* do not give our signal mask to the new process */
173 sigfillset(&allsigs);
174 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200175
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500176 /* Launch clients as the user. */
177 seteuid(getuid());
178
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500179 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
180 * non-CLOEXEC fd to pass through exec. */
181 clientfd = dup(sockfd);
182 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200183 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500184 return;
185 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200186
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500187 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200188 setenv("WAYLAND_SOCKET", s, 1);
189
190 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200191 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200192 path);
193}
194
195WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500196weston_client_launch(struct weston_compositor *compositor,
197 struct weston_process *proc,
198 const char *path,
199 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200200{
201 int sv[2];
202 pid_t pid;
203 struct wl_client *client;
204
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300205 weston_log("launching '%s'\n", path);
206
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300207 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200208 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200209 "socketpair failed while launching '%s': %m\n",
210 path);
211 return NULL;
212 }
213
214 pid = fork();
215 if (pid == -1) {
216 close(sv[0]);
217 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200218 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200219 "fork failed while launching '%s': %m\n", path);
220 return NULL;
221 }
222
223 if (pid == 0) {
224 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700225 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200226 }
227
228 close(sv[1]);
229
230 client = wl_client_create(compositor->wl_display, sv[0]);
231 if (!client) {
232 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200233 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200234 "wl_client_create failed while launching '%s'.\n",
235 path);
236 return NULL;
237 }
238
239 proc->pid = pid;
240 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500241 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242
243 return client;
244}
245
246static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300247surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
248{
249 struct weston_surface *surface =
250 container_of(listener, struct weston_surface,
251 pending.buffer_destroy_listener);
252
253 surface->pending.buffer = NULL;
254}
255
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200256static void
257empty_region(pixman_region32_t *region)
258{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300259 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200260 pixman_region32_init(region);
261}
262
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300263static void
264region_init_infinite(pixman_region32_t *region)
265{
266 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
267 UINT32_MAX, UINT32_MAX);
268}
269
Pekka Paalanene67858b2013-04-25 13:57:42 +0300270static struct weston_subsurface *
271weston_surface_to_subsurface(struct weston_surface *surface);
272
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500273WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500274weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500275{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500276 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400277
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200278 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400279 if (surface == NULL)
280 return NULL;
281
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500282 wl_signal_init(&surface->destroy_signal);
283
284 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500285
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500286 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500287 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500288
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500289 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400290 surface->alpha = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400291
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100292 if (compositor->renderer->create_surface(surface) < 0) {
293 free(surface);
294 return NULL;
295 }
296
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200297 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200298 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200299 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200300 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400301 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400302 surface->plane = &compositor->primary_plane;
Giulio Camuffo184df502013-02-21 11:29:21 +0100303 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200304
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400305 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500306 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500307 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300308 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200309 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500310 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400311
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200312 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200313 wl_list_insert(&surface->geometry.transformation_list,
314 &surface->transform.position.link);
315 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200316 wl_list_init(&surface->geometry.child_list);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200317 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200318 surface->transform.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500319
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300320 surface->pending.buffer_destroy_listener.notify =
321 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300322 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300323 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300324 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300325 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300326
Pekka Paalanene67858b2013-04-25 13:57:42 +0300327 wl_list_init(&surface->subsurface_list);
328 wl_list_init(&surface->subsurface_list_pending);
329
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400330 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500331}
332
Alex Wu8811bf92012-02-28 18:07:54 +0800333WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500334weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200335 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500336{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100337 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500338}
339
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400340WL_EXPORT void
341weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200342 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200343{
344 if (surface->transform.enabled) {
345 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
346
347 weston_matrix_transform(&surface->transform.matrix, &v);
348
349 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200350 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700351 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200352 v.f[3]);
353 *x = 0;
354 *y = 0;
355 return;
356 }
357
358 *x = v.f[0] / v.f[3];
359 *y = v.f[1] / v.f[3];
360 } else {
361 *x = sx + surface->geometry.x;
362 *y = sy + surface->geometry.y;
363 }
364}
365
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500366WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200367weston_transformed_coord(int width, int height,
368 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200369 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200370 float sx, float sy, float *bx, float *by)
371{
372 switch (transform) {
373 case WL_OUTPUT_TRANSFORM_NORMAL:
374 default:
375 *bx = sx;
376 *by = sy;
377 break;
378 case WL_OUTPUT_TRANSFORM_FLIPPED:
379 *bx = width - sx;
380 *by = sy;
381 break;
382 case WL_OUTPUT_TRANSFORM_90:
383 *bx = height - sy;
384 *by = sx;
385 break;
386 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
387 *bx = height - sy;
388 *by = width - sx;
389 break;
390 case WL_OUTPUT_TRANSFORM_180:
391 *bx = width - sx;
392 *by = height - sy;
393 break;
394 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
395 *bx = sx;
396 *by = height - sy;
397 break;
398 case WL_OUTPUT_TRANSFORM_270:
399 *bx = sy;
400 *by = width - sx;
401 break;
402 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
403 *bx = sy;
404 *by = sx;
405 break;
406 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200407
408 *bx *= scale;
409 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200410}
411
412WL_EXPORT pixman_box32_t
413weston_transformed_rect(int width, int height,
414 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200415 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200416 pixman_box32_t rect)
417{
418 float x1, x2, y1, y2;
419
420 pixman_box32_t ret;
421
Alexander Larsson4ea95522013-05-22 14:41:37 +0200422 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200423 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200424 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200425 rect.x2, rect.y2, &x2, &y2);
426
427 if (x1 <= x2) {
428 ret.x1 = x1;
429 ret.x2 = x2;
430 } else {
431 ret.x1 = x2;
432 ret.x2 = x1;
433 }
434
435 if (y1 <= y2) {
436 ret.y1 = y1;
437 ret.y2 = y2;
438 } else {
439 ret.y1 = y2;
440 ret.y2 = y1;
441 }
442
443 return ret;
444}
445
446WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200447weston_surface_to_buffer_float(struct weston_surface *surface,
448 float sx, float sy, float *bx, float *by)
449{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200450 weston_transformed_coord(surface->geometry.width,
451 surface->geometry.height,
452 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200453 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200454 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200455}
456
Alexander Larsson4ea95522013-05-22 14:41:37 +0200457WL_EXPORT void
458weston_surface_to_buffer(struct weston_surface *surface,
459 int sx, int sy, int *bx, int *by)
460{
461 float bxf, byf;
462
463 weston_transformed_coord(surface->geometry.width,
464 surface->geometry.height,
465 surface->buffer_transform,
466 surface->buffer_scale,
467 sx, sy, &bxf, &byf);
468 *bx = floorf(bxf);
469 *by = floorf(byf);
470}
471
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200472WL_EXPORT pixman_box32_t
473weston_surface_to_buffer_rect(struct weston_surface *surface,
474 pixman_box32_t rect)
475{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200476 return weston_transformed_rect(surface->geometry.width,
477 surface->geometry.height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200478 surface->buffer_transform,
479 surface->buffer_scale,
480 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200481}
482
483WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400484weston_surface_move_to_plane(struct weston_surface *surface,
485 struct weston_plane *plane)
486{
487 if (surface->plane == plane)
488 return;
489
490 weston_surface_damage_below(surface);
491 surface->plane = plane;
492 weston_surface_damage(surface);
493}
494
495WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500496weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200497{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500498 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200499
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500500 pixman_region32_init(&damage);
501 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
502 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400503 pixman_region32_union(&surface->plane->damage,
504 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500505 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200506}
507
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400508static void
509weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
510{
511 uint32_t different = es->output_mask ^ mask;
512 uint32_t entered = mask & different;
513 uint32_t left = es->output_mask & different;
514 struct weston_output *output;
515 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500516 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400517
518 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500519 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400520 return;
521 if (different == 0)
522 return;
523
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500524 client = wl_resource_get_client(es->resource);
525
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400526 wl_list_for_each(output, &es->compositor->output_list, link) {
527 if (1 << output->id & different)
528 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500529 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400530 client);
531 if (resource == NULL)
532 continue;
533 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500534 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400535 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500536 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400537 }
538}
539
540static void
541weston_surface_assign_output(struct weston_surface *es)
542{
543 struct weston_compositor *ec = es->compositor;
544 struct weston_output *output, *new_output;
545 pixman_region32_t region;
546 uint32_t max, area, mask;
547 pixman_box32_t *e;
548
549 new_output = NULL;
550 max = 0;
551 mask = 0;
552 pixman_region32_init(&region);
553 wl_list_for_each(output, &ec->output_list, link) {
554 pixman_region32_intersect(&region, &es->transform.boundingbox,
555 &output->region);
556
557 e = pixman_region32_extents(&region);
558 area = (e->x2 - e->x1) * (e->y2 - e->y1);
559
560 if (area > 0)
561 mask |= 1 << output->id;
562
563 if (area >= max) {
564 new_output = output;
565 max = area;
566 }
567 }
568 pixman_region32_fini(&region);
569
570 es->output = new_output;
571 weston_surface_update_output_mask(es, mask);
572}
573
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200574static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200575surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
576 int32_t width, int32_t height,
577 pixman_region32_t *bbox)
578{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200579 float min_x = HUGE_VALF, min_y = HUGE_VALF;
580 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200581 int32_t s[4][2] = {
582 { sx, sy },
583 { sx, sy + height },
584 { sx + width, sy },
585 { sx + width, sy + height }
586 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200587 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200588 int i;
589
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300590 if (width == 0 || height == 0) {
591 /* avoid rounding empty bbox to 1x1 */
592 pixman_region32_init(bbox);
593 return;
594 }
595
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200596 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200597 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400598 weston_surface_to_global_float(surface,
599 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200600 if (x < min_x)
601 min_x = x;
602 if (x > max_x)
603 max_x = x;
604 if (y < min_y)
605 min_y = y;
606 if (y > max_y)
607 max_y = y;
608 }
609
Pekka Paalanen219b9822012-02-08 15:38:37 +0200610 int_x = floorf(min_x);
611 int_y = floorf(min_y);
612 pixman_region32_init_rect(bbox, int_x, int_y,
613 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200614}
615
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200616static void
617weston_surface_update_transform_disable(struct weston_surface *surface)
618{
619 surface->transform.enabled = 0;
620
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200621 /* round off fractions when not transformed */
622 surface->geometry.x = roundf(surface->geometry.x);
623 surface->geometry.y = roundf(surface->geometry.y);
624
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500625 /* Otherwise identity matrix, but with x and y translation. */
626 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
627 surface->transform.position.matrix.d[12] = surface->geometry.x;
628 surface->transform.position.matrix.d[13] = surface->geometry.y;
629
630 surface->transform.matrix = surface->transform.position.matrix;
631
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500632 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500633 surface->transform.inverse.d[12] = -surface->geometry.x;
634 surface->transform.inverse.d[13] = -surface->geometry.y;
635
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200636 pixman_region32_init_rect(&surface->transform.boundingbox,
637 surface->geometry.x,
638 surface->geometry.y,
639 surface->geometry.width,
640 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500641
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400642 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500643 pixman_region32_copy(&surface->transform.opaque,
644 &surface->opaque);
645 pixman_region32_translate(&surface->transform.opaque,
646 surface->geometry.x,
647 surface->geometry.y);
648 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200649}
650
651static int
652weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200653{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200654 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200655 struct weston_matrix *matrix = &surface->transform.matrix;
656 struct weston_matrix *inverse = &surface->transform.inverse;
657 struct weston_transform *tform;
658
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200659 surface->transform.enabled = 1;
660
661 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300662 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200663 surface->transform.position.matrix.d[12] = surface->geometry.x;
664 surface->transform.position.matrix.d[13] = surface->geometry.y;
665
666 weston_matrix_init(matrix);
667 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
668 weston_matrix_multiply(matrix, &tform->matrix);
669
Pekka Paalanen483243f2013-03-08 14:56:50 +0200670 if (parent)
671 weston_matrix_multiply(matrix, &parent->transform.matrix);
672
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200673 if (weston_matrix_invert(inverse, matrix) < 0) {
674 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200675 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200676 " transformation not invertible.\n", surface);
677 return -1;
678 }
679
680 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
681 surface->geometry.height,
682 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500683
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200684 return 0;
685}
686
687WL_EXPORT void
688weston_surface_update_transform(struct weston_surface *surface)
689{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200690 struct weston_surface *parent = surface->geometry.parent;
691
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200692 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200693 return;
694
Pekka Paalanen483243f2013-03-08 14:56:50 +0200695 if (parent)
696 weston_surface_update_transform(parent);
697
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200698 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200699
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500700 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200701
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200702 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500703 pixman_region32_fini(&surface->transform.opaque);
704 pixman_region32_init(&surface->transform.opaque);
705
Pekka Paalanencd403622012-01-25 13:37:39 +0200706 /* transform.position is always in transformation_list */
707 if (surface->geometry.transformation_list.next ==
708 &surface->transform.position.link &&
709 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200710 &surface->transform.position.link &&
711 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200712 weston_surface_update_transform_disable(surface);
713 } else {
714 if (weston_surface_update_transform_enable(surface) < 0)
715 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200716 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200717
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400718 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200719
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300720 weston_surface_assign_output(surface);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300721
722 wl_signal_emit(&surface->compositor->transform_signal, surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200723}
724
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200725WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200726weston_surface_geometry_dirty(struct weston_surface *surface)
727{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200728 struct weston_surface *child;
729
730 /*
731 * The invariant: if surface->geometry.dirty, then all surfaces
732 * in surface->geometry.child_list have geometry.dirty too.
733 * Corollary: if not parent->geometry.dirty, then all ancestors
734 * are not dirty.
735 */
736
737 if (surface->transform.dirty)
738 return;
739
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200740 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200741
742 wl_list_for_each(child, &surface->geometry.child_list,
743 geometry.parent_link)
744 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200745}
746
747WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100748weston_surface_to_global_fixed(struct weston_surface *surface,
749 wl_fixed_t sx, wl_fixed_t sy,
750 wl_fixed_t *x, wl_fixed_t *y)
751{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200752 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100753
754 weston_surface_to_global_float(surface,
755 wl_fixed_to_double(sx),
756 wl_fixed_to_double(sy),
757 &xf, &yf);
758 *x = wl_fixed_from_double(xf);
759 *y = wl_fixed_from_double(yf);
760}
761
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400762WL_EXPORT void
763weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200764 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200765{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200766 if (surface->transform.enabled) {
767 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
768
769 weston_matrix_transform(&surface->transform.inverse, &v);
770
771 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200772 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200773 "weston_surface_from_global(), divisor = %g\n",
774 v.f[3]);
775 *sx = 0;
776 *sy = 0;
777 return;
778 }
779
Pekka Paalanencd403622012-01-25 13:37:39 +0200780 *sx = v.f[0] / v.f[3];
781 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200782 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200783 *sx = x - surface->geometry.x;
784 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200785 }
786}
787
788WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100789weston_surface_from_global_fixed(struct weston_surface *surface,
790 wl_fixed_t x, wl_fixed_t y,
791 wl_fixed_t *sx, wl_fixed_t *sy)
792{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200793 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100794
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400795 weston_surface_from_global_float(surface,
796 wl_fixed_to_double(x),
797 wl_fixed_to_double(y),
798 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100799 *sx = wl_fixed_from_double(sxf);
800 *sy = wl_fixed_from_double(syf);
801}
802
803WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200804weston_surface_from_global(struct weston_surface *surface,
805 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
806{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200807 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200808
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400809 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200810 *sx = floorf(sxf);
811 *sy = floorf(syf);
812}
813
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400814WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400815weston_surface_schedule_repaint(struct weston_surface *surface)
816{
817 struct weston_output *output;
818
819 wl_list_for_each(output, &surface->compositor->output_list, link)
820 if (surface->output_mask & (1 << output->id))
821 weston_output_schedule_repaint(output);
822}
823
824WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500825weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500826{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400827 pixman_region32_union_rect(&surface->damage, &surface->damage,
828 0, 0, surface->geometry.width,
829 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200830
Kristian Høgsberg98238702012-08-03 16:29:12 -0400831 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500832}
833
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400834WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500835weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200836 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400837{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200838 surface->geometry.x = x;
839 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200840 surface->geometry.width = width;
841 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200842 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400843}
844
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200845WL_EXPORT void
846weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200847 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200848{
849 surface->geometry.x = x;
850 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200851 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200852}
853
Pekka Paalanen483243f2013-03-08 14:56:50 +0200854static void
855transform_parent_handle_parent_destroy(struct wl_listener *listener,
856 void *data)
857{
858 struct weston_surface *surface =
859 container_of(listener, struct weston_surface,
860 geometry.parent_destroy_listener);
861
862 weston_surface_set_transform_parent(surface, NULL);
863}
864
865WL_EXPORT void
866weston_surface_set_transform_parent(struct weston_surface *surface,
867 struct weston_surface *parent)
868{
869 if (surface->geometry.parent) {
870 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
871 wl_list_remove(&surface->geometry.parent_link);
872 }
873
874 surface->geometry.parent = parent;
875
876 surface->geometry.parent_destroy_listener.notify =
877 transform_parent_handle_parent_destroy;
878 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500879 wl_signal_add(&parent->destroy_signal,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200880 &surface->geometry.parent_destroy_listener);
881 wl_list_insert(&parent->geometry.child_list,
882 &surface->geometry.parent_link);
883 }
884
885 weston_surface_geometry_dirty(surface);
886}
887
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300888WL_EXPORT int
889weston_surface_is_mapped(struct weston_surface *surface)
890{
891 if (surface->output)
892 return 1;
893 else
894 return 0;
895}
896
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200897WL_EXPORT int32_t
898weston_surface_buffer_width(struct weston_surface *surface)
899{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200900 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200901 switch (surface->buffer_transform) {
902 case WL_OUTPUT_TRANSFORM_90:
903 case WL_OUTPUT_TRANSFORM_270:
904 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
905 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200906 width = surface->buffer_ref.buffer->height;
907 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200908 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200909 width = surface->buffer_ref.buffer->width;
910 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200911 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200912 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200913}
914
915WL_EXPORT int32_t
916weston_surface_buffer_height(struct weston_surface *surface)
917{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200918 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200919 switch (surface->buffer_transform) {
920 case WL_OUTPUT_TRANSFORM_90:
921 case WL_OUTPUT_TRANSFORM_270:
922 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
923 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200924 height = surface->buffer_ref.buffer->width;
925 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200926 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200927 height = surface->buffer_ref.buffer->height;
928 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200929 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200930 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200931}
932
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400933WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500934weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500935{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400936 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500937
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400938 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500939
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400940 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500941}
942
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400943WL_EXPORT struct weston_surface *
Tiago Vignatti9d393522012-02-10 16:26:19 +0200944weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100945 wl_fixed_t x, wl_fixed_t y,
946 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200947{
948 struct weston_surface *surface;
949
950 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100951 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500952 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100953 wl_fixed_to_int(*sx),
954 wl_fixed_to_int(*sy),
955 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200956 return surface;
957 }
958
959 return NULL;
960}
961
962static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500963weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400964{
Daniel Stone37816df2012-05-16 18:45:18 +0100965 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400966
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500967 if (!compositor->focus)
968 return;
969
Daniel Stone37816df2012-05-16 18:45:18 +0100970 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400971 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400972}
973
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400974WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500975weston_surface_unmap(struct weston_surface *surface)
976{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100977 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500978
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500979 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500980 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500981 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500982
Daniel Stone4dab5db2012-05-30 16:31:53 +0100983 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400984 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400985 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400986 if (seat->pointer && seat->pointer->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400987 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400988 NULL,
989 wl_fixed_from_int(0),
990 wl_fixed_from_int(0));
Daniel Stone4dab5db2012-05-30 16:31:53 +0100991 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500992
Kristian Høgsberg98238702012-08-03 16:29:12 -0400993 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500994}
995
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400996struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -0500997 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400998 struct wl_list link;
999};
1000
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001001
1002WL_EXPORT void
1003weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001004{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001005 /* Not a valid way to destroy a client surface */
1006 assert(surface->resource == NULL);
1007
1008 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1009
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001010 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001011 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001012
Pekka Paalanen483243f2013-03-08 14:56:50 +02001013 assert(wl_list_empty(&surface->geometry.child_list));
Pekka Paalanene67858b2013-04-25 13:57:42 +03001014 assert(wl_list_empty(&surface->subsurface_list_pending));
1015 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001016
Rob Bradford27b17932013-06-26 18:08:46 +01001017 if (weston_surface_is_mapped(surface)) {
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001018 weston_surface_unmap(surface);
Rob Bradford27b17932013-06-26 18:08:46 +01001019 weston_compositor_build_surface_list(compositor);
1020 }
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001021
Pekka Paalanenbc106382012-10-10 12:49:31 +03001022 wl_list_for_each_safe(cb, next,
1023 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001024 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001025
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001026 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001027 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001028 pixman_region32_fini(&surface->pending.damage);
1029
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001030 if (surface->pending.buffer)
1031 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1032
Pekka Paalanende685b82012-12-04 15:58:12 +02001033 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001034
Kristian Høgsberg42263852012-09-06 21:59:29 -04001035 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001036
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001037 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001038 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001039 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001040 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001041 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001042
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001043 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001044 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001045
Pekka Paalanen483243f2013-03-08 14:56:50 +02001046 weston_surface_set_transform_parent(surface, NULL);
1047
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001048 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001049}
1050
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001051static void
1052destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001053{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001054 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001055
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001056 surface->resource = NULL;
1057 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001058}
1059
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001060static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001061weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1062{
1063 struct weston_buffer *buffer =
1064 container_of(listener, struct weston_buffer, destroy_listener);
1065
1066 wl_signal_emit(&buffer->destroy_signal, buffer);
1067 free(buffer);
1068}
1069
1070struct weston_buffer *
1071weston_buffer_from_resource(struct wl_resource *resource)
1072{
1073 struct weston_buffer *buffer;
1074 struct wl_listener *listener;
1075
1076 listener = wl_resource_get_destroy_listener(resource,
1077 weston_buffer_destroy_handler);
1078
1079 if (listener) {
1080 buffer = container_of(listener, struct weston_buffer,
1081 destroy_listener);
1082 } else {
1083 buffer = malloc(sizeof *buffer);
1084 memset(buffer, 0, sizeof *buffer);
1085
1086 buffer->resource = resource;
1087 wl_signal_init(&buffer->destroy_signal);
1088 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
1089 wl_resource_add_destroy_listener(resource,
1090 &buffer->destroy_listener);
1091 }
1092
1093 return buffer;
1094}
1095
1096static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001097weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1098 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001099{
Pekka Paalanende685b82012-12-04 15:58:12 +02001100 struct weston_buffer_reference *ref =
1101 container_of(listener, struct weston_buffer_reference,
1102 destroy_listener);
1103
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001104 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001105 ref->buffer = NULL;
1106}
1107
1108WL_EXPORT void
1109weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001110 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001111{
1112 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001113 ref->buffer->busy_count--;
1114 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001115 assert(wl_resource_get_client(ref->buffer->resource));
1116 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001117 WL_BUFFER_RELEASE);
1118 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001119 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001120 }
1121
Pekka Paalanende685b82012-12-04 15:58:12 +02001122 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001123 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001124 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001125 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001126 }
1127
Pekka Paalanende685b82012-12-04 15:58:12 +02001128 ref->buffer = buffer;
1129 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1130}
1131
1132static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001133weston_surface_attach(struct weston_surface *surface,
1134 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001135{
1136 weston_buffer_reference(&surface->buffer_ref, buffer);
1137
Pekka Paalanena6421c42012-12-04 15:58:10 +02001138 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001139 if (weston_surface_is_mapped(surface))
1140 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001141 }
1142
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001143 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001144}
1145
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001146WL_EXPORT void
1147weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001148{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001149 wl_list_remove(&surface->layer_link);
1150 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001151 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001152 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001153}
1154
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001155WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001156weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001157{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001158 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001159
1160 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001161 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001162}
1163
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001164WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001165weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001166{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001167 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001168
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001169 pixman_region32_union(&compositor->primary_plane.damage,
1170 &compositor->primary_plane.damage,
1171 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001172 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001173}
1174
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001175static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001176surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001177 pixman_region32_t *opaque)
1178{
Pekka Paalanende685b82012-12-04 15:58:12 +02001179 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001180 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001181 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001182
1183 if (surface->transform.enabled) {
1184 pixman_box32_t *extents;
1185
1186 extents = pixman_region32_extents(&surface->damage);
1187 surface_compute_bbox(surface, extents->x1, extents->y1,
1188 extents->x2 - extents->x1,
1189 extents->y2 - extents->y1,
1190 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001191 pixman_region32_translate(&surface->damage,
1192 -surface->plane->x,
1193 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001194 } else {
1195 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001196 surface->geometry.x - surface->plane->x,
1197 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001198 }
1199
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001200 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001201 pixman_region32_union(&surface->plane->damage,
1202 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001203 empty_region(&surface->damage);
1204 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001205 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001206}
1207
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001208static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001209compositor_accumulate_damage(struct weston_compositor *ec)
1210{
1211 struct weston_plane *plane;
1212 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001213 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001214
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001215 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001216
1217 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001218 pixman_region32_copy(&plane->clip, &clip);
1219
1220 pixman_region32_init(&opaque);
1221
1222 wl_list_for_each(es, &ec->surface_list, link) {
1223 if (es->plane != plane)
1224 continue;
1225
1226 surface_accumulate_damage(es, &opaque);
1227 }
1228
1229 pixman_region32_union(&clip, &clip, &opaque);
1230 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001231 }
1232
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001233 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001234
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001235 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001236 /* Both the renderer and the backend have seen the buffer
1237 * by now. If renderer needs the buffer, it has its own
1238 * reference set. If the backend wants to keep the buffer
1239 * around for migrating the surface into a non-primary plane
1240 * later, keep_buffer is true. Otherwise, drop the core
1241 * reference now, and allow early buffer release. This enables
1242 * clients to use single-buffering.
1243 */
1244 if (!es->keep_buffer)
1245 weston_buffer_reference(&es->buffer_ref, NULL);
1246 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001247}
1248
1249static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001250surface_list_add(struct weston_compositor *compositor,
1251 struct weston_surface *surface)
1252{
1253 struct weston_subsurface *sub;
1254
1255 if (wl_list_empty(&surface->subsurface_list)) {
1256 weston_surface_update_transform(surface);
1257 wl_list_insert(compositor->surface_list.prev, &surface->link);
1258 return;
1259 }
1260
1261 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1262 if (!weston_surface_is_mapped(sub->surface))
1263 continue;
1264
1265 if (sub->surface == surface) {
1266 weston_surface_update_transform(sub->surface);
1267 wl_list_insert(compositor->surface_list.prev,
1268 &sub->surface->link);
1269 } else {
1270 surface_list_add(compositor, sub->surface);
1271 }
1272 }
1273}
1274
1275static void
1276weston_compositor_build_surface_list(struct weston_compositor *compositor)
1277{
1278 struct weston_surface *surface;
1279 struct weston_layer *layer;
1280
1281 wl_list_init(&compositor->surface_list);
1282 wl_list_for_each(layer, &compositor->layer_list, link) {
1283 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1284 surface_list_add(compositor, surface);
1285 }
1286 }
1287}
1288
1289static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001290weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001291{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001292 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001293 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001294 struct weston_animation *animation, *next;
1295 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001296 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001297 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001298
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001299 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001300 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001301
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001302 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001303 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001304 else
1305 wl_list_for_each(es, &ec->surface_list, link)
1306 weston_surface_move_to_plane(es, &ec->primary_plane);
1307
Pekka Paalanene67858b2013-04-25 13:57:42 +03001308 wl_list_init(&frame_callback_list);
1309 wl_list_for_each(es, &ec->surface_list, link) {
1310 if (es->output == output) {
1311 wl_list_insert_list(&frame_callback_list,
1312 &es->frame_callback_list);
1313 wl_list_init(&es->frame_callback_list);
1314 }
1315 }
1316
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001317 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001318
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001319 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001320 pixman_region32_intersect(&output_damage,
1321 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001322 pixman_region32_subtract(&output_damage,
1323 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001324
Scott Moreauccbf29d2012-02-22 14:21:41 -07001325 if (output->dirty)
1326 weston_output_update_matrix(output);
1327
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001328 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001329
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001330 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001331
Kristian Høgsbergef044142011-06-21 15:02:12 -04001332 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001333
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001334 weston_compositor_repick(ec);
1335 wl_event_loop_dispatch(ec->input_loop, 0);
1336
Jonas Ådahldb773762012-06-13 00:01:21 +02001337 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001338 wl_callback_send_done(cb->resource, msecs);
1339 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001340 }
1341
Scott Moreaud64cf212012-06-08 19:40:54 -06001342 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001343 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001344 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001345 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001346}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001347
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001348static int
1349weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001350{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001351 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001352
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001353 wl_event_loop_dispatch(compositor->input_loop, 0);
1354
1355 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001356}
1357
1358WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001359weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001360{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001361 struct weston_compositor *compositor = output->compositor;
1362 struct wl_event_loop *loop =
1363 wl_display_get_event_loop(compositor->wl_display);
1364 int fd;
1365
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001366 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001367 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001368 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001369 return;
1370 }
1371
1372 output->repaint_scheduled = 0;
1373 if (compositor->input_loop_source)
1374 return;
1375
1376 fd = wl_event_loop_get_fd(compositor->input_loop);
1377 compositor->input_loop_source =
1378 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1379 weston_compositor_read_input, compositor);
1380}
1381
1382static void
1383idle_repaint(void *data)
1384{
1385 struct weston_output *output = data;
1386
Jonas Ådahle5a12252013-04-05 23:07:11 +02001387 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001388}
1389
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001390WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001391weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1392{
1393 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001394 if (below != NULL)
1395 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001396}
1397
1398WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001399weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001400{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001401 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001402 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001403
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001404 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1405 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001406 return;
1407
Kristian Høgsbergef044142011-06-21 15:02:12 -04001408 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001409 output->repaint_needed = 1;
1410 if (output->repaint_scheduled)
1411 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001412
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001413 wl_event_loop_add_idle(loop, idle_repaint, output);
1414 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001415
1416 if (compositor->input_loop_source) {
1417 wl_event_source_remove(compositor->input_loop_source);
1418 compositor->input_loop_source = NULL;
1419 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001420}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001421
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001422WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001423weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1424{
1425 struct weston_output *output;
1426
1427 wl_list_for_each(output, &compositor->output_list, link)
1428 weston_output_schedule_repaint(output);
1429}
1430
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001431static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001432surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001433{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001434 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001435}
1436
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001437static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001438surface_attach(struct wl_client *client,
1439 struct wl_resource *resource,
1440 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1441{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001442 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001443 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001444
1445 if (buffer_resource)
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001446 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001447
Pekka Paalanende685b82012-12-04 15:58:12 +02001448 /* Attach, attach, without commit in between does not send
1449 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001450 if (surface->pending.buffer)
1451 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001452
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001453 surface->pending.sx = sx;
1454 surface->pending.sy = sy;
1455 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001456 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001457 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001458 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001459 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001460 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001461}
1462
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001463static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001464surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001465 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001466 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001467{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001468 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001469
Pekka Paalanen8e159182012-10-10 12:49:25 +03001470 pixman_region32_union_rect(&surface->pending.damage,
1471 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001472 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001473}
1474
Kristian Høgsberg33418202011-08-16 23:01:28 -04001475static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001476destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001477{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001478 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001479
1480 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001481 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001482}
1483
1484static void
1485surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001486 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001487{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001488 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001489 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001490
1491 cb = malloc(sizeof *cb);
1492 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001493 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001494 return;
1495 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001496
Jason Ekstranda85118c2013-06-27 20:17:02 -05001497 cb->resource = wl_resource_create(client,
1498 &wl_callback_interface,
1499 1, callback);
1500 wl_resource_set_implementation(cb->resource, NULL, cb,
1501 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001502
Pekka Paalanenbc106382012-10-10 12:49:31 +03001503 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001504}
1505
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001506static void
1507surface_set_opaque_region(struct wl_client *client,
1508 struct wl_resource *resource,
1509 struct wl_resource *region_resource)
1510{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001511 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001512 struct weston_region *region;
1513
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001514 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001515 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001516 pixman_region32_copy(&surface->pending.opaque,
1517 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001518 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001519 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001520 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001521}
1522
1523static void
1524surface_set_input_region(struct wl_client *client,
1525 struct wl_resource *resource,
1526 struct wl_resource *region_resource)
1527{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001528 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001529 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001530
1531 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001532 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001533 pixman_region32_copy(&surface->pending.input,
1534 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001535 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001536 pixman_region32_fini(&surface->pending.input);
1537 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001538 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001539}
1540
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001541static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001542weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001543{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001544 struct weston_subsurface *sub;
1545
1546 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1547 parent_link_pending) {
1548 wl_list_remove(&sub->parent_link);
1549 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1550 }
1551}
1552
1553static void
1554weston_surface_commit(struct weston_surface *surface)
1555{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001556 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001557 int surface_width = 0;
1558 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001559
Alexander Larsson4ea95522013-05-22 14:41:37 +02001560 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001561 surface->buffer_transform = surface->pending.buffer_transform;
1562
Alexander Larsson4ea95522013-05-22 14:41:37 +02001563 /* wl_surface.set_buffer_scale */
1564 surface->buffer_scale = surface->pending.buffer_scale;
1565
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001566 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001567 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001568 weston_surface_attach(surface, surface->pending.buffer);
1569
Giulio Camuffo184df502013-02-21 11:29:21 +01001570 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001571 surface_width = weston_surface_buffer_width(surface);
1572 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001573 }
1574
1575 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001576 surface->configure(surface,
1577 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001578 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001579
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001580 if (surface->pending.buffer)
1581 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1582 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001583 surface->pending.sx = 0;
1584 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001585 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001586
1587 /* wl_surface.damage */
1588 pixman_region32_union(&surface->damage, &surface->damage,
1589 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001590 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1591 0, 0,
1592 surface->geometry.width,
1593 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001594 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001595
1596 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001597 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001598 surface->geometry.width,
1599 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001600 pixman_region32_intersect(&opaque,
1601 &opaque, &surface->pending.opaque);
1602
1603 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1604 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001605 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001606 }
1607
1608 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001609
1610 /* wl_surface.set_input_region */
1611 pixman_region32_fini(&surface->input);
1612 pixman_region32_init_rect(&surface->input, 0, 0,
1613 surface->geometry.width,
1614 surface->geometry.height);
1615 pixman_region32_intersect(&surface->input,
1616 &surface->input, &surface->pending.input);
1617
Pekka Paalanenbc106382012-10-10 12:49:31 +03001618 /* wl_surface.frame */
1619 wl_list_insert_list(&surface->frame_callback_list,
1620 &surface->pending.frame_callback_list);
1621 wl_list_init(&surface->pending.frame_callback_list);
1622
Pekka Paalanene67858b2013-04-25 13:57:42 +03001623 weston_surface_commit_subsurface_order(surface);
1624
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001625 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001626}
1627
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001628static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001629weston_subsurface_commit(struct weston_subsurface *sub);
1630
1631static void
1632weston_subsurface_parent_commit(struct weston_subsurface *sub,
1633 int parent_is_synchronized);
1634
1635static void
1636surface_commit(struct wl_client *client, struct wl_resource *resource)
1637{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001638 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001639 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1640
1641 if (sub) {
1642 weston_subsurface_commit(sub);
1643 return;
1644 }
1645
1646 weston_surface_commit(surface);
1647
1648 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1649 if (sub->surface != surface)
1650 weston_subsurface_parent_commit(sub, 0);
1651 }
1652}
1653
1654static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001655surface_set_buffer_transform(struct wl_client *client,
1656 struct wl_resource *resource, int transform)
1657{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001658 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001659
1660 surface->pending.buffer_transform = transform;
1661}
1662
Alexander Larsson4ea95522013-05-22 14:41:37 +02001663static void
1664surface_set_buffer_scale(struct wl_client *client,
1665 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001666 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001667{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001668 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001669
1670 surface->pending.buffer_scale = scale;
1671}
1672
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001673static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001674 surface_destroy,
1675 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001676 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001677 surface_frame,
1678 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001679 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001680 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001681 surface_set_buffer_transform,
1682 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001683};
1684
1685static void
1686compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001687 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001688{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04001689 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001690 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001691
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001692 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001693 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001694 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001695 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001696 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001697
Jason Ekstranda85118c2013-06-27 20:17:02 -05001698 surface->resource =
1699 wl_resource_create(client, &wl_surface_interface,
1700 wl_resource_get_version(resource), id);
1701 wl_resource_set_implementation(surface->resource, &surface_interface,
1702 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001703}
1704
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001705static void
1706destroy_region(struct wl_resource *resource)
1707{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001708 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001709
1710 pixman_region32_fini(&region->region);
1711 free(region);
1712}
1713
1714static void
1715region_destroy(struct wl_client *client, struct wl_resource *resource)
1716{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001717 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001718}
1719
1720static void
1721region_add(struct wl_client *client, struct wl_resource *resource,
1722 int32_t x, int32_t y, int32_t width, int32_t height)
1723{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001724 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001725
1726 pixman_region32_union_rect(&region->region, &region->region,
1727 x, y, width, height);
1728}
1729
1730static void
1731region_subtract(struct wl_client *client, struct wl_resource *resource,
1732 int32_t x, int32_t y, int32_t width, int32_t height)
1733{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001734 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001735 pixman_region32_t rect;
1736
1737 pixman_region32_init_rect(&rect, x, y, width, height);
1738 pixman_region32_subtract(&region->region, &region->region, &rect);
1739 pixman_region32_fini(&rect);
1740}
1741
1742static const struct wl_region_interface region_interface = {
1743 region_destroy,
1744 region_add,
1745 region_subtract
1746};
1747
1748static void
1749compositor_create_region(struct wl_client *client,
1750 struct wl_resource *resource, uint32_t id)
1751{
1752 struct weston_region *region;
1753
1754 region = malloc(sizeof *region);
1755 if (region == NULL) {
1756 wl_resource_post_no_memory(resource);
1757 return;
1758 }
1759
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001760 pixman_region32_init(&region->region);
1761
Jason Ekstranda85118c2013-06-27 20:17:02 -05001762 region->resource =
1763 wl_resource_create(client, &wl_region_interface, 1, id);
1764 wl_resource_set_implementation(region->resource, &region_interface,
1765 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001766}
1767
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001768static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001769 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001770 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001771};
1772
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001773static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001774weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1775{
1776 struct weston_surface *surface = sub->surface;
1777 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001778 int surface_width = 0;
1779 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001780
Alexander Larsson4ea95522013-05-22 14:41:37 +02001781 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001782 surface->buffer_transform = sub->cached.buffer_transform;
1783
Alexander Larsson4ea95522013-05-22 14:41:37 +02001784 /* wl_surface.set_buffer_scale */
1785 surface->buffer_scale = sub->cached.buffer_scale;
1786
Pekka Paalanene67858b2013-04-25 13:57:42 +03001787 /* wl_surface.attach */
1788 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1789 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1790 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1791
1792 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001793 surface_width = weston_surface_buffer_width(surface);
1794 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001795 }
1796
1797 if (surface->configure && sub->cached.newly_attached)
1798 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001799 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001800 sub->cached.sx = 0;
1801 sub->cached.sy = 0;
1802 sub->cached.newly_attached = 0;
1803
1804 /* wl_surface.damage */
1805 pixman_region32_union(&surface->damage, &surface->damage,
1806 &sub->cached.damage);
1807 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1808 0, 0,
1809 surface->geometry.width,
1810 surface->geometry.height);
1811 empty_region(&sub->cached.damage);
1812
1813 /* wl_surface.set_opaque_region */
1814 pixman_region32_init_rect(&opaque, 0, 0,
1815 surface->geometry.width,
1816 surface->geometry.height);
1817 pixman_region32_intersect(&opaque,
1818 &opaque, &sub->cached.opaque);
1819
1820 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1821 pixman_region32_copy(&surface->opaque, &opaque);
1822 weston_surface_geometry_dirty(surface);
1823 }
1824
1825 pixman_region32_fini(&opaque);
1826
1827 /* wl_surface.set_input_region */
1828 pixman_region32_fini(&surface->input);
1829 pixman_region32_init_rect(&surface->input, 0, 0,
1830 surface->geometry.width,
1831 surface->geometry.height);
1832 pixman_region32_intersect(&surface->input,
1833 &surface->input, &sub->cached.input);
1834
1835 /* wl_surface.frame */
1836 wl_list_insert_list(&surface->frame_callback_list,
1837 &sub->cached.frame_callback_list);
1838 wl_list_init(&sub->cached.frame_callback_list);
1839
1840 weston_surface_commit_subsurface_order(surface);
1841
1842 weston_surface_schedule_repaint(surface);
1843
1844 sub->cached.has_data = 0;
1845}
1846
1847static void
1848weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1849{
1850 struct weston_surface *surface = sub->surface;
1851
1852 /*
1853 * If this commit would cause the surface to move by the
1854 * attach(dx, dy) parameters, the old damage region must be
1855 * translated to correspond to the new surface coordinate system
1856 * origin.
1857 */
1858 pixman_region32_translate(&sub->cached.damage,
1859 -surface->pending.sx, -surface->pending.sy);
1860 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1861 &surface->pending.damage);
1862 empty_region(&surface->pending.damage);
1863
1864 if (surface->pending.newly_attached) {
1865 sub->cached.newly_attached = 1;
1866 weston_buffer_reference(&sub->cached.buffer_ref,
1867 surface->pending.buffer);
1868 }
1869 sub->cached.sx += surface->pending.sx;
1870 sub->cached.sy += surface->pending.sy;
1871 surface->pending.sx = 0;
1872 surface->pending.sy = 0;
1873 surface->pending.newly_attached = 0;
1874
1875 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001876 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001877
1878 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1879
1880 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1881
1882 wl_list_insert_list(&sub->cached.frame_callback_list,
1883 &surface->pending.frame_callback_list);
1884 wl_list_init(&surface->pending.frame_callback_list);
1885
1886 sub->cached.has_data = 1;
1887}
1888
1889static int
1890weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1891{
1892 while (sub) {
1893 if (sub->synchronized)
1894 return 1;
1895
1896 if (!sub->parent)
1897 return 0;
1898
1899 sub = weston_surface_to_subsurface(sub->parent);
1900 }
1901
1902 return 0;
1903}
1904
1905static void
1906weston_subsurface_commit(struct weston_subsurface *sub)
1907{
1908 struct weston_surface *surface = sub->surface;
1909 struct weston_subsurface *tmp;
1910
1911 /* Recursive check for effectively synchronized. */
1912 if (weston_subsurface_is_synchronized(sub)) {
1913 weston_subsurface_commit_to_cache(sub);
1914 } else {
1915 if (sub->cached.has_data) {
1916 /* flush accumulated state from cache */
1917 weston_subsurface_commit_to_cache(sub);
1918 weston_subsurface_commit_from_cache(sub);
1919 } else {
1920 weston_surface_commit(surface);
1921 }
1922
1923 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1924 if (tmp->surface != surface)
1925 weston_subsurface_parent_commit(tmp, 0);
1926 }
1927 }
1928}
1929
1930static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001931weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001932{
1933 struct weston_surface *surface = sub->surface;
1934 struct weston_subsurface *tmp;
1935
Pekka Paalanene67858b2013-04-25 13:57:42 +03001936 /* From now on, commit_from_cache the whole sub-tree, regardless of
1937 * the synchronized mode of each child. This sub-surface or some
1938 * of its ancestors were synchronized, so we are synchronized
1939 * all the way down.
1940 */
1941
1942 if (sub->cached.has_data)
1943 weston_subsurface_commit_from_cache(sub);
1944
1945 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1946 if (tmp->surface != surface)
1947 weston_subsurface_parent_commit(tmp, 1);
1948 }
1949}
1950
1951static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001952weston_subsurface_parent_commit(struct weston_subsurface *sub,
1953 int parent_is_synchronized)
1954{
1955 if (sub->position.set) {
1956 weston_surface_set_position(sub->surface,
1957 sub->position.x, sub->position.y);
1958 sub->position.set = 0;
1959 }
1960
1961 if (parent_is_synchronized || sub->synchronized)
1962 weston_subsurface_synchronized_commit(sub);
1963}
1964
1965static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001966subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
1967 int32_t width, int32_t height)
1968{
1969 struct weston_compositor *compositor = surface->compositor;
1970
1971 weston_surface_configure(surface,
1972 surface->geometry.x + dx,
1973 surface->geometry.y + dy,
1974 width, height);
1975
1976 /* No need to check parent mappedness, because if parent is not
1977 * mapped, parent is not in a visible layer, so this sub-surface
1978 * will not be drawn either.
1979 */
1980 if (!weston_surface_is_mapped(surface)) {
1981 wl_list_init(&surface->layer_link);
1982
1983 /* Cannot call weston_surface_update_transform(),
1984 * because that would call it also for the parent surface,
1985 * which might not be mapped yet. That would lead to
1986 * inconsistent state, where the window could never be
1987 * mapped.
1988 *
1989 * Instead just assing any output, to make
1990 * weston_surface_is_mapped() return true, so that when the
1991 * parent surface does get mapped, this one will get
1992 * included, too. See surface_list_add().
1993 */
1994 assert(!wl_list_empty(&compositor->output_list));
1995 surface->output = container_of(compositor->output_list.next,
1996 struct weston_output, link);
1997 }
1998}
1999
2000static struct weston_subsurface *
2001weston_surface_to_subsurface(struct weston_surface *surface)
2002{
2003 if (surface->configure == subsurface_configure)
2004 return surface->configure_private;
2005
2006 return NULL;
2007}
2008
Pekka Paalanen01388e22013-04-25 13:57:44 +03002009WL_EXPORT struct weston_surface *
2010weston_surface_get_main_surface(struct weston_surface *surface)
2011{
2012 struct weston_subsurface *sub;
2013
2014 while (surface && (sub = weston_surface_to_subsurface(surface)))
2015 surface = sub->parent;
2016
2017 return surface;
2018}
2019
Pekka Paalanene67858b2013-04-25 13:57:42 +03002020static void
2021subsurface_set_position(struct wl_client *client,
2022 struct wl_resource *resource, int32_t x, int32_t y)
2023{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002024 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002025
2026 if (!sub)
2027 return;
2028
2029 sub->position.x = x;
2030 sub->position.y = y;
2031 sub->position.set = 1;
2032}
2033
2034static struct weston_subsurface *
2035subsurface_from_surface(struct weston_surface *surface)
2036{
2037 struct weston_subsurface *sub;
2038
2039 sub = weston_surface_to_subsurface(surface);
2040 if (sub)
2041 return sub;
2042
2043 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2044 if (sub->surface == surface)
2045 return sub;
2046
2047 return NULL;
2048}
2049
2050static struct weston_subsurface *
2051subsurface_sibling_check(struct weston_subsurface *sub,
2052 struct weston_surface *surface,
2053 const char *request)
2054{
2055 struct weston_subsurface *sibling;
2056
2057 sibling = subsurface_from_surface(surface);
2058
2059 if (!sibling) {
2060 wl_resource_post_error(sub->resource,
2061 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2062 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002063 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002064 return NULL;
2065 }
2066
2067 if (sibling->parent != sub->parent) {
2068 wl_resource_post_error(sub->resource,
2069 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2070 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002071 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002072 return NULL;
2073 }
2074
2075 return sibling;
2076}
2077
2078static void
2079subsurface_place_above(struct wl_client *client,
2080 struct wl_resource *resource,
2081 struct wl_resource *sibling_resource)
2082{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002083 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002084 struct weston_surface *surface =
2085 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002086 struct weston_subsurface *sibling;
2087
2088 if (!sub)
2089 return;
2090
2091 sibling = subsurface_sibling_check(sub, surface, "place_above");
2092 if (!sibling)
2093 return;
2094
2095 wl_list_remove(&sub->parent_link_pending);
2096 wl_list_insert(sibling->parent_link_pending.prev,
2097 &sub->parent_link_pending);
2098}
2099
2100static void
2101subsurface_place_below(struct wl_client *client,
2102 struct wl_resource *resource,
2103 struct wl_resource *sibling_resource)
2104{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002105 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002106 struct weston_surface *surface =
2107 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002108 struct weston_subsurface *sibling;
2109
2110 if (!sub)
2111 return;
2112
2113 sibling = subsurface_sibling_check(sub, surface, "place_below");
2114 if (!sibling)
2115 return;
2116
2117 wl_list_remove(&sub->parent_link_pending);
2118 wl_list_insert(&sibling->parent_link_pending,
2119 &sub->parent_link_pending);
2120}
2121
2122static void
2123subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2124{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002125 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002126
2127 if (sub)
2128 sub->synchronized = 1;
2129}
2130
2131static void
2132subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2133{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002134 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002135
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002136 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002137 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002138
2139 /* If sub became effectively desynchronized, flush. */
2140 if (!weston_subsurface_is_synchronized(sub))
2141 weston_subsurface_synchronized_commit(sub);
2142 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002143}
2144
2145static void
2146weston_subsurface_cache_init(struct weston_subsurface *sub)
2147{
2148 pixman_region32_init(&sub->cached.damage);
2149 pixman_region32_init(&sub->cached.opaque);
2150 pixman_region32_init(&sub->cached.input);
2151 wl_list_init(&sub->cached.frame_callback_list);
2152 sub->cached.buffer_ref.buffer = NULL;
2153}
2154
2155static void
2156weston_subsurface_cache_fini(struct weston_subsurface *sub)
2157{
2158 struct weston_frame_callback *cb, *tmp;
2159
2160 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002161 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002162
2163 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2164 pixman_region32_fini(&sub->cached.damage);
2165 pixman_region32_fini(&sub->cached.opaque);
2166 pixman_region32_fini(&sub->cached.input);
2167}
2168
2169static void
2170weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2171{
2172 wl_list_remove(&sub->parent_link);
2173 wl_list_remove(&sub->parent_link_pending);
2174 wl_list_remove(&sub->parent_destroy_listener.link);
2175 sub->parent = NULL;
2176}
2177
2178static void
2179weston_subsurface_destroy(struct weston_subsurface *sub);
2180
2181static void
2182subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2183{
2184 struct weston_subsurface *sub =
2185 container_of(listener, struct weston_subsurface,
2186 surface_destroy_listener);
2187 assert(data == &sub->surface->resource);
2188
2189 /* The protocol object (wl_resource) is left inert. */
2190 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002191 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002192
2193 weston_subsurface_destroy(sub);
2194}
2195
2196static void
2197subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2198{
2199 struct weston_subsurface *sub =
2200 container_of(listener, struct weston_subsurface,
2201 parent_destroy_listener);
2202 assert(data == &sub->parent->resource);
2203 assert(sub->surface != sub->parent);
2204
2205 if (weston_surface_is_mapped(sub->surface))
2206 weston_surface_unmap(sub->surface);
2207
2208 weston_subsurface_unlink_parent(sub);
2209}
2210
2211static void
2212subsurface_resource_destroy(struct wl_resource *resource)
2213{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002214 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002215
2216 if (sub)
2217 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002218}
2219
2220static void
2221subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2222{
2223 wl_resource_destroy(resource);
2224}
2225
2226static void
2227weston_subsurface_link_parent(struct weston_subsurface *sub,
2228 struct weston_surface *parent)
2229{
2230 sub->parent = parent;
2231 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002232 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002233 &sub->parent_destroy_listener);
2234
2235 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2236 wl_list_insert(&parent->subsurface_list_pending,
2237 &sub->parent_link_pending);
2238}
2239
2240static void
2241weston_subsurface_link_surface(struct weston_subsurface *sub,
2242 struct weston_surface *surface)
2243{
2244 sub->surface = surface;
2245 sub->surface_destroy_listener.notify =
2246 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002247 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002248 &sub->surface_destroy_listener);
2249}
2250
2251static void
2252weston_subsurface_destroy(struct weston_subsurface *sub)
2253{
2254 assert(sub->surface);
2255
2256 if (sub->resource) {
2257 assert(weston_surface_to_subsurface(sub->surface) == sub);
2258 assert(sub->parent_destroy_listener.notify ==
2259 subsurface_handle_parent_destroy);
2260
2261 weston_surface_set_transform_parent(sub->surface, NULL);
2262 if (sub->parent)
2263 weston_subsurface_unlink_parent(sub);
2264
2265 weston_subsurface_cache_fini(sub);
2266
2267 sub->surface->configure = NULL;
2268 sub->surface->configure_private = NULL;
2269 } else {
2270 /* the dummy weston_subsurface for the parent itself */
2271 assert(sub->parent_destroy_listener.notify == NULL);
2272 wl_list_remove(&sub->parent_link);
2273 wl_list_remove(&sub->parent_link_pending);
2274 }
2275
2276 wl_list_remove(&sub->surface_destroy_listener.link);
2277 free(sub);
2278}
2279
2280static const struct wl_subsurface_interface subsurface_implementation = {
2281 subsurface_destroy,
2282 subsurface_set_position,
2283 subsurface_place_above,
2284 subsurface_place_below,
2285 subsurface_set_sync,
2286 subsurface_set_desync
2287};
2288
2289static struct weston_subsurface *
2290weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2291 struct weston_surface *parent)
2292{
2293 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002294 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002295
2296 sub = calloc(1, sizeof *sub);
2297 if (!sub)
2298 return NULL;
2299
Jason Ekstranda85118c2013-06-27 20:17:02 -05002300 sub->resource =
2301 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002302 if (!sub->resource) {
2303 free(sub);
2304 return NULL;
2305 }
2306
Jason Ekstranda85118c2013-06-27 20:17:02 -05002307 wl_resource_set_implementation(sub->resource,
2308 &subsurface_implementation,
2309 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002310 weston_subsurface_link_surface(sub, surface);
2311 weston_subsurface_link_parent(sub, parent);
2312 weston_subsurface_cache_init(sub);
2313 sub->synchronized = 1;
2314 weston_surface_set_transform_parent(surface, parent);
2315
2316 return sub;
2317}
2318
2319/* Create a dummy subsurface for having the parent itself in its
2320 * sub-surface lists. Makes stacking order manipulation easy.
2321 */
2322static struct weston_subsurface *
2323weston_subsurface_create_for_parent(struct weston_surface *parent)
2324{
2325 struct weston_subsurface *sub;
2326
2327 sub = calloc(1, sizeof *sub);
2328 if (!sub)
2329 return NULL;
2330
2331 weston_subsurface_link_surface(sub, parent);
2332 sub->parent = parent;
2333 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2334 wl_list_insert(&parent->subsurface_list_pending,
2335 &sub->parent_link_pending);
2336
2337 return sub;
2338}
2339
2340static void
2341subcompositor_get_subsurface(struct wl_client *client,
2342 struct wl_resource *resource,
2343 uint32_t id,
2344 struct wl_resource *surface_resource,
2345 struct wl_resource *parent_resource)
2346{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002347 struct weston_surface *surface =
2348 wl_resource_get_user_data(surface_resource);
2349 struct weston_surface *parent =
2350 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002351 struct weston_subsurface *sub;
2352 static const char where[] = "get_subsurface: wl_subsurface@";
2353
2354 if (surface == parent) {
2355 wl_resource_post_error(resource,
2356 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2357 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002358 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002359 return;
2360 }
2361
2362 if (weston_surface_to_subsurface(surface)) {
2363 wl_resource_post_error(resource,
2364 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2365 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002366 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002367 return;
2368 }
2369
2370 if (surface->configure) {
2371 wl_resource_post_error(resource,
2372 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2373 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002374 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002375 return;
2376 }
2377
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002378 if (weston_surface_get_main_surface(parent) == surface) {
2379 wl_resource_post_error(resource,
2380 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2381 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002382 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002383 return;
2384 }
2385
Pekka Paalanene67858b2013-04-25 13:57:42 +03002386 /* make sure the parent is in its own list */
2387 if (wl_list_empty(&parent->subsurface_list)) {
2388 if (!weston_subsurface_create_for_parent(parent)) {
2389 wl_resource_post_no_memory(resource);
2390 return;
2391 }
2392 }
2393
2394 sub = weston_subsurface_create(id, surface, parent);
2395 if (!sub) {
2396 wl_resource_post_no_memory(resource);
2397 return;
2398 }
2399
2400 surface->configure = subsurface_configure;
2401 surface->configure_private = sub;
2402}
2403
2404static void
2405subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2406{
2407 wl_resource_destroy(resource);
2408}
2409
2410static const struct wl_subcompositor_interface subcompositor_interface = {
2411 subcompositor_destroy,
2412 subcompositor_get_subsurface
2413};
2414
2415static void
2416bind_subcompositor(struct wl_client *client,
2417 void *data, uint32_t version, uint32_t id)
2418{
2419 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002420 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002421
Jason Ekstranda85118c2013-06-27 20:17:02 -05002422 resource =
2423 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
2424 if (resource)
2425 wl_resource_set_implementation(resource,
2426 &subcompositor_interface,
2427 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002428}
2429
2430static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002431weston_compositor_dpms(struct weston_compositor *compositor,
2432 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002433{
2434 struct weston_output *output;
2435
2436 wl_list_for_each(output, &compositor->output_list, link)
2437 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002438 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002439}
2440
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002441WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002442weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002443{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002444 switch (compositor->state) {
2445 case WESTON_COMPOSITOR_SLEEPING:
2446 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2447 /* fall through */
2448 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002449 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002450 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002451 /* fall through */
2452 default:
2453 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2454 wl_event_source_timer_update(compositor->idle_source,
2455 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002456 }
2457}
2458
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002459WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002460weston_compositor_offscreen(struct weston_compositor *compositor)
2461{
2462 switch (compositor->state) {
2463 case WESTON_COMPOSITOR_OFFSCREEN:
2464 return;
2465 case WESTON_COMPOSITOR_SLEEPING:
2466 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2467 /* fall through */
2468 default:
2469 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2470 wl_event_source_timer_update(compositor->idle_source, 0);
2471 }
2472}
2473
2474WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002475weston_compositor_sleep(struct weston_compositor *compositor)
2476{
2477 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2478 return;
2479
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002480 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002481 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2482 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2483}
2484
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002485static int
2486idle_handler(void *data)
2487{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002488 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002489
2490 if (compositor->idle_inhibit)
2491 return 1;
2492
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002493 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002494 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002495
2496 return 1;
2497}
2498
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002499WL_EXPORT void
2500weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2501{
2502 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002503 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002504 plane->x = x;
2505 plane->y = y;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002506
2507 /* Init the link so that the call to wl_list_remove() when releasing
2508 * the plane without ever stacking doesn't lead to a crash */
2509 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002510}
2511
2512WL_EXPORT void
2513weston_plane_release(struct weston_plane *plane)
2514{
2515 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002516 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002517
2518 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002519}
2520
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002521WL_EXPORT void
2522weston_compositor_stack_plane(struct weston_compositor *ec,
2523 struct weston_plane *plane,
2524 struct weston_plane *above)
2525{
2526 if (above)
2527 wl_list_insert(above->link.prev, &plane->link);
2528 else
2529 wl_list_insert(&ec->plane_list, &plane->link);
2530}
2531
Casey Dahlin9074db52012-04-19 22:50:09 -04002532static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002533{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002534 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002535}
2536
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002537static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002538bind_output(struct wl_client *client,
2539 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002540{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002541 struct weston_output *output = data;
2542 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002543 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002544
Jason Ekstranda85118c2013-06-27 20:17:02 -05002545 resource = wl_resource_create(client, &wl_output_interface,
2546 MIN(version, 2), id);
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002547
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002548 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002549 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002550
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002551 wl_output_send_geometry(resource,
2552 output->x,
2553 output->y,
2554 output->mm_width,
2555 output->mm_height,
2556 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002557 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002558 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002559 if (version >= 2)
2560 wl_output_send_scale(resource,
2561 output->scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002562
2563 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002564 wl_output_send_mode(resource,
2565 mode->flags,
2566 mode->width,
2567 mode->height,
2568 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002569 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002570
2571 if (version >= 2)
2572 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002573}
2574
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002575WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002576weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002577{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002578 struct weston_compositor *c = output->compositor;
2579
Richard Hughes64ddde12013-05-01 21:52:10 +01002580 wl_signal_emit(&output->destroy_signal, output);
2581
Richard Hughesafe690c2013-05-02 10:10:04 +01002582 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002583 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002584 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002585 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002586
2587 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002588}
2589
Scott Moreau1bad5db2012-08-18 01:04:05 -06002590static void
2591weston_output_compute_transform(struct weston_output *output)
2592{
2593 struct weston_matrix transform;
2594 int flip;
2595
2596 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002597 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002598
2599 switch(output->transform) {
2600 case WL_OUTPUT_TRANSFORM_FLIPPED:
2601 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2602 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2603 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002604 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002605 flip = -1;
2606 break;
2607 default:
2608 flip = 1;
2609 break;
2610 }
2611
2612 switch(output->transform) {
2613 case WL_OUTPUT_TRANSFORM_NORMAL:
2614 case WL_OUTPUT_TRANSFORM_FLIPPED:
2615 transform.d[0] = flip;
2616 transform.d[1] = 0;
2617 transform.d[4] = 0;
2618 transform.d[5] = 1;
2619 break;
2620 case WL_OUTPUT_TRANSFORM_90:
2621 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2622 transform.d[0] = 0;
2623 transform.d[1] = -flip;
2624 transform.d[4] = 1;
2625 transform.d[5] = 0;
2626 break;
2627 case WL_OUTPUT_TRANSFORM_180:
2628 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2629 transform.d[0] = -flip;
2630 transform.d[1] = 0;
2631 transform.d[4] = 0;
2632 transform.d[5] = -1;
2633 break;
2634 case WL_OUTPUT_TRANSFORM_270:
2635 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2636 transform.d[0] = 0;
2637 transform.d[1] = flip;
2638 transform.d[4] = -1;
2639 transform.d[5] = 0;
2640 break;
2641 default:
2642 break;
2643 }
2644
2645 weston_matrix_multiply(&output->matrix, &transform);
2646}
2647
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002648WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002649weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002650{
Scott Moreau850ca422012-05-21 15:21:25 -06002651 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002652 struct weston_matrix camera;
2653 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002654
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002655 weston_matrix_init(&output->matrix);
2656 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002657 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2658 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002659
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002660 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002661 2.0 / (output->width + output->border.left + output->border.right),
2662 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2663
2664 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002665
Scott Moreauccbf29d2012-02-22 14:21:41 -07002666 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002667 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002668 weston_matrix_init(&camera);
2669 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002670 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002671 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002672 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002673 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002674 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002675 weston_matrix_multiply(&output->matrix, &modelview);
2676 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002677
Scott Moreauccbf29d2012-02-22 14:21:41 -07002678 output->dirty = 0;
2679}
2680
Scott Moreau1bad5db2012-08-18 01:04:05 -06002681static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002682weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002683{
2684 output->transform = transform;
2685
2686 switch (transform) {
2687 case WL_OUTPUT_TRANSFORM_90:
2688 case WL_OUTPUT_TRANSFORM_270:
2689 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2690 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2691 /* Swap width and height */
2692 output->width = output->current->height;
2693 output->height = output->current->width;
2694 break;
2695 case WL_OUTPUT_TRANSFORM_NORMAL:
2696 case WL_OUTPUT_TRANSFORM_180:
2697 case WL_OUTPUT_TRANSFORM_FLIPPED:
2698 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2699 output->width = output->current->width;
2700 output->height = output->current->height;
2701 break;
2702 default:
2703 break;
2704 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002705
Alexander Larsson4ea95522013-05-22 14:41:37 +02002706 output->scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002707 output->width /= scale;
2708 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002709}
2710
Scott Moreauccbf29d2012-02-22 14:21:41 -07002711WL_EXPORT void
2712weston_output_move(struct weston_output *output, int x, int y)
2713{
2714 output->x = x;
2715 output->y = y;
2716
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002717 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002718 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002719 output->width,
2720 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002721}
2722
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002723WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002724weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002725 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002726 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002727{
2728 output->compositor = c;
2729 output->x = x;
2730 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002731 output->border.top = 0;
2732 output->border.bottom = 0;
2733 output->border.left = 0;
2734 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002735 output->mm_width = mm_width;
2736 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002737 output->dirty = 1;
Alexander Larssone32c3762013-05-28 16:23:37 +02002738 output->origin_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002739
Alexander Larsson0b135062013-05-28 16:23:36 +02002740 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002741 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002742
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002743 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002744 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002745
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002746 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002747 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002748 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002749 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002750
Casey Dahlin58ba1372012-04-19 22:50:08 -04002751 output->id = ffs(~output->compositor->output_id_pool) - 1;
2752 output->compositor->output_id_pool |= 1 << output->id;
2753
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002754 output->global =
2755 wl_display_add_global(c->wl_display, &wl_output_interface,
2756 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002757 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002758}
2759
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002760static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002761compositor_bind(struct wl_client *client,
2762 void *data, uint32_t version, uint32_t id)
2763{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002764 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002765 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002766
Jason Ekstranda85118c2013-06-27 20:17:02 -05002767 resource = wl_resource_create(client, &wl_compositor_interface,
2768 MIN(version, 3), id);
2769 if (resource)
2770 wl_resource_set_implementation(resource, &compositor_interface,
2771 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05002772}
2773
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002774static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002775log_uname(void)
2776{
2777 struct utsname usys;
2778
2779 uname(&usys);
2780
2781 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2782 usys.version, usys.machine);
2783}
2784
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002785WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002786weston_environment_get_fd(const char *env)
2787{
2788 char *e, *end;
2789 int fd, flags;
2790
2791 e = getenv(env);
2792 if (!e)
2793 return -1;
2794 fd = strtol(e, &end, 0);
2795 if (*end != '\0')
2796 return -1;
2797
2798 flags = fcntl(fd, F_GETFD);
2799 if (flags == -1)
2800 return -1;
2801
2802 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2803 unsetenv(env);
2804
2805 return fd;
2806}
2807
2808WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002809weston_compositor_init(struct weston_compositor *ec,
2810 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002811 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002812 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002813{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002814 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002815 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002816 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002817
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002818 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002819 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002820 wl_signal_init(&ec->destroy_signal);
2821 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002822 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002823 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002824 wl_signal_init(&ec->idle_signal);
2825 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002826 wl_signal_init(&ec->show_input_panel_signal);
2827 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002828 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002829 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002830 wl_signal_init(&ec->output_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002831 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002832
Casey Dahlin58ba1372012-04-19 22:50:08 -04002833 ec->output_id_pool = 0;
2834
Kristian Høgsberga8873122011-11-23 10:39:34 -05002835 if (!wl_display_add_global(display, &wl_compositor_interface,
2836 ec, compositor_bind))
2837 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002838
Pekka Paalanene67858b2013-04-25 13:57:42 +03002839 if (!wl_display_add_global(display, &wl_subcompositor_interface,
2840 ec, bind_subcompositor))
2841 return -1;
2842
Daniel Stone725c2c32012-06-22 14:04:36 +01002843 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002844 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002845 wl_list_init(&ec->layer_list);
2846 wl_list_init(&ec->seat_list);
2847 wl_list_init(&ec->output_list);
2848 wl_list_init(&ec->key_binding_list);
2849 wl_list_init(&ec->button_binding_list);
2850 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002851 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002852
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002853 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002854 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002855
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002856 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
2857 weston_config_section_get_string(s, "keymap_rules",
2858 (char **) &xkb_names.rules, NULL);
2859 weston_config_section_get_string(s, "keymap_model",
2860 (char **) &xkb_names.model, NULL);
2861 weston_config_section_get_string(s, "keymap_layout",
2862 (char **) &xkb_names.layout, NULL);
2863 weston_config_section_get_string(s, "keymap_variant",
2864 (char **) &xkb_names.variant, NULL);
2865 weston_config_section_get_string(s, "keymap_options",
2866 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01002867
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002868 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
2869 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01002870
2871 ec->ping_handler = NULL;
2872
2873 screenshooter_create(ec);
2874 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002875 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002876
2877 wl_data_device_manager_init(ec->wl_display);
2878
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002879 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002880
Daniel Stone725c2c32012-06-22 14:04:36 +01002881 loop = wl_display_get_event_loop(ec->wl_display);
2882 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2883 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2884
2885 ec->input_loop = wl_event_loop_create();
2886
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002887 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2888 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2889
2890 weston_compositor_schedule_repaint(ec);
2891
Daniel Stone725c2c32012-06-22 14:04:36 +01002892 return 0;
2893}
2894
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002895WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002896weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002897{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002898 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002899
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002900 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002901 if (ec->input_loop_source)
2902 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002903
Matt Roper361d2ad2011-08-29 13:52:23 -07002904 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002905 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002906 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002907
Daniel Stone325fc2d2012-05-30 16:31:58 +01002908 weston_binding_list_destroy_all(&ec->key_binding_list);
2909 weston_binding_list_destroy_all(&ec->button_binding_list);
2910 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002911 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002912
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002913 weston_plane_release(&ec->primary_plane);
2914
Jonas Ådahlc97af922012-03-28 22:36:09 +02002915 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002916
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002917 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07002918}
2919
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05002920WL_EXPORT void
2921weston_version(int *major, int *minor, int *micro)
2922{
2923 *major = WESTON_VERSION_MAJOR;
2924 *minor = WESTON_VERSION_MINOR;
2925 *micro = WESTON_VERSION_MICRO;
2926}
2927
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002928static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002929 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002930 const char *desc;
2931} capability_strings[] = {
2932 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002933 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002934};
2935
2936static void
2937weston_compositor_log_capabilities(struct weston_compositor *compositor)
2938{
2939 unsigned i;
2940 int yes;
2941
2942 weston_log("Compositor capabilities:\n");
2943 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
2944 yes = compositor->capabilities & capability_strings[i].bit;
2945 weston_log_continue(STAMP_SPACE "%s %s\n",
2946 capability_strings[i].desc,
2947 yes ? "yes" : "no");
2948 }
2949}
2950
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002951static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002952{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002953 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002954
Martin Minarik6d118362012-06-07 18:01:59 +02002955 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002956 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002957
2958 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002959}
2960
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002961#ifdef HAVE_LIBUNWIND
2962
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002963static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002964print_backtrace(void)
2965{
2966 unw_cursor_t cursor;
2967 unw_context_t context;
2968 unw_word_t off;
2969 unw_proc_info_t pip;
2970 int ret, i = 0;
2971 char procname[256];
2972 const char *filename;
2973 Dl_info dlinfo;
2974
2975 pip.unwind_info = NULL;
2976 ret = unw_getcontext(&context);
2977 if (ret) {
2978 weston_log("unw_getcontext: %d\n", ret);
2979 return;
2980 }
2981
2982 ret = unw_init_local(&cursor, &context);
2983 if (ret) {
2984 weston_log("unw_init_local: %d\n", ret);
2985 return;
2986 }
2987
2988 ret = unw_step(&cursor);
2989 while (ret > 0) {
2990 ret = unw_get_proc_info(&cursor, &pip);
2991 if (ret) {
2992 weston_log("unw_get_proc_info: %d\n", ret);
2993 break;
2994 }
2995
2996 ret = unw_get_proc_name(&cursor, procname, 256, &off);
2997 if (ret && ret != -UNW_ENOMEM) {
2998 if (ret != -UNW_EUNSPEC)
2999 weston_log("unw_get_proc_name: %d\n", ret);
3000 procname[0] = '?';
3001 procname[1] = 0;
3002 }
3003
3004 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3005 *dlinfo.dli_fname)
3006 filename = dlinfo.dli_fname;
3007 else
3008 filename = "?";
3009
3010 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3011 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3012
3013 ret = unw_step(&cursor);
3014 if (ret < 0)
3015 weston_log("unw_step: %d\n", ret);
3016 }
3017}
3018
3019#else
3020
3021static void
3022print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003023{
3024 void *buffer[32];
3025 int i, count;
3026 Dl_info info;
3027
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003028 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3029 for (i = 0; i < count; i++) {
3030 dladdr(buffer[i], &info);
3031 weston_log(" [%016lx] %s (%s)\n",
3032 (long) buffer[i],
3033 info.dli_sname ? info.dli_sname : "--",
3034 info.dli_fname);
3035 }
3036}
3037
3038#endif
3039
3040static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003041on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003042{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003043 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003044 * then call the backend restore function, which will switch
3045 * back to the vt we launched from or ungrab X etc and then
3046 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003047 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003048 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003049 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003050
Peter Maatmane5b42e42013-03-27 22:38:53 +01003051 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003052
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003053 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003054
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003055 segv_compositor->restore(segv_compositor);
3056
3057 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003058}
3059
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003060static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003061load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003062{
3063 char path[PATH_MAX];
3064 void *module, *init;
3065
3066 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003067 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003068 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003069 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003070
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003071 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3072 if (module) {
3073 weston_log("Module '%s' already loaded\n", path);
3074 dlclose(module);
3075 return NULL;
3076 }
3077
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003078 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003079 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003080 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003081 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003082 return NULL;
3083 }
3084
3085 init = dlsym(module, entrypoint);
3086 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003087 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003088 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003089 return NULL;
3090 }
3091
3092 return init;
3093}
3094
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003095static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003096load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003097 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003098{
3099 const char *p, *end;
3100 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003101 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003102 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003103
3104 if (modules == NULL)
3105 return 0;
3106
3107 p = modules;
3108 while (*p) {
3109 end = strchrnul(p, ',');
3110 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3111 module_init = load_module(buffer, "module_init");
3112 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003113 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003114 p = end;
3115 while (*p == ',')
3116 p++;
3117
3118 }
3119
3120 return 0;
3121}
3122
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003123static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003124 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3125
3126static const char xdg_wrong_message[] =
3127 "fatal: environment variable XDG_RUNTIME_DIR\n"
3128 "is set to \"%s\", which is not a directory.\n";
3129
3130static const char xdg_wrong_mode_message[] =
3131 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3132 "correctly. Unix access mode must be 0700 but is %o,\n"
3133 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003134 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003135
3136static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003137 "Refer to your distribution on how to get it, or\n"
3138 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3139 "on how to implement it.\n";
3140
Martin Minarik37032f82012-06-18 20:15:18 +02003141static void
3142verify_xdg_runtime_dir(void)
3143{
3144 char *dir = getenv("XDG_RUNTIME_DIR");
3145 struct stat s;
3146
3147 if (!dir) {
3148 weston_log(xdg_error_message);
3149 weston_log_continue(xdg_detail_message);
3150 exit(EXIT_FAILURE);
3151 }
3152
3153 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3154 weston_log(xdg_wrong_message, dir);
3155 weston_log_continue(xdg_detail_message);
3156 exit(EXIT_FAILURE);
3157 }
3158
3159 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3160 weston_log(xdg_wrong_mode_message,
3161 dir, s.st_mode & 0777, s.st_uid);
3162 weston_log_continue(xdg_detail_message);
3163 }
3164}
3165
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003166static int
3167usage(int error_code)
3168{
3169 fprintf(stderr,
3170 "Usage: weston [OPTIONS]\n\n"
3171 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3172 "Weston supports multiple backends, and depending on which backend is in use\n"
3173 "different options will be accepted.\n\n"
3174
3175
3176 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003177 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003178 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003179 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3180 "\t\t\t\twayland-backend.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003181 " -S, --socket=NAME\tName of socket to listen on\n"
3182 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003183 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003184 " --log==FILE\t\tLog to the given file\n"
3185 " -h, --help\t\tThis help message\n\n");
3186
3187 fprintf(stderr,
3188 "Options for drm-backend.so:\n\n"
3189 " --connector=ID\tBring up only this connector\n"
3190 " --seat=SEAT\t\tThe seat that weston should run on\n"
3191 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003192 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003193 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3194
3195 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003196 "Options for fbdev-backend.so:\n\n"
3197 " --tty=TTY\t\tThe tty to use\n"
3198 " --device=DEVICE\tThe framebuffer device to use\n\n");
3199
3200 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003201 "Options for x11-backend.so:\n\n"
3202 " --width=WIDTH\t\tWidth of X window\n"
3203 " --height=HEIGHT\tHeight of X window\n"
3204 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003205 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003206 " --output-count=COUNT\tCreate multiple outputs\n"
3207 " --no-input\t\tDont create input devices\n\n");
3208
3209 fprintf(stderr,
3210 "Options for wayland-backend.so:\n\n"
3211 " --width=WIDTH\t\tWidth of Wayland surface\n"
3212 " --height=HEIGHT\tHeight of Wayland surface\n"
3213 " --display=DISPLAY\tWayland display to connect to\n\n");
3214
Pekka Paalanene31e0532013-05-22 18:03:07 +03003215#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3216 fprintf(stderr,
3217 "Options for rpi-backend.so:\n\n"
3218 " --tty=TTY\t\tThe tty to use\n"
3219 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3220 " --transform=TR\tThe output transformation, TR is one of:\n"
3221 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3222 "\n");
3223#endif
3224
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003225 exit(error_code);
3226}
3227
Peter Maatmane5b42e42013-03-27 22:38:53 +01003228static void
3229catch_signals(void)
3230{
3231 struct sigaction action;
3232
3233 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3234 action.sa_sigaction = on_caught_signal;
3235 sigemptyset(&action.sa_mask);
3236 sigaction(SIGSEGV, &action, NULL);
3237 sigaction(SIGABRT, &action, NULL);
3238}
3239
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003240int main(int argc, char *argv[])
3241{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003242 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003243 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003244 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003245 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003246 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003247 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003248 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003249 int *argc, char *argv[],
3250 struct weston_config *config);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003251 int i, config_fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003252 char *backend = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003253 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003254 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003255 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003256 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003257 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003258 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003259 struct weston_config *config;
3260 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003261
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003262 const struct weston_option core_options[] = {
3263 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3264 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3265 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003266 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003267 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003268 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003269 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003270 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003271
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003272 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003273
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003274 if (help)
3275 usage(EXIT_SUCCESS);
3276
Scott Moreau12245142012-08-29 15:15:58 -06003277 if (version) {
3278 printf(PACKAGE_STRING "\n");
3279 return EXIT_SUCCESS;
3280 }
3281
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003282 weston_log_file_open(log);
3283
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003284 weston_log("%s\n"
3285 STAMP_SPACE "%s\n"
3286 STAMP_SPACE "Bug reports to: %s\n"
3287 STAMP_SPACE "Build: %s\n",
3288 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003289 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003290 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003291
Martin Minarik37032f82012-06-18 20:15:18 +02003292 verify_xdg_runtime_dir();
3293
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003294 display = wl_display_create();
3295
Tiago Vignatti2116b892011-08-08 05:52:59 -07003296 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003297 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3298 display);
3299 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3300 display);
3301 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3302 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003303
3304 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003305 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3306 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003307
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003308 if (!backend) {
3309 if (getenv("WAYLAND_DISPLAY"))
3310 backend = "wayland-backend.so";
3311 else if (getenv("DISPLAY"))
3312 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003313 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003314 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003315 }
3316
Ossama Othmana50e6e42013-05-14 09:48:26 -07003317 config_fd = open_config_file("weston.ini");
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003318 config = weston_config_parse(config_fd);
3319 close(config_fd);
3320
3321 section = weston_config_get_section(config, "core", NULL, NULL);
3322 weston_config_section_get_string(section, "modules",
3323 &modules, "desktop-shell.so");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003324
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003325 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003326 if (!backend_init)
3327 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003328
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003329 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003330 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003331 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003332 exit(EXIT_FAILURE);
3333 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003334
Peter Maatmane5b42e42013-03-27 22:38:53 +01003335 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003336 segv_compositor = ec;
3337
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003338 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003339
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003340 setenv("WAYLAND_DISPLAY", socket_name, 1);
3341
Ossama Othmana50e6e42013-05-14 09:48:26 -07003342 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003343 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003344 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003345 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003346
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003347 for (i = 1; i < argc; i++)
3348 weston_log("fatal: unhandled option: %s\n", argv[i]);
3349 if (argc > 1) {
3350 ret = EXIT_FAILURE;
3351 goto out;
3352 }
3353
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003354 weston_compositor_log_capabilities(ec);
3355
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003356 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003357 weston_log("fatal: failed to add socket: %m\n");
3358 ret = EXIT_FAILURE;
3359 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003360 }
3361
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003362 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003363
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003364 wl_display_run(display);
3365
3366 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003367 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003368 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003369
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003370 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003371
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003372 for (i = ARRAY_LENGTH(signals); i;)
3373 wl_event_source_remove(signals[--i]);
3374
Daniel Stone855028f2012-05-01 20:37:10 +01003375 weston_compositor_xkb_destroy(ec);
3376
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003377 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003378 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003379
Martin Minarik19e6f262012-06-07 13:08:46 +02003380 weston_log_file_close();
3381
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003382 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003383}