blob: 9e1531049a4401cd5824c571a4869e7c35baf660 [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
924WL_EXPORT pixman_box32_t
925weston_surface_to_buffer_rect(struct weston_surface *surface,
926 pixman_box32_t rect)
927{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200928 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100929 float xf, yf;
930
931 /* first transform box coordinates if the scaler is set */
932 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
933 rect.x1 = floorf(xf);
934 rect.y1 = floorf(yf);
935
936 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600937 rect.x2 = ceilf(xf);
938 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100939
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500940 return weston_transformed_rect(surface->width_from_buffer,
941 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200942 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200943 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200944}
945
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200946/** Transform a region from surface coordinates to buffer coordinates
947 *
948 * \param surface The surface to fetch wl_viewport and buffer transformation
949 * from.
950 * \param surface_region[in] The region in surface coordinates.
951 * \param buffer_region[out] The region converted to buffer coordinates.
952 *
953 * Buffer_region must be init'd, but will be completely overwritten.
954 *
955 * Viewport and buffer transformations can only do translation, scaling,
956 * and rotations in 90-degree steps. Therefore the only loss in the
957 * conversion is coordinate flooring (rounding).
958 */
959WL_EXPORT void
960weston_surface_to_buffer_region(struct weston_surface *surface,
961 pixman_region32_t *surface_region,
962 pixman_region32_t *buffer_region)
963{
964 pixman_box32_t *src_rects, *dest_rects;
965 int nrects, i;
966
967 src_rects = pixman_region32_rectangles(surface_region, &nrects);
968 dest_rects = malloc(nrects * sizeof(*dest_rects));
969 if (!dest_rects)
970 return;
971
972 for (i = 0; i < nrects; i++) {
973 dest_rects[i] = weston_surface_to_buffer_rect(surface,
974 src_rects[i]);
975 }
976
977 pixman_region32_fini(buffer_region);
978 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
979 free(dest_rects);
980}
981
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200982WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500983weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400984 struct weston_plane *plane)
985{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500986 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400987 return;
988
Jason Ekstranda7af7042013-10-12 22:38:11 -0500989 weston_view_damage_below(view);
990 view->plane = plane;
991 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400992}
993
Pekka Paalanen51723d52015-02-17 13:10:01 +0200994/** Inflict damage on the plane where the view is visible.
995 *
996 * \param view The view that causes the damage.
997 *
998 * If the view is currently on a plane (including the primary plane),
999 * take the view's boundingbox, subtract all the opaque views that cover it,
1000 * and add the remaining region as damage to the plane. This corresponds
1001 * to the damage inflicted to the plane if this view disappeared.
1002 *
1003 * A repaint is scheduled for this view.
1004 *
1005 * The region of all opaque views covering this view is stored in
1006 * weston_view::clip and updated by view_accumulate_damage() during
1007 * weston_output_repaint(). Specifically, that region matches the
1008 * scenegraph as it was last painted.
1009 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001010WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001011weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001012{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001013 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001014
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001015 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001016 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001017 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001018 if (view->plane)
1019 pixman_region32_union(&view->plane->damage,
1020 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001021 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001022 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001023}
1024
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001025static void
1026weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1027{
1028 uint32_t different = es->output_mask ^ mask;
1029 uint32_t entered = mask & different;
1030 uint32_t left = es->output_mask & different;
1031 struct weston_output *output;
1032 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001033 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001034
1035 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001036 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001037 return;
1038 if (different == 0)
1039 return;
1040
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001041 client = wl_resource_get_client(es->resource);
1042
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001043 wl_list_for_each(output, &es->compositor->output_list, link) {
1044 if (1 << output->id & different)
1045 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001046 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001047 client);
1048 if (resource == NULL)
1049 continue;
1050 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001051 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001052 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001053 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001054 }
1055}
1056
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001057
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001058static void
1059weston_surface_assign_output(struct weston_surface *es)
1060{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001061 struct weston_output *new_output;
1062 struct weston_view *view;
1063 pixman_region32_t region;
1064 uint32_t max, area, mask;
1065 pixman_box32_t *e;
1066
1067 new_output = NULL;
1068 max = 0;
1069 mask = 0;
1070 pixman_region32_init(&region);
1071 wl_list_for_each(view, &es->views, surface_link) {
1072 if (!view->output)
1073 continue;
1074
1075 pixman_region32_intersect(&region, &view->transform.boundingbox,
1076 &view->output->region);
1077
1078 e = pixman_region32_extents(&region);
1079 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1080
1081 mask |= view->output_mask;
1082
1083 if (area >= max) {
1084 new_output = view->output;
1085 max = area;
1086 }
1087 }
1088 pixman_region32_fini(&region);
1089
1090 es->output = new_output;
1091 weston_surface_update_output_mask(es, mask);
1092}
1093
1094static void
1095weston_view_assign_output(struct weston_view *ev)
1096{
1097 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001098 struct weston_output *output, *new_output;
1099 pixman_region32_t region;
1100 uint32_t max, area, mask;
1101 pixman_box32_t *e;
1102
1103 new_output = NULL;
1104 max = 0;
1105 mask = 0;
1106 pixman_region32_init(&region);
1107 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001108 if (output->destroying)
1109 continue;
1110
Jason Ekstranda7af7042013-10-12 22:38:11 -05001111 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001112 &output->region);
1113
1114 e = pixman_region32_extents(&region);
1115 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1116
1117 if (area > 0)
1118 mask |= 1 << output->id;
1119
1120 if (area >= max) {
1121 new_output = output;
1122 max = area;
1123 }
1124 }
1125 pixman_region32_fini(&region);
1126
Jason Ekstranda7af7042013-10-12 22:38:11 -05001127 ev->output = new_output;
1128 ev->output_mask = mask;
1129
1130 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001131}
1132
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001133static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001134weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1135 int from_x, int from_y, int *to_x, int *to_y)
1136{
1137 float x, y;
1138
1139 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1140 weston_view_from_global_float(to, x, y, &x, &y);
1141
1142 *to_x = round(x);
1143 *to_y = round(y);
1144}
1145
1146static void
1147weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1148{
1149 pixman_box32_t *a;
1150 pixman_box32_t b;
1151
1152 a = pixman_region32_extents(&from->geometry.scissor);
1153
1154 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1155 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1156
1157 pixman_region32_fini(&to->geometry.scissor);
1158 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1159}
1160
1161static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001162view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001164{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001165 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1166 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001167 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001168 { inbox->x1, inbox->y1 },
1169 { inbox->x1, inbox->y2 },
1170 { inbox->x2, inbox->y1 },
1171 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001172 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001173 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001174 int i;
1175
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001176 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001177 /* avoid rounding empty bbox to 1x1 */
1178 pixman_region32_init(bbox);
1179 return;
1180 }
1181
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001182 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001183 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001185 if (x < min_x)
1186 min_x = x;
1187 if (x > max_x)
1188 max_x = x;
1189 if (y < min_y)
1190 min_y = y;
1191 if (y > max_y)
1192 max_y = y;
1193 }
1194
Pekka Paalanen219b9822012-02-08 15:38:37 +02001195 int_x = floorf(min_x);
1196 int_y = floorf(min_y);
1197 pixman_region32_init_rect(bbox, int_x, int_y,
1198 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001199}
1200
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001201static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001203{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001205
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001206 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 view->geometry.x = roundf(view->geometry.x);
1208 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001209
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001210 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001211 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1212 view->transform.position.matrix.d[12] = view->geometry.x;
1213 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001214
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001216
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217 view->transform.inverse = view->transform.position.matrix;
1218 view->transform.inverse.d[12] = -view->geometry.x;
1219 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001220
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001222 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001223 view->surface->width,
1224 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001225 if (view->geometry.scissor_enabled)
1226 pixman_region32_intersect(&view->transform.boundingbox,
1227 &view->transform.boundingbox,
1228 &view->geometry.scissor);
1229
1230 pixman_region32_translate(&view->transform.boundingbox,
1231 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001232
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 if (view->alpha == 1.0) {
1234 pixman_region32_copy(&view->transform.opaque,
1235 &view->surface->opaque);
1236 pixman_region32_translate(&view->transform.opaque,
1237 view->geometry.x,
1238 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001239 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001240}
1241
1242static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001244{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 struct weston_view *parent = view->geometry.parent;
1246 struct weston_matrix *matrix = &view->transform.matrix;
1247 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001248 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001249 pixman_region32_t surfregion;
1250 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001251
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001253
1254 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1256 view->transform.position.matrix.d[12] = view->geometry.x;
1257 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001258
1259 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001261 weston_matrix_multiply(matrix, &tform->matrix);
1262
Pekka Paalanen483243f2013-03-08 14:56:50 +02001263 if (parent)
1264 weston_matrix_multiply(matrix, &parent->transform.matrix);
1265
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001266 if (weston_matrix_invert(inverse, matrix) < 0) {
1267 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 weston_log("error: weston_view %p"
1269 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001270 return -1;
1271 }
1272
Pekka Paalanen380adf52015-02-16 14:39:11 +02001273 pixman_region32_init_rect(&surfregion, 0, 0,
1274 view->surface->width, view->surface->height);
1275 if (view->geometry.scissor_enabled)
1276 pixman_region32_intersect(&surfregion, &surfregion,
1277 &view->geometry.scissor);
1278 surfbox = pixman_region32_extents(&surfregion);
1279
1280 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1281 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001282
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001283 return 0;
1284}
1285
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001286static struct weston_layer *
1287get_view_layer(struct weston_view *view)
1288{
1289 if (view->parent_view)
1290 return get_view_layer(view->parent_view);
1291 return view->layer_link.layer;
1292}
1293
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001294WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001296{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001298 struct weston_layer *layer;
1299 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001300
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001302 return;
1303
Pekka Paalanen483243f2013-03-08 14:56:50 +02001304 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001306
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001308
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001310
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 pixman_region32_fini(&view->transform.boundingbox);
1312 pixman_region32_fini(&view->transform.opaque);
1313 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001314
Pekka Paalanencd403622012-01-25 13:37:39 +02001315 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 if (view->geometry.transformation_list.next ==
1317 &view->transform.position.link &&
1318 view->geometry.transformation_list.prev ==
1319 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001320 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001322 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001323 if (weston_view_update_transform_enable(view) < 0)
1324 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001325 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001326
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001327 layer = get_view_layer(view);
1328 if (layer) {
1329 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001330 pixman_region32_intersect(&view->transform.boundingbox,
1331 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001332 pixman_region32_intersect(&view->transform.opaque,
1333 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001334 pixman_region32_fini(&mask);
1335 }
1336
Pekka Paalanen380adf52015-02-16 14:39:11 +02001337 if (parent) {
1338 if (parent->geometry.scissor_enabled) {
1339 view->geometry.scissor_enabled = true;
1340 weston_view_transfer_scissor(parent, view);
1341 } else {
1342 view->geometry.scissor_enabled = false;
1343 }
1344 }
1345
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001347
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001349
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350 wl_signal_emit(&view->surface->compositor->transform_signal,
1351 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001352}
1353
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001354WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001356{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001358
1359 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 * The invariant: if view->geometry.dirty, then all views
1361 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001362 * Corollary: if not parent->geometry.dirty, then all ancestors
1363 * are not dirty.
1364 */
1365
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001367 return;
1368
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001370
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001372 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001374}
1375
1376WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377weston_view_to_global_fixed(struct weston_view *view,
1378 wl_fixed_t vx, wl_fixed_t vy,
1379 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001380{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001381 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001382
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383 weston_view_to_global_float(view,
1384 wl_fixed_to_double(vx),
1385 wl_fixed_to_double(vy),
1386 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001387 *x = wl_fixed_from_double(xf);
1388 *y = wl_fixed_from_double(yf);
1389}
1390
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001391WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392weston_view_from_global_float(struct weston_view *view,
1393 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001394{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001396 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1397
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001399
1400 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001401 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001403 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404 *vx = 0;
1405 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001406 return;
1407 }
1408
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409 *vx = v.f[0] / v.f[3];
1410 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001411 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001412 *vx = x - view->geometry.x;
1413 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001414 }
1415}
1416
1417WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418weston_view_from_global_fixed(struct weston_view *view,
1419 wl_fixed_t x, wl_fixed_t y,
1420 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001421{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001423
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 weston_view_from_global_float(view,
1425 wl_fixed_to_double(x),
1426 wl_fixed_to_double(y),
1427 &vxf, &vyf);
1428 *vx = wl_fixed_from_double(vxf);
1429 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001430}
1431
1432WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433weston_view_from_global(struct weston_view *view,
1434 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001435{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001437
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1439 *vx = floorf(vxf);
1440 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001441}
1442
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001443WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001444weston_surface_schedule_repaint(struct weston_surface *surface)
1445{
1446 struct weston_output *output;
1447
1448 wl_list_for_each(output, &surface->compositor->output_list, link)
1449 if (surface->output_mask & (1 << output->id))
1450 weston_output_schedule_repaint(output);
1451}
1452
1453WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454weston_view_schedule_repaint(struct weston_view *view)
1455{
1456 struct weston_output *output;
1457
1458 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1459 if (view->output_mask & (1 << output->id))
1460 weston_output_schedule_repaint(output);
1461}
1462
Pekka Paalanene508ce62015-02-19 13:59:55 +02001463/**
1464 * XXX: This function does it the wrong way.
1465 * surface->damage is the damage from the client, and causes
1466 * surface_flush_damage() to copy pixels. No window management action can
1467 * cause damage to the client-provided content, warranting re-upload!
1468 *
1469 * Instead of surface->damage, this function should record the damage
1470 * with all the views for this surface to avoid extraneous texture
1471 * uploads.
1472 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001474weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001475{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001476 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477 0, 0, surface->width,
1478 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001479
Kristian Høgsberg98238702012-08-03 16:29:12 -04001480 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001481}
1482
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001483WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001484weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001485{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001486 if (view->geometry.x == x && view->geometry.y == y)
1487 return;
1488
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 view->geometry.x = x;
1490 view->geometry.y = y;
1491 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001492}
1493
Pekka Paalanen483243f2013-03-08 14:56:50 +02001494static void
1495transform_parent_handle_parent_destroy(struct wl_listener *listener,
1496 void *data)
1497{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 struct weston_view *view =
1499 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001500 geometry.parent_destroy_listener);
1501
Jason Ekstranda7af7042013-10-12 22:38:11 -05001502 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001503}
1504
1505WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001506weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001507 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001508{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001509 if (view->geometry.parent) {
1510 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1511 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001512
1513 if (!parent)
1514 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001515 }
1516
Jason Ekstranda7af7042013-10-12 22:38:11 -05001517 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001518
Jason Ekstranda7af7042013-10-12 22:38:11 -05001519 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001520 transform_parent_handle_parent_destroy;
1521 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001522 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001524 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001525 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001526 }
1527
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 weston_view_geometry_dirty(view);
1529}
1530
Pekka Paalanen380adf52015-02-16 14:39:11 +02001531/** Set a clip mask rectangle on a view
1532 *
1533 * \param view The view to set the clip mask on.
1534 * \param x Top-left corner X coordinate of the clip rectangle.
1535 * \param y Top-left corner Y coordinate of the clip rectangle.
1536 * \param width Width of the clip rectangle, non-negative.
1537 * \param height Height of the clip rectangle, non-negative.
1538 *
1539 * A shell may set a clip mask rectangle on a view. Everything outside
1540 * the rectangle is cut away for input and output purposes: it is
1541 * not drawn and cannot be hit by hit-test based input like pointer
1542 * motion or touch-downs. Everything inside the rectangle will behave
1543 * normally. Clients are unaware of clipping.
1544 *
1545 * The rectangle is set in the surface local coordinates. Setting a clip
1546 * mask rectangle does not affect the view position, the view is positioned
1547 * as it would be without a clip. The clip also does not change
1548 * weston_surface::width,height.
1549 *
1550 * The clip mask rectangle is part of transformation inheritance
1551 * (weston_view_set_transform_parent()). A clip set in the root of the
1552 * transformation inheritance tree will affect all views in the tree.
1553 * A clip can be set only on the root view. Attempting to set a clip
1554 * on view that has a transformation parent will fail. Assigning a parent
1555 * to a view that has a clip set will cause the clip to be forgotten.
1556 *
1557 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1558 * on the additional transformations in the child views. These transformations
1559 * may not rotate the coordinate axes, i.e., only translation and scaling
1560 * are allowed. Violating this restriction causes the clipping to malfunction.
1561 * Furthermore, using scaling may cause rounding errors in child clipping.
1562 *
1563 * The clip mask rectangle is not automatically adjusted based on
1564 * wl_surface.attach dx and dy arguments.
1565 *
1566 * A clip mask rectangle can be set only if the compositor capability
1567 * WESTON_CAP_VIEW_CLIP_MASK is present.
1568 *
1569 * This function sets the clip mask rectangle and schedules a repaint for
1570 * the view.
1571 */
1572WL_EXPORT void
1573weston_view_set_mask(struct weston_view *view,
1574 int x, int y, int width, int height)
1575{
1576 struct weston_compositor *compositor = view->surface->compositor;
1577
1578 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1579 weston_log("%s not allowed without capability!\n", __func__);
1580 return;
1581 }
1582
1583 if (view->geometry.parent) {
1584 weston_log("view %p has a parent, clip forbidden!\n", view);
1585 return;
1586 }
1587
1588 if (width < 0 || height < 0) {
1589 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1590 x, y, width, height);
1591 return;
1592 }
1593
1594 pixman_region32_fini(&view->geometry.scissor);
1595 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1596 view->geometry.scissor_enabled = true;
1597 weston_view_geometry_dirty(view);
1598 weston_view_schedule_repaint(view);
1599}
1600
1601/** Remove the clip mask from a view
1602 *
1603 * \param view The view to remove the clip mask from.
1604 *
1605 * Removed the clip mask rectangle and schedules a repaint.
1606 *
1607 * \sa weston_view_set_mask
1608 */
1609WL_EXPORT void
1610weston_view_set_mask_infinite(struct weston_view *view)
1611{
1612 view->geometry.scissor_enabled = false;
1613 weston_view_geometry_dirty(view);
1614 weston_view_schedule_repaint(view);
1615}
1616
Derek Foreman280e7dd2014-10-03 13:13:42 -05001617WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001618weston_view_is_mapped(struct weston_view *view)
1619{
1620 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001621 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001622 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001623 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001624}
1625
Derek Foreman280e7dd2014-10-03 13:13:42 -05001626WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001627weston_surface_is_mapped(struct weston_surface *surface)
1628{
1629 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001630 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001631 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001632 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001633}
1634
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001635static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001636surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001637{
1638 struct weston_view *view;
1639
1640 if (surface->width == width && surface->height == height)
1641 return;
1642
1643 surface->width = width;
1644 surface->height = height;
1645
1646 wl_list_for_each(view, &surface->views, surface_link)
1647 weston_view_geometry_dirty(view);
1648}
1649
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001650WL_EXPORT void
1651weston_surface_set_size(struct weston_surface *surface,
1652 int32_t width, int32_t height)
1653{
1654 assert(!surface->resource);
1655 surface_set_size(surface, width, height);
1656}
1657
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001658static int
1659fixed_round_up_to_int(wl_fixed_t f)
1660{
1661 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1662}
1663
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001664static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001665weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001666{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001667 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001668 int32_t width, height;
1669
1670 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001671 surface->width_from_buffer = 0;
1672 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001673 return;
1674 }
1675
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001676 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001677 case WL_OUTPUT_TRANSFORM_90:
1678 case WL_OUTPUT_TRANSFORM_270:
1679 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1680 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001681 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1682 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001683 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001684 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001685 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1686 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001687 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001688 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001689
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001690 surface->width_from_buffer = width;
1691 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001692}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001693
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001694static void
1695weston_surface_update_size(struct weston_surface *surface)
1696{
1697 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1698 int32_t width, height;
1699
1700 width = surface->width_from_buffer;
1701 height = surface->height_from_buffer;
1702
1703 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001704 surface_set_size(surface,
1705 vp->surface.width, vp->surface.height);
1706 return;
1707 }
1708
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001709 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001710 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1711 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1712
1713 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001714 return;
1715 }
1716
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001717 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001718}
1719
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001720WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001721weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001722{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001723 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001724
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001725 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001726
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001727 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001728}
1729
Jason Ekstranda7af7042013-10-12 22:38:11 -05001730WL_EXPORT struct weston_view *
1731weston_compositor_pick_view(struct weston_compositor *compositor,
1732 wl_fixed_t x, wl_fixed_t y,
1733 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001734{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001736 wl_fixed_t view_x, view_y;
1737 int view_ix, view_iy;
1738 int ix = wl_fixed_to_int(x);
1739 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001740
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001742 if (!pixman_region32_contains_point(
1743 &view->transform.boundingbox, ix, iy, NULL))
1744 continue;
1745
1746 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1747 view_ix = wl_fixed_to_int(view_x);
1748 view_iy = wl_fixed_to_int(view_y);
1749
1750 if (!pixman_region32_contains_point(&view->surface->input,
1751 view_ix, view_iy, NULL))
1752 continue;
1753
Pekka Paalanen380adf52015-02-16 14:39:11 +02001754 if (view->geometry.scissor_enabled &&
1755 !pixman_region32_contains_point(&view->geometry.scissor,
1756 view_ix, view_iy, NULL))
1757 continue;
1758
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001759 *vx = view_x;
1760 *vy = view_y;
1761 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001762 }
1763
Derek Foremanf9318d12015-05-11 15:40:11 -05001764 *vx = wl_fixed_from_int(-1000000);
1765 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001766 return NULL;
1767}
1768
1769static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001770weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001771{
Daniel Stone37816df2012-05-16 18:45:18 +01001772 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001773
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001774 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001775 return;
1776
Daniel Stone37816df2012-05-16 18:45:18 +01001777 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001778 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001779}
1780
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001781WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001782weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001783{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001784 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001785
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786 if (!weston_view_is_mapped(view))
1787 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001788
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789 weston_view_damage_below(view);
1790 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001791 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001792 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 wl_list_remove(&view->link);
1794 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001795 view->output_mask = 0;
1796 weston_surface_assign_output(view->surface);
1797
1798 if (weston_surface_is_mapped(view->surface))
1799 return;
1800
1801 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001802 struct weston_touch *touch = weston_seat_get_touch(seat);
1803 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1804 struct weston_keyboard *keyboard =
1805 weston_seat_get_keyboard(seat);
1806
1807 if (keyboard && keyboard->focus == view->surface)
1808 weston_keyboard_set_focus(keyboard, NULL);
1809 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001810 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001811 if (touch && touch->focus == view)
1812 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001813 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001814}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001815
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816WL_EXPORT void
1817weston_surface_unmap(struct weston_surface *surface)
1818{
1819 struct weston_view *view;
1820
1821 wl_list_for_each(view, &surface->views, surface_link)
1822 weston_view_unmap(view);
1823 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001824}
1825
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001826static void
1827weston_surface_reset_pending_buffer(struct weston_surface *surface)
1828{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001829 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001830 surface->pending.sx = 0;
1831 surface->pending.sy = 0;
1832 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001833 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001834}
1835
Jason Ekstranda7af7042013-10-12 22:38:11 -05001836WL_EXPORT void
1837weston_view_destroy(struct weston_view *view)
1838{
1839 wl_signal_emit(&view->destroy_signal, view);
1840
1841 assert(wl_list_empty(&view->geometry.child_list));
1842
1843 if (weston_view_is_mapped(view)) {
1844 weston_view_unmap(view);
1845 weston_compositor_build_view_list(view->surface->compositor);
1846 }
1847
1848 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001849 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001850
1851 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001852 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001853 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001854 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001855
1856 weston_view_set_transform_parent(view, NULL);
1857
Jason Ekstranda7af7042013-10-12 22:38:11 -05001858 wl_list_remove(&view->surface_link);
1859
1860 free(view);
1861}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001862
1863WL_EXPORT void
1864weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001865{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001866 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001867 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001868
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001869 if (--surface->ref_count > 0)
1870 return;
1871
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001872 assert(surface->resource == NULL);
1873
Pekka Paalanenca790762015-04-17 14:23:38 +03001874 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001875
Pekka Paalanene67858b2013-04-25 13:57:42 +03001876 assert(wl_list_empty(&surface->subsurface_list_pending));
1877 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001878
Jason Ekstranda7af7042013-10-12 22:38:11 -05001879 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1880 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001881
Jason Ekstrand7b982072014-05-20 14:33:03 -05001882 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001883
Pekka Paalanende685b82012-12-04 15:58:12 +02001884 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001885
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001886 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001887 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001888 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001889
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001890 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001891 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001892
Pekka Paalanen133e4392014-09-23 22:08:46 -04001893 weston_presentation_feedback_discard_list(&surface->feedback_list);
1894
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001895 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001896}
1897
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001898static void
1899destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001900{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001901 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001902
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001903 assert(surface);
1904
Giulio Camuffo0d379742013-11-15 22:06:15 +01001905 /* Set the resource to NULL, since we don't want to leave a
1906 * dangling pointer if the surface was refcounted and survives
1907 * the weston_surface_destroy() call. */
1908 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001909 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001910}
1911
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001912static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001913weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1914{
1915 struct weston_buffer *buffer =
1916 container_of(listener, struct weston_buffer, destroy_listener);
1917
1918 wl_signal_emit(&buffer->destroy_signal, buffer);
1919 free(buffer);
1920}
1921
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001922WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001923weston_buffer_from_resource(struct wl_resource *resource)
1924{
1925 struct weston_buffer *buffer;
1926 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001927
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001928 listener = wl_resource_get_destroy_listener(resource,
1929 weston_buffer_destroy_handler);
1930
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001931 if (listener)
1932 return container_of(listener, struct weston_buffer,
1933 destroy_listener);
1934
1935 buffer = zalloc(sizeof *buffer);
1936 if (buffer == NULL)
1937 return NULL;
1938
1939 buffer->resource = resource;
1940 wl_signal_init(&buffer->destroy_signal);
1941 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001942 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001943 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001944
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001945 return buffer;
1946}
1947
1948static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001949weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1950 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001951{
Pekka Paalanende685b82012-12-04 15:58:12 +02001952 struct weston_buffer_reference *ref =
1953 container_of(listener, struct weston_buffer_reference,
1954 destroy_listener);
1955
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001956 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001957 ref->buffer = NULL;
1958}
1959
1960WL_EXPORT void
1961weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001962 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001963{
1964 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001965 ref->buffer->busy_count--;
1966 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001967 assert(wl_resource_get_client(ref->buffer->resource));
1968 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001969 WL_BUFFER_RELEASE);
1970 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001971 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001972 }
1973
Pekka Paalanende685b82012-12-04 15:58:12 +02001974 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001975 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001976 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001977 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001978 }
1979
Pekka Paalanende685b82012-12-04 15:58:12 +02001980 ref->buffer = buffer;
1981 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1982}
1983
1984static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001985weston_surface_attach(struct weston_surface *surface,
1986 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001987{
1988 weston_buffer_reference(&surface->buffer_ref, buffer);
1989
Pekka Paalanena6421c42012-12-04 15:58:10 +02001990 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001991 if (weston_surface_is_mapped(surface))
1992 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001993 }
1994
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001995 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001996
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001997 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001998 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001999}
2000
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002001WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002002weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002003{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002004 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002005
2006 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002007 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002008}
2009
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002010WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002011weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002012{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002013 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002014
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002015 pixman_region32_union(&compositor->primary_plane.damage,
2016 &compositor->primary_plane.damage,
2017 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002018 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002019}
2020
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002021static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002022surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002023{
Pekka Paalanende685b82012-12-04 15:58:12 +02002024 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002025 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002026 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002027
Pekka Paalanenb5026542014-11-12 15:09:24 +02002028 if (weston_timeline_enabled_ &&
2029 pixman_region32_not_empty(&surface->damage))
2030 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2031 TLP_OUTPUT(surface->output), TLP_END);
2032
Jason Ekstrandef540082014-06-26 10:37:36 -07002033 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002034}
2035
2036static void
2037view_accumulate_damage(struct weston_view *view,
2038 pixman_region32_t *opaque)
2039{
2040 pixman_region32_t damage;
2041
2042 pixman_region32_init(&damage);
2043 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002044 pixman_box32_t *extents;
2045
Jason Ekstranda7af7042013-10-12 22:38:11 -05002046 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002047 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002048 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049 pixman_region32_copy(&damage, &view->surface->damage);
2050 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002051 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002052 }
2053
Pekka Paalanen380adf52015-02-16 14:39:11 +02002054 pixman_region32_intersect(&damage, &damage,
2055 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002056 pixman_region32_subtract(&damage, &damage, opaque);
2057 pixman_region32_union(&view->plane->damage,
2058 &view->plane->damage, &damage);
2059 pixman_region32_fini(&damage);
2060 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002061 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002062}
2063
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002064static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002065compositor_accumulate_damage(struct weston_compositor *ec)
2066{
2067 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002068 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002069 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002070
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002071 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002072
2073 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002074 pixman_region32_copy(&plane->clip, &clip);
2075
2076 pixman_region32_init(&opaque);
2077
Jason Ekstranda7af7042013-10-12 22:38:11 -05002078 wl_list_for_each(ev, &ec->view_list, link) {
2079 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002080 continue;
2081
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002083 }
2084
2085 pixman_region32_union(&clip, &clip, &opaque);
2086 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002087 }
2088
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002089 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002090
Jason Ekstranda7af7042013-10-12 22:38:11 -05002091 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002092 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002093
2094 wl_list_for_each(ev, &ec->view_list, link) {
2095 if (ev->surface->touched)
2096 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002097 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002098
2099 surface_flush_damage(ev->surface);
2100
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002101 /* Both the renderer and the backend have seen the buffer
2102 * by now. If renderer needs the buffer, it has its own
2103 * reference set. If the backend wants to keep the buffer
2104 * around for migrating the surface into a non-primary plane
2105 * later, keep_buffer is true. Otherwise, drop the core
2106 * reference now, and allow early buffer release. This enables
2107 * clients to use single-buffering.
2108 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109 if (!ev->surface->keep_buffer)
2110 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002111 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002112}
2113
2114static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002115surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002116{
2117 struct weston_subsurface *sub;
2118
Pekka Paalanene67858b2013-04-25 13:57:42 +03002119 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002120 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002121 continue;
2122
Jason Ekstranda7af7042013-10-12 22:38:11 -05002123 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2124 wl_list_init(&sub->surface->views);
2125
2126 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002127 }
2128}
2129
2130static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002132{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002133 struct weston_subsurface *sub;
2134 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002135
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2137 if (sub->surface == surface)
2138 continue;
2139
George Kiagiadakised04d382014-06-13 18:10:26 +02002140 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2141 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002142 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002143 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144
2145 surface_free_unused_subsurface_views(sub->surface);
2146 }
2147}
2148
2149static void
2150view_list_add_subsurface_view(struct weston_compositor *compositor,
2151 struct weston_subsurface *sub,
2152 struct weston_view *parent)
2153{
2154 struct weston_subsurface *child;
2155 struct weston_view *view = NULL, *iv;
2156
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002157 if (!weston_surface_is_mapped(sub->surface))
2158 return;
2159
Jason Ekstranda7af7042013-10-12 22:38:11 -05002160 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2161 if (iv->geometry.parent == parent) {
2162 view = iv;
2163 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002164 }
2165 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002166
2167 if (view) {
2168 /* Put it back in the surface's list of views */
2169 wl_list_remove(&view->surface_link);
2170 wl_list_insert(&sub->surface->views, &view->surface_link);
2171 } else {
2172 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002173 weston_view_set_position(view,
2174 sub->position.x,
2175 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002176 weston_view_set_transform_parent(view, parent);
2177 }
2178
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002179 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181
Pekka Paalanenb188e912013-11-19 14:03:35 +02002182 if (wl_list_empty(&sub->surface->subsurface_list)) {
2183 wl_list_insert(compositor->view_list.prev, &view->link);
2184 return;
2185 }
2186
2187 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2188 if (child->surface == sub->surface)
2189 wl_list_insert(compositor->view_list.prev, &view->link);
2190 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002192 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002193}
2194
2195static void
2196view_list_add(struct weston_compositor *compositor,
2197 struct weston_view *view)
2198{
2199 struct weston_subsurface *sub;
2200
2201 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002202
Pekka Paalanenb188e912013-11-19 14:03:35 +02002203 if (wl_list_empty(&view->surface->subsurface_list)) {
2204 wl_list_insert(compositor->view_list.prev, &view->link);
2205 return;
2206 }
2207
2208 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2209 if (sub->surface == view->surface)
2210 wl_list_insert(compositor->view_list.prev, &view->link);
2211 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002213 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002214}
2215
2216static void
2217weston_compositor_build_view_list(struct weston_compositor *compositor)
2218{
2219 struct weston_view *view;
2220 struct weston_layer *layer;
2221
2222 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002223 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002224 surface_stash_subsurface_views(view->surface);
2225
2226 wl_list_init(&compositor->view_list);
2227 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002228 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002229 view_list_add(compositor, view);
2230 }
2231 }
2232
2233 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002234 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002235 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002236}
2237
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002238static void
2239weston_output_take_feedback_list(struct weston_output *output,
2240 struct weston_surface *surface)
2241{
2242 struct weston_view *view;
2243 struct weston_presentation_feedback *feedback;
2244 uint32_t flags = 0xffffffff;
2245
2246 if (wl_list_empty(&surface->feedback_list))
2247 return;
2248
2249 /* All views must have the flag for the flag to survive. */
2250 wl_list_for_each(view, &surface->views, surface_link) {
2251 /* ignore views that are not on this output at all */
2252 if (view->output_mask & (1u << output->id))
2253 flags &= view->psf_flags;
2254 }
2255
2256 wl_list_for_each(feedback, &surface->feedback_list, link)
2257 feedback->psf_flags = flags;
2258
2259 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2260 wl_list_init(&surface->feedback_list);
2261}
2262
David Herrmann1edf44c2013-10-22 17:11:26 +02002263static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002264weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002265{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002266 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002267 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002268 struct weston_animation *animation, *next;
2269 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002270 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002271 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002272 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002273
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002274 if (output->destroying)
2275 return 0;
2276
Pekka Paalanenb5026542014-11-12 15:09:24 +02002277 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2278
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002279 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002280 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002281
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002282 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002283 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002284 } else {
2285 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002286 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002287 ev->psf_flags = 0;
2288 }
2289 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002290
Pekka Paalanene67858b2013-04-25 13:57:42 +03002291 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002292 wl_list_for_each(ev, &ec->view_list, link) {
2293 /* Note: This operation is safe to do multiple times on the
2294 * same surface.
2295 */
2296 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002297 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002298 &ev->surface->frame_callback_list);
2299 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002300
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002301 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002302 }
2303 }
2304
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002305 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002306
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002307 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002308 pixman_region32_intersect(&output_damage,
2309 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002310 pixman_region32_subtract(&output_damage,
2311 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002312
Scott Moreauccbf29d2012-02-22 14:21:41 -07002313 if (output->dirty)
2314 weston_output_update_matrix(output);
2315
David Herrmann1edf44c2013-10-22 17:11:26 +02002316 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002317
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002318 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002319
Kristian Høgsbergef044142011-06-21 15:02:12 -04002320 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002321
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002322 weston_compositor_repick(ec);
2323 wl_event_loop_dispatch(ec->input_loop, 0);
2324
Jonas Ådahldb773762012-06-13 00:01:21 +02002325 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002326 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002327 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002328 }
2329
Scott Moreaud64cf212012-06-08 19:40:54 -06002330 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002331 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002332 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002333 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002334
Pekka Paalanenb5026542014-11-12 15:09:24 +02002335 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2336
David Herrmann1edf44c2013-10-22 17:11:26 +02002337 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002338}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002339
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002340static int
2341weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002342{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002343 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002344
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002345 wl_event_loop_dispatch(compositor->input_loop, 0);
2346
2347 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002348}
2349
Pekka Paalanen82919792014-05-21 13:51:49 +03002350static void
2351weston_output_schedule_repaint_reset(struct weston_output *output)
2352{
2353 struct weston_compositor *compositor = output->compositor;
2354 struct wl_event_loop *loop;
2355 int fd;
2356
2357 output->repaint_scheduled = 0;
2358 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2359
2360 if (compositor->input_loop_source)
2361 return;
2362
2363 loop = wl_display_get_event_loop(compositor->wl_display);
2364 fd = wl_event_loop_get_fd(compositor->input_loop);
2365 compositor->input_loop_source =
2366 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2367 weston_compositor_read_input, compositor);
2368}
2369
Pekka Paalanen0513a952014-05-21 16:17:27 +03002370static int
2371output_repaint_timer_handler(void *data)
2372{
2373 struct weston_output *output = data;
2374 struct weston_compositor *compositor = output->compositor;
2375
2376 if (output->repaint_needed &&
2377 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2378 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2379 weston_output_repaint(output) == 0)
2380 return 0;
2381
2382 weston_output_schedule_repaint_reset(output);
2383
2384 return 0;
2385}
2386
Kristian Høgsbergef044142011-06-21 15:02:12 -04002387WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002388weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002389 const struct timespec *stamp,
2390 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002391{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002392 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002393 int32_t refresh_nsec;
2394 struct timespec now;
2395 struct timespec gone;
2396 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002397
Pekka Paalanenb5026542014-11-12 15:09:24 +02002398 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2399 TLP_VBLANK(stamp), TLP_END);
2400
Pekka Paalanend7894d02015-07-03 15:08:53 +03002401 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002402 weston_presentation_feedback_present_list(&output->feedback_list,
2403 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002404 output->msc,
2405 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002406
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002407 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002408
Pekka Paalanen0513a952014-05-21 16:17:27 +03002409 weston_compositor_read_presentation_clock(compositor, &now);
2410 timespec_sub(&gone, &now, stamp);
2411 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2412 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002413
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002414 if (msec < -1000 || msec > 1000) {
2415 static bool warned;
2416
2417 if (!warned)
2418 weston_log("Warning: computed repaint delay is "
2419 "insane: %d msec\n", msec);
2420 warned = true;
2421
2422 msec = 0;
2423 }
2424
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002425 /* Called from restart_repaint_loop and restart happens already after
2426 * the deadline given by repaint_msec? In that case we delay until
2427 * the deadline of the next frame, to give clients a more predictable
2428 * timing of the repaint cycle to lock on. */
2429 if (presented_flags == PRESENTATION_FEEDBACK_INVALID && msec < 0)
2430 msec += refresh_nsec / 1000000;
2431
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002432 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002433 output_repaint_timer_handler(output);
2434 else
2435 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002436}
2437
2438static void
2439idle_repaint(void *data)
2440{
2441 struct weston_output *output = data;
2442
Jonas Ådahle5a12252013-04-05 23:07:11 +02002443 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002444}
2445
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002446WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002447weston_layer_entry_insert(struct weston_layer_entry *list,
2448 struct weston_layer_entry *entry)
2449{
2450 wl_list_insert(&list->link, &entry->link);
2451 entry->layer = list->layer;
2452}
2453
2454WL_EXPORT void
2455weston_layer_entry_remove(struct weston_layer_entry *entry)
2456{
2457 wl_list_remove(&entry->link);
2458 wl_list_init(&entry->link);
2459 entry->layer = NULL;
2460}
2461
2462WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002463weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2464{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002465 wl_list_init(&layer->view_list.link);
2466 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002467 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002468 if (below != NULL)
2469 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002470}
2471
2472WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002473weston_layer_set_mask(struct weston_layer *layer,
2474 int x, int y, int width, int height)
2475{
2476 struct weston_view *view;
2477
2478 layer->mask.x1 = x;
2479 layer->mask.x2 = x + width;
2480 layer->mask.y1 = y;
2481 layer->mask.y2 = y + height;
2482
2483 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2484 weston_view_geometry_dirty(view);
2485 }
2486}
2487
2488WL_EXPORT void
2489weston_layer_set_mask_infinite(struct weston_layer *layer)
2490{
2491 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2492 UINT32_MAX, UINT32_MAX);
2493}
2494
2495WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002496weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002497{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002498 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002499 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002500
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002501 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2502 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002503 return;
2504
Pekka Paalanenb5026542014-11-12 15:09:24 +02002505 if (!output->repaint_needed)
2506 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2507
Kristian Høgsbergef044142011-06-21 15:02:12 -04002508 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002509 output->repaint_needed = 1;
2510 if (output->repaint_scheduled)
2511 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002512
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002513 wl_event_loop_add_idle(loop, idle_repaint, output);
2514 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002515 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2516
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002517
2518 if (compositor->input_loop_source) {
2519 wl_event_source_remove(compositor->input_loop_source);
2520 compositor->input_loop_source = NULL;
2521 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002522}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002523
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002524WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002525weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2526{
2527 struct weston_output *output;
2528
2529 wl_list_for_each(output, &compositor->output_list, link)
2530 weston_output_schedule_repaint(output);
2531}
2532
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002533static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002534surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002535{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002536 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002537}
2538
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002539static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002540surface_attach(struct wl_client *client,
2541 struct wl_resource *resource,
2542 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2543{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002544 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002545 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002546
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002547 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002548 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002549 if (buffer == NULL) {
2550 wl_client_post_no_memory(client);
2551 return;
2552 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002553 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002554
Pekka Paalanende685b82012-12-04 15:58:12 +02002555 /* Attach, attach, without commit in between does not send
2556 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002557 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002558
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002559 surface->pending.sx = sx;
2560 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002561 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002562}
2563
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002564static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002565surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002566 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002567 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002568{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002569 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002570
Derek Foreman57e92ed2015-11-17 14:11:35 -06002571 if (width <= 0 || height <= 0)
2572 return;
2573
Derek Foreman152254b2015-11-26 14:17:48 -06002574 pixman_region32_union_rect(&surface->pending.damage_surface,
2575 &surface->pending.damage_surface,
2576 x, y, width, height);
2577}
2578
2579static void
2580surface_damage_buffer(struct wl_client *client,
2581 struct wl_resource *resource,
2582 int32_t x, int32_t y, int32_t width, int32_t height)
2583{
2584 struct weston_surface *surface = wl_resource_get_user_data(resource);
2585
2586 if (width <= 0 || height <= 0)
2587 return;
2588
2589 pixman_region32_union_rect(&surface->pending.damage_buffer,
2590 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002591 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002592}
2593
Kristian Høgsberg33418202011-08-16 23:01:28 -04002594static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002595destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002596{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002597 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002598
2599 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002600 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002601}
2602
2603static void
2604surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002605 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002606{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002607 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002608 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002609
2610 cb = malloc(sizeof *cb);
2611 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002612 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002613 return;
2614 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002615
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002616 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2617 callback);
2618 if (cb->resource == NULL) {
2619 free(cb);
2620 wl_resource_post_no_memory(resource);
2621 return;
2622 }
2623
Jason Ekstranda85118c2013-06-27 20:17:02 -05002624 wl_resource_set_implementation(cb->resource, NULL, cb,
2625 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002626
Pekka Paalanenbc106382012-10-10 12:49:31 +03002627 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002628}
2629
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002630static void
2631surface_set_opaque_region(struct wl_client *client,
2632 struct wl_resource *resource,
2633 struct wl_resource *region_resource)
2634{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002635 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002636 struct weston_region *region;
2637
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002638 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002639 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002640 pixman_region32_copy(&surface->pending.opaque,
2641 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002642 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002643 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002644 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002645}
2646
2647static void
2648surface_set_input_region(struct wl_client *client,
2649 struct wl_resource *resource,
2650 struct wl_resource *region_resource)
2651{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002652 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002653 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002654
2655 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002656 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002657 pixman_region32_copy(&surface->pending.input,
2658 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002659 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002660 pixman_region32_fini(&surface->pending.input);
2661 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002662 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002663}
2664
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002665static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002666weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002667{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002668 struct weston_subsurface *sub;
2669
2670 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2671 parent_link_pending) {
2672 wl_list_remove(&sub->parent_link);
2673 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2674 }
2675}
2676
2677static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002678weston_surface_build_buffer_matrix(struct weston_surface *surface,
2679 struct weston_matrix *matrix)
2680{
2681 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2682 double src_width, src_height, dest_width, dest_height;
2683
2684 weston_matrix_init(matrix);
2685
2686 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2687 src_width = surface->width_from_buffer;
2688 src_height = surface->height_from_buffer;
2689 } else {
2690 src_width = wl_fixed_to_double(vp->buffer.src_width);
2691 src_height = wl_fixed_to_double(vp->buffer.src_height);
2692 }
2693
2694 if (vp->surface.width == -1) {
2695 dest_width = src_width;
2696 dest_height = src_height;
2697 } else {
2698 dest_width = vp->surface.width;
2699 dest_height = vp->surface.height;
2700 }
2701
2702 if (src_width != dest_width || src_height != dest_height)
2703 weston_matrix_scale(matrix,
2704 src_width / dest_width,
2705 src_height / dest_height, 1);
2706
2707 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2708 weston_matrix_translate(matrix,
2709 wl_fixed_to_double(vp->buffer.src_x),
2710 wl_fixed_to_double(vp->buffer.src_y),
2711 0);
2712
2713 switch (vp->buffer.transform) {
2714 case WL_OUTPUT_TRANSFORM_FLIPPED:
2715 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2716 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2717 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2718 weston_matrix_scale(matrix, -1, 1, 1);
2719 weston_matrix_translate(matrix,
2720 surface->width_from_buffer, 0, 0);
2721 break;
2722 }
2723
2724 switch (vp->buffer.transform) {
2725 default:
2726 case WL_OUTPUT_TRANSFORM_NORMAL:
2727 case WL_OUTPUT_TRANSFORM_FLIPPED:
2728 break;
2729 case WL_OUTPUT_TRANSFORM_90:
2730 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2731 weston_matrix_rotate_xy(matrix, 0, 1);
2732 weston_matrix_translate(matrix,
2733 surface->height_from_buffer, 0, 0);
2734 break;
2735 case WL_OUTPUT_TRANSFORM_180:
2736 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2737 weston_matrix_rotate_xy(matrix, -1, 0);
2738 weston_matrix_translate(matrix,
2739 surface->width_from_buffer,
2740 surface->height_from_buffer, 0);
2741 break;
2742 case WL_OUTPUT_TRANSFORM_270:
2743 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2744 weston_matrix_rotate_xy(matrix, 0, -1);
2745 weston_matrix_translate(matrix,
2746 0, surface->width_from_buffer, 0);
2747 break;
2748 }
2749
2750 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2751}
2752
Derek Foreman152254b2015-11-26 14:17:48 -06002753/* Translate pending damage in buffer co-ordinates to surface
2754 * co-ordinates and union it with a pixman_region32_t.
2755 * This should only be called after the buffer is attached.
2756 */
2757static void
2758apply_damage_buffer(pixman_region32_t *dest,
2759 struct weston_surface *surface,
2760 struct weston_surface_state *state)
2761{
2762 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2763
2764 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2765 * translated into surface co-ordinates and unioned with
2766 * any other surface damage.
2767 * None of this makes sense if there is no buffer though.
2768 */
2769 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2770 pixman_region32_t buffer_damage;
2771
2772 pixman_region32_intersect_rect(&state->damage_buffer,
2773 &state->damage_buffer,
2774 0, 0, buffer->width,
2775 buffer->height);
2776 pixman_region32_init(&buffer_damage);
2777 weston_matrix_transform_region(&buffer_damage,
2778 &surface->buffer_to_surface_matrix,
2779 &state->damage_buffer);
2780 pixman_region32_union(dest, dest, &buffer_damage);
2781 pixman_region32_fini(&buffer_damage);
2782 }
2783 /* We should clear this on commit even if there was no buffer */
2784 pixman_region32_clear(&state->damage_buffer);
2785}
2786
Jason Ekstrand1e059042014-10-16 10:55:19 -05002787static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002788weston_surface_commit_state(struct weston_surface *surface,
2789 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002790{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002792 pixman_region32_t opaque;
2793
Alexander Larsson4ea95522013-05-22 14:41:37 +02002794 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002795 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002796 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002797 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002798
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002799 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002800 if (state->newly_attached)
2801 weston_surface_attach(surface, state->buffer);
2802 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002803
Jason Ekstrand1e059042014-10-16 10:55:19 -05002804 weston_surface_build_buffer_matrix(surface,
2805 &surface->surface_to_buffer_matrix);
2806 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2807 &surface->surface_to_buffer_matrix);
2808
Jason Ekstrand7b982072014-05-20 14:33:03 -05002809 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002810 weston_surface_update_size(surface);
2811 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002812 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002813 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002814
Jason Ekstrand7b982072014-05-20 14:33:03 -05002815 state->sx = 0;
2816 state->sy = 0;
2817 state->newly_attached = 0;
2818 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002819
Derek Foreman152254b2015-11-26 14:17:48 -06002820 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002821 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002822 (pixman_region32_not_empty(&state->damage_surface) ||
2823 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002824 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002825
Pekka Paalanen8e159182012-10-10 12:49:25 +03002826 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002827 &state->damage_surface);
2828
2829 apply_damage_buffer(&surface->damage, surface, state);
2830
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002831 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002832 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002833 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002834
2835 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002836 pixman_region32_init(&opaque);
2837 pixman_region32_intersect_rect(&opaque, &state->opaque,
2838 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002839
2840 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2841 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002842 wl_list_for_each(view, &surface->views, surface_link)
2843 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002844 }
2845
2846 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002847
2848 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002849 pixman_region32_intersect_rect(&surface->input, &state->input,
2850 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002851
Pekka Paalanenbc106382012-10-10 12:49:31 +03002852 /* wl_surface.frame */
2853 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002854 &state->frame_callback_list);
2855 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002856
2857 /* XXX:
2858 * What should happen with a feedback request, if there
2859 * is no wl_buffer attached for this commit?
2860 */
2861
2862 /* presentation.feedback */
2863 wl_list_insert_list(&surface->feedback_list,
2864 &state->feedback_list);
2865 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002866}
2867
2868static void
2869weston_surface_commit(struct weston_surface *surface)
2870{
2871 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002872
Pekka Paalanene67858b2013-04-25 13:57:42 +03002873 weston_surface_commit_subsurface_order(surface);
2874
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002875 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002876}
2877
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002878static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002879weston_subsurface_commit(struct weston_subsurface *sub);
2880
2881static void
2882weston_subsurface_parent_commit(struct weston_subsurface *sub,
2883 int parent_is_synchronized);
2884
2885static void
2886surface_commit(struct wl_client *client, struct wl_resource *resource)
2887{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002888 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002889 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2890
2891 if (sub) {
2892 weston_subsurface_commit(sub);
2893 return;
2894 }
2895
2896 weston_surface_commit(surface);
2897
2898 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2899 if (sub->surface != surface)
2900 weston_subsurface_parent_commit(sub, 0);
2901 }
2902}
2903
2904static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002905surface_set_buffer_transform(struct wl_client *client,
2906 struct wl_resource *resource, int transform)
2907{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002908 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002909
Jonny Lamba55f1392014-05-30 12:07:15 +02002910 /* if wl_output.transform grows more members this will need to be updated. */
2911 if (transform < 0 ||
2912 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2913 wl_resource_post_error(resource,
2914 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2915 "buffer transform must be a valid transform "
2916 "('%d' specified)", transform);
2917 return;
2918 }
2919
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002920 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002921 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002922}
2923
Alexander Larsson4ea95522013-05-22 14:41:37 +02002924static void
2925surface_set_buffer_scale(struct wl_client *client,
2926 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002927 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002928{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002929 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002930
Jonny Lamba55f1392014-05-30 12:07:15 +02002931 if (scale < 1) {
2932 wl_resource_post_error(resource,
2933 WL_SURFACE_ERROR_INVALID_SCALE,
2934 "buffer scale must be at least one "
2935 "('%d' specified)", scale);
2936 return;
2937 }
2938
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002939 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002940 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002941}
2942
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002943static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002944 surface_destroy,
2945 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002946 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002947 surface_frame,
2948 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002949 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002950 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002951 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06002952 surface_set_buffer_scale,
2953 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002954};
2955
2956static void
2957compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002958 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002959{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002960 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002961 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002962
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002963 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002964 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002965 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002966 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002967 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002968
Jason Ekstranda85118c2013-06-27 20:17:02 -05002969 surface->resource =
2970 wl_resource_create(client, &wl_surface_interface,
2971 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002972 if (surface->resource == NULL) {
2973 weston_surface_destroy(surface);
2974 wl_resource_post_no_memory(resource);
2975 return;
2976 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002977 wl_resource_set_implementation(surface->resource, &surface_interface,
2978 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002979
2980 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002981}
2982
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002983static void
2984destroy_region(struct wl_resource *resource)
2985{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002986 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002987
2988 pixman_region32_fini(&region->region);
2989 free(region);
2990}
2991
2992static void
2993region_destroy(struct wl_client *client, struct wl_resource *resource)
2994{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002995 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002996}
2997
2998static void
2999region_add(struct wl_client *client, struct wl_resource *resource,
3000 int32_t x, int32_t y, int32_t width, int32_t height)
3001{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003002 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003003
3004 pixman_region32_union_rect(&region->region, &region->region,
3005 x, y, width, height);
3006}
3007
3008static void
3009region_subtract(struct wl_client *client, struct wl_resource *resource,
3010 int32_t x, int32_t y, int32_t width, int32_t height)
3011{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003012 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003013 pixman_region32_t rect;
3014
3015 pixman_region32_init_rect(&rect, x, y, width, height);
3016 pixman_region32_subtract(&region->region, &region->region, &rect);
3017 pixman_region32_fini(&rect);
3018}
3019
3020static const struct wl_region_interface region_interface = {
3021 region_destroy,
3022 region_add,
3023 region_subtract
3024};
3025
3026static void
3027compositor_create_region(struct wl_client *client,
3028 struct wl_resource *resource, uint32_t id)
3029{
3030 struct weston_region *region;
3031
3032 region = malloc(sizeof *region);
3033 if (region == NULL) {
3034 wl_resource_post_no_memory(resource);
3035 return;
3036 }
3037
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003038 pixman_region32_init(&region->region);
3039
Jason Ekstranda85118c2013-06-27 20:17:02 -05003040 region->resource =
3041 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003042 if (region->resource == NULL) {
3043 free(region);
3044 wl_resource_post_no_memory(resource);
3045 return;
3046 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003047 wl_resource_set_implementation(region->resource, &region_interface,
3048 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003049}
3050
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003051static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003052 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003053 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003054};
3055
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003056static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003057weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3058{
3059 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003060
Jason Ekstrand7b982072014-05-20 14:33:03 -05003061 weston_surface_commit_state(surface, &sub->cached);
3062 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003063
3064 weston_surface_commit_subsurface_order(surface);
3065
3066 weston_surface_schedule_repaint(surface);
3067
Jason Ekstrand7b982072014-05-20 14:33:03 -05003068 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003069}
3070
3071static void
3072weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3073{
3074 struct weston_surface *surface = sub->surface;
3075
3076 /*
3077 * If this commit would cause the surface to move by the
3078 * attach(dx, dy) parameters, the old damage region must be
3079 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003080 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003081 */
Derek Foreman152254b2015-11-26 14:17:48 -06003082 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003083 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003084 pixman_region32_union(&sub->cached.damage_surface,
3085 &sub->cached.damage_surface,
3086 &surface->pending.damage_surface);
3087 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003088
3089 if (surface->pending.newly_attached) {
3090 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003091 weston_surface_state_set_buffer(&sub->cached,
3092 surface->pending.buffer);
3093 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003094 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003095 weston_presentation_feedback_discard_list(
3096 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003097 }
3098 sub->cached.sx += surface->pending.sx;
3099 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003100
Derek Foreman152254b2015-11-26 14:17:48 -06003101 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3102
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003103 sub->cached.buffer_viewport.changed |=
3104 surface->pending.buffer_viewport.changed;
3105 sub->cached.buffer_viewport.buffer =
3106 surface->pending.buffer_viewport.buffer;
3107 sub->cached.buffer_viewport.surface =
3108 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003109
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003110 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003111
3112 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3113
3114 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3115
3116 wl_list_insert_list(&sub->cached.frame_callback_list,
3117 &surface->pending.frame_callback_list);
3118 wl_list_init(&surface->pending.frame_callback_list);
3119
Pekka Paalanen133e4392014-09-23 22:08:46 -04003120 wl_list_insert_list(&sub->cached.feedback_list,
3121 &surface->pending.feedback_list);
3122 wl_list_init(&surface->pending.feedback_list);
3123
Jason Ekstrand7b982072014-05-20 14:33:03 -05003124 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003125}
3126
Derek Foreman280e7dd2014-10-03 13:13:42 -05003127static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003128weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3129{
3130 while (sub) {
3131 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003132 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003133
3134 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003135 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003136
3137 sub = weston_surface_to_subsurface(sub->parent);
3138 }
3139
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003140 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003141}
3142
3143static void
3144weston_subsurface_commit(struct weston_subsurface *sub)
3145{
3146 struct weston_surface *surface = sub->surface;
3147 struct weston_subsurface *tmp;
3148
3149 /* Recursive check for effectively synchronized. */
3150 if (weston_subsurface_is_synchronized(sub)) {
3151 weston_subsurface_commit_to_cache(sub);
3152 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003153 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003154 /* flush accumulated state from cache */
3155 weston_subsurface_commit_to_cache(sub);
3156 weston_subsurface_commit_from_cache(sub);
3157 } else {
3158 weston_surface_commit(surface);
3159 }
3160
3161 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3162 if (tmp->surface != surface)
3163 weston_subsurface_parent_commit(tmp, 0);
3164 }
3165 }
3166}
3167
3168static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003169weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003170{
3171 struct weston_surface *surface = sub->surface;
3172 struct weston_subsurface *tmp;
3173
Pekka Paalanene67858b2013-04-25 13:57:42 +03003174 /* From now on, commit_from_cache the whole sub-tree, regardless of
3175 * the synchronized mode of each child. This sub-surface or some
3176 * of its ancestors were synchronized, so we are synchronized
3177 * all the way down.
3178 */
3179
Jason Ekstrand7b982072014-05-20 14:33:03 -05003180 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003181 weston_subsurface_commit_from_cache(sub);
3182
3183 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3184 if (tmp->surface != surface)
3185 weston_subsurface_parent_commit(tmp, 1);
3186 }
3187}
3188
3189static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003190weston_subsurface_parent_commit(struct weston_subsurface *sub,
3191 int parent_is_synchronized)
3192{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003193 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003194 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003195 wl_list_for_each(view, &sub->surface->views, surface_link)
3196 weston_view_set_position(view,
3197 sub->position.x,
3198 sub->position.y);
3199
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003200 sub->position.set = 0;
3201 }
3202
3203 if (parent_is_synchronized || sub->synchronized)
3204 weston_subsurface_synchronized_commit(sub);
3205}
3206
Pekka Paalanen8274d902014-08-06 19:36:51 +03003207static int
3208subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3209{
3210 return snprintf(buf, len, "sub-surface");
3211}
3212
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003213static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003214subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003215{
3216 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003217 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003218
Jason Ekstranda7af7042013-10-12 22:38:11 -05003219 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003220 weston_view_set_position(view,
3221 view->geometry.x + dx,
3222 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003223
3224 /* No need to check parent mappedness, because if parent is not
3225 * mapped, parent is not in a visible layer, so this sub-surface
3226 * will not be drawn either.
3227 */
3228 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003229 struct weston_output *output;
3230
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003231 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003232 * because that would call it also for the parent surface,
3233 * which might not be mapped yet. That would lead to
3234 * inconsistent state, where the window could never be
3235 * mapped.
3236 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003237 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003238 * weston_surface_is_mapped() return true, so that when the
3239 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003240 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003241 */
3242 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003243 output = container_of(compositor->output_list.next,
3244 struct weston_output, link);
3245
3246 surface->output = output;
3247 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003248 }
3249}
3250
3251static struct weston_subsurface *
3252weston_surface_to_subsurface(struct weston_surface *surface)
3253{
3254 if (surface->configure == subsurface_configure)
3255 return surface->configure_private;
3256
3257 return NULL;
3258}
3259
Pekka Paalanen01388e22013-04-25 13:57:44 +03003260WL_EXPORT struct weston_surface *
3261weston_surface_get_main_surface(struct weston_surface *surface)
3262{
3263 struct weston_subsurface *sub;
3264
3265 while (surface && (sub = weston_surface_to_subsurface(surface)))
3266 surface = sub->parent;
3267
3268 return surface;
3269}
3270
Pekka Paalanen50b67472014-10-01 15:02:41 +03003271WL_EXPORT int
3272weston_surface_set_role(struct weston_surface *surface,
3273 const char *role_name,
3274 struct wl_resource *error_resource,
3275 uint32_t error_code)
3276{
3277 assert(role_name);
3278
3279 if (surface->role_name == NULL ||
3280 surface->role_name == role_name ||
3281 strcmp(surface->role_name, role_name) == 0) {
3282 surface->role_name = role_name;
3283
3284 return 0;
3285 }
3286
3287 wl_resource_post_error(error_resource, error_code,
3288 "Cannot assign role %s to wl_surface@%d,"
3289 " already has role %s\n",
3290 role_name,
3291 wl_resource_get_id(surface->resource),
3292 surface->role_name);
3293 return -1;
3294}
3295
Pekka Paalanen8274d902014-08-06 19:36:51 +03003296WL_EXPORT void
3297weston_surface_set_label_func(struct weston_surface *surface,
3298 int (*desc)(struct weston_surface *,
3299 char *, size_t))
3300{
3301 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003302 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003303}
3304
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003305/** Get the size of surface contents
3306 *
3307 * \param surface The surface to query.
3308 * \param width Returns the width of raw contents.
3309 * \param height Returns the height of raw contents.
3310 *
3311 * Retrieves the raw surface content size in pixels for the given surface.
3312 * This is the whole content size in buffer pixels. If the surface
3313 * has no content or the renderer does not implement this feature,
3314 * zeroes are returned.
3315 *
3316 * This function is used to determine the buffer size needed for
3317 * a weston_surface_copy_content() call.
3318 */
3319WL_EXPORT void
3320weston_surface_get_content_size(struct weston_surface *surface,
3321 int *width, int *height)
3322{
3323 struct weston_renderer *rer = surface->compositor->renderer;
3324
3325 if (!rer->surface_get_content_size) {
3326 *width = 0;
3327 *height = 0;
3328 return;
3329 }
3330
3331 rer->surface_get_content_size(surface, width, height);
3332}
3333
3334/** Copy surface contents to system memory.
3335 *
3336 * \param surface The surface to copy from.
3337 * \param target Pointer to the target memory buffer.
3338 * \param size Size of the target buffer in bytes.
3339 * \param src_x X location on contents to copy from.
3340 * \param src_y Y location on contents to copy from.
3341 * \param width Width in pixels of the area to copy.
3342 * \param height Height in pixels of the area to copy.
3343 * \return 0 for success, -1 for failure.
3344 *
3345 * Surface contents are maintained by the renderer. They can be in a
3346 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3347 * else.
3348 *
3349 * Surface contents are copied into memory pointed to by target,
3350 * which has size bytes of space available. The target memory
3351 * may be larger than needed, but being smaller returns an error.
3352 * The extra bytes in target may or may not be written; their content is
3353 * unspecified. Size must be large enough to hold the image.
3354 *
3355 * The image in the target memory will be arranged in rows from
3356 * top to bottom, and pixels on a row from left to right. The pixel
3357 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3358 * width * 4.
3359 *
3360 * Parameters src_x and src_y define the upper-left corner in buffer
3361 * coordinates (pixels) to copy from. Parameters width and height
3362 * define the size of the area to copy in pixels.
3363 *
3364 * The rectangle defined by src_x, src_y, width, height must fit in
3365 * the surface contents. Otherwise an error is returned.
3366 *
3367 * Use surface_get_data_size to determine the content size; the
3368 * needed target buffer size and rectangle limits.
3369 *
3370 * CURRENT IMPLEMENTATION RESTRICTIONS:
3371 * - the machine must be little-endian due to Pixman formats.
3372 *
3373 * NOTE: Pixman formats are premultiplied.
3374 */
3375WL_EXPORT int
3376weston_surface_copy_content(struct weston_surface *surface,
3377 void *target, size_t size,
3378 int src_x, int src_y,
3379 int width, int height)
3380{
3381 struct weston_renderer *rer = surface->compositor->renderer;
3382 int cw, ch;
3383 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3384
3385 if (!rer->surface_copy_content)
3386 return -1;
3387
3388 weston_surface_get_content_size(surface, &cw, &ch);
3389
3390 if (src_x < 0 || src_y < 0)
3391 return -1;
3392
3393 if (width <= 0 || height <= 0)
3394 return -1;
3395
3396 if (src_x + width > cw || src_y + height > ch)
3397 return -1;
3398
3399 if (width * bytespp * height > size)
3400 return -1;
3401
3402 return rer->surface_copy_content(surface, target, size,
3403 src_x, src_y, width, height);
3404}
3405
Pekka Paalanene67858b2013-04-25 13:57:42 +03003406static void
3407subsurface_set_position(struct wl_client *client,
3408 struct wl_resource *resource, int32_t x, int32_t y)
3409{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003410 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003411
3412 if (!sub)
3413 return;
3414
3415 sub->position.x = x;
3416 sub->position.y = y;
3417 sub->position.set = 1;
3418}
3419
3420static struct weston_subsurface *
3421subsurface_from_surface(struct weston_surface *surface)
3422{
3423 struct weston_subsurface *sub;
3424
3425 sub = weston_surface_to_subsurface(surface);
3426 if (sub)
3427 return sub;
3428
3429 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3430 if (sub->surface == surface)
3431 return sub;
3432
3433 return NULL;
3434}
3435
3436static struct weston_subsurface *
3437subsurface_sibling_check(struct weston_subsurface *sub,
3438 struct weston_surface *surface,
3439 const char *request)
3440{
3441 struct weston_subsurface *sibling;
3442
3443 sibling = subsurface_from_surface(surface);
3444
3445 if (!sibling) {
3446 wl_resource_post_error(sub->resource,
3447 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3448 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003449 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003450 return NULL;
3451 }
3452
3453 if (sibling->parent != sub->parent) {
3454 wl_resource_post_error(sub->resource,
3455 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3456 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003457 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003458 return NULL;
3459 }
3460
3461 return sibling;
3462}
3463
3464static void
3465subsurface_place_above(struct wl_client *client,
3466 struct wl_resource *resource,
3467 struct wl_resource *sibling_resource)
3468{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003469 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003470 struct weston_surface *surface =
3471 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003472 struct weston_subsurface *sibling;
3473
3474 if (!sub)
3475 return;
3476
3477 sibling = subsurface_sibling_check(sub, surface, "place_above");
3478 if (!sibling)
3479 return;
3480
3481 wl_list_remove(&sub->parent_link_pending);
3482 wl_list_insert(sibling->parent_link_pending.prev,
3483 &sub->parent_link_pending);
3484}
3485
3486static void
3487subsurface_place_below(struct wl_client *client,
3488 struct wl_resource *resource,
3489 struct wl_resource *sibling_resource)
3490{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003491 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003492 struct weston_surface *surface =
3493 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003494 struct weston_subsurface *sibling;
3495
3496 if (!sub)
3497 return;
3498
3499 sibling = subsurface_sibling_check(sub, surface, "place_below");
3500 if (!sibling)
3501 return;
3502
3503 wl_list_remove(&sub->parent_link_pending);
3504 wl_list_insert(&sibling->parent_link_pending,
3505 &sub->parent_link_pending);
3506}
3507
3508static void
3509subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3510{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003511 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003512
3513 if (sub)
3514 sub->synchronized = 1;
3515}
3516
3517static void
3518subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3519{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003520 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003521
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003522 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003523 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003524
3525 /* If sub became effectively desynchronized, flush. */
3526 if (!weston_subsurface_is_synchronized(sub))
3527 weston_subsurface_synchronized_commit(sub);
3528 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003529}
3530
3531static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003532weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3533{
3534 wl_list_remove(&sub->parent_link);
3535 wl_list_remove(&sub->parent_link_pending);
3536 wl_list_remove(&sub->parent_destroy_listener.link);
3537 sub->parent = NULL;
3538}
3539
3540static void
3541weston_subsurface_destroy(struct weston_subsurface *sub);
3542
3543static void
3544subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3545{
3546 struct weston_subsurface *sub =
3547 container_of(listener, struct weston_subsurface,
3548 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003549 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003550
3551 /* The protocol object (wl_resource) is left inert. */
3552 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003553 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003554
3555 weston_subsurface_destroy(sub);
3556}
3557
3558static void
3559subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3560{
3561 struct weston_subsurface *sub =
3562 container_of(listener, struct weston_subsurface,
3563 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003564 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003565 assert(sub->surface != sub->parent);
3566
3567 if (weston_surface_is_mapped(sub->surface))
3568 weston_surface_unmap(sub->surface);
3569
3570 weston_subsurface_unlink_parent(sub);
3571}
3572
3573static void
3574subsurface_resource_destroy(struct wl_resource *resource)
3575{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003576 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003577
3578 if (sub)
3579 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003580}
3581
3582static void
3583subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3584{
3585 wl_resource_destroy(resource);
3586}
3587
3588static void
3589weston_subsurface_link_parent(struct weston_subsurface *sub,
3590 struct weston_surface *parent)
3591{
3592 sub->parent = parent;
3593 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003594 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003595 &sub->parent_destroy_listener);
3596
3597 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3598 wl_list_insert(&parent->subsurface_list_pending,
3599 &sub->parent_link_pending);
3600}
3601
3602static void
3603weston_subsurface_link_surface(struct weston_subsurface *sub,
3604 struct weston_surface *surface)
3605{
3606 sub->surface = surface;
3607 sub->surface_destroy_listener.notify =
3608 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003609 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003610 &sub->surface_destroy_listener);
3611}
3612
3613static void
3614weston_subsurface_destroy(struct weston_subsurface *sub)
3615{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003616 struct weston_view *view, *next;
3617
Pekka Paalanene67858b2013-04-25 13:57:42 +03003618 assert(sub->surface);
3619
3620 if (sub->resource) {
3621 assert(weston_surface_to_subsurface(sub->surface) == sub);
3622 assert(sub->parent_destroy_listener.notify ==
3623 subsurface_handle_parent_destroy);
3624
George Kiagiadakised04d382014-06-13 18:10:26 +02003625 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3626 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003627 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003628 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003629
Pekka Paalanene67858b2013-04-25 13:57:42 +03003630 if (sub->parent)
3631 weston_subsurface_unlink_parent(sub);
3632
Jason Ekstrand7b982072014-05-20 14:33:03 -05003633 weston_surface_state_fini(&sub->cached);
3634 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003635
3636 sub->surface->configure = NULL;
3637 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003638 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003639 } else {
3640 /* the dummy weston_subsurface for the parent itself */
3641 assert(sub->parent_destroy_listener.notify == NULL);
3642 wl_list_remove(&sub->parent_link);
3643 wl_list_remove(&sub->parent_link_pending);
3644 }
3645
3646 wl_list_remove(&sub->surface_destroy_listener.link);
3647 free(sub);
3648}
3649
3650static const struct wl_subsurface_interface subsurface_implementation = {
3651 subsurface_destroy,
3652 subsurface_set_position,
3653 subsurface_place_above,
3654 subsurface_place_below,
3655 subsurface_set_sync,
3656 subsurface_set_desync
3657};
3658
3659static struct weston_subsurface *
3660weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3661 struct weston_surface *parent)
3662{
3663 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003664 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003665
Bryce Harringtonde16d892014-11-20 22:21:57 -08003666 sub = zalloc(sizeof *sub);
3667 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003668 return NULL;
3669
Jason Ekstranda7af7042013-10-12 22:38:11 -05003670 wl_list_init(&sub->unused_views);
3671
Jason Ekstranda85118c2013-06-27 20:17:02 -05003672 sub->resource =
3673 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003674 if (!sub->resource) {
3675 free(sub);
3676 return NULL;
3677 }
3678
Jason Ekstranda85118c2013-06-27 20:17:02 -05003679 wl_resource_set_implementation(sub->resource,
3680 &subsurface_implementation,
3681 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003682 weston_subsurface_link_surface(sub, surface);
3683 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003684 weston_surface_state_init(&sub->cached);
3685 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003686 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003687
3688 return sub;
3689}
3690
3691/* Create a dummy subsurface for having the parent itself in its
3692 * sub-surface lists. Makes stacking order manipulation easy.
3693 */
3694static struct weston_subsurface *
3695weston_subsurface_create_for_parent(struct weston_surface *parent)
3696{
3697 struct weston_subsurface *sub;
3698
Bryce Harringtonde16d892014-11-20 22:21:57 -08003699 sub = zalloc(sizeof *sub);
3700 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003701 return NULL;
3702
3703 weston_subsurface_link_surface(sub, parent);
3704 sub->parent = parent;
3705 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3706 wl_list_insert(&parent->subsurface_list_pending,
3707 &sub->parent_link_pending);
3708
3709 return sub;
3710}
3711
3712static void
3713subcompositor_get_subsurface(struct wl_client *client,
3714 struct wl_resource *resource,
3715 uint32_t id,
3716 struct wl_resource *surface_resource,
3717 struct wl_resource *parent_resource)
3718{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003719 struct weston_surface *surface =
3720 wl_resource_get_user_data(surface_resource);
3721 struct weston_surface *parent =
3722 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003723 struct weston_subsurface *sub;
3724 static const char where[] = "get_subsurface: wl_subsurface@";
3725
3726 if (surface == parent) {
3727 wl_resource_post_error(resource,
3728 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3729 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003730 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003731 return;
3732 }
3733
3734 if (weston_surface_to_subsurface(surface)) {
3735 wl_resource_post_error(resource,
3736 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3737 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003738 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003739 return;
3740 }
3741
Pekka Paalanen50b67472014-10-01 15:02:41 +03003742 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3743 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003744 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003745
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003746 if (weston_surface_get_main_surface(parent) == surface) {
3747 wl_resource_post_error(resource,
3748 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3749 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003750 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003751 return;
3752 }
3753
Pekka Paalanene67858b2013-04-25 13:57:42 +03003754 /* make sure the parent is in its own list */
3755 if (wl_list_empty(&parent->subsurface_list)) {
3756 if (!weston_subsurface_create_for_parent(parent)) {
3757 wl_resource_post_no_memory(resource);
3758 return;
3759 }
3760 }
3761
3762 sub = weston_subsurface_create(id, surface, parent);
3763 if (!sub) {
3764 wl_resource_post_no_memory(resource);
3765 return;
3766 }
3767
3768 surface->configure = subsurface_configure;
3769 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003770 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003771}
3772
3773static void
3774subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3775{
3776 wl_resource_destroy(resource);
3777}
3778
3779static const struct wl_subcompositor_interface subcompositor_interface = {
3780 subcompositor_destroy,
3781 subcompositor_get_subsurface
3782};
3783
3784static void
3785bind_subcompositor(struct wl_client *client,
3786 void *data, uint32_t version, uint32_t id)
3787{
3788 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003789 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003790
Jason Ekstranda85118c2013-06-27 20:17:02 -05003791 resource =
3792 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003793 if (resource == NULL) {
3794 wl_client_post_no_memory(client);
3795 return;
3796 }
3797 wl_resource_set_implementation(resource, &subcompositor_interface,
3798 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003799}
3800
3801static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003802weston_compositor_dpms(struct weston_compositor *compositor,
3803 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003804{
3805 struct weston_output *output;
3806
3807 wl_list_for_each(output, &compositor->output_list, link)
3808 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003809 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003810}
3811
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003812WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003813weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003814{
Neil Roberts8b62e202013-09-30 13:14:47 +01003815 uint32_t old_state = compositor->state;
3816
3817 /* The state needs to be changed before emitting the wake
3818 * signal because that may try to schedule a repaint which
3819 * will not work if the compositor is still sleeping */
3820 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3821
3822 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003823 case WESTON_COMPOSITOR_SLEEPING:
3824 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3825 /* fall through */
3826 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003827 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003828 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003829 /* fall through */
3830 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003831 wl_event_source_timer_update(compositor->idle_source,
3832 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003833 }
3834}
3835
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003836WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003837weston_compositor_offscreen(struct weston_compositor *compositor)
3838{
3839 switch (compositor->state) {
3840 case WESTON_COMPOSITOR_OFFSCREEN:
3841 return;
3842 case WESTON_COMPOSITOR_SLEEPING:
3843 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3844 /* fall through */
3845 default:
3846 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3847 wl_event_source_timer_update(compositor->idle_source, 0);
3848 }
3849}
3850
3851WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003852weston_compositor_sleep(struct weston_compositor *compositor)
3853{
3854 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3855 return;
3856
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003857 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003858 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3859 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3860}
3861
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003862static int
3863idle_handler(void *data)
3864{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003865 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003866
3867 if (compositor->idle_inhibit)
3868 return 1;
3869
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003870 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003871 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003872
3873 return 1;
3874}
3875
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003876WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003877weston_plane_init(struct weston_plane *plane,
3878 struct weston_compositor *ec,
3879 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003880{
3881 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003882 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003883 plane->x = x;
3884 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003885 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003886
3887 /* Init the link so that the call to wl_list_remove() when releasing
3888 * the plane without ever stacking doesn't lead to a crash */
3889 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003890}
3891
3892WL_EXPORT void
3893weston_plane_release(struct weston_plane *plane)
3894{
Xiong Zhang97116532013-10-23 13:58:31 +08003895 struct weston_view *view;
3896
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003897 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003898 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003899
Xiong Zhang97116532013-10-23 13:58:31 +08003900 wl_list_for_each(view, &plane->compositor->view_list, link) {
3901 if (view->plane == plane)
3902 view->plane = NULL;
3903 }
3904
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003905 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003906}
3907
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003908WL_EXPORT void
3909weston_compositor_stack_plane(struct weston_compositor *ec,
3910 struct weston_plane *plane,
3911 struct weston_plane *above)
3912{
3913 if (above)
3914 wl_list_insert(above->link.prev, &plane->link);
3915 else
3916 wl_list_insert(&ec->plane_list, &plane->link);
3917}
3918
Casey Dahlin9074db52012-04-19 22:50:09 -04003919static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003920{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003921 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003922}
3923
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003924static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003925bind_output(struct wl_client *client,
3926 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003927{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003928 struct weston_output *output = data;
3929 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003930 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003931
Jason Ekstranda85118c2013-06-27 20:17:02 -05003932 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06003933 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003934 if (resource == NULL) {
3935 wl_client_post_no_memory(client);
3936 return;
3937 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003938
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003939 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003940 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003941
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003942 wl_output_send_geometry(resource,
3943 output->x,
3944 output->y,
3945 output->mm_width,
3946 output->mm_height,
3947 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003948 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003949 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003950 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003951 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003952 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003953
3954 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003955 wl_output_send_mode(resource,
3956 mode->flags,
3957 mode->width,
3958 mode->height,
3959 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003960 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003961
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003962 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003963 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003964}
3965
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003966/* Move other outputs when one is removed so the space remains contiguos. */
3967static void
3968weston_compositor_remove_output(struct weston_compositor *compositor,
3969 struct weston_output *remove_output)
3970{
3971 struct weston_output *output;
3972 int offset = 0;
3973
3974 wl_list_for_each(output, &compositor->output_list, link) {
3975 if (output == remove_output) {
3976 offset = output->width;
3977 continue;
3978 }
3979
3980 if (offset > 0) {
3981 weston_output_move(output,
3982 output->x - offset, output->y);
3983 output->dirty = 1;
3984 }
3985 }
3986}
3987
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003988WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003989weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003990{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003991 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003992 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003993
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003994 output->destroying = 1;
3995
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003996 wl_list_for_each(view, &output->compositor->view_list, link) {
3997 if (view->output_mask & (1 << output->id))
3998 weston_view_assign_output(view);
3999 }
4000
Pekka Paalanen0513a952014-05-21 16:17:27 +03004001 wl_event_source_remove(output->repaint_timer);
4002
Pekka Paalanen133e4392014-09-23 22:08:46 -04004003 weston_presentation_feedback_discard_list(&output->feedback_list);
4004
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004005 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004006 wl_list_remove(&output->link);
4007
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004008 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004009 wl_signal_emit(&output->destroy_signal, output);
4010
Richard Hughesafe690c2013-05-02 10:10:04 +01004011 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004012 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004013 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04004014 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004015
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004016 wl_resource_for_each(resource, &output->resource_list) {
4017 wl_resource_set_destructor(resource, NULL);
4018 }
4019
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004020 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004021}
4022
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004023WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004024weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004025{
Scott Moreau850ca422012-05-21 15:21:25 -06004026 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004027
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004028 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004029 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004030
Scott Moreauccbf29d2012-02-22 14:21:41 -07004031 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004032 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004033 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004034 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004035 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004036 weston_matrix_scale(&output->matrix, magnification,
4037 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004038 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004039
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004040 switch (output->transform) {
4041 case WL_OUTPUT_TRANSFORM_FLIPPED:
4042 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4043 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4044 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4045 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4046 weston_matrix_scale(&output->matrix, -1, 1, 1);
4047 break;
4048 }
4049
4050 switch (output->transform) {
4051 default:
4052 case WL_OUTPUT_TRANSFORM_NORMAL:
4053 case WL_OUTPUT_TRANSFORM_FLIPPED:
4054 break;
4055 case WL_OUTPUT_TRANSFORM_90:
4056 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4057 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4058 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4059 break;
4060 case WL_OUTPUT_TRANSFORM_180:
4061 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4062 weston_matrix_translate(&output->matrix,
4063 -output->width, -output->height, 0);
4064 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4065 break;
4066 case WL_OUTPUT_TRANSFORM_270:
4067 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4068 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4069 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4070 break;
4071 }
4072
4073 if (output->current_scale != 1)
4074 weston_matrix_scale(&output->matrix,
4075 output->current_scale,
4076 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004077
Scott Moreauccbf29d2012-02-22 14:21:41 -07004078 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004079
4080 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004081}
4082
Scott Moreau1bad5db2012-08-18 01:04:05 -06004083static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004084weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004085{
4086 output->transform = transform;
4087
4088 switch (transform) {
4089 case WL_OUTPUT_TRANSFORM_90:
4090 case WL_OUTPUT_TRANSFORM_270:
4091 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4092 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4093 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02004094 output->width = output->current_mode->height;
4095 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004096 break;
4097 case WL_OUTPUT_TRANSFORM_NORMAL:
4098 case WL_OUTPUT_TRANSFORM_180:
4099 case WL_OUTPUT_TRANSFORM_FLIPPED:
4100 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02004101 output->width = output->current_mode->width;
4102 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004103 break;
4104 default:
4105 break;
4106 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06004107
Hardening57388e42013-09-18 23:56:36 +02004108 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02004109 output->width /= scale;
4110 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004111}
4112
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004113static void
4114weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004115{
4116 output->x = x;
4117 output->y = y;
4118
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004119 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004120 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004121 output->width,
4122 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004123}
4124
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004125WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004126weston_output_move(struct weston_output *output, int x, int y)
4127{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004128 struct wl_resource *resource;
4129
4130 output->move_x = x - output->x;
4131 output->move_y = y - output->y;
4132
4133 if (output->move_x == 0 && output->move_y == 0)
4134 return;
4135
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004136 weston_output_init_geometry(output, x, y);
4137
4138 output->dirty = 1;
4139
4140 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004141 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004142
4143 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004144 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004145 wl_output_send_geometry(resource,
4146 output->x,
4147 output->y,
4148 output->mm_width,
4149 output->mm_height,
4150 output->subpixel,
4151 output->make,
4152 output->model,
4153 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004154
4155 if (wl_resource_get_version(resource) >= 2)
4156 wl_output_send_done(resource);
4157 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004158}
4159
4160WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004161weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004162 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004163 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004164{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004165 struct wl_event_loop *loop;
4166
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004167 output->compositor = c;
4168 output->x = x;
4169 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004170 output->mm_width = mm_width;
4171 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004172 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004173 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004174
Alexander Larsson0b135062013-05-28 16:23:36 +02004175 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004176 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004177
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004178 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004179 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004180
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004181 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004182 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004183 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004184 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004185 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004186 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004187
Pekka Paalanen0513a952014-05-21 16:17:27 +03004188 loop = wl_display_get_event_loop(c->wl_display);
4189 output->repaint_timer = wl_event_loop_add_timer(loop,
4190 output_repaint_timer_handler, output);
4191
Casey Dahlin58ba1372012-04-19 22:50:08 -04004192 output->id = ffs(~output->compositor->output_id_pool) - 1;
4193 output->compositor->output_id_pool |= 1 << output->id;
4194
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004195 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004196 wl_global_create(c->wl_display, &wl_output_interface, 2,
4197 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004198}
4199
4200/** Adds an output to the compositor's output list and
4201 * send the compositor's output_created signal.
4202 *
4203 * \param compositor The compositor instance.
4204 * \param output The output to be added.
4205 */
4206WL_EXPORT void
4207weston_compositor_add_output(struct weston_compositor *compositor,
4208 struct weston_output *output)
4209{
4210 wl_list_insert(compositor->output_list.prev, &output->link);
4211 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004212}
4213
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004214WL_EXPORT void
4215weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004216 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004217 wl_fixed_t *x, wl_fixed_t *y)
4218{
Derek Foreman0f679412014-10-02 13:41:17 -05004219 struct weston_vector p = { {
4220 wl_fixed_to_double(device_x),
4221 wl_fixed_to_double(device_y),
4222 0.0,
4223 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004224
Derek Foreman67a18b92015-03-24 11:36:14 -05004225 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004226
Derek Foreman0f679412014-10-02 13:41:17 -05004227 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4228 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004229}
4230
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004231static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004232destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004233{
Jonny Lamb74130762013-11-26 18:19:46 +01004234 struct weston_surface *surface =
4235 wl_resource_get_user_data(resource);
4236
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004237 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004238 surface->pending.buffer_viewport.buffer.src_width =
4239 wl_fixed_from_int(-1);
4240 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004241 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004242}
4243
4244static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004245viewport_destroy(struct wl_client *client,
4246 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004247{
4248 wl_resource_destroy(resource);
4249}
4250
4251static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004252viewport_set(struct wl_client *client,
4253 struct wl_resource *resource,
4254 wl_fixed_t src_x,
4255 wl_fixed_t src_y,
4256 wl_fixed_t src_width,
4257 wl_fixed_t src_height,
4258 int32_t dst_width,
4259 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004260{
Jonny Lamb74130762013-11-26 18:19:46 +01004261 struct weston_surface *surface =
4262 wl_resource_get_user_data(resource);
4263
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004264 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004265
Jonny Lamb8ae35902013-11-26 18:19:45 +01004266 if (wl_fixed_to_double(src_width) < 0 ||
4267 wl_fixed_to_double(src_height) < 0) {
4268 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004269 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004270 "source dimensions must be non-negative (%fx%f)",
4271 wl_fixed_to_double(src_width),
4272 wl_fixed_to_double(src_height));
4273 return;
4274 }
4275
4276 if (dst_width <= 0 || dst_height <= 0) {
4277 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004278 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004279 "destination dimensions must be positive (%dx%d)",
4280 dst_width, dst_height);
4281 return;
4282 }
4283
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004284 surface->pending.buffer_viewport.buffer.src_x = src_x;
4285 surface->pending.buffer_viewport.buffer.src_y = src_y;
4286 surface->pending.buffer_viewport.buffer.src_width = src_width;
4287 surface->pending.buffer_viewport.buffer.src_height = src_height;
4288 surface->pending.buffer_viewport.surface.width = dst_width;
4289 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004290 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004291}
4292
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004293static void
4294viewport_set_source(struct wl_client *client,
4295 struct wl_resource *resource,
4296 wl_fixed_t src_x,
4297 wl_fixed_t src_y,
4298 wl_fixed_t src_width,
4299 wl_fixed_t src_height)
4300{
4301 struct weston_surface *surface =
4302 wl_resource_get_user_data(resource);
4303
4304 assert(surface->viewport_resource != NULL);
4305
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004306 if (src_width == wl_fixed_from_int(-1) &&
4307 src_height == wl_fixed_from_int(-1)) {
4308 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004309 surface->pending.buffer_viewport.buffer.src_width =
4310 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004311 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004312 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004313 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004314
4315 if (src_width <= 0 || src_height <= 0) {
4316 wl_resource_post_error(resource,
4317 WL_VIEWPORT_ERROR_BAD_VALUE,
4318 "source size must be positive (%fx%f)",
4319 wl_fixed_to_double(src_width),
4320 wl_fixed_to_double(src_height));
4321 return;
4322 }
4323
4324 surface->pending.buffer_viewport.buffer.src_x = src_x;
4325 surface->pending.buffer_viewport.buffer.src_y = src_y;
4326 surface->pending.buffer_viewport.buffer.src_width = src_width;
4327 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004328 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004329}
4330
4331static void
4332viewport_set_destination(struct wl_client *client,
4333 struct wl_resource *resource,
4334 int32_t dst_width,
4335 int32_t dst_height)
4336{
4337 struct weston_surface *surface =
4338 wl_resource_get_user_data(resource);
4339
4340 assert(surface->viewport_resource != NULL);
4341
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004342 if (dst_width == -1 && dst_height == -1) {
4343 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004344 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004345 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004346 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004347 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004348
4349 if (dst_width <= 0 || dst_height <= 0) {
4350 wl_resource_post_error(resource,
4351 WL_VIEWPORT_ERROR_BAD_VALUE,
4352 "destination size must be positive (%dx%d)",
4353 dst_width, dst_height);
4354 return;
4355 }
4356
4357 surface->pending.buffer_viewport.surface.width = dst_width;
4358 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004359 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004360}
4361
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004362static const struct wl_viewport_interface viewport_interface = {
4363 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004364 viewport_set,
4365 viewport_set_source,
4366 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004367};
4368
4369static void
4370scaler_destroy(struct wl_client *client,
4371 struct wl_resource *resource)
4372{
4373 wl_resource_destroy(resource);
4374}
4375
4376static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004377scaler_get_viewport(struct wl_client *client,
4378 struct wl_resource *scaler,
4379 uint32_t id,
4380 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004381{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004382 int version = wl_resource_get_version(scaler);
4383 struct weston_surface *surface =
4384 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004385 struct wl_resource *resource;
4386
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004387 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004388 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004389 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4390 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004391 return;
4392 }
4393
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004394 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004395 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004396 if (resource == NULL) {
4397 wl_client_post_no_memory(client);
4398 return;
4399 }
4400
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004401 wl_resource_set_implementation(resource, &viewport_interface,
4402 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004403
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004404 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004405}
4406
4407static const struct wl_scaler_interface scaler_interface = {
4408 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004409 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004410};
4411
4412static void
4413bind_scaler(struct wl_client *client,
4414 void *data, uint32_t version, uint32_t id)
4415{
4416 struct wl_resource *resource;
4417
4418 resource = wl_resource_create(client, &wl_scaler_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004419 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004420 if (resource == NULL) {
4421 wl_client_post_no_memory(client);
4422 return;
4423 }
4424
4425 wl_resource_set_implementation(resource, &scaler_interface,
4426 NULL, NULL);
4427}
4428
4429static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004430destroy_presentation_feedback(struct wl_resource *feedback_resource)
4431{
4432 struct weston_presentation_feedback *feedback;
4433
4434 feedback = wl_resource_get_user_data(feedback_resource);
4435
4436 wl_list_remove(&feedback->link);
4437 free(feedback);
4438}
4439
4440static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004441presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4442{
4443 wl_resource_destroy(resource);
4444}
4445
4446static void
4447presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004448 struct wl_resource *presentation_resource,
4449 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004450 uint32_t callback)
4451{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004452 struct weston_surface *surface;
4453 struct weston_presentation_feedback *feedback;
4454
4455 surface = wl_resource_get_user_data(surface_resource);
4456
Bryce Harringtonde16d892014-11-20 22:21:57 -08004457 feedback = zalloc(sizeof *feedback);
4458 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004459 goto err_calloc;
4460
4461 feedback->resource = wl_resource_create(client,
4462 &presentation_feedback_interface,
4463 1, callback);
4464 if (!feedback->resource)
4465 goto err_create;
4466
4467 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4468 destroy_presentation_feedback);
4469
4470 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4471
4472 return;
4473
4474err_create:
4475 free(feedback);
4476
4477err_calloc:
4478 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004479}
4480
4481static const struct presentation_interface presentation_implementation = {
4482 presentation_destroy,
4483 presentation_feedback
4484};
4485
4486static void
4487bind_presentation(struct wl_client *client,
4488 void *data, uint32_t version, uint32_t id)
4489{
4490 struct weston_compositor *compositor = data;
4491 struct wl_resource *resource;
4492
4493 resource = wl_resource_create(client, &presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004494 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004495 if (resource == NULL) {
4496 wl_client_post_no_memory(client);
4497 return;
4498 }
4499
4500 wl_resource_set_implementation(resource, &presentation_implementation,
4501 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004502 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004503}
4504
4505static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004506compositor_bind(struct wl_client *client,
4507 void *data, uint32_t version, uint32_t id)
4508{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004509 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004510 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004511
Jason Ekstranda85118c2013-06-27 20:17:02 -05004512 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004513 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004514 if (resource == NULL) {
4515 wl_client_post_no_memory(client);
4516 return;
4517 }
4518
4519 wl_resource_set_implementation(resource, &compositor_interface,
4520 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004521}
4522
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004523WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004524weston_environment_get_fd(const char *env)
4525{
4526 char *e, *end;
4527 int fd, flags;
4528
4529 e = getenv(env);
4530 if (!e)
4531 return -1;
4532 fd = strtol(e, &end, 0);
4533 if (*end != '\0')
4534 return -1;
4535
4536 flags = fcntl(fd, F_GETFD);
4537 if (flags == -1)
4538 return -1;
4539
4540 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4541 unsetenv(env);
4542
4543 return fd;
4544}
4545
Pekka Paalanenb5026542014-11-12 15:09:24 +02004546static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004547timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004548 uint32_t key, void *data)
4549{
4550 struct weston_compositor *compositor = data;
4551
4552 if (weston_timeline_enabled_)
4553 weston_timeline_close();
4554 else
4555 weston_timeline_open(compositor);
4556}
4557
Giulio Camuffo459137b2014-10-11 23:56:24 +03004558/** Create the compositor.
4559 *
4560 * This functions creates and initializes a compositor instance.
4561 *
4562 * \param display The Wayland display to be used.
4563 * \param user_data A pointer to an object that can later be retrieved
4564 * using the \ref weston_compositor_get_user_data function.
4565 * \return The compositor instance on success or NULL on failure.
4566 */
4567WL_EXPORT struct weston_compositor *
4568weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004569{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004570 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004571 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004572
Giulio Camuffo459137b2014-10-11 23:56:24 +03004573 ec = zalloc(sizeof *ec);
4574 if (!ec)
4575 return NULL;
4576
4577 ec->wl_display = display;
4578 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004579 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004580 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004581 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004582 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004583 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004584 wl_signal_init(&ec->idle_signal);
4585 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004586 wl_signal_init(&ec->show_input_panel_signal);
4587 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004588 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004589 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004590 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004591 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004592 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004593 wl_signal_init(&ec->session_signal);
4594 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004595
Casey Dahlin58ba1372012-04-19 22:50:08 -04004596 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004597 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004598
Derek Foreman152254b2015-11-26 14:17:48 -06004599 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004600 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004601 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004602
Giulio Camuffo954f1832014-10-11 18:27:30 +03004603 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004604 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004605 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004606
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004607 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004608 ec, bind_scaler))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004609 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004610
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004611 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4612 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004613 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004614
Jason Ekstranda7af7042013-10-12 22:38:11 -05004615 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004616 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004617 wl_list_init(&ec->layer_list);
4618 wl_list_init(&ec->seat_list);
4619 wl_list_init(&ec->output_list);
4620 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004621 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004622 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004623 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004624 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004625 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004626
Xiong Zhang97116532013-10-23 13:58:31 +08004627 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004628 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004629
Giulio Camuffo459137b2014-10-11 23:56:24 +03004630 wl_data_device_manager_init(ec->wl_display);
4631
4632 wl_display_init_shm(ec->wl_display);
4633
4634 loop = wl_display_get_event_loop(ec->wl_display);
4635 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004636
4637 ec->input_loop = wl_event_loop_create();
4638
4639 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4640 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4641
4642 weston_compositor_add_debug_binding(ec, KEY_T,
4643 timeline_key_binding_handler, ec);
4644
Giulio Camuffo459137b2014-10-11 23:56:24 +03004645 return ec;
4646
4647fail:
4648 free(ec);
4649 return NULL;
4650}
4651
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004652WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004653weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004654{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004655 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004656
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004657 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004658 if (ec->input_loop_source)
4659 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004660
Matt Roper361d2ad2011-08-29 13:52:23 -07004661 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004662 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004663 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004664
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004665 if (ec->renderer)
4666 ec->renderer->destroy(ec);
4667
Daniel Stone325fc2d2012-05-30 16:31:58 +01004668 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004669 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004670 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004671 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004672 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004673 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004674
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004675 weston_plane_release(&ec->primary_plane);
4676
Jonas Ådahlc97af922012-03-28 22:36:09 +02004677 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07004678}
4679
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004680WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004681weston_compositor_exit_with_code(struct weston_compositor *compositor,
4682 int exit_code)
4683{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004684 if (compositor->exit_code == EXIT_SUCCESS)
4685 compositor->exit_code = exit_code;
4686
Giulio Camuffo459137b2014-10-11 23:56:24 +03004687 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004688}
4689
4690WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004691weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4692 const struct weston_pointer_grab_interface *interface)
4693{
4694 struct weston_seat *seat;
4695
4696 ec->default_pointer_grab = interface;
4697 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004698 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4699
4700 if (pointer)
4701 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004702 }
4703}
4704
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004705WL_EXPORT int
4706weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4707 clockid_t clk_id)
4708{
4709 struct timespec ts;
4710
4711 if (clock_gettime(clk_id, &ts) < 0)
4712 return -1;
4713
4714 compositor->presentation_clock = clk_id;
4715
4716 return 0;
4717}
4718
4719/*
4720 * For choosing the software clock, when the display hardware or API
4721 * does not expose a compatible presentation timestamp.
4722 */
4723WL_EXPORT int
4724weston_compositor_set_presentation_clock_software(
4725 struct weston_compositor *compositor)
4726{
4727 /* In order of preference */
4728 static const clockid_t clocks[] = {
4729 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4730 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4731 CLOCK_MONOTONIC, /* no jumps, may crawl */
4732 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4733 CLOCK_REALTIME /* may jump and crawl */
4734 };
4735 unsigned i;
4736
4737 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4738 if (weston_compositor_set_presentation_clock(compositor,
4739 clocks[i]) == 0)
4740 return 0;
4741
4742 weston_log("Error: no suitable presentation clock available.\n");
4743
4744 return -1;
4745}
4746
Pekka Paalanen662f3842015-03-18 12:17:26 +02004747/** Read the current time from the Presentation clock
4748 *
4749 * \param compositor
4750 * \param ts[out] The current time.
4751 *
4752 * \note Reading the current time in user space is always imprecise to some
4753 * degree.
4754 *
4755 * This function is never meant to fail. If reading the clock does fail,
4756 * an error message is logged and a zero time is returned. Callers are not
4757 * supposed to detect or react to failures.
4758 */
4759WL_EXPORT void
4760weston_compositor_read_presentation_clock(
4761 const struct weston_compositor *compositor,
4762 struct timespec *ts)
4763{
4764 static bool warned;
4765 int ret;
4766
4767 ret = clock_gettime(compositor->presentation_clock, ts);
4768 if (ret < 0) {
4769 ts->tv_sec = 0;
4770 ts->tv_nsec = 0;
4771
4772 if (!warned)
4773 weston_log("Error: failure to read "
4774 "the presentation clock %#x: '%m' (%d)\n",
4775 compositor->presentation_clock, errno);
4776 warned = true;
4777 }
4778}
4779
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004780/** Import dmabuf buffer into current renderer
4781 *
4782 * \param compositor
4783 * \param buffer the dmabuf buffer to import
4784 * \return true on usable buffers, false otherwise
4785 *
4786 * This function tests that the linux_dmabuf_buffer is usable
4787 * for the current renderer. Returns false on unusable buffers. Usually
4788 * usability is tested by importing the dmabufs for composition.
4789 *
4790 * This hook is also used for detecting if the renderer supports
4791 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4792 * supported.
4793 * */
4794WL_EXPORT bool
4795weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4796 struct linux_dmabuf_buffer *buffer)
4797{
4798 struct weston_renderer *renderer;
4799
4800 renderer = compositor->renderer;
4801
4802 if (renderer->import_dmabuf == NULL)
4803 return false;
4804
4805 return renderer->import_dmabuf(compositor, buffer);
4806}
4807
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004808WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004809weston_version(int *major, int *minor, int *micro)
4810{
4811 *major = WESTON_VERSION_MAJOR;
4812 *minor = WESTON_VERSION_MINOR;
4813 *micro = WESTON_VERSION_MICRO;
4814}
4815
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004816WL_EXPORT void *
4817weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004818{
Derek Foreman3f86e502015-06-08 11:46:54 -05004819 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004820 char path[PATH_MAX];
4821 void *module, *init;
4822
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004823 if (name == NULL)
4824 return NULL;
4825
Derek Foreman3f86e502015-06-08 11:46:54 -05004826 if (name[0] != '/') {
4827 if (builddir)
4828 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4829 else
4830 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
4831 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004832 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004833 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004834
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004835 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4836 if (module) {
4837 weston_log("Module '%s' already loaded\n", path);
4838 dlclose(module);
4839 return NULL;
4840 }
4841
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004842 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004843 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004844 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004845 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004846 return NULL;
4847 }
4848
4849 init = dlsym(module, entrypoint);
4850 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004851 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004852 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004853 return NULL;
4854 }
4855
4856 return init;
4857}
4858
Giulio Camuffo459137b2014-10-11 23:56:24 +03004859
4860/** Destroys the compositor.
4861 *
4862 * This function cleans up the compositor state and destroys it.
4863 *
4864 * \param compositor The compositor to be destroyed.
4865 */
4866WL_EXPORT void
4867weston_compositor_destroy(struct weston_compositor *compositor)
4868{
4869 /* prevent further rendering while shutting down */
4870 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4871
4872 wl_signal_emit(&compositor->destroy_signal, compositor);
4873
4874 weston_compositor_xkb_destroy(compositor);
4875
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004876 if (compositor->backend)
4877 compositor->backend->destroy(compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004878 free(compositor);
4879}
4880
4881/** Instruct the compositor to exit.
4882 *
4883 * This functions does not directly destroy the compositor object, it merely
4884 * command it to start the tear down process. It is not guaranteed that the
4885 * tear down will happen immediately.
4886 *
4887 * \param compositor The compositor to tear down.
4888 */
4889WL_EXPORT void
4890weston_compositor_exit(struct weston_compositor *compositor)
4891{
4892 compositor->exit(compositor);
4893}
4894
4895/** Return the user data stored in the compositor.
4896 *
4897 * This function returns the user data pointer set with user_data parameter
4898 * to the \ref weston_compositor_create function.
4899 */
4900WL_EXPORT void *
4901weston_compositor_get_user_data(struct weston_compositor *compositor)
4902{
4903 return compositor->user_data;
4904}