blob: 254e9e48e2d5e569d5317e7c3f50d43221ee6fe6 [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 Paalanenc647ed72015-02-09 13:16:57 +02004 * Copyright © 2012-2015 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050026 */
27
Kristian Høgsberga9410222011-01-14 17:22:35 -050028#include "config.h"
29
Daniel Stoneb7452fe2012-06-01 12:14:06 +010030#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010035#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020037#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040038#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010039#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040040#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020041#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020042#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020043#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050045#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040046#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040047#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040048#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050049#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040050#include <sys/time.h>
51#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030052#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050053
Pekka Paalanenb5026542014-11-12 15:09:24 +020054#include "timeline.h"
55
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010057#include "scaler-server-protocol.h"
Pekka Paalanen31f7d782014-09-23 22:08:43 -040058#include "presentation_timing-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070059#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070060#include "shared/os-compatibility.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030061#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040062#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050063#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050064
Pekka Paalanen0513a952014-05-21 16:17:27 +030065#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
66
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020067static void
Alexander Larsson0b135062013-05-28 16:23:36 +020068weston_output_transform_scale_init(struct weston_output *output,
69 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020070
Rob Bradford27b17932013-06-26 18:08:46 +010071static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050072weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010073
Derek Foreman6ae7bc92014-11-04 10:47:33 -060074static void weston_mode_switch_finish(struct weston_output *output,
75 int mode_changed,
76 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080077{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020078 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020079 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020080 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060081 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020082
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020083 pixman_region32_init(&old_output_region);
84 pixman_region32_copy(&old_output_region, &output->region);
85
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020086 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020087 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020088
89 pixman_region32_init(&output->previous_damage);
90 pixman_region32_init_rect(&output->region, output->x, output->y,
91 output->width, output->height);
92
93 weston_output_update_matrix(output);
94
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020095 /* If a pointer falls outside the outputs new geometry, move it to its
96 * lower-right corner */
97 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -050098 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020099 int32_t x, y;
100
101 if (!pointer)
102 continue;
103
104 x = wl_fixed_to_int(pointer->x);
105 y = wl_fixed_to_int(pointer->y);
106
107 if (!pixman_region32_contains_point(&old_output_region,
108 x, y, NULL) ||
109 pixman_region32_contains_point(&output->region,
110 x, y, NULL))
111 continue;
112
113 if (x >= output->x + output->width)
114 x = output->x + output->width - 1;
115 if (y >= output->y + output->height)
116 y = output->y + output->height - 1;
117
118 pointer->x = wl_fixed_from_int(x);
119 pointer->y = wl_fixed_from_int(y);
120 }
121
122 pixman_region32_fini(&old_output_region);
123
Derek Foremandd4cd332014-11-10 10:29:59 -0600124 if (!mode_changed && !scale_changed)
125 return;
126
Hardening57388e42013-09-18 23:56:36 +0200127 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600128 wl_resource_for_each(resource, &output->resource_list) {
129 if (mode_changed) {
130 wl_output_send_mode(resource,
131 output->current_mode->flags,
132 output->current_mode->width,
133 output->current_mode->height,
134 output->current_mode->refresh);
135 }
Hardening57388e42013-09-18 23:56:36 +0200136
Derek Foreman41bdc272014-11-05 13:26:57 -0600137 version = wl_resource_get_version(resource);
138 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600139 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200140
Derek Foreman41bdc272014-11-05 13:26:57 -0600141 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
142 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600143 }
144}
145
146WL_EXPORT int
147weston_output_mode_set_native(struct weston_output *output,
148 struct weston_mode *mode,
149 int32_t scale)
150{
151 int ret;
152 int mode_changed = 0, scale_changed = 0;
153
154 if (!output->switch_mode)
155 return -1;
156
157 if (!output->original_mode) {
158 mode_changed = 1;
159 ret = output->switch_mode(output, mode);
160 if (ret < 0)
161 return ret;
162 if (output->current_scale != scale) {
163 scale_changed = 1;
164 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200165 }
166 }
167
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600168 output->native_mode = mode;
169 output->native_scale = scale;
170
171 weston_mode_switch_finish(output, mode_changed, scale_changed);
172
173 return 0;
174}
175
176WL_EXPORT int
177weston_output_mode_switch_to_native(struct weston_output *output)
178{
179 int ret;
180 int mode_changed = 0, scale_changed = 0;
181
182 if (!output->switch_mode)
183 return -1;
184
185 if (!output->original_mode) {
186 weston_log("already in the native mode\n");
187 return -1;
188 }
189 /* the non fullscreen clients haven't seen a mode set since we
190 * switched into a temporary, so we need to notify them if the
191 * mode at that time is different from the native mode now.
192 */
193 mode_changed = (output->original_mode != output->native_mode);
194 scale_changed = (output->original_scale != output->native_scale);
195
196 ret = output->switch_mode(output, output->native_mode);
197 if (ret < 0)
198 return ret;
199
200 output->current_scale = output->native_scale;
201
202 output->original_mode = NULL;
203 output->original_scale = 0;
204
205 weston_mode_switch_finish(output, mode_changed, scale_changed);
206
207 return 0;
208}
209
210WL_EXPORT int
211weston_output_mode_switch_to_temporary(struct weston_output *output,
212 struct weston_mode *mode,
213 int32_t scale)
214{
215 int ret;
216
217 if (!output->switch_mode)
218 return -1;
219
220 /* original_mode is the last mode non full screen clients have seen,
221 * so we shouldn't change it if we already have one set.
222 */
223 if (!output->original_mode) {
224 output->original_mode = output->native_mode;
225 output->original_scale = output->native_scale;
226 }
227 ret = output->switch_mode(output, mode);
228 if (ret < 0)
229 return ret;
230
231 output->current_scale = scale;
232
233 weston_mode_switch_finish(output, 0, 0);
234
235 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800236}
237
Benjamin Franzke06286262011-05-06 19:12:33 +0200238static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200239child_client_exec(int sockfd, const char *path)
240{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500241 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200243 sigset_t allsigs;
244
245 /* do not give our signal mask to the new process */
246 sigfillset(&allsigs);
247 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200248
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100249 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
250 if (seteuid(getuid()) == -1) {
251 weston_log("compositor: failed seteuid\n");
252 return;
253 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500254
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500255 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
256 * non-CLOEXEC fd to pass through exec. */
257 clientfd = dup(sockfd);
258 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200259 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500260 return;
261 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200262
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500263 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200264 setenv("WAYLAND_SOCKET", s, 1);
265
266 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200267 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200268 path);
269}
270
271WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272weston_client_launch(struct weston_compositor *compositor,
273 struct weston_process *proc,
274 const char *path,
275 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200276{
277 int sv[2];
278 pid_t pid;
279 struct wl_client *client;
280
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300281 weston_log("launching '%s'\n", path);
282
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300283 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200284 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200285 "socketpair failed while launching '%s': %m\n",
286 path);
287 return NULL;
288 }
289
290 pid = fork();
291 if (pid == -1) {
292 close(sv[0]);
293 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200295 "fork failed while launching '%s': %m\n", path);
296 return NULL;
297 }
298
299 if (pid == 0) {
300 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700301 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200302 }
303
304 close(sv[1]);
305
306 client = wl_client_create(compositor->wl_display, sv[0]);
307 if (!client) {
308 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200310 "wl_client_create failed while launching '%s'.\n",
311 path);
312 return NULL;
313 }
314
315 proc->pid = pid;
316 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200318
319 return client;
320}
321
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300322struct process_info {
323 struct weston_process proc;
324 char *path;
325};
326
327static void
328process_handle_sigchld(struct weston_process *process, int status)
329{
330 struct process_info *pinfo =
331 container_of(process, struct process_info, proc);
332
333 /*
334 * There are no guarantees whether this runs before or after
335 * the wl_client destructor.
336 */
337
338 if (WIFEXITED(status)) {
339 weston_log("%s exited with status %d\n", pinfo->path,
340 WEXITSTATUS(status));
341 } else if (WIFSIGNALED(status)) {
342 weston_log("%s died on signal %d\n", pinfo->path,
343 WTERMSIG(status));
344 } else {
345 weston_log("%s disappeared\n", pinfo->path);
346 }
347
348 free(pinfo->path);
349 free(pinfo);
350}
351
352WL_EXPORT struct wl_client *
353weston_client_start(struct weston_compositor *compositor, const char *path)
354{
355 struct process_info *pinfo;
356 struct wl_client *client;
357
358 pinfo = zalloc(sizeof *pinfo);
359 if (!pinfo)
360 return NULL;
361
362 pinfo->path = strdup(path);
363 if (!pinfo->path)
364 goto out_free;
365
366 client = weston_client_launch(compositor, &pinfo->proc, path,
367 process_handle_sigchld);
368 if (!client)
369 goto out_str;
370
371 return client;
372
373out_str:
374 free(pinfo->path);
375
376out_free:
377 free(pinfo);
378
379 return NULL;
380}
381
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200382static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300383region_init_infinite(pixman_region32_t *region)
384{
385 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
386 UINT32_MAX, UINT32_MAX);
387}
388
Pekka Paalanene67858b2013-04-25 13:57:42 +0300389static struct weston_subsurface *
390weston_surface_to_subsurface(struct weston_surface *surface);
391
Jason Ekstranda7af7042013-10-12 22:38:11 -0500392WL_EXPORT struct weston_view *
393weston_view_create(struct weston_surface *surface)
394{
395 struct weston_view *view;
396
Bryce Harringtonde16d892014-11-20 22:21:57 -0800397 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500398 if (view == NULL)
399 return NULL;
400
401 view->surface = surface;
402
Jason Ekstranda7af7042013-10-12 22:38:11 -0500403 /* Assign to surface */
404 wl_list_insert(&surface->views, &view->surface_link);
405
406 wl_signal_init(&view->destroy_signal);
407 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300408 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500409
Jason Ekstranda7af7042013-10-12 22:38:11 -0500410 pixman_region32_init(&view->clip);
411
412 view->alpha = 1.0;
413 pixman_region32_init(&view->transform.opaque);
414
415 wl_list_init(&view->geometry.transformation_list);
416 wl_list_insert(&view->geometry.transformation_list,
417 &view->transform.position.link);
418 weston_matrix_init(&view->transform.position.matrix);
419 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200420 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500421 pixman_region32_init(&view->transform.boundingbox);
422 view->transform.dirty = 1;
423
Jason Ekstranda7af7042013-10-12 22:38:11 -0500424 return view;
425}
426
Jason Ekstrand108865d2014-06-26 10:04:49 -0700427struct weston_frame_callback {
428 struct wl_resource *resource;
429 struct wl_list link;
430};
431
Pekka Paalanen133e4392014-09-23 22:08:46 -0400432struct weston_presentation_feedback {
433 struct wl_resource *resource;
434
435 /* XXX: could use just wl_resource_get_link() instead */
436 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200437
438 /* The per-surface feedback flags */
439 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400440};
441
442static void
443weston_presentation_feedback_discard(
444 struct weston_presentation_feedback *feedback)
445{
446 presentation_feedback_send_discarded(feedback->resource);
447 wl_resource_destroy(feedback->resource);
448}
449
450static void
451weston_presentation_feedback_discard_list(struct wl_list *list)
452{
453 struct weston_presentation_feedback *feedback, *tmp;
454
455 wl_list_for_each_safe(feedback, tmp, list, link)
456 weston_presentation_feedback_discard(feedback);
457}
458
459static void
460weston_presentation_feedback_present(
461 struct weston_presentation_feedback *feedback,
462 struct weston_output *output,
463 uint32_t refresh_nsec,
464 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200465 uint64_t seq,
466 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400467{
468 struct wl_client *client = wl_resource_get_client(feedback->resource);
469 struct wl_resource *o;
470 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400471
472 wl_resource_for_each(o, &output->resource_list) {
473 if (wl_resource_get_client(o) != client)
474 continue;
475
476 presentation_feedback_send_sync_output(feedback->resource, o);
477 }
478
479 secs = ts->tv_sec;
480 presentation_feedback_send_presented(feedback->resource,
481 secs >> 32, secs & 0xffffffff,
482 ts->tv_nsec,
483 refresh_nsec,
484 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200485 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400486 wl_resource_destroy(feedback->resource);
487}
488
489static void
490weston_presentation_feedback_present_list(struct wl_list *list,
491 struct weston_output *output,
492 uint32_t refresh_nsec,
493 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200494 uint64_t seq,
495 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400496{
497 struct weston_presentation_feedback *feedback, *tmp;
498
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200499 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
500 wl_list_empty(list));
501
Pekka Paalanen133e4392014-09-23 22:08:46 -0400502 wl_list_for_each_safe(feedback, tmp, list, link)
503 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200504 refresh_nsec, ts, seq,
505 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400506}
507
Jason Ekstrand7b982072014-05-20 14:33:03 -0500508static void
509surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
510{
511 struct weston_surface_state *state =
512 container_of(listener, struct weston_surface_state,
513 buffer_destroy_listener);
514
515 state->buffer = NULL;
516}
517
518static void
519weston_surface_state_init(struct weston_surface_state *state)
520{
521 state->newly_attached = 0;
522 state->buffer = NULL;
523 state->buffer_destroy_listener.notify =
524 surface_state_handle_buffer_destroy;
525 state->sx = 0;
526 state->sy = 0;
527
Derek Foreman152254b2015-11-26 14:17:48 -0600528 pixman_region32_init(&state->damage_surface);
529 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500530 pixman_region32_init(&state->opaque);
531 region_init_infinite(&state->input);
532
533 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400534 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500535
536 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
537 state->buffer_viewport.buffer.scale = 1;
538 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
539 state->buffer_viewport.surface.width = -1;
540 state->buffer_viewport.changed = 0;
541}
542
543static void
544weston_surface_state_fini(struct weston_surface_state *state)
545{
546 struct weston_frame_callback *cb, *next;
547
548 wl_list_for_each_safe(cb, next,
549 &state->frame_callback_list, link)
550 wl_resource_destroy(cb->resource);
551
Pekka Paalanen133e4392014-09-23 22:08:46 -0400552 weston_presentation_feedback_discard_list(&state->feedback_list);
553
Jason Ekstrand7b982072014-05-20 14:33:03 -0500554 pixman_region32_fini(&state->input);
555 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600556 pixman_region32_fini(&state->damage_surface);
557 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500558
559 if (state->buffer)
560 wl_list_remove(&state->buffer_destroy_listener.link);
561 state->buffer = NULL;
562}
563
564static void
565weston_surface_state_set_buffer(struct weston_surface_state *state,
566 struct weston_buffer *buffer)
567{
568 if (state->buffer == buffer)
569 return;
570
571 if (state->buffer)
572 wl_list_remove(&state->buffer_destroy_listener.link);
573 state->buffer = buffer;
574 if (state->buffer)
575 wl_signal_add(&state->buffer->destroy_signal,
576 &state->buffer_destroy_listener);
577}
578
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500579WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500580weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500581{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500582 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400583
Bryce Harringtonde16d892014-11-20 22:21:57 -0800584 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400585 if (surface == NULL)
586 return NULL;
587
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500588 wl_signal_init(&surface->destroy_signal);
589
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500590 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200591 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400592
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200593 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
594 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200595 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
596 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500597
598 weston_surface_state_init(&surface->pending);
599
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400600 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500601 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300602 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400603
Jason Ekstranda7af7042013-10-12 22:38:11 -0500604 wl_list_init(&surface->views);
605
606 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400607 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500608
Pekka Paalanene67858b2013-04-25 13:57:42 +0300609 wl_list_init(&surface->subsurface_list);
610 wl_list_init(&surface->subsurface_list_pending);
611
Jason Ekstrand1e059042014-10-16 10:55:19 -0500612 weston_matrix_init(&surface->buffer_to_surface_matrix);
613 weston_matrix_init(&surface->surface_to_buffer_matrix);
614
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400615 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500616}
617
Alex Wu8811bf92012-02-28 18:07:54 +0800618WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500619weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200620 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500621{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100622 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500623}
624
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400625WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500626weston_view_to_global_float(struct weston_view *view,
627 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200628{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500629 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200630 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
631
Jason Ekstranda7af7042013-10-12 22:38:11 -0500632 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200633
634 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200635 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700636 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200637 v.f[3]);
638 *x = 0;
639 *y = 0;
640 return;
641 }
642
643 *x = v.f[0] / v.f[3];
644 *y = v.f[1] / v.f[3];
645 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500646 *x = sx + view->geometry.x;
647 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200648 }
649}
650
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500651WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200652weston_transformed_coord(int width, int height,
653 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200654 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200655 float sx, float sy, float *bx, float *by)
656{
657 switch (transform) {
658 case WL_OUTPUT_TRANSFORM_NORMAL:
659 default:
660 *bx = sx;
661 *by = sy;
662 break;
663 case WL_OUTPUT_TRANSFORM_FLIPPED:
664 *bx = width - sx;
665 *by = sy;
666 break;
667 case WL_OUTPUT_TRANSFORM_90:
668 *bx = height - sy;
669 *by = sx;
670 break;
671 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
672 *bx = height - sy;
673 *by = width - sx;
674 break;
675 case WL_OUTPUT_TRANSFORM_180:
676 *bx = width - sx;
677 *by = height - sy;
678 break;
679 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
680 *bx = sx;
681 *by = height - sy;
682 break;
683 case WL_OUTPUT_TRANSFORM_270:
684 *bx = sy;
685 *by = width - sx;
686 break;
687 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
688 *bx = sy;
689 *by = sx;
690 break;
691 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200692
693 *bx *= scale;
694 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200695}
696
697WL_EXPORT pixman_box32_t
698weston_transformed_rect(int width, int height,
699 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200700 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200701 pixman_box32_t rect)
702{
703 float x1, x2, y1, y2;
704
705 pixman_box32_t ret;
706
Alexander Larsson4ea95522013-05-22 14:41:37 +0200707 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200708 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200709 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200710 rect.x2, rect.y2, &x2, &y2);
711
712 if (x1 <= x2) {
713 ret.x1 = x1;
714 ret.x2 = x2;
715 } else {
716 ret.x1 = x2;
717 ret.x2 = x1;
718 }
719
720 if (y1 <= y2) {
721 ret.y1 = y1;
722 ret.y2 = y2;
723 } else {
724 ret.y1 = y2;
725 ret.y2 = y1;
726 }
727
728 return ret;
729}
730
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600731/** Transform a region by a matrix, restricted to axis-aligned transformations
732 *
733 * Warning: This function does not work for projective, affine, or matrices
734 * that encode arbitrary rotations. Only 90-degree step rotations are
735 * supported.
736 */
737WL_EXPORT void
738weston_matrix_transform_region(pixman_region32_t *dest,
739 struct weston_matrix *matrix,
740 pixman_region32_t *src)
741{
742 pixman_box32_t *src_rects, *dest_rects;
743 int nrects, i;
744
745 src_rects = pixman_region32_rectangles(src, &nrects);
746 dest_rects = malloc(nrects * sizeof(*dest_rects));
747 if (!dest_rects)
748 return;
749
750 for (i = 0; i < nrects; i++) {
751 struct weston_vector vec1 = {{
752 src_rects[i].x1, src_rects[i].y1, 0, 1
753 }};
754 weston_matrix_transform(matrix, &vec1);
755 vec1.f[0] /= vec1.f[3];
756 vec1.f[1] /= vec1.f[3];
757
758 struct weston_vector vec2 = {{
759 src_rects[i].x2, src_rects[i].y2, 0, 1
760 }};
761 weston_matrix_transform(matrix, &vec2);
762 vec2.f[0] /= vec2.f[3];
763 vec2.f[1] /= vec2.f[3];
764
765 if (vec1.f[0] < vec2.f[0]) {
766 dest_rects[i].x1 = floor(vec1.f[0]);
767 dest_rects[i].x2 = ceil(vec2.f[0]);
768 } else {
769 dest_rects[i].x1 = floor(vec2.f[0]);
770 dest_rects[i].x2 = ceil(vec1.f[0]);
771 }
772
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600773 if (vec1.f[1] < vec2.f[1]) {
774 dest_rects[i].y1 = floor(vec1.f[1]);
775 dest_rects[i].y2 = ceil(vec2.f[1]);
776 } else {
777 dest_rects[i].y1 = floor(vec2.f[1]);
778 dest_rects[i].y2 = ceil(vec1.f[1]);
779 }
780 }
781
782 pixman_region32_clear(dest);
783 pixman_region32_init_rects(dest, dest_rects, nrects);
784 free(dest_rects);
785}
786
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200787WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500788weston_transformed_region(int width, int height,
789 enum wl_output_transform transform,
790 int32_t scale,
791 pixman_region32_t *src, pixman_region32_t *dest)
792{
793 pixman_box32_t *src_rects, *dest_rects;
794 int nrects, i;
795
796 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
797 if (src != dest)
798 pixman_region32_copy(dest, src);
799 return;
800 }
801
802 src_rects = pixman_region32_rectangles(src, &nrects);
803 dest_rects = malloc(nrects * sizeof(*dest_rects));
804 if (!dest_rects)
805 return;
806
807 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
808 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
809 } else {
810 for (i = 0; i < nrects; i++) {
811 switch (transform) {
812 default:
813 case WL_OUTPUT_TRANSFORM_NORMAL:
814 dest_rects[i].x1 = src_rects[i].x1;
815 dest_rects[i].y1 = src_rects[i].y1;
816 dest_rects[i].x2 = src_rects[i].x2;
817 dest_rects[i].y2 = src_rects[i].y2;
818 break;
819 case WL_OUTPUT_TRANSFORM_90:
820 dest_rects[i].x1 = height - src_rects[i].y2;
821 dest_rects[i].y1 = src_rects[i].x1;
822 dest_rects[i].x2 = height - src_rects[i].y1;
823 dest_rects[i].y2 = src_rects[i].x2;
824 break;
825 case WL_OUTPUT_TRANSFORM_180:
826 dest_rects[i].x1 = width - src_rects[i].x2;
827 dest_rects[i].y1 = height - src_rects[i].y2;
828 dest_rects[i].x2 = width - src_rects[i].x1;
829 dest_rects[i].y2 = height - src_rects[i].y1;
830 break;
831 case WL_OUTPUT_TRANSFORM_270:
832 dest_rects[i].x1 = src_rects[i].y1;
833 dest_rects[i].y1 = width - src_rects[i].x2;
834 dest_rects[i].x2 = src_rects[i].y2;
835 dest_rects[i].y2 = width - src_rects[i].x1;
836 break;
837 case WL_OUTPUT_TRANSFORM_FLIPPED:
838 dest_rects[i].x1 = width - src_rects[i].x2;
839 dest_rects[i].y1 = src_rects[i].y1;
840 dest_rects[i].x2 = width - src_rects[i].x1;
841 dest_rects[i].y2 = src_rects[i].y2;
842 break;
843 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
844 dest_rects[i].x1 = height - src_rects[i].y2;
845 dest_rects[i].y1 = width - src_rects[i].x2;
846 dest_rects[i].x2 = height - src_rects[i].y1;
847 dest_rects[i].y2 = width - src_rects[i].x1;
848 break;
849 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
850 dest_rects[i].x1 = src_rects[i].x1;
851 dest_rects[i].y1 = height - src_rects[i].y2;
852 dest_rects[i].x2 = src_rects[i].x2;
853 dest_rects[i].y2 = height - src_rects[i].y1;
854 break;
855 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
856 dest_rects[i].x1 = src_rects[i].y1;
857 dest_rects[i].y1 = src_rects[i].x1;
858 dest_rects[i].x2 = src_rects[i].y2;
859 dest_rects[i].y2 = src_rects[i].x2;
860 break;
861 }
862 }
863 }
864
865 if (scale != 1) {
866 for (i = 0; i < nrects; i++) {
867 dest_rects[i].x1 *= scale;
868 dest_rects[i].x2 *= scale;
869 dest_rects[i].y1 *= scale;
870 dest_rects[i].y2 *= scale;
871 }
872 }
873
874 pixman_region32_clear(dest);
875 pixman_region32_init_rects(dest, dest_rects, nrects);
876 free(dest_rects);
877}
878
Jonny Lamb74130762013-11-26 18:19:46 +0100879static void
880scaler_surface_to_buffer(struct weston_surface *surface,
881 float sx, float sy, float *bx, float *by)
882{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200883 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200884 double src_width, src_height;
885 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200886
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200887 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
888 if (vp->surface.width == -1) {
889 *bx = sx;
890 *by = sy;
891 return;
892 }
Jonny Lamb74130762013-11-26 18:19:46 +0100893
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200894 src_x = 0.0;
895 src_y = 0.0;
896 src_width = surface->width_from_buffer;
897 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100898 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200899 src_x = wl_fixed_to_double(vp->buffer.src_x);
900 src_y = wl_fixed_to_double(vp->buffer.src_y);
901 src_width = wl_fixed_to_double(vp->buffer.src_width);
902 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100903 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200904
905 *bx = sx * src_width / surface->width + src_x;
906 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100907}
908
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500909WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200910weston_surface_to_buffer_float(struct weston_surface *surface,
911 float sx, float sy, float *bx, float *by)
912{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200913 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
914
Jonny Lamb74130762013-11-26 18:19:46 +0100915 /* first transform coordinates if the scaler is set */
916 scaler_surface_to_buffer(surface, sx, sy, bx, by);
917
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500918 weston_transformed_coord(surface->width_from_buffer,
919 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200920 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100921 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200922}
923
Derek Foreman99739672015-12-03 16:38:11 -0600924/** Transform a rectangle from surface coordinates to buffer coordinates
925 *
926 * \param surface The surface to fetch wl_viewport and buffer transformation
927 * from.
928 * \param rect The rectangle to transform.
929 * \return The transformed rectangle.
930 *
931 * Viewport and buffer transformations can only do translation, scaling,
932 * and rotations in 90-degree steps. Therefore the only loss in the
933 * conversion is coordinate rounding.
934 *
935 * However, some coordinate rounding takes place as an intermediate
936 * step before the buffer scale factor is applied, so the rectangle
937 * boundary may not be exactly as expected.
938 *
939 * This is OK for damage tracking since a little extra coverage is
940 * not a problem.
941 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200942WL_EXPORT pixman_box32_t
943weston_surface_to_buffer_rect(struct weston_surface *surface,
944 pixman_box32_t rect)
945{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200946 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100947 float xf, yf;
948
949 /* first transform box coordinates if the scaler is set */
950 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
951 rect.x1 = floorf(xf);
952 rect.y1 = floorf(yf);
953
954 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600955 rect.x2 = ceilf(xf);
956 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100957
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500958 return weston_transformed_rect(surface->width_from_buffer,
959 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200960 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200961 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200962}
963
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200964/** Transform a region from surface coordinates to buffer coordinates
965 *
966 * \param surface The surface to fetch wl_viewport and buffer transformation
967 * from.
968 * \param surface_region[in] The region in surface coordinates.
969 * \param buffer_region[out] The region converted to buffer coordinates.
970 *
971 * Buffer_region must be init'd, but will be completely overwritten.
972 *
973 * Viewport and buffer transformations can only do translation, scaling,
974 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600975 * conversion is from the coordinate rounding that takes place in
976 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200977 */
978WL_EXPORT void
979weston_surface_to_buffer_region(struct weston_surface *surface,
980 pixman_region32_t *surface_region,
981 pixman_region32_t *buffer_region)
982{
983 pixman_box32_t *src_rects, *dest_rects;
984 int nrects, i;
985
986 src_rects = pixman_region32_rectangles(surface_region, &nrects);
987 dest_rects = malloc(nrects * sizeof(*dest_rects));
988 if (!dest_rects)
989 return;
990
991 for (i = 0; i < nrects; i++) {
992 dest_rects[i] = weston_surface_to_buffer_rect(surface,
993 src_rects[i]);
994 }
995
996 pixman_region32_fini(buffer_region);
997 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
998 free(dest_rects);
999}
1000
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001001WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001003 struct weston_plane *plane)
1004{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001006 return;
1007
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 weston_view_damage_below(view);
1009 view->plane = plane;
1010 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001011}
1012
Pekka Paalanen51723d52015-02-17 13:10:01 +02001013/** Inflict damage on the plane where the view is visible.
1014 *
1015 * \param view The view that causes the damage.
1016 *
1017 * If the view is currently on a plane (including the primary plane),
1018 * take the view's boundingbox, subtract all the opaque views that cover it,
1019 * and add the remaining region as damage to the plane. This corresponds
1020 * to the damage inflicted to the plane if this view disappeared.
1021 *
1022 * A repaint is scheduled for this view.
1023 *
1024 * The region of all opaque views covering this view is stored in
1025 * weston_view::clip and updated by view_accumulate_damage() during
1026 * weston_output_repaint(). Specifically, that region matches the
1027 * scenegraph as it was last painted.
1028 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001029WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001030weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001031{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001032 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001033
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001034 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001035 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001037 if (view->plane)
1038 pixman_region32_union(&view->plane->damage,
1039 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001040 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001041 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001042}
1043
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001044static void
1045weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1046{
1047 uint32_t different = es->output_mask ^ mask;
1048 uint32_t entered = mask & different;
1049 uint32_t left = es->output_mask & different;
1050 struct weston_output *output;
1051 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001052 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001053
1054 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001055 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001056 return;
1057 if (different == 0)
1058 return;
1059
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001060 client = wl_resource_get_client(es->resource);
1061
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001062 wl_list_for_each(output, &es->compositor->output_list, link) {
1063 if (1 << output->id & different)
1064 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001065 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001066 client);
1067 if (resource == NULL)
1068 continue;
1069 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001070 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001071 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001072 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001073 }
1074}
1075
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001076
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001077static void
1078weston_surface_assign_output(struct weston_surface *es)
1079{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 struct weston_output *new_output;
1081 struct weston_view *view;
1082 pixman_region32_t region;
1083 uint32_t max, area, mask;
1084 pixman_box32_t *e;
1085
1086 new_output = NULL;
1087 max = 0;
1088 mask = 0;
1089 pixman_region32_init(&region);
1090 wl_list_for_each(view, &es->views, surface_link) {
1091 if (!view->output)
1092 continue;
1093
1094 pixman_region32_intersect(&region, &view->transform.boundingbox,
1095 &view->output->region);
1096
1097 e = pixman_region32_extents(&region);
1098 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1099
1100 mask |= view->output_mask;
1101
1102 if (area >= max) {
1103 new_output = view->output;
1104 max = area;
1105 }
1106 }
1107 pixman_region32_fini(&region);
1108
1109 es->output = new_output;
1110 weston_surface_update_output_mask(es, mask);
1111}
1112
1113static void
1114weston_view_assign_output(struct weston_view *ev)
1115{
1116 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001117 struct weston_output *output, *new_output;
1118 pixman_region32_t region;
1119 uint32_t max, area, mask;
1120 pixman_box32_t *e;
1121
1122 new_output = NULL;
1123 max = 0;
1124 mask = 0;
1125 pixman_region32_init(&region);
1126 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001127 if (output->destroying)
1128 continue;
1129
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001131 &output->region);
1132
1133 e = pixman_region32_extents(&region);
1134 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1135
1136 if (area > 0)
1137 mask |= 1 << output->id;
1138
1139 if (area >= max) {
1140 new_output = output;
1141 max = area;
1142 }
1143 }
1144 pixman_region32_fini(&region);
1145
Jason Ekstranda7af7042013-10-12 22:38:11 -05001146 ev->output = new_output;
1147 ev->output_mask = mask;
1148
1149 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001150}
1151
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001152static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001153weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1154 int from_x, int from_y, int *to_x, int *to_y)
1155{
1156 float x, y;
1157
1158 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1159 weston_view_from_global_float(to, x, y, &x, &y);
1160
1161 *to_x = round(x);
1162 *to_y = round(y);
1163}
1164
1165static void
1166weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1167{
1168 pixman_box32_t *a;
1169 pixman_box32_t b;
1170
1171 a = pixman_region32_extents(&from->geometry.scissor);
1172
1173 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1174 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1175
1176 pixman_region32_fini(&to->geometry.scissor);
1177 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1178}
1179
1180static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001181view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001183{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001184 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1185 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001186 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001187 { inbox->x1, inbox->y1 },
1188 { inbox->x1, inbox->y2 },
1189 { inbox->x2, inbox->y1 },
1190 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001191 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001192 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001193 int i;
1194
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001195 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001196 /* avoid rounding empty bbox to 1x1 */
1197 pixman_region32_init(bbox);
1198 return;
1199 }
1200
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001201 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001202 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001204 if (x < min_x)
1205 min_x = x;
1206 if (x > max_x)
1207 max_x = x;
1208 if (y < min_y)
1209 min_y = y;
1210 if (y > max_y)
1211 max_y = y;
1212 }
1213
Pekka Paalanen219b9822012-02-08 15:38:37 +02001214 int_x = floorf(min_x);
1215 int_y = floorf(min_y);
1216 pixman_region32_init_rect(bbox, int_x, int_y,
1217 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001218}
1219
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001220static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001222{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001224
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001225 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 view->geometry.x = roundf(view->geometry.x);
1227 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001228
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001229 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1231 view->transform.position.matrix.d[12] = view->geometry.x;
1232 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001233
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001235
Jason Ekstranda7af7042013-10-12 22:38:11 -05001236 view->transform.inverse = view->transform.position.matrix;
1237 view->transform.inverse.d[12] = -view->geometry.x;
1238 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001239
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001241 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001242 view->surface->width,
1243 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001244 if (view->geometry.scissor_enabled)
1245 pixman_region32_intersect(&view->transform.boundingbox,
1246 &view->transform.boundingbox,
1247 &view->geometry.scissor);
1248
1249 pixman_region32_translate(&view->transform.boundingbox,
1250 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001251
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 if (view->alpha == 1.0) {
1253 pixman_region32_copy(&view->transform.opaque,
1254 &view->surface->opaque);
1255 pixman_region32_translate(&view->transform.opaque,
1256 view->geometry.x,
1257 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001258 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001259}
1260
1261static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001263{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 struct weston_view *parent = view->geometry.parent;
1265 struct weston_matrix *matrix = &view->transform.matrix;
1266 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001267 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001268 pixman_region32_t surfregion;
1269 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001270
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001272
1273 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1275 view->transform.position.matrix.d[12] = view->geometry.x;
1276 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001277
1278 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001280 weston_matrix_multiply(matrix, &tform->matrix);
1281
Pekka Paalanen483243f2013-03-08 14:56:50 +02001282 if (parent)
1283 weston_matrix_multiply(matrix, &parent->transform.matrix);
1284
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001285 if (weston_matrix_invert(inverse, matrix) < 0) {
1286 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 weston_log("error: weston_view %p"
1288 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001289 return -1;
1290 }
1291
Pekka Paalanen380adf52015-02-16 14:39:11 +02001292 pixman_region32_init_rect(&surfregion, 0, 0,
1293 view->surface->width, view->surface->height);
1294 if (view->geometry.scissor_enabled)
1295 pixman_region32_intersect(&surfregion, &surfregion,
1296 &view->geometry.scissor);
1297 surfbox = pixman_region32_extents(&surfregion);
1298
1299 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1300 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001301
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001302 return 0;
1303}
1304
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001305static struct weston_layer *
1306get_view_layer(struct weston_view *view)
1307{
1308 if (view->parent_view)
1309 return get_view_layer(view->parent_view);
1310 return view->layer_link.layer;
1311}
1312
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001313WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001315{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001317 struct weston_layer *layer;
1318 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001319
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001321 return;
1322
Pekka Paalanen483243f2013-03-08 14:56:50 +02001323 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001324 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001325
Jason Ekstranda7af7042013-10-12 22:38:11 -05001326 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001327
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001329
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 pixman_region32_fini(&view->transform.boundingbox);
1331 pixman_region32_fini(&view->transform.opaque);
1332 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001333
Pekka Paalanencd403622012-01-25 13:37:39 +02001334 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 if (view->geometry.transformation_list.next ==
1336 &view->transform.position.link &&
1337 view->geometry.transformation_list.prev ==
1338 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001339 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001341 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342 if (weston_view_update_transform_enable(view) < 0)
1343 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001344 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001345
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001346 layer = get_view_layer(view);
1347 if (layer) {
1348 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001349 pixman_region32_intersect(&view->transform.boundingbox,
1350 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001351 pixman_region32_intersect(&view->transform.opaque,
1352 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001353 pixman_region32_fini(&mask);
1354 }
1355
Pekka Paalanen380adf52015-02-16 14:39:11 +02001356 if (parent) {
1357 if (parent->geometry.scissor_enabled) {
1358 view->geometry.scissor_enabled = true;
1359 weston_view_transfer_scissor(parent, view);
1360 } else {
1361 view->geometry.scissor_enabled = false;
1362 }
1363 }
1364
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001366
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001368
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 wl_signal_emit(&view->surface->compositor->transform_signal,
1370 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001371}
1372
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001373WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001375{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001376 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001377
1378 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 * The invariant: if view->geometry.dirty, then all views
1380 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001381 * Corollary: if not parent->geometry.dirty, then all ancestors
1382 * are not dirty.
1383 */
1384
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001386 return;
1387
Jason Ekstranda7af7042013-10-12 22:38:11 -05001388 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001389
Jason Ekstranda7af7042013-10-12 22:38:11 -05001390 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001391 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001393}
1394
1395WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396weston_view_to_global_fixed(struct weston_view *view,
1397 wl_fixed_t vx, wl_fixed_t vy,
1398 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001399{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001400 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001401
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402 weston_view_to_global_float(view,
1403 wl_fixed_to_double(vx),
1404 wl_fixed_to_double(vy),
1405 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001406 *x = wl_fixed_from_double(xf);
1407 *y = wl_fixed_from_double(yf);
1408}
1409
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001410WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411weston_view_from_global_float(struct weston_view *view,
1412 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001413{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001415 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1416
Jason Ekstranda7af7042013-10-12 22:38:11 -05001417 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001418
1419 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001420 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001422 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423 *vx = 0;
1424 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001425 return;
1426 }
1427
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 *vx = v.f[0] / v.f[3];
1429 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001430 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431 *vx = x - view->geometry.x;
1432 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001433 }
1434}
1435
1436WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437weston_view_from_global_fixed(struct weston_view *view,
1438 wl_fixed_t x, wl_fixed_t y,
1439 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001440{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001442
Jason Ekstranda7af7042013-10-12 22:38:11 -05001443 weston_view_from_global_float(view,
1444 wl_fixed_to_double(x),
1445 wl_fixed_to_double(y),
1446 &vxf, &vyf);
1447 *vx = wl_fixed_from_double(vxf);
1448 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001449}
1450
1451WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452weston_view_from_global(struct weston_view *view,
1453 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001454{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001456
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1458 *vx = floorf(vxf);
1459 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001460}
1461
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001462WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001463weston_surface_schedule_repaint(struct weston_surface *surface)
1464{
1465 struct weston_output *output;
1466
1467 wl_list_for_each(output, &surface->compositor->output_list, link)
1468 if (surface->output_mask & (1 << output->id))
1469 weston_output_schedule_repaint(output);
1470}
1471
1472WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473weston_view_schedule_repaint(struct weston_view *view)
1474{
1475 struct weston_output *output;
1476
1477 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1478 if (view->output_mask & (1 << output->id))
1479 weston_output_schedule_repaint(output);
1480}
1481
Pekka Paalanene508ce62015-02-19 13:59:55 +02001482/**
1483 * XXX: This function does it the wrong way.
1484 * surface->damage is the damage from the client, and causes
1485 * surface_flush_damage() to copy pixels. No window management action can
1486 * cause damage to the client-provided content, warranting re-upload!
1487 *
1488 * Instead of surface->damage, this function should record the damage
1489 * with all the views for this surface to avoid extraneous texture
1490 * uploads.
1491 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001493weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001494{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001495 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496 0, 0, surface->width,
1497 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001498
Kristian Høgsberg98238702012-08-03 16:29:12 -04001499 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001500}
1501
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001502WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001503weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001504{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001505 if (view->geometry.x == x && view->geometry.y == y)
1506 return;
1507
Jason Ekstranda7af7042013-10-12 22:38:11 -05001508 view->geometry.x = x;
1509 view->geometry.y = y;
1510 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001511}
1512
Pekka Paalanen483243f2013-03-08 14:56:50 +02001513static void
1514transform_parent_handle_parent_destroy(struct wl_listener *listener,
1515 void *data)
1516{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001517 struct weston_view *view =
1518 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001519 geometry.parent_destroy_listener);
1520
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001522}
1523
1524WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001525weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001526 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001527{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 if (view->geometry.parent) {
1529 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1530 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001531
1532 if (!parent)
1533 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001534 }
1535
Jason Ekstranda7af7042013-10-12 22:38:11 -05001536 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001537
Jason Ekstranda7af7042013-10-12 22:38:11 -05001538 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001539 transform_parent_handle_parent_destroy;
1540 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001541 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001542 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001543 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001544 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001545 }
1546
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 weston_view_geometry_dirty(view);
1548}
1549
Pekka Paalanen380adf52015-02-16 14:39:11 +02001550/** Set a clip mask rectangle on a view
1551 *
1552 * \param view The view to set the clip mask on.
1553 * \param x Top-left corner X coordinate of the clip rectangle.
1554 * \param y Top-left corner Y coordinate of the clip rectangle.
1555 * \param width Width of the clip rectangle, non-negative.
1556 * \param height Height of the clip rectangle, non-negative.
1557 *
1558 * A shell may set a clip mask rectangle on a view. Everything outside
1559 * the rectangle is cut away for input and output purposes: it is
1560 * not drawn and cannot be hit by hit-test based input like pointer
1561 * motion or touch-downs. Everything inside the rectangle will behave
1562 * normally. Clients are unaware of clipping.
1563 *
1564 * The rectangle is set in the surface local coordinates. Setting a clip
1565 * mask rectangle does not affect the view position, the view is positioned
1566 * as it would be without a clip. The clip also does not change
1567 * weston_surface::width,height.
1568 *
1569 * The clip mask rectangle is part of transformation inheritance
1570 * (weston_view_set_transform_parent()). A clip set in the root of the
1571 * transformation inheritance tree will affect all views in the tree.
1572 * A clip can be set only on the root view. Attempting to set a clip
1573 * on view that has a transformation parent will fail. Assigning a parent
1574 * to a view that has a clip set will cause the clip to be forgotten.
1575 *
1576 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1577 * on the additional transformations in the child views. These transformations
1578 * may not rotate the coordinate axes, i.e., only translation and scaling
1579 * are allowed. Violating this restriction causes the clipping to malfunction.
1580 * Furthermore, using scaling may cause rounding errors in child clipping.
1581 *
1582 * The clip mask rectangle is not automatically adjusted based on
1583 * wl_surface.attach dx and dy arguments.
1584 *
1585 * A clip mask rectangle can be set only if the compositor capability
1586 * WESTON_CAP_VIEW_CLIP_MASK is present.
1587 *
1588 * This function sets the clip mask rectangle and schedules a repaint for
1589 * the view.
1590 */
1591WL_EXPORT void
1592weston_view_set_mask(struct weston_view *view,
1593 int x, int y, int width, int height)
1594{
1595 struct weston_compositor *compositor = view->surface->compositor;
1596
1597 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1598 weston_log("%s not allowed without capability!\n", __func__);
1599 return;
1600 }
1601
1602 if (view->geometry.parent) {
1603 weston_log("view %p has a parent, clip forbidden!\n", view);
1604 return;
1605 }
1606
1607 if (width < 0 || height < 0) {
1608 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1609 x, y, width, height);
1610 return;
1611 }
1612
1613 pixman_region32_fini(&view->geometry.scissor);
1614 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1615 view->geometry.scissor_enabled = true;
1616 weston_view_geometry_dirty(view);
1617 weston_view_schedule_repaint(view);
1618}
1619
1620/** Remove the clip mask from a view
1621 *
1622 * \param view The view to remove the clip mask from.
1623 *
1624 * Removed the clip mask rectangle and schedules a repaint.
1625 *
1626 * \sa weston_view_set_mask
1627 */
1628WL_EXPORT void
1629weston_view_set_mask_infinite(struct weston_view *view)
1630{
1631 view->geometry.scissor_enabled = false;
1632 weston_view_geometry_dirty(view);
1633 weston_view_schedule_repaint(view);
1634}
1635
Derek Foreman280e7dd2014-10-03 13:13:42 -05001636WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001637weston_view_is_mapped(struct weston_view *view)
1638{
1639 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001640 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001641 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001642 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001643}
1644
Derek Foreman280e7dd2014-10-03 13:13:42 -05001645WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001646weston_surface_is_mapped(struct weston_surface *surface)
1647{
1648 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001649 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001650 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001651 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001652}
1653
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001654static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001655surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001656{
1657 struct weston_view *view;
1658
1659 if (surface->width == width && surface->height == height)
1660 return;
1661
1662 surface->width = width;
1663 surface->height = height;
1664
1665 wl_list_for_each(view, &surface->views, surface_link)
1666 weston_view_geometry_dirty(view);
1667}
1668
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001669WL_EXPORT void
1670weston_surface_set_size(struct weston_surface *surface,
1671 int32_t width, int32_t height)
1672{
1673 assert(!surface->resource);
1674 surface_set_size(surface, width, height);
1675}
1676
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001677static int
1678fixed_round_up_to_int(wl_fixed_t f)
1679{
1680 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1681}
1682
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001683static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001684weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001685{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001686 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001687 int32_t width, height;
1688
1689 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001690 surface->width_from_buffer = 0;
1691 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001692 return;
1693 }
1694
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001695 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001696 case WL_OUTPUT_TRANSFORM_90:
1697 case WL_OUTPUT_TRANSFORM_270:
1698 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1699 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001700 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1701 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001702 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001703 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001704 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1705 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001706 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001707 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001708
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001709 surface->width_from_buffer = width;
1710 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001711}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001712
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001713static void
1714weston_surface_update_size(struct weston_surface *surface)
1715{
1716 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1717 int32_t width, height;
1718
1719 width = surface->width_from_buffer;
1720 height = surface->height_from_buffer;
1721
1722 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001723 surface_set_size(surface,
1724 vp->surface.width, vp->surface.height);
1725 return;
1726 }
1727
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001728 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001729 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1730 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1731
1732 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001733 return;
1734 }
1735
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001736 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001737}
1738
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001739WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001740weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001741{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001742 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001743
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001744 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001745
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001746 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001747}
1748
Jason Ekstranda7af7042013-10-12 22:38:11 -05001749WL_EXPORT struct weston_view *
1750weston_compositor_pick_view(struct weston_compositor *compositor,
1751 wl_fixed_t x, wl_fixed_t y,
1752 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001753{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001754 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001755 wl_fixed_t view_x, view_y;
1756 int view_ix, view_iy;
1757 int ix = wl_fixed_to_int(x);
1758 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001759
Jason Ekstranda7af7042013-10-12 22:38:11 -05001760 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001761 if (!pixman_region32_contains_point(
1762 &view->transform.boundingbox, ix, iy, NULL))
1763 continue;
1764
1765 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1766 view_ix = wl_fixed_to_int(view_x);
1767 view_iy = wl_fixed_to_int(view_y);
1768
1769 if (!pixman_region32_contains_point(&view->surface->input,
1770 view_ix, view_iy, NULL))
1771 continue;
1772
Pekka Paalanen380adf52015-02-16 14:39:11 +02001773 if (view->geometry.scissor_enabled &&
1774 !pixman_region32_contains_point(&view->geometry.scissor,
1775 view_ix, view_iy, NULL))
1776 continue;
1777
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001778 *vx = view_x;
1779 *vy = view_y;
1780 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001781 }
1782
Derek Foremanf9318d12015-05-11 15:40:11 -05001783 *vx = wl_fixed_from_int(-1000000);
1784 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001785 return NULL;
1786}
1787
1788static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001789weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001790{
Daniel Stone37816df2012-05-16 18:45:18 +01001791 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001792
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001793 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001794 return;
1795
Daniel Stone37816df2012-05-16 18:45:18 +01001796 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001797 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001798}
1799
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001800WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001802{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001803 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001804
Jason Ekstranda7af7042013-10-12 22:38:11 -05001805 if (!weston_view_is_mapped(view))
1806 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001807
Jason Ekstranda7af7042013-10-12 22:38:11 -05001808 weston_view_damage_below(view);
1809 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001810 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001811 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001812 wl_list_remove(&view->link);
1813 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001814 view->output_mask = 0;
1815 weston_surface_assign_output(view->surface);
1816
1817 if (weston_surface_is_mapped(view->surface))
1818 return;
1819
1820 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001821 struct weston_touch *touch = weston_seat_get_touch(seat);
1822 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1823 struct weston_keyboard *keyboard =
1824 weston_seat_get_keyboard(seat);
1825
1826 if (keyboard && keyboard->focus == view->surface)
1827 weston_keyboard_set_focus(keyboard, NULL);
1828 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001829 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001830 if (touch && touch->focus == view)
1831 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001832 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001834
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835WL_EXPORT void
1836weston_surface_unmap(struct weston_surface *surface)
1837{
1838 struct weston_view *view;
1839
1840 wl_list_for_each(view, &surface->views, surface_link)
1841 weston_view_unmap(view);
1842 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001843}
1844
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001845static void
1846weston_surface_reset_pending_buffer(struct weston_surface *surface)
1847{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001848 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001849 surface->pending.sx = 0;
1850 surface->pending.sy = 0;
1851 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001852 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001853}
1854
Jason Ekstranda7af7042013-10-12 22:38:11 -05001855WL_EXPORT void
1856weston_view_destroy(struct weston_view *view)
1857{
1858 wl_signal_emit(&view->destroy_signal, view);
1859
1860 assert(wl_list_empty(&view->geometry.child_list));
1861
1862 if (weston_view_is_mapped(view)) {
1863 weston_view_unmap(view);
1864 weston_compositor_build_view_list(view->surface->compositor);
1865 }
1866
1867 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001868 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869
1870 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001871 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001872 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001873 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001874
1875 weston_view_set_transform_parent(view, NULL);
1876
Jason Ekstranda7af7042013-10-12 22:38:11 -05001877 wl_list_remove(&view->surface_link);
1878
1879 free(view);
1880}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001881
1882WL_EXPORT void
1883weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001884{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001885 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001886 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001887
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001888 if (--surface->ref_count > 0)
1889 return;
1890
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001891 assert(surface->resource == NULL);
1892
Pekka Paalanenca790762015-04-17 14:23:38 +03001893 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001894
Pekka Paalanene67858b2013-04-25 13:57:42 +03001895 assert(wl_list_empty(&surface->subsurface_list_pending));
1896 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001897
Jason Ekstranda7af7042013-10-12 22:38:11 -05001898 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1899 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001900
Jason Ekstrand7b982072014-05-20 14:33:03 -05001901 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001902
Pekka Paalanende685b82012-12-04 15:58:12 +02001903 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001904
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001905 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001906 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001907 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001908
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001909 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001910 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001911
Pekka Paalanen133e4392014-09-23 22:08:46 -04001912 weston_presentation_feedback_discard_list(&surface->feedback_list);
1913
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001914 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001915}
1916
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001917static void
1918destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001919{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001920 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001921
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001922 assert(surface);
1923
Giulio Camuffo0d379742013-11-15 22:06:15 +01001924 /* Set the resource to NULL, since we don't want to leave a
1925 * dangling pointer if the surface was refcounted and survives
1926 * the weston_surface_destroy() call. */
1927 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001928 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001929}
1930
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001931static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001932weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1933{
1934 struct weston_buffer *buffer =
1935 container_of(listener, struct weston_buffer, destroy_listener);
1936
1937 wl_signal_emit(&buffer->destroy_signal, buffer);
1938 free(buffer);
1939}
1940
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001941WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001942weston_buffer_from_resource(struct wl_resource *resource)
1943{
1944 struct weston_buffer *buffer;
1945 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001946
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001947 listener = wl_resource_get_destroy_listener(resource,
1948 weston_buffer_destroy_handler);
1949
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001950 if (listener)
1951 return container_of(listener, struct weston_buffer,
1952 destroy_listener);
1953
1954 buffer = zalloc(sizeof *buffer);
1955 if (buffer == NULL)
1956 return NULL;
1957
1958 buffer->resource = resource;
1959 wl_signal_init(&buffer->destroy_signal);
1960 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001961 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001962 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001963
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001964 return buffer;
1965}
1966
1967static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001968weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1969 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001970{
Pekka Paalanende685b82012-12-04 15:58:12 +02001971 struct weston_buffer_reference *ref =
1972 container_of(listener, struct weston_buffer_reference,
1973 destroy_listener);
1974
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001975 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001976 ref->buffer = NULL;
1977}
1978
1979WL_EXPORT void
1980weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001981 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001982{
1983 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001984 ref->buffer->busy_count--;
1985 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001986 assert(wl_resource_get_client(ref->buffer->resource));
1987 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001988 WL_BUFFER_RELEASE);
1989 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001990 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001991 }
1992
Pekka Paalanende685b82012-12-04 15:58:12 +02001993 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001994 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001995 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001996 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001997 }
1998
Pekka Paalanende685b82012-12-04 15:58:12 +02001999 ref->buffer = buffer;
2000 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2001}
2002
2003static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002004weston_surface_attach(struct weston_surface *surface,
2005 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002006{
2007 weston_buffer_reference(&surface->buffer_ref, buffer);
2008
Pekka Paalanena6421c42012-12-04 15:58:10 +02002009 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002010 if (weston_surface_is_mapped(surface))
2011 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002012 }
2013
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002014 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002015
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002016 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002017 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002018}
2019
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002020WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002021weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002022{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002023 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002024
2025 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002026 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002027}
2028
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002029WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002030weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002031{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002032 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002033
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002034 pixman_region32_union(&compositor->primary_plane.damage,
2035 &compositor->primary_plane.damage,
2036 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002037 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002038}
2039
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002040static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002041surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002042{
Pekka Paalanende685b82012-12-04 15:58:12 +02002043 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002044 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002045 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002046
Pekka Paalanenb5026542014-11-12 15:09:24 +02002047 if (weston_timeline_enabled_ &&
2048 pixman_region32_not_empty(&surface->damage))
2049 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2050 TLP_OUTPUT(surface->output), TLP_END);
2051
Jason Ekstrandef540082014-06-26 10:37:36 -07002052 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002053}
2054
2055static void
2056view_accumulate_damage(struct weston_view *view,
2057 pixman_region32_t *opaque)
2058{
2059 pixman_region32_t damage;
2060
2061 pixman_region32_init(&damage);
2062 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002063 pixman_box32_t *extents;
2064
Jason Ekstranda7af7042013-10-12 22:38:11 -05002065 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002066 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002067 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002068 pixman_region32_copy(&damage, &view->surface->damage);
2069 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002070 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002071 }
2072
Pekka Paalanen380adf52015-02-16 14:39:11 +02002073 pixman_region32_intersect(&damage, &damage,
2074 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002075 pixman_region32_subtract(&damage, &damage, opaque);
2076 pixman_region32_union(&view->plane->damage,
2077 &view->plane->damage, &damage);
2078 pixman_region32_fini(&damage);
2079 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002080 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002081}
2082
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002083static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002084compositor_accumulate_damage(struct weston_compositor *ec)
2085{
2086 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002088 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002089
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002090 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002091
2092 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002093 pixman_region32_copy(&plane->clip, &clip);
2094
2095 pixman_region32_init(&opaque);
2096
Jason Ekstranda7af7042013-10-12 22:38:11 -05002097 wl_list_for_each(ev, &ec->view_list, link) {
2098 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002099 continue;
2100
Jason Ekstranda7af7042013-10-12 22:38:11 -05002101 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002102 }
2103
2104 pixman_region32_union(&clip, &clip, &opaque);
2105 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002106 }
2107
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002108 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002109
Jason Ekstranda7af7042013-10-12 22:38:11 -05002110 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002111 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112
2113 wl_list_for_each(ev, &ec->view_list, link) {
2114 if (ev->surface->touched)
2115 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002116 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002117
2118 surface_flush_damage(ev->surface);
2119
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002120 /* Both the renderer and the backend have seen the buffer
2121 * by now. If renderer needs the buffer, it has its own
2122 * reference set. If the backend wants to keep the buffer
2123 * around for migrating the surface into a non-primary plane
2124 * later, keep_buffer is true. Otherwise, drop the core
2125 * reference now, and allow early buffer release. This enables
2126 * clients to use single-buffering.
2127 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002128 if (!ev->surface->keep_buffer)
2129 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002130 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002131}
2132
2133static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002134surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002135{
2136 struct weston_subsurface *sub;
2137
Pekka Paalanene67858b2013-04-25 13:57:42 +03002138 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002139 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002140 continue;
2141
Jason Ekstranda7af7042013-10-12 22:38:11 -05002142 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2143 wl_list_init(&sub->surface->views);
2144
2145 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002146 }
2147}
2148
2149static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002150surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002151{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002152 struct weston_subsurface *sub;
2153 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002154
Jason Ekstranda7af7042013-10-12 22:38:11 -05002155 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2156 if (sub->surface == surface)
2157 continue;
2158
George Kiagiadakised04d382014-06-13 18:10:26 +02002159 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2160 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002162 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002163
2164 surface_free_unused_subsurface_views(sub->surface);
2165 }
2166}
2167
2168static void
2169view_list_add_subsurface_view(struct weston_compositor *compositor,
2170 struct weston_subsurface *sub,
2171 struct weston_view *parent)
2172{
2173 struct weston_subsurface *child;
2174 struct weston_view *view = NULL, *iv;
2175
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002176 if (!weston_surface_is_mapped(sub->surface))
2177 return;
2178
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2180 if (iv->geometry.parent == parent) {
2181 view = iv;
2182 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002183 }
2184 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002185
2186 if (view) {
2187 /* Put it back in the surface's list of views */
2188 wl_list_remove(&view->surface_link);
2189 wl_list_insert(&sub->surface->views, &view->surface_link);
2190 } else {
2191 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002192 weston_view_set_position(view,
2193 sub->position.x,
2194 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002195 weston_view_set_transform_parent(view, parent);
2196 }
2197
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002198 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002199 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002200
Pekka Paalanenb188e912013-11-19 14:03:35 +02002201 if (wl_list_empty(&sub->surface->subsurface_list)) {
2202 wl_list_insert(compositor->view_list.prev, &view->link);
2203 return;
2204 }
2205
2206 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2207 if (child->surface == sub->surface)
2208 wl_list_insert(compositor->view_list.prev, &view->link);
2209 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002210 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002211 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212}
2213
2214static void
2215view_list_add(struct weston_compositor *compositor,
2216 struct weston_view *view)
2217{
2218 struct weston_subsurface *sub;
2219
2220 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002221
Pekka Paalanenb188e912013-11-19 14:03:35 +02002222 if (wl_list_empty(&view->surface->subsurface_list)) {
2223 wl_list_insert(compositor->view_list.prev, &view->link);
2224 return;
2225 }
2226
2227 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2228 if (sub->surface == view->surface)
2229 wl_list_insert(compositor->view_list.prev, &view->link);
2230 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002231 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002232 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002233}
2234
2235static void
2236weston_compositor_build_view_list(struct weston_compositor *compositor)
2237{
2238 struct weston_view *view;
2239 struct weston_layer *layer;
2240
2241 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002242 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002243 surface_stash_subsurface_views(view->surface);
2244
2245 wl_list_init(&compositor->view_list);
2246 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002247 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002248 view_list_add(compositor, view);
2249 }
2250 }
2251
2252 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002253 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002254 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002255}
2256
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002257static void
2258weston_output_take_feedback_list(struct weston_output *output,
2259 struct weston_surface *surface)
2260{
2261 struct weston_view *view;
2262 struct weston_presentation_feedback *feedback;
2263 uint32_t flags = 0xffffffff;
2264
2265 if (wl_list_empty(&surface->feedback_list))
2266 return;
2267
2268 /* All views must have the flag for the flag to survive. */
2269 wl_list_for_each(view, &surface->views, surface_link) {
2270 /* ignore views that are not on this output at all */
2271 if (view->output_mask & (1u << output->id))
2272 flags &= view->psf_flags;
2273 }
2274
2275 wl_list_for_each(feedback, &surface->feedback_list, link)
2276 feedback->psf_flags = flags;
2277
2278 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2279 wl_list_init(&surface->feedback_list);
2280}
2281
David Herrmann1edf44c2013-10-22 17:11:26 +02002282static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002283weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002284{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002285 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002286 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002287 struct weston_animation *animation, *next;
2288 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002289 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002290 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002291 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002292
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002293 if (output->destroying)
2294 return 0;
2295
Pekka Paalanenb5026542014-11-12 15:09:24 +02002296 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2297
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002298 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002299 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002300
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002301 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002302 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002303 } else {
2304 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002306 ev->psf_flags = 0;
2307 }
2308 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002309
Pekka Paalanene67858b2013-04-25 13:57:42 +03002310 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002311 wl_list_for_each(ev, &ec->view_list, link) {
2312 /* Note: This operation is safe to do multiple times on the
2313 * same surface.
2314 */
2315 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002316 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002317 &ev->surface->frame_callback_list);
2318 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002319
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002320 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002321 }
2322 }
2323
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002324 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002325
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002326 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002327 pixman_region32_intersect(&output_damage,
2328 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002329 pixman_region32_subtract(&output_damage,
2330 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002331
Scott Moreauccbf29d2012-02-22 14:21:41 -07002332 if (output->dirty)
2333 weston_output_update_matrix(output);
2334
David Herrmann1edf44c2013-10-22 17:11:26 +02002335 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002336
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002337 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002338
Kristian Høgsbergef044142011-06-21 15:02:12 -04002339 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002340
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002341 weston_compositor_repick(ec);
2342 wl_event_loop_dispatch(ec->input_loop, 0);
2343
Jonas Ådahldb773762012-06-13 00:01:21 +02002344 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002345 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002346 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002347 }
2348
Scott Moreaud64cf212012-06-08 19:40:54 -06002349 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002350 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002351 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002352 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002353
Pekka Paalanenb5026542014-11-12 15:09:24 +02002354 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2355
David Herrmann1edf44c2013-10-22 17:11:26 +02002356 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002357}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002358
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002359static int
2360weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002361{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002362 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002363
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002364 wl_event_loop_dispatch(compositor->input_loop, 0);
2365
2366 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002367}
2368
Pekka Paalanen82919792014-05-21 13:51:49 +03002369static void
2370weston_output_schedule_repaint_reset(struct weston_output *output)
2371{
2372 struct weston_compositor *compositor = output->compositor;
2373 struct wl_event_loop *loop;
2374 int fd;
2375
2376 output->repaint_scheduled = 0;
2377 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2378
2379 if (compositor->input_loop_source)
2380 return;
2381
2382 loop = wl_display_get_event_loop(compositor->wl_display);
2383 fd = wl_event_loop_get_fd(compositor->input_loop);
2384 compositor->input_loop_source =
2385 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2386 weston_compositor_read_input, compositor);
2387}
2388
Pekka Paalanen0513a952014-05-21 16:17:27 +03002389static int
2390output_repaint_timer_handler(void *data)
2391{
2392 struct weston_output *output = data;
2393 struct weston_compositor *compositor = output->compositor;
2394
2395 if (output->repaint_needed &&
2396 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2397 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2398 weston_output_repaint(output) == 0)
2399 return 0;
2400
2401 weston_output_schedule_repaint_reset(output);
2402
2403 return 0;
2404}
2405
Kristian Høgsbergef044142011-06-21 15:02:12 -04002406WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002407weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002408 const struct timespec *stamp,
2409 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002410{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002411 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002412 int32_t refresh_nsec;
2413 struct timespec now;
2414 struct timespec gone;
2415 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002416
Pekka Paalanenb5026542014-11-12 15:09:24 +02002417 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2418 TLP_VBLANK(stamp), TLP_END);
2419
Pekka Paalanend7894d02015-07-03 15:08:53 +03002420 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002421 weston_presentation_feedback_present_list(&output->feedback_list,
2422 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002423 output->msc,
2424 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002425
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002426 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002427
Pekka Paalanen0513a952014-05-21 16:17:27 +03002428 weston_compositor_read_presentation_clock(compositor, &now);
2429 timespec_sub(&gone, &now, stamp);
2430 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2431 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002432
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002433 if (msec < -1000 || msec > 1000) {
2434 static bool warned;
2435
2436 if (!warned)
2437 weston_log("Warning: computed repaint delay is "
2438 "insane: %d msec\n", msec);
2439 warned = true;
2440
2441 msec = 0;
2442 }
2443
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002444 /* Called from restart_repaint_loop and restart happens already after
2445 * the deadline given by repaint_msec? In that case we delay until
2446 * the deadline of the next frame, to give clients a more predictable
2447 * timing of the repaint cycle to lock on. */
2448 if (presented_flags == PRESENTATION_FEEDBACK_INVALID && msec < 0)
2449 msec += refresh_nsec / 1000000;
2450
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002451 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002452 output_repaint_timer_handler(output);
2453 else
2454 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002455}
2456
2457static void
2458idle_repaint(void *data)
2459{
2460 struct weston_output *output = data;
2461
Jonas Ådahle5a12252013-04-05 23:07:11 +02002462 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002463}
2464
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002465WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002466weston_layer_entry_insert(struct weston_layer_entry *list,
2467 struct weston_layer_entry *entry)
2468{
2469 wl_list_insert(&list->link, &entry->link);
2470 entry->layer = list->layer;
2471}
2472
2473WL_EXPORT void
2474weston_layer_entry_remove(struct weston_layer_entry *entry)
2475{
2476 wl_list_remove(&entry->link);
2477 wl_list_init(&entry->link);
2478 entry->layer = NULL;
2479}
2480
2481WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002482weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2483{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002484 wl_list_init(&layer->view_list.link);
2485 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002486 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002487 if (below != NULL)
2488 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002489}
2490
2491WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002492weston_layer_set_mask(struct weston_layer *layer,
2493 int x, int y, int width, int height)
2494{
2495 struct weston_view *view;
2496
2497 layer->mask.x1 = x;
2498 layer->mask.x2 = x + width;
2499 layer->mask.y1 = y;
2500 layer->mask.y2 = y + height;
2501
2502 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2503 weston_view_geometry_dirty(view);
2504 }
2505}
2506
2507WL_EXPORT void
2508weston_layer_set_mask_infinite(struct weston_layer *layer)
2509{
2510 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2511 UINT32_MAX, UINT32_MAX);
2512}
2513
2514WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002515weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002516{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002517 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002518 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002519
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002520 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2521 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002522 return;
2523
Pekka Paalanenb5026542014-11-12 15:09:24 +02002524 if (!output->repaint_needed)
2525 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2526
Kristian Høgsbergef044142011-06-21 15:02:12 -04002527 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002528 output->repaint_needed = 1;
2529 if (output->repaint_scheduled)
2530 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002531
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002532 wl_event_loop_add_idle(loop, idle_repaint, output);
2533 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002534 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2535
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002536
2537 if (compositor->input_loop_source) {
2538 wl_event_source_remove(compositor->input_loop_source);
2539 compositor->input_loop_source = NULL;
2540 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002541}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002542
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002543WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002544weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2545{
2546 struct weston_output *output;
2547
2548 wl_list_for_each(output, &compositor->output_list, link)
2549 weston_output_schedule_repaint(output);
2550}
2551
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002552static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002553surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002554{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002555 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002556}
2557
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002558static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002559surface_attach(struct wl_client *client,
2560 struct wl_resource *resource,
2561 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2562{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002563 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002564 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002565
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002566 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002567 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002568 if (buffer == NULL) {
2569 wl_client_post_no_memory(client);
2570 return;
2571 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002572 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002573
Pekka Paalanende685b82012-12-04 15:58:12 +02002574 /* Attach, attach, without commit in between does not send
2575 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002576 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002577
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002578 surface->pending.sx = sx;
2579 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002580 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002581}
2582
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002583static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002584surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002585 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002586 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002587{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002588 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002589
Derek Foreman57e92ed2015-11-17 14:11:35 -06002590 if (width <= 0 || height <= 0)
2591 return;
2592
Derek Foreman152254b2015-11-26 14:17:48 -06002593 pixman_region32_union_rect(&surface->pending.damage_surface,
2594 &surface->pending.damage_surface,
2595 x, y, width, height);
2596}
2597
2598static void
2599surface_damage_buffer(struct wl_client *client,
2600 struct wl_resource *resource,
2601 int32_t x, int32_t y, int32_t width, int32_t height)
2602{
2603 struct weston_surface *surface = wl_resource_get_user_data(resource);
2604
2605 if (width <= 0 || height <= 0)
2606 return;
2607
2608 pixman_region32_union_rect(&surface->pending.damage_buffer,
2609 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002610 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002611}
2612
Kristian Høgsberg33418202011-08-16 23:01:28 -04002613static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002614destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002615{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002616 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002617
2618 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002619 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002620}
2621
2622static void
2623surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002624 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002625{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002626 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002627 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002628
2629 cb = malloc(sizeof *cb);
2630 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002631 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002632 return;
2633 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002634
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002635 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2636 callback);
2637 if (cb->resource == NULL) {
2638 free(cb);
2639 wl_resource_post_no_memory(resource);
2640 return;
2641 }
2642
Jason Ekstranda85118c2013-06-27 20:17:02 -05002643 wl_resource_set_implementation(cb->resource, NULL, cb,
2644 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002645
Pekka Paalanenbc106382012-10-10 12:49:31 +03002646 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002647}
2648
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002649static void
2650surface_set_opaque_region(struct wl_client *client,
2651 struct wl_resource *resource,
2652 struct wl_resource *region_resource)
2653{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002654 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002655 struct weston_region *region;
2656
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002657 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002658 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002659 pixman_region32_copy(&surface->pending.opaque,
2660 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002661 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002662 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002663 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002664}
2665
2666static void
2667surface_set_input_region(struct wl_client *client,
2668 struct wl_resource *resource,
2669 struct wl_resource *region_resource)
2670{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002671 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002672 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002673
2674 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002675 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002676 pixman_region32_copy(&surface->pending.input,
2677 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002678 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002679 pixman_region32_fini(&surface->pending.input);
2680 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002681 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002682}
2683
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002684static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002685weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002686{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002687 struct weston_subsurface *sub;
2688
2689 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2690 parent_link_pending) {
2691 wl_list_remove(&sub->parent_link);
2692 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2693 }
2694}
2695
2696static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002697weston_surface_build_buffer_matrix(struct weston_surface *surface,
2698 struct weston_matrix *matrix)
2699{
2700 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2701 double src_width, src_height, dest_width, dest_height;
2702
2703 weston_matrix_init(matrix);
2704
2705 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2706 src_width = surface->width_from_buffer;
2707 src_height = surface->height_from_buffer;
2708 } else {
2709 src_width = wl_fixed_to_double(vp->buffer.src_width);
2710 src_height = wl_fixed_to_double(vp->buffer.src_height);
2711 }
2712
2713 if (vp->surface.width == -1) {
2714 dest_width = src_width;
2715 dest_height = src_height;
2716 } else {
2717 dest_width = vp->surface.width;
2718 dest_height = vp->surface.height;
2719 }
2720
2721 if (src_width != dest_width || src_height != dest_height)
2722 weston_matrix_scale(matrix,
2723 src_width / dest_width,
2724 src_height / dest_height, 1);
2725
2726 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2727 weston_matrix_translate(matrix,
2728 wl_fixed_to_double(vp->buffer.src_x),
2729 wl_fixed_to_double(vp->buffer.src_y),
2730 0);
2731
2732 switch (vp->buffer.transform) {
2733 case WL_OUTPUT_TRANSFORM_FLIPPED:
2734 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2735 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2736 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2737 weston_matrix_scale(matrix, -1, 1, 1);
2738 weston_matrix_translate(matrix,
2739 surface->width_from_buffer, 0, 0);
2740 break;
2741 }
2742
2743 switch (vp->buffer.transform) {
2744 default:
2745 case WL_OUTPUT_TRANSFORM_NORMAL:
2746 case WL_OUTPUT_TRANSFORM_FLIPPED:
2747 break;
2748 case WL_OUTPUT_TRANSFORM_90:
2749 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2750 weston_matrix_rotate_xy(matrix, 0, 1);
2751 weston_matrix_translate(matrix,
2752 surface->height_from_buffer, 0, 0);
2753 break;
2754 case WL_OUTPUT_TRANSFORM_180:
2755 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2756 weston_matrix_rotate_xy(matrix, -1, 0);
2757 weston_matrix_translate(matrix,
2758 surface->width_from_buffer,
2759 surface->height_from_buffer, 0);
2760 break;
2761 case WL_OUTPUT_TRANSFORM_270:
2762 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2763 weston_matrix_rotate_xy(matrix, 0, -1);
2764 weston_matrix_translate(matrix,
2765 0, surface->width_from_buffer, 0);
2766 break;
2767 }
2768
2769 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2770}
2771
Derek Foreman152254b2015-11-26 14:17:48 -06002772/* Translate pending damage in buffer co-ordinates to surface
2773 * co-ordinates and union it with a pixman_region32_t.
2774 * This should only be called after the buffer is attached.
2775 */
2776static void
2777apply_damage_buffer(pixman_region32_t *dest,
2778 struct weston_surface *surface,
2779 struct weston_surface_state *state)
2780{
2781 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2782
2783 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2784 * translated into surface co-ordinates and unioned with
2785 * any other surface damage.
2786 * None of this makes sense if there is no buffer though.
2787 */
2788 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2789 pixman_region32_t buffer_damage;
2790
2791 pixman_region32_intersect_rect(&state->damage_buffer,
2792 &state->damage_buffer,
2793 0, 0, buffer->width,
2794 buffer->height);
2795 pixman_region32_init(&buffer_damage);
2796 weston_matrix_transform_region(&buffer_damage,
2797 &surface->buffer_to_surface_matrix,
2798 &state->damage_buffer);
2799 pixman_region32_union(dest, dest, &buffer_damage);
2800 pixman_region32_fini(&buffer_damage);
2801 }
2802 /* We should clear this on commit even if there was no buffer */
2803 pixman_region32_clear(&state->damage_buffer);
2804}
2805
Jason Ekstrand1e059042014-10-16 10:55:19 -05002806static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002807weston_surface_commit_state(struct weston_surface *surface,
2808 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002809{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002810 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002811 pixman_region32_t opaque;
2812
Alexander Larsson4ea95522013-05-22 14:41:37 +02002813 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002814 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002815 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002816 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002817
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002818 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002819 if (state->newly_attached)
2820 weston_surface_attach(surface, state->buffer);
2821 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002822
Jason Ekstrand1e059042014-10-16 10:55:19 -05002823 weston_surface_build_buffer_matrix(surface,
2824 &surface->surface_to_buffer_matrix);
2825 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2826 &surface->surface_to_buffer_matrix);
2827
Jason Ekstrand7b982072014-05-20 14:33:03 -05002828 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002829 weston_surface_update_size(surface);
2830 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002831 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002832 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002833
Jason Ekstrand7b982072014-05-20 14:33:03 -05002834 state->sx = 0;
2835 state->sy = 0;
2836 state->newly_attached = 0;
2837 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002838
Derek Foreman152254b2015-11-26 14:17:48 -06002839 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002840 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002841 (pixman_region32_not_empty(&state->damage_surface) ||
2842 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002843 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002844
Pekka Paalanen8e159182012-10-10 12:49:25 +03002845 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002846 &state->damage_surface);
2847
2848 apply_damage_buffer(&surface->damage, surface, state);
2849
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002850 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002851 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002852 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002853
2854 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002855 pixman_region32_init(&opaque);
2856 pixman_region32_intersect_rect(&opaque, &state->opaque,
2857 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002858
2859 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2860 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002861 wl_list_for_each(view, &surface->views, surface_link)
2862 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002863 }
2864
2865 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002866
2867 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002868 pixman_region32_intersect_rect(&surface->input, &state->input,
2869 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002870
Pekka Paalanenbc106382012-10-10 12:49:31 +03002871 /* wl_surface.frame */
2872 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002873 &state->frame_callback_list);
2874 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002875
2876 /* XXX:
2877 * What should happen with a feedback request, if there
2878 * is no wl_buffer attached for this commit?
2879 */
2880
2881 /* presentation.feedback */
2882 wl_list_insert_list(&surface->feedback_list,
2883 &state->feedback_list);
2884 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002885}
2886
2887static void
2888weston_surface_commit(struct weston_surface *surface)
2889{
2890 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002891
Pekka Paalanene67858b2013-04-25 13:57:42 +03002892 weston_surface_commit_subsurface_order(surface);
2893
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002894 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002895}
2896
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002897static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002898weston_subsurface_commit(struct weston_subsurface *sub);
2899
2900static void
2901weston_subsurface_parent_commit(struct weston_subsurface *sub,
2902 int parent_is_synchronized);
2903
2904static void
2905surface_commit(struct wl_client *client, struct wl_resource *resource)
2906{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002907 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002908 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2909
2910 if (sub) {
2911 weston_subsurface_commit(sub);
2912 return;
2913 }
2914
2915 weston_surface_commit(surface);
2916
2917 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2918 if (sub->surface != surface)
2919 weston_subsurface_parent_commit(sub, 0);
2920 }
2921}
2922
2923static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002924surface_set_buffer_transform(struct wl_client *client,
2925 struct wl_resource *resource, int transform)
2926{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002927 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002928
Jonny Lamba55f1392014-05-30 12:07:15 +02002929 /* if wl_output.transform grows more members this will need to be updated. */
2930 if (transform < 0 ||
2931 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2932 wl_resource_post_error(resource,
2933 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2934 "buffer transform must be a valid transform "
2935 "('%d' specified)", transform);
2936 return;
2937 }
2938
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002939 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002940 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002941}
2942
Alexander Larsson4ea95522013-05-22 14:41:37 +02002943static void
2944surface_set_buffer_scale(struct wl_client *client,
2945 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002946 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002947{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002948 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002949
Jonny Lamba55f1392014-05-30 12:07:15 +02002950 if (scale < 1) {
2951 wl_resource_post_error(resource,
2952 WL_SURFACE_ERROR_INVALID_SCALE,
2953 "buffer scale must be at least one "
2954 "('%d' specified)", scale);
2955 return;
2956 }
2957
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002958 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002959 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002960}
2961
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002962static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002963 surface_destroy,
2964 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002965 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002966 surface_frame,
2967 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002968 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002969 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002970 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06002971 surface_set_buffer_scale,
2972 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002973};
2974
2975static void
2976compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002977 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002978{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002979 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002980 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002981
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002982 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002983 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002984 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002985 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002986 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002987
Jason Ekstranda85118c2013-06-27 20:17:02 -05002988 surface->resource =
2989 wl_resource_create(client, &wl_surface_interface,
2990 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002991 if (surface->resource == NULL) {
2992 weston_surface_destroy(surface);
2993 wl_resource_post_no_memory(resource);
2994 return;
2995 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002996 wl_resource_set_implementation(surface->resource, &surface_interface,
2997 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002998
2999 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003000}
3001
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003002static void
3003destroy_region(struct wl_resource *resource)
3004{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003005 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003006
3007 pixman_region32_fini(&region->region);
3008 free(region);
3009}
3010
3011static void
3012region_destroy(struct wl_client *client, struct wl_resource *resource)
3013{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003014 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003015}
3016
3017static void
3018region_add(struct wl_client *client, struct wl_resource *resource,
3019 int32_t x, int32_t y, int32_t width, int32_t height)
3020{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003021 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003022
3023 pixman_region32_union_rect(&region->region, &region->region,
3024 x, y, width, height);
3025}
3026
3027static void
3028region_subtract(struct wl_client *client, struct wl_resource *resource,
3029 int32_t x, int32_t y, int32_t width, int32_t height)
3030{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003031 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003032 pixman_region32_t rect;
3033
3034 pixman_region32_init_rect(&rect, x, y, width, height);
3035 pixman_region32_subtract(&region->region, &region->region, &rect);
3036 pixman_region32_fini(&rect);
3037}
3038
3039static const struct wl_region_interface region_interface = {
3040 region_destroy,
3041 region_add,
3042 region_subtract
3043};
3044
3045static void
3046compositor_create_region(struct wl_client *client,
3047 struct wl_resource *resource, uint32_t id)
3048{
3049 struct weston_region *region;
3050
3051 region = malloc(sizeof *region);
3052 if (region == NULL) {
3053 wl_resource_post_no_memory(resource);
3054 return;
3055 }
3056
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003057 pixman_region32_init(&region->region);
3058
Jason Ekstranda85118c2013-06-27 20:17:02 -05003059 region->resource =
3060 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003061 if (region->resource == NULL) {
3062 free(region);
3063 wl_resource_post_no_memory(resource);
3064 return;
3065 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003066 wl_resource_set_implementation(region->resource, &region_interface,
3067 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003068}
3069
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003070static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003071 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003072 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003073};
3074
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003075static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003076weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3077{
3078 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003079
Jason Ekstrand7b982072014-05-20 14:33:03 -05003080 weston_surface_commit_state(surface, &sub->cached);
3081 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003082
3083 weston_surface_commit_subsurface_order(surface);
3084
3085 weston_surface_schedule_repaint(surface);
3086
Jason Ekstrand7b982072014-05-20 14:33:03 -05003087 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003088}
3089
3090static void
3091weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3092{
3093 struct weston_surface *surface = sub->surface;
3094
3095 /*
3096 * If this commit would cause the surface to move by the
3097 * attach(dx, dy) parameters, the old damage region must be
3098 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003099 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003100 */
Derek Foreman152254b2015-11-26 14:17:48 -06003101 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003102 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003103 pixman_region32_union(&sub->cached.damage_surface,
3104 &sub->cached.damage_surface,
3105 &surface->pending.damage_surface);
3106 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003107
3108 if (surface->pending.newly_attached) {
3109 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003110 weston_surface_state_set_buffer(&sub->cached,
3111 surface->pending.buffer);
3112 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003113 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003114 weston_presentation_feedback_discard_list(
3115 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003116 }
3117 sub->cached.sx += surface->pending.sx;
3118 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003119
Derek Foreman152254b2015-11-26 14:17:48 -06003120 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3121
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003122 sub->cached.buffer_viewport.changed |=
3123 surface->pending.buffer_viewport.changed;
3124 sub->cached.buffer_viewport.buffer =
3125 surface->pending.buffer_viewport.buffer;
3126 sub->cached.buffer_viewport.surface =
3127 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003128
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003129 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003130
3131 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3132
3133 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3134
3135 wl_list_insert_list(&sub->cached.frame_callback_list,
3136 &surface->pending.frame_callback_list);
3137 wl_list_init(&surface->pending.frame_callback_list);
3138
Pekka Paalanen133e4392014-09-23 22:08:46 -04003139 wl_list_insert_list(&sub->cached.feedback_list,
3140 &surface->pending.feedback_list);
3141 wl_list_init(&surface->pending.feedback_list);
3142
Jason Ekstrand7b982072014-05-20 14:33:03 -05003143 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003144}
3145
Derek Foreman280e7dd2014-10-03 13:13:42 -05003146static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003147weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3148{
3149 while (sub) {
3150 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003151 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003152
3153 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003154 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003155
3156 sub = weston_surface_to_subsurface(sub->parent);
3157 }
3158
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003159 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003160}
3161
3162static void
3163weston_subsurface_commit(struct weston_subsurface *sub)
3164{
3165 struct weston_surface *surface = sub->surface;
3166 struct weston_subsurface *tmp;
3167
3168 /* Recursive check for effectively synchronized. */
3169 if (weston_subsurface_is_synchronized(sub)) {
3170 weston_subsurface_commit_to_cache(sub);
3171 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003172 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003173 /* flush accumulated state from cache */
3174 weston_subsurface_commit_to_cache(sub);
3175 weston_subsurface_commit_from_cache(sub);
3176 } else {
3177 weston_surface_commit(surface);
3178 }
3179
3180 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3181 if (tmp->surface != surface)
3182 weston_subsurface_parent_commit(tmp, 0);
3183 }
3184 }
3185}
3186
3187static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003188weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189{
3190 struct weston_surface *surface = sub->surface;
3191 struct weston_subsurface *tmp;
3192
Pekka Paalanene67858b2013-04-25 13:57:42 +03003193 /* From now on, commit_from_cache the whole sub-tree, regardless of
3194 * the synchronized mode of each child. This sub-surface or some
3195 * of its ancestors were synchronized, so we are synchronized
3196 * all the way down.
3197 */
3198
Jason Ekstrand7b982072014-05-20 14:33:03 -05003199 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003200 weston_subsurface_commit_from_cache(sub);
3201
3202 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3203 if (tmp->surface != surface)
3204 weston_subsurface_parent_commit(tmp, 1);
3205 }
3206}
3207
3208static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003209weston_subsurface_parent_commit(struct weston_subsurface *sub,
3210 int parent_is_synchronized)
3211{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003212 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003213 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003214 wl_list_for_each(view, &sub->surface->views, surface_link)
3215 weston_view_set_position(view,
3216 sub->position.x,
3217 sub->position.y);
3218
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003219 sub->position.set = 0;
3220 }
3221
3222 if (parent_is_synchronized || sub->synchronized)
3223 weston_subsurface_synchronized_commit(sub);
3224}
3225
Pekka Paalanen8274d902014-08-06 19:36:51 +03003226static int
3227subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3228{
3229 return snprintf(buf, len, "sub-surface");
3230}
3231
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003232static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003233subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003234{
3235 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003236 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003237
Jason Ekstranda7af7042013-10-12 22:38:11 -05003238 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003239 weston_view_set_position(view,
3240 view->geometry.x + dx,
3241 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003242
3243 /* No need to check parent mappedness, because if parent is not
3244 * mapped, parent is not in a visible layer, so this sub-surface
3245 * will not be drawn either.
3246 */
3247 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003248 struct weston_output *output;
3249
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003250 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003251 * because that would call it also for the parent surface,
3252 * which might not be mapped yet. That would lead to
3253 * inconsistent state, where the window could never be
3254 * mapped.
3255 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003256 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003257 * weston_surface_is_mapped() return true, so that when the
3258 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003259 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003260 */
3261 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003262 output = container_of(compositor->output_list.next,
3263 struct weston_output, link);
3264
3265 surface->output = output;
3266 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003267 }
3268}
3269
3270static struct weston_subsurface *
3271weston_surface_to_subsurface(struct weston_surface *surface)
3272{
3273 if (surface->configure == subsurface_configure)
3274 return surface->configure_private;
3275
3276 return NULL;
3277}
3278
Pekka Paalanen01388e22013-04-25 13:57:44 +03003279WL_EXPORT struct weston_surface *
3280weston_surface_get_main_surface(struct weston_surface *surface)
3281{
3282 struct weston_subsurface *sub;
3283
3284 while (surface && (sub = weston_surface_to_subsurface(surface)))
3285 surface = sub->parent;
3286
3287 return surface;
3288}
3289
Pekka Paalanen50b67472014-10-01 15:02:41 +03003290WL_EXPORT int
3291weston_surface_set_role(struct weston_surface *surface,
3292 const char *role_name,
3293 struct wl_resource *error_resource,
3294 uint32_t error_code)
3295{
3296 assert(role_name);
3297
3298 if (surface->role_name == NULL ||
3299 surface->role_name == role_name ||
3300 strcmp(surface->role_name, role_name) == 0) {
3301 surface->role_name = role_name;
3302
3303 return 0;
3304 }
3305
3306 wl_resource_post_error(error_resource, error_code,
3307 "Cannot assign role %s to wl_surface@%d,"
3308 " already has role %s\n",
3309 role_name,
3310 wl_resource_get_id(surface->resource),
3311 surface->role_name);
3312 return -1;
3313}
3314
Pekka Paalanen8274d902014-08-06 19:36:51 +03003315WL_EXPORT void
3316weston_surface_set_label_func(struct weston_surface *surface,
3317 int (*desc)(struct weston_surface *,
3318 char *, size_t))
3319{
3320 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003321 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003322}
3323
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003324/** Get the size of surface contents
3325 *
3326 * \param surface The surface to query.
3327 * \param width Returns the width of raw contents.
3328 * \param height Returns the height of raw contents.
3329 *
3330 * Retrieves the raw surface content size in pixels for the given surface.
3331 * This is the whole content size in buffer pixels. If the surface
3332 * has no content or the renderer does not implement this feature,
3333 * zeroes are returned.
3334 *
3335 * This function is used to determine the buffer size needed for
3336 * a weston_surface_copy_content() call.
3337 */
3338WL_EXPORT void
3339weston_surface_get_content_size(struct weston_surface *surface,
3340 int *width, int *height)
3341{
3342 struct weston_renderer *rer = surface->compositor->renderer;
3343
3344 if (!rer->surface_get_content_size) {
3345 *width = 0;
3346 *height = 0;
3347 return;
3348 }
3349
3350 rer->surface_get_content_size(surface, width, height);
3351}
3352
3353/** Copy surface contents to system memory.
3354 *
3355 * \param surface The surface to copy from.
3356 * \param target Pointer to the target memory buffer.
3357 * \param size Size of the target buffer in bytes.
3358 * \param src_x X location on contents to copy from.
3359 * \param src_y Y location on contents to copy from.
3360 * \param width Width in pixels of the area to copy.
3361 * \param height Height in pixels of the area to copy.
3362 * \return 0 for success, -1 for failure.
3363 *
3364 * Surface contents are maintained by the renderer. They can be in a
3365 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3366 * else.
3367 *
3368 * Surface contents are copied into memory pointed to by target,
3369 * which has size bytes of space available. The target memory
3370 * may be larger than needed, but being smaller returns an error.
3371 * The extra bytes in target may or may not be written; their content is
3372 * unspecified. Size must be large enough to hold the image.
3373 *
3374 * The image in the target memory will be arranged in rows from
3375 * top to bottom, and pixels on a row from left to right. The pixel
3376 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3377 * width * 4.
3378 *
3379 * Parameters src_x and src_y define the upper-left corner in buffer
3380 * coordinates (pixels) to copy from. Parameters width and height
3381 * define the size of the area to copy in pixels.
3382 *
3383 * The rectangle defined by src_x, src_y, width, height must fit in
3384 * the surface contents. Otherwise an error is returned.
3385 *
3386 * Use surface_get_data_size to determine the content size; the
3387 * needed target buffer size and rectangle limits.
3388 *
3389 * CURRENT IMPLEMENTATION RESTRICTIONS:
3390 * - the machine must be little-endian due to Pixman formats.
3391 *
3392 * NOTE: Pixman formats are premultiplied.
3393 */
3394WL_EXPORT int
3395weston_surface_copy_content(struct weston_surface *surface,
3396 void *target, size_t size,
3397 int src_x, int src_y,
3398 int width, int height)
3399{
3400 struct weston_renderer *rer = surface->compositor->renderer;
3401 int cw, ch;
3402 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3403
3404 if (!rer->surface_copy_content)
3405 return -1;
3406
3407 weston_surface_get_content_size(surface, &cw, &ch);
3408
3409 if (src_x < 0 || src_y < 0)
3410 return -1;
3411
3412 if (width <= 0 || height <= 0)
3413 return -1;
3414
3415 if (src_x + width > cw || src_y + height > ch)
3416 return -1;
3417
3418 if (width * bytespp * height > size)
3419 return -1;
3420
3421 return rer->surface_copy_content(surface, target, size,
3422 src_x, src_y, width, height);
3423}
3424
Pekka Paalanene67858b2013-04-25 13:57:42 +03003425static void
3426subsurface_set_position(struct wl_client *client,
3427 struct wl_resource *resource, int32_t x, int32_t y)
3428{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003429 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003430
3431 if (!sub)
3432 return;
3433
3434 sub->position.x = x;
3435 sub->position.y = y;
3436 sub->position.set = 1;
3437}
3438
3439static struct weston_subsurface *
3440subsurface_from_surface(struct weston_surface *surface)
3441{
3442 struct weston_subsurface *sub;
3443
3444 sub = weston_surface_to_subsurface(surface);
3445 if (sub)
3446 return sub;
3447
3448 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3449 if (sub->surface == surface)
3450 return sub;
3451
3452 return NULL;
3453}
3454
3455static struct weston_subsurface *
3456subsurface_sibling_check(struct weston_subsurface *sub,
3457 struct weston_surface *surface,
3458 const char *request)
3459{
3460 struct weston_subsurface *sibling;
3461
3462 sibling = subsurface_from_surface(surface);
3463
3464 if (!sibling) {
3465 wl_resource_post_error(sub->resource,
3466 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3467 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003468 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003469 return NULL;
3470 }
3471
3472 if (sibling->parent != sub->parent) {
3473 wl_resource_post_error(sub->resource,
3474 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3475 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003476 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003477 return NULL;
3478 }
3479
3480 return sibling;
3481}
3482
3483static void
3484subsurface_place_above(struct wl_client *client,
3485 struct wl_resource *resource,
3486 struct wl_resource *sibling_resource)
3487{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003488 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003489 struct weston_surface *surface =
3490 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003491 struct weston_subsurface *sibling;
3492
3493 if (!sub)
3494 return;
3495
3496 sibling = subsurface_sibling_check(sub, surface, "place_above");
3497 if (!sibling)
3498 return;
3499
3500 wl_list_remove(&sub->parent_link_pending);
3501 wl_list_insert(sibling->parent_link_pending.prev,
3502 &sub->parent_link_pending);
3503}
3504
3505static void
3506subsurface_place_below(struct wl_client *client,
3507 struct wl_resource *resource,
3508 struct wl_resource *sibling_resource)
3509{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003510 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003511 struct weston_surface *surface =
3512 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003513 struct weston_subsurface *sibling;
3514
3515 if (!sub)
3516 return;
3517
3518 sibling = subsurface_sibling_check(sub, surface, "place_below");
3519 if (!sibling)
3520 return;
3521
3522 wl_list_remove(&sub->parent_link_pending);
3523 wl_list_insert(&sibling->parent_link_pending,
3524 &sub->parent_link_pending);
3525}
3526
3527static void
3528subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3529{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003530 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003531
3532 if (sub)
3533 sub->synchronized = 1;
3534}
3535
3536static void
3537subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3538{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003539 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003540
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003541 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003542 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003543
3544 /* If sub became effectively desynchronized, flush. */
3545 if (!weston_subsurface_is_synchronized(sub))
3546 weston_subsurface_synchronized_commit(sub);
3547 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003548}
3549
3550static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003551weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3552{
3553 wl_list_remove(&sub->parent_link);
3554 wl_list_remove(&sub->parent_link_pending);
3555 wl_list_remove(&sub->parent_destroy_listener.link);
3556 sub->parent = NULL;
3557}
3558
3559static void
3560weston_subsurface_destroy(struct weston_subsurface *sub);
3561
3562static void
3563subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3564{
3565 struct weston_subsurface *sub =
3566 container_of(listener, struct weston_subsurface,
3567 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003568 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003569
3570 /* The protocol object (wl_resource) is left inert. */
3571 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003572 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003573
3574 weston_subsurface_destroy(sub);
3575}
3576
3577static void
3578subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3579{
3580 struct weston_subsurface *sub =
3581 container_of(listener, struct weston_subsurface,
3582 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003583 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003584 assert(sub->surface != sub->parent);
3585
3586 if (weston_surface_is_mapped(sub->surface))
3587 weston_surface_unmap(sub->surface);
3588
3589 weston_subsurface_unlink_parent(sub);
3590}
3591
3592static void
3593subsurface_resource_destroy(struct wl_resource *resource)
3594{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003595 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003596
3597 if (sub)
3598 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003599}
3600
3601static void
3602subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3603{
3604 wl_resource_destroy(resource);
3605}
3606
3607static void
3608weston_subsurface_link_parent(struct weston_subsurface *sub,
3609 struct weston_surface *parent)
3610{
3611 sub->parent = parent;
3612 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003613 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003614 &sub->parent_destroy_listener);
3615
3616 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3617 wl_list_insert(&parent->subsurface_list_pending,
3618 &sub->parent_link_pending);
3619}
3620
3621static void
3622weston_subsurface_link_surface(struct weston_subsurface *sub,
3623 struct weston_surface *surface)
3624{
3625 sub->surface = surface;
3626 sub->surface_destroy_listener.notify =
3627 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003628 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003629 &sub->surface_destroy_listener);
3630}
3631
3632static void
3633weston_subsurface_destroy(struct weston_subsurface *sub)
3634{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003635 struct weston_view *view, *next;
3636
Pekka Paalanene67858b2013-04-25 13:57:42 +03003637 assert(sub->surface);
3638
3639 if (sub->resource) {
3640 assert(weston_surface_to_subsurface(sub->surface) == sub);
3641 assert(sub->parent_destroy_listener.notify ==
3642 subsurface_handle_parent_destroy);
3643
George Kiagiadakised04d382014-06-13 18:10:26 +02003644 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3645 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003646 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003647 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003648
Pekka Paalanene67858b2013-04-25 13:57:42 +03003649 if (sub->parent)
3650 weston_subsurface_unlink_parent(sub);
3651
Jason Ekstrand7b982072014-05-20 14:33:03 -05003652 weston_surface_state_fini(&sub->cached);
3653 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003654
3655 sub->surface->configure = NULL;
3656 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003657 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003658 } else {
3659 /* the dummy weston_subsurface for the parent itself */
3660 assert(sub->parent_destroy_listener.notify == NULL);
3661 wl_list_remove(&sub->parent_link);
3662 wl_list_remove(&sub->parent_link_pending);
3663 }
3664
3665 wl_list_remove(&sub->surface_destroy_listener.link);
3666 free(sub);
3667}
3668
3669static const struct wl_subsurface_interface subsurface_implementation = {
3670 subsurface_destroy,
3671 subsurface_set_position,
3672 subsurface_place_above,
3673 subsurface_place_below,
3674 subsurface_set_sync,
3675 subsurface_set_desync
3676};
3677
3678static struct weston_subsurface *
3679weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3680 struct weston_surface *parent)
3681{
3682 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003683 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003684
Bryce Harringtonde16d892014-11-20 22:21:57 -08003685 sub = zalloc(sizeof *sub);
3686 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003687 return NULL;
3688
Jason Ekstranda7af7042013-10-12 22:38:11 -05003689 wl_list_init(&sub->unused_views);
3690
Jason Ekstranda85118c2013-06-27 20:17:02 -05003691 sub->resource =
3692 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003693 if (!sub->resource) {
3694 free(sub);
3695 return NULL;
3696 }
3697
Jason Ekstranda85118c2013-06-27 20:17:02 -05003698 wl_resource_set_implementation(sub->resource,
3699 &subsurface_implementation,
3700 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003701 weston_subsurface_link_surface(sub, surface);
3702 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003703 weston_surface_state_init(&sub->cached);
3704 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003705 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003706
3707 return sub;
3708}
3709
3710/* Create a dummy subsurface for having the parent itself in its
3711 * sub-surface lists. Makes stacking order manipulation easy.
3712 */
3713static struct weston_subsurface *
3714weston_subsurface_create_for_parent(struct weston_surface *parent)
3715{
3716 struct weston_subsurface *sub;
3717
Bryce Harringtonde16d892014-11-20 22:21:57 -08003718 sub = zalloc(sizeof *sub);
3719 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003720 return NULL;
3721
3722 weston_subsurface_link_surface(sub, parent);
3723 sub->parent = parent;
3724 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3725 wl_list_insert(&parent->subsurface_list_pending,
3726 &sub->parent_link_pending);
3727
3728 return sub;
3729}
3730
3731static void
3732subcompositor_get_subsurface(struct wl_client *client,
3733 struct wl_resource *resource,
3734 uint32_t id,
3735 struct wl_resource *surface_resource,
3736 struct wl_resource *parent_resource)
3737{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003738 struct weston_surface *surface =
3739 wl_resource_get_user_data(surface_resource);
3740 struct weston_surface *parent =
3741 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003742 struct weston_subsurface *sub;
3743 static const char where[] = "get_subsurface: wl_subsurface@";
3744
3745 if (surface == parent) {
3746 wl_resource_post_error(resource,
3747 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3748 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003749 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003750 return;
3751 }
3752
3753 if (weston_surface_to_subsurface(surface)) {
3754 wl_resource_post_error(resource,
3755 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3756 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003757 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003758 return;
3759 }
3760
Pekka Paalanen50b67472014-10-01 15:02:41 +03003761 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3762 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003763 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003764
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003765 if (weston_surface_get_main_surface(parent) == surface) {
3766 wl_resource_post_error(resource,
3767 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3768 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003769 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003770 return;
3771 }
3772
Pekka Paalanene67858b2013-04-25 13:57:42 +03003773 /* make sure the parent is in its own list */
3774 if (wl_list_empty(&parent->subsurface_list)) {
3775 if (!weston_subsurface_create_for_parent(parent)) {
3776 wl_resource_post_no_memory(resource);
3777 return;
3778 }
3779 }
3780
3781 sub = weston_subsurface_create(id, surface, parent);
3782 if (!sub) {
3783 wl_resource_post_no_memory(resource);
3784 return;
3785 }
3786
3787 surface->configure = subsurface_configure;
3788 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003789 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003790}
3791
3792static void
3793subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3794{
3795 wl_resource_destroy(resource);
3796}
3797
3798static const struct wl_subcompositor_interface subcompositor_interface = {
3799 subcompositor_destroy,
3800 subcompositor_get_subsurface
3801};
3802
3803static void
3804bind_subcompositor(struct wl_client *client,
3805 void *data, uint32_t version, uint32_t id)
3806{
3807 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003808 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003809
Jason Ekstranda85118c2013-06-27 20:17:02 -05003810 resource =
3811 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003812 if (resource == NULL) {
3813 wl_client_post_no_memory(client);
3814 return;
3815 }
3816 wl_resource_set_implementation(resource, &subcompositor_interface,
3817 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003818}
3819
3820static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003821weston_compositor_dpms(struct weston_compositor *compositor,
3822 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003823{
3824 struct weston_output *output;
3825
3826 wl_list_for_each(output, &compositor->output_list, link)
3827 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003828 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003829}
3830
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003831WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003832weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003833{
Neil Roberts8b62e202013-09-30 13:14:47 +01003834 uint32_t old_state = compositor->state;
3835
3836 /* The state needs to be changed before emitting the wake
3837 * signal because that may try to schedule a repaint which
3838 * will not work if the compositor is still sleeping */
3839 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3840
3841 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003842 case WESTON_COMPOSITOR_SLEEPING:
3843 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3844 /* fall through */
3845 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003846 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003847 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003848 /* fall through */
3849 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003850 wl_event_source_timer_update(compositor->idle_source,
3851 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003852 }
3853}
3854
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003855WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003856weston_compositor_offscreen(struct weston_compositor *compositor)
3857{
3858 switch (compositor->state) {
3859 case WESTON_COMPOSITOR_OFFSCREEN:
3860 return;
3861 case WESTON_COMPOSITOR_SLEEPING:
3862 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3863 /* fall through */
3864 default:
3865 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3866 wl_event_source_timer_update(compositor->idle_source, 0);
3867 }
3868}
3869
3870WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003871weston_compositor_sleep(struct weston_compositor *compositor)
3872{
3873 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3874 return;
3875
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003876 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003877 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3878 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3879}
3880
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003881static int
3882idle_handler(void *data)
3883{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003884 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003885
3886 if (compositor->idle_inhibit)
3887 return 1;
3888
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003889 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003890 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003891
3892 return 1;
3893}
3894
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003895WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003896weston_plane_init(struct weston_plane *plane,
3897 struct weston_compositor *ec,
3898 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003899{
3900 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003901 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003902 plane->x = x;
3903 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003904 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003905
3906 /* Init the link so that the call to wl_list_remove() when releasing
3907 * the plane without ever stacking doesn't lead to a crash */
3908 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003909}
3910
3911WL_EXPORT void
3912weston_plane_release(struct weston_plane *plane)
3913{
Xiong Zhang97116532013-10-23 13:58:31 +08003914 struct weston_view *view;
3915
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003916 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003917 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003918
Xiong Zhang97116532013-10-23 13:58:31 +08003919 wl_list_for_each(view, &plane->compositor->view_list, link) {
3920 if (view->plane == plane)
3921 view->plane = NULL;
3922 }
3923
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003924 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003925}
3926
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003927WL_EXPORT void
3928weston_compositor_stack_plane(struct weston_compositor *ec,
3929 struct weston_plane *plane,
3930 struct weston_plane *above)
3931{
3932 if (above)
3933 wl_list_insert(above->link.prev, &plane->link);
3934 else
3935 wl_list_insert(&ec->plane_list, &plane->link);
3936}
3937
Casey Dahlin9074db52012-04-19 22:50:09 -04003938static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003939{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003940 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003941}
3942
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003943static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003944bind_output(struct wl_client *client,
3945 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003946{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003947 struct weston_output *output = data;
3948 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003949 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003950
Jason Ekstranda85118c2013-06-27 20:17:02 -05003951 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06003952 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003953 if (resource == NULL) {
3954 wl_client_post_no_memory(client);
3955 return;
3956 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003957
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003958 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003959 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003960
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003961 wl_output_send_geometry(resource,
3962 output->x,
3963 output->y,
3964 output->mm_width,
3965 output->mm_height,
3966 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003967 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003968 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003969 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003970 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003971 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003972
3973 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003974 wl_output_send_mode(resource,
3975 mode->flags,
3976 mode->width,
3977 mode->height,
3978 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003979 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003980
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003981 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003982 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003983}
3984
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003985/* Move other outputs when one is removed so the space remains contiguos. */
3986static void
3987weston_compositor_remove_output(struct weston_compositor *compositor,
3988 struct weston_output *remove_output)
3989{
3990 struct weston_output *output;
3991 int offset = 0;
3992
3993 wl_list_for_each(output, &compositor->output_list, link) {
3994 if (output == remove_output) {
3995 offset = output->width;
3996 continue;
3997 }
3998
3999 if (offset > 0) {
4000 weston_output_move(output,
4001 output->x - offset, output->y);
4002 output->dirty = 1;
4003 }
4004 }
4005}
4006
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004007WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004008weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004009{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004010 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004011 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004012
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02004013 output->destroying = 1;
4014
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004015 wl_list_for_each(view, &output->compositor->view_list, link) {
4016 if (view->output_mask & (1 << output->id))
4017 weston_view_assign_output(view);
4018 }
4019
Pekka Paalanen0513a952014-05-21 16:17:27 +03004020 wl_event_source_remove(output->repaint_timer);
4021
Pekka Paalanen133e4392014-09-23 22:08:46 -04004022 weston_presentation_feedback_discard_list(&output->feedback_list);
4023
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004024 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004025 wl_list_remove(&output->link);
4026
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004027 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004028 wl_signal_emit(&output->destroy_signal, output);
4029
Richard Hughesafe690c2013-05-02 10:10:04 +01004030 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004031 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004032 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04004033 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004034
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004035 wl_resource_for_each(resource, &output->resource_list) {
4036 wl_resource_set_destructor(resource, NULL);
4037 }
4038
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004039 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004040}
4041
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004042WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004043weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004044{
Scott Moreau850ca422012-05-21 15:21:25 -06004045 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004046
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004047 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004048 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004049
Scott Moreauccbf29d2012-02-22 14:21:41 -07004050 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004051 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004052 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004053 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004054 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004055 weston_matrix_scale(&output->matrix, magnification,
4056 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004057 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004058
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004059 switch (output->transform) {
4060 case WL_OUTPUT_TRANSFORM_FLIPPED:
4061 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4062 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4063 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4064 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4065 weston_matrix_scale(&output->matrix, -1, 1, 1);
4066 break;
4067 }
4068
4069 switch (output->transform) {
4070 default:
4071 case WL_OUTPUT_TRANSFORM_NORMAL:
4072 case WL_OUTPUT_TRANSFORM_FLIPPED:
4073 break;
4074 case WL_OUTPUT_TRANSFORM_90:
4075 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4076 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4077 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4078 break;
4079 case WL_OUTPUT_TRANSFORM_180:
4080 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4081 weston_matrix_translate(&output->matrix,
4082 -output->width, -output->height, 0);
4083 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4084 break;
4085 case WL_OUTPUT_TRANSFORM_270:
4086 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4087 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4088 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4089 break;
4090 }
4091
4092 if (output->current_scale != 1)
4093 weston_matrix_scale(&output->matrix,
4094 output->current_scale,
4095 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004096
Scott Moreauccbf29d2012-02-22 14:21:41 -07004097 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004098
4099 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004100}
4101
Scott Moreau1bad5db2012-08-18 01:04:05 -06004102static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004103weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004104{
4105 output->transform = transform;
4106
4107 switch (transform) {
4108 case WL_OUTPUT_TRANSFORM_90:
4109 case WL_OUTPUT_TRANSFORM_270:
4110 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4111 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4112 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02004113 output->width = output->current_mode->height;
4114 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004115 break;
4116 case WL_OUTPUT_TRANSFORM_NORMAL:
4117 case WL_OUTPUT_TRANSFORM_180:
4118 case WL_OUTPUT_TRANSFORM_FLIPPED:
4119 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02004120 output->width = output->current_mode->width;
4121 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004122 break;
4123 default:
4124 break;
4125 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06004126
Hardening57388e42013-09-18 23:56:36 +02004127 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02004128 output->width /= scale;
4129 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004130}
4131
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004132static void
4133weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004134{
4135 output->x = x;
4136 output->y = y;
4137
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004138 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004139 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004140 output->width,
4141 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004142}
4143
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004144WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004145weston_output_move(struct weston_output *output, int x, int y)
4146{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004147 struct wl_resource *resource;
4148
4149 output->move_x = x - output->x;
4150 output->move_y = y - output->y;
4151
4152 if (output->move_x == 0 && output->move_y == 0)
4153 return;
4154
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004155 weston_output_init_geometry(output, x, y);
4156
4157 output->dirty = 1;
4158
4159 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004160 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004161
4162 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004163 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004164 wl_output_send_geometry(resource,
4165 output->x,
4166 output->y,
4167 output->mm_width,
4168 output->mm_height,
4169 output->subpixel,
4170 output->make,
4171 output->model,
4172 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004173
4174 if (wl_resource_get_version(resource) >= 2)
4175 wl_output_send_done(resource);
4176 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004177}
4178
4179WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004180weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004181 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004182 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004183{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004184 struct wl_event_loop *loop;
4185
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004186 output->compositor = c;
4187 output->x = x;
4188 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004189 output->mm_width = mm_width;
4190 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004191 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004192 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004193
Alexander Larsson0b135062013-05-28 16:23:36 +02004194 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004195 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004196
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004197 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004198 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004199
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004200 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004201 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004202 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004203 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004204 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004205 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004206
Pekka Paalanen0513a952014-05-21 16:17:27 +03004207 loop = wl_display_get_event_loop(c->wl_display);
4208 output->repaint_timer = wl_event_loop_add_timer(loop,
4209 output_repaint_timer_handler, output);
4210
Casey Dahlin58ba1372012-04-19 22:50:08 -04004211 output->id = ffs(~output->compositor->output_id_pool) - 1;
4212 output->compositor->output_id_pool |= 1 << output->id;
4213
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004214 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004215 wl_global_create(c->wl_display, &wl_output_interface, 2,
4216 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004217}
4218
4219/** Adds an output to the compositor's output list and
4220 * send the compositor's output_created signal.
4221 *
4222 * \param compositor The compositor instance.
4223 * \param output The output to be added.
4224 */
4225WL_EXPORT void
4226weston_compositor_add_output(struct weston_compositor *compositor,
4227 struct weston_output *output)
4228{
4229 wl_list_insert(compositor->output_list.prev, &output->link);
4230 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004231}
4232
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004233WL_EXPORT void
4234weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004235 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004236 wl_fixed_t *x, wl_fixed_t *y)
4237{
Derek Foreman0f679412014-10-02 13:41:17 -05004238 struct weston_vector p = { {
4239 wl_fixed_to_double(device_x),
4240 wl_fixed_to_double(device_y),
4241 0.0,
4242 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004243
Derek Foreman67a18b92015-03-24 11:36:14 -05004244 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004245
Derek Foreman0f679412014-10-02 13:41:17 -05004246 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4247 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004248}
4249
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004250static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004251destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004252{
Jonny Lamb74130762013-11-26 18:19:46 +01004253 struct weston_surface *surface =
4254 wl_resource_get_user_data(resource);
4255
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004256 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004257 surface->pending.buffer_viewport.buffer.src_width =
4258 wl_fixed_from_int(-1);
4259 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004260 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004261}
4262
4263static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004264viewport_destroy(struct wl_client *client,
4265 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004266{
4267 wl_resource_destroy(resource);
4268}
4269
4270static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004271viewport_set(struct wl_client *client,
4272 struct wl_resource *resource,
4273 wl_fixed_t src_x,
4274 wl_fixed_t src_y,
4275 wl_fixed_t src_width,
4276 wl_fixed_t src_height,
4277 int32_t dst_width,
4278 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004279{
Jonny Lamb74130762013-11-26 18:19:46 +01004280 struct weston_surface *surface =
4281 wl_resource_get_user_data(resource);
4282
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004283 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004284
Jonny Lamb8ae35902013-11-26 18:19:45 +01004285 if (wl_fixed_to_double(src_width) < 0 ||
4286 wl_fixed_to_double(src_height) < 0) {
4287 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004288 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004289 "source dimensions must be non-negative (%fx%f)",
4290 wl_fixed_to_double(src_width),
4291 wl_fixed_to_double(src_height));
4292 return;
4293 }
4294
4295 if (dst_width <= 0 || dst_height <= 0) {
4296 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004297 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004298 "destination dimensions must be positive (%dx%d)",
4299 dst_width, dst_height);
4300 return;
4301 }
4302
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004303 surface->pending.buffer_viewport.buffer.src_x = src_x;
4304 surface->pending.buffer_viewport.buffer.src_y = src_y;
4305 surface->pending.buffer_viewport.buffer.src_width = src_width;
4306 surface->pending.buffer_viewport.buffer.src_height = src_height;
4307 surface->pending.buffer_viewport.surface.width = dst_width;
4308 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004309 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004310}
4311
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004312static void
4313viewport_set_source(struct wl_client *client,
4314 struct wl_resource *resource,
4315 wl_fixed_t src_x,
4316 wl_fixed_t src_y,
4317 wl_fixed_t src_width,
4318 wl_fixed_t src_height)
4319{
4320 struct weston_surface *surface =
4321 wl_resource_get_user_data(resource);
4322
4323 assert(surface->viewport_resource != NULL);
4324
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004325 if (src_width == wl_fixed_from_int(-1) &&
4326 src_height == wl_fixed_from_int(-1)) {
4327 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004328 surface->pending.buffer_viewport.buffer.src_width =
4329 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004330 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004331 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004332 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004333
4334 if (src_width <= 0 || src_height <= 0) {
4335 wl_resource_post_error(resource,
4336 WL_VIEWPORT_ERROR_BAD_VALUE,
4337 "source size must be positive (%fx%f)",
4338 wl_fixed_to_double(src_width),
4339 wl_fixed_to_double(src_height));
4340 return;
4341 }
4342
4343 surface->pending.buffer_viewport.buffer.src_x = src_x;
4344 surface->pending.buffer_viewport.buffer.src_y = src_y;
4345 surface->pending.buffer_viewport.buffer.src_width = src_width;
4346 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004347 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004348}
4349
4350static void
4351viewport_set_destination(struct wl_client *client,
4352 struct wl_resource *resource,
4353 int32_t dst_width,
4354 int32_t dst_height)
4355{
4356 struct weston_surface *surface =
4357 wl_resource_get_user_data(resource);
4358
4359 assert(surface->viewport_resource != NULL);
4360
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004361 if (dst_width == -1 && dst_height == -1) {
4362 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004363 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004364 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004365 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004366 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004367
4368 if (dst_width <= 0 || dst_height <= 0) {
4369 wl_resource_post_error(resource,
4370 WL_VIEWPORT_ERROR_BAD_VALUE,
4371 "destination size must be positive (%dx%d)",
4372 dst_width, dst_height);
4373 return;
4374 }
4375
4376 surface->pending.buffer_viewport.surface.width = dst_width;
4377 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004378 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004379}
4380
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004381static const struct wl_viewport_interface viewport_interface = {
4382 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004383 viewport_set,
4384 viewport_set_source,
4385 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004386};
4387
4388static void
4389scaler_destroy(struct wl_client *client,
4390 struct wl_resource *resource)
4391{
4392 wl_resource_destroy(resource);
4393}
4394
4395static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004396scaler_get_viewport(struct wl_client *client,
4397 struct wl_resource *scaler,
4398 uint32_t id,
4399 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004400{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004401 int version = wl_resource_get_version(scaler);
4402 struct weston_surface *surface =
4403 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004404 struct wl_resource *resource;
4405
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004406 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004407 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004408 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4409 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004410 return;
4411 }
4412
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004413 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004414 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004415 if (resource == NULL) {
4416 wl_client_post_no_memory(client);
4417 return;
4418 }
4419
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004420 wl_resource_set_implementation(resource, &viewport_interface,
4421 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004422
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004423 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004424}
4425
4426static const struct wl_scaler_interface scaler_interface = {
4427 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004428 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004429};
4430
4431static void
4432bind_scaler(struct wl_client *client,
4433 void *data, uint32_t version, uint32_t id)
4434{
4435 struct wl_resource *resource;
4436
4437 resource = wl_resource_create(client, &wl_scaler_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004438 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004439 if (resource == NULL) {
4440 wl_client_post_no_memory(client);
4441 return;
4442 }
4443
4444 wl_resource_set_implementation(resource, &scaler_interface,
4445 NULL, NULL);
4446}
4447
4448static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004449destroy_presentation_feedback(struct wl_resource *feedback_resource)
4450{
4451 struct weston_presentation_feedback *feedback;
4452
4453 feedback = wl_resource_get_user_data(feedback_resource);
4454
4455 wl_list_remove(&feedback->link);
4456 free(feedback);
4457}
4458
4459static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004460presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4461{
4462 wl_resource_destroy(resource);
4463}
4464
4465static void
4466presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004467 struct wl_resource *presentation_resource,
4468 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004469 uint32_t callback)
4470{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004471 struct weston_surface *surface;
4472 struct weston_presentation_feedback *feedback;
4473
4474 surface = wl_resource_get_user_data(surface_resource);
4475
Bryce Harringtonde16d892014-11-20 22:21:57 -08004476 feedback = zalloc(sizeof *feedback);
4477 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004478 goto err_calloc;
4479
4480 feedback->resource = wl_resource_create(client,
4481 &presentation_feedback_interface,
4482 1, callback);
4483 if (!feedback->resource)
4484 goto err_create;
4485
4486 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4487 destroy_presentation_feedback);
4488
4489 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4490
4491 return;
4492
4493err_create:
4494 free(feedback);
4495
4496err_calloc:
4497 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004498}
4499
4500static const struct presentation_interface presentation_implementation = {
4501 presentation_destroy,
4502 presentation_feedback
4503};
4504
4505static void
4506bind_presentation(struct wl_client *client,
4507 void *data, uint32_t version, uint32_t id)
4508{
4509 struct weston_compositor *compositor = data;
4510 struct wl_resource *resource;
4511
4512 resource = wl_resource_create(client, &presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004513 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004514 if (resource == NULL) {
4515 wl_client_post_no_memory(client);
4516 return;
4517 }
4518
4519 wl_resource_set_implementation(resource, &presentation_implementation,
4520 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004521 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004522}
4523
4524static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004525compositor_bind(struct wl_client *client,
4526 void *data, uint32_t version, uint32_t id)
4527{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004528 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004529 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004530
Jason Ekstranda85118c2013-06-27 20:17:02 -05004531 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004532 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004533 if (resource == NULL) {
4534 wl_client_post_no_memory(client);
4535 return;
4536 }
4537
4538 wl_resource_set_implementation(resource, &compositor_interface,
4539 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004540}
4541
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004542WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004543weston_environment_get_fd(const char *env)
4544{
4545 char *e, *end;
4546 int fd, flags;
4547
4548 e = getenv(env);
4549 if (!e)
4550 return -1;
4551 fd = strtol(e, &end, 0);
4552 if (*end != '\0')
4553 return -1;
4554
4555 flags = fcntl(fd, F_GETFD);
4556 if (flags == -1)
4557 return -1;
4558
4559 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4560 unsetenv(env);
4561
4562 return fd;
4563}
4564
Pekka Paalanenb5026542014-11-12 15:09:24 +02004565static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004566timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004567 uint32_t key, void *data)
4568{
4569 struct weston_compositor *compositor = data;
4570
4571 if (weston_timeline_enabled_)
4572 weston_timeline_close();
4573 else
4574 weston_timeline_open(compositor);
4575}
4576
Giulio Camuffo459137b2014-10-11 23:56:24 +03004577/** Create the compositor.
4578 *
4579 * This functions creates and initializes a compositor instance.
4580 *
4581 * \param display The Wayland display to be used.
4582 * \param user_data A pointer to an object that can later be retrieved
4583 * using the \ref weston_compositor_get_user_data function.
4584 * \return The compositor instance on success or NULL on failure.
4585 */
4586WL_EXPORT struct weston_compositor *
4587weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004588{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004589 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004590 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004591
Giulio Camuffo459137b2014-10-11 23:56:24 +03004592 ec = zalloc(sizeof *ec);
4593 if (!ec)
4594 return NULL;
4595
4596 ec->wl_display = display;
4597 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004598 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004599 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004600 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004601 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004602 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004603 wl_signal_init(&ec->idle_signal);
4604 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004605 wl_signal_init(&ec->show_input_panel_signal);
4606 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004607 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004608 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004609 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004610 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004611 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004612 wl_signal_init(&ec->session_signal);
4613 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004614
Casey Dahlin58ba1372012-04-19 22:50:08 -04004615 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004616 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004617
Derek Foreman152254b2015-11-26 14:17:48 -06004618 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004619 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004620 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004621
Giulio Camuffo954f1832014-10-11 18:27:30 +03004622 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004623 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004624 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004625
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004626 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004627 ec, bind_scaler))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004628 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004629
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004630 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4631 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004632 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004633
Jason Ekstranda7af7042013-10-12 22:38:11 -05004634 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004635 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004636 wl_list_init(&ec->layer_list);
4637 wl_list_init(&ec->seat_list);
4638 wl_list_init(&ec->output_list);
4639 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004640 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004641 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004642 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004643 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004644 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004645
Xiong Zhang97116532013-10-23 13:58:31 +08004646 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004647 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004648
Giulio Camuffo459137b2014-10-11 23:56:24 +03004649 wl_data_device_manager_init(ec->wl_display);
4650
4651 wl_display_init_shm(ec->wl_display);
4652
4653 loop = wl_display_get_event_loop(ec->wl_display);
4654 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004655
4656 ec->input_loop = wl_event_loop_create();
4657
4658 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4659 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4660
4661 weston_compositor_add_debug_binding(ec, KEY_T,
4662 timeline_key_binding_handler, ec);
4663
Giulio Camuffo459137b2014-10-11 23:56:24 +03004664 return ec;
4665
4666fail:
4667 free(ec);
4668 return NULL;
4669}
4670
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004671WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004672weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004673{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004674 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004675
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004676 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004677 if (ec->input_loop_source)
4678 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004679
Matt Roper361d2ad2011-08-29 13:52:23 -07004680 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004681 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004682 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004683
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004684 if (ec->renderer)
4685 ec->renderer->destroy(ec);
4686
Daniel Stone325fc2d2012-05-30 16:31:58 +01004687 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004688 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004689 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004690 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004691 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004692 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004693
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004694 weston_plane_release(&ec->primary_plane);
4695
Jonas Ådahlc97af922012-03-28 22:36:09 +02004696 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07004697}
4698
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004699WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004700weston_compositor_exit_with_code(struct weston_compositor *compositor,
4701 int exit_code)
4702{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004703 if (compositor->exit_code == EXIT_SUCCESS)
4704 compositor->exit_code = exit_code;
4705
Giulio Camuffo459137b2014-10-11 23:56:24 +03004706 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004707}
4708
4709WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004710weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4711 const struct weston_pointer_grab_interface *interface)
4712{
4713 struct weston_seat *seat;
4714
4715 ec->default_pointer_grab = interface;
4716 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004717 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4718
4719 if (pointer)
4720 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004721 }
4722}
4723
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004724WL_EXPORT int
4725weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4726 clockid_t clk_id)
4727{
4728 struct timespec ts;
4729
4730 if (clock_gettime(clk_id, &ts) < 0)
4731 return -1;
4732
4733 compositor->presentation_clock = clk_id;
4734
4735 return 0;
4736}
4737
4738/*
4739 * For choosing the software clock, when the display hardware or API
4740 * does not expose a compatible presentation timestamp.
4741 */
4742WL_EXPORT int
4743weston_compositor_set_presentation_clock_software(
4744 struct weston_compositor *compositor)
4745{
4746 /* In order of preference */
4747 static const clockid_t clocks[] = {
4748 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4749 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4750 CLOCK_MONOTONIC, /* no jumps, may crawl */
4751 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4752 CLOCK_REALTIME /* may jump and crawl */
4753 };
4754 unsigned i;
4755
4756 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4757 if (weston_compositor_set_presentation_clock(compositor,
4758 clocks[i]) == 0)
4759 return 0;
4760
4761 weston_log("Error: no suitable presentation clock available.\n");
4762
4763 return -1;
4764}
4765
Pekka Paalanen662f3842015-03-18 12:17:26 +02004766/** Read the current time from the Presentation clock
4767 *
4768 * \param compositor
4769 * \param ts[out] The current time.
4770 *
4771 * \note Reading the current time in user space is always imprecise to some
4772 * degree.
4773 *
4774 * This function is never meant to fail. If reading the clock does fail,
4775 * an error message is logged and a zero time is returned. Callers are not
4776 * supposed to detect or react to failures.
4777 */
4778WL_EXPORT void
4779weston_compositor_read_presentation_clock(
4780 const struct weston_compositor *compositor,
4781 struct timespec *ts)
4782{
4783 static bool warned;
4784 int ret;
4785
4786 ret = clock_gettime(compositor->presentation_clock, ts);
4787 if (ret < 0) {
4788 ts->tv_sec = 0;
4789 ts->tv_nsec = 0;
4790
4791 if (!warned)
4792 weston_log("Error: failure to read "
4793 "the presentation clock %#x: '%m' (%d)\n",
4794 compositor->presentation_clock, errno);
4795 warned = true;
4796 }
4797}
4798
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004799/** Import dmabuf buffer into current renderer
4800 *
4801 * \param compositor
4802 * \param buffer the dmabuf buffer to import
4803 * \return true on usable buffers, false otherwise
4804 *
4805 * This function tests that the linux_dmabuf_buffer is usable
4806 * for the current renderer. Returns false on unusable buffers. Usually
4807 * usability is tested by importing the dmabufs for composition.
4808 *
4809 * This hook is also used for detecting if the renderer supports
4810 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4811 * supported.
4812 * */
4813WL_EXPORT bool
4814weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4815 struct linux_dmabuf_buffer *buffer)
4816{
4817 struct weston_renderer *renderer;
4818
4819 renderer = compositor->renderer;
4820
4821 if (renderer->import_dmabuf == NULL)
4822 return false;
4823
4824 return renderer->import_dmabuf(compositor, buffer);
4825}
4826
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004827WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004828weston_version(int *major, int *minor, int *micro)
4829{
4830 *major = WESTON_VERSION_MAJOR;
4831 *minor = WESTON_VERSION_MINOR;
4832 *micro = WESTON_VERSION_MICRO;
4833}
4834
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004835WL_EXPORT void *
4836weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004837{
Derek Foreman3f86e502015-06-08 11:46:54 -05004838 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004839 char path[PATH_MAX];
4840 void *module, *init;
4841
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004842 if (name == NULL)
4843 return NULL;
4844
Derek Foreman3f86e502015-06-08 11:46:54 -05004845 if (name[0] != '/') {
4846 if (builddir)
4847 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4848 else
4849 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
4850 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004851 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004852 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004853
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004854 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4855 if (module) {
4856 weston_log("Module '%s' already loaded\n", path);
4857 dlclose(module);
4858 return NULL;
4859 }
4860
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004861 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004862 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004863 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004864 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004865 return NULL;
4866 }
4867
4868 init = dlsym(module, entrypoint);
4869 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004870 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004871 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004872 return NULL;
4873 }
4874
4875 return init;
4876}
4877
Giulio Camuffo459137b2014-10-11 23:56:24 +03004878
4879/** Destroys the compositor.
4880 *
4881 * This function cleans up the compositor state and destroys it.
4882 *
4883 * \param compositor The compositor to be destroyed.
4884 */
4885WL_EXPORT void
4886weston_compositor_destroy(struct weston_compositor *compositor)
4887{
4888 /* prevent further rendering while shutting down */
4889 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4890
4891 wl_signal_emit(&compositor->destroy_signal, compositor);
4892
4893 weston_compositor_xkb_destroy(compositor);
4894
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004895 if (compositor->backend)
4896 compositor->backend->destroy(compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004897 free(compositor);
4898}
4899
4900/** Instruct the compositor to exit.
4901 *
4902 * This functions does not directly destroy the compositor object, it merely
4903 * command it to start the tear down process. It is not guaranteed that the
4904 * tear down will happen immediately.
4905 *
4906 * \param compositor The compositor to tear down.
4907 */
4908WL_EXPORT void
4909weston_compositor_exit(struct weston_compositor *compositor)
4910{
4911 compositor->exit(compositor);
4912}
4913
4914/** Return the user data stored in the compositor.
4915 *
4916 * This function returns the user data pointer set with user_data parameter
4917 * to the \ref weston_compositor_create function.
4918 */
4919WL_EXPORT void *
4920weston_compositor_get_user_data(struct weston_compositor *compositor)
4921{
4922 return compositor->user_data;
4923}