blob: 3e3fc8ca749846d91e1f03385c6c8f3d3f7befc6 [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 Paalanenb00c79b2016-02-18 16:53:27 +020058#include "presentation-time-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{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200446 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400447 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
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200476 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400477 }
478
479 secs = ts->tv_sec;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200480 wp_presentation_feedback_send_presented(feedback->resource,
481 secs >> 32, secs & 0xffffffff,
482 ts->tv_nsec,
483 refresh_nsec,
484 seq >> 32, seq & 0xffffffff,
485 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 Paalanenb00c79b2016-02-18 16:53:27 +0200499 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200500 wl_list_empty(list));
501
Pekka Paalanen133e4392014-09-23 22:08:46 -0400502 wl_list_for_each_safe(feedback, tmp, list, link)
503 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200504 refresh_nsec, ts, seq,
505 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400506}
507
Jason Ekstrand7b982072014-05-20 14:33:03 -0500508static void
509surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
510{
511 struct weston_surface_state *state =
512 container_of(listener, struct weston_surface_state,
513 buffer_destroy_listener);
514
515 state->buffer = NULL;
516}
517
518static void
519weston_surface_state_init(struct weston_surface_state *state)
520{
521 state->newly_attached = 0;
522 state->buffer = NULL;
523 state->buffer_destroy_listener.notify =
524 surface_state_handle_buffer_destroy;
525 state->sx = 0;
526 state->sy = 0;
527
Derek Foreman152254b2015-11-26 14:17:48 -0600528 pixman_region32_init(&state->damage_surface);
529 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500530 pixman_region32_init(&state->opaque);
531 region_init_infinite(&state->input);
532
533 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400534 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500535
536 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
537 state->buffer_viewport.buffer.scale = 1;
538 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
539 state->buffer_viewport.surface.width = -1;
540 state->buffer_viewport.changed = 0;
541}
542
543static void
544weston_surface_state_fini(struct weston_surface_state *state)
545{
546 struct weston_frame_callback *cb, *next;
547
548 wl_list_for_each_safe(cb, next,
549 &state->frame_callback_list, link)
550 wl_resource_destroy(cb->resource);
551
Pekka Paalanen133e4392014-09-23 22:08:46 -0400552 weston_presentation_feedback_discard_list(&state->feedback_list);
553
Jason Ekstrand7b982072014-05-20 14:33:03 -0500554 pixman_region32_fini(&state->input);
555 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600556 pixman_region32_fini(&state->damage_surface);
557 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500558
559 if (state->buffer)
560 wl_list_remove(&state->buffer_destroy_listener.link);
561 state->buffer = NULL;
562}
563
564static void
565weston_surface_state_set_buffer(struct weston_surface_state *state,
566 struct weston_buffer *buffer)
567{
568 if (state->buffer == buffer)
569 return;
570
571 if (state->buffer)
572 wl_list_remove(&state->buffer_destroy_listener.link);
573 state->buffer = buffer;
574 if (state->buffer)
575 wl_signal_add(&state->buffer->destroy_signal,
576 &state->buffer_destroy_listener);
577}
578
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500579WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500580weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500581{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500582 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400583
Bryce Harringtonde16d892014-11-20 22:21:57 -0800584 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400585 if (surface == NULL)
586 return NULL;
587
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500588 wl_signal_init(&surface->destroy_signal);
589
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500590 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200591 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400592
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200593 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
594 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200595 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
596 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500597
598 weston_surface_state_init(&surface->pending);
599
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400600 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500601 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300602 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400603
Jason Ekstranda7af7042013-10-12 22:38:11 -0500604 wl_list_init(&surface->views);
605
606 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400607 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500608
Pekka Paalanene67858b2013-04-25 13:57:42 +0300609 wl_list_init(&surface->subsurface_list);
610 wl_list_init(&surface->subsurface_list_pending);
611
Jason Ekstrand1e059042014-10-16 10:55:19 -0500612 weston_matrix_init(&surface->buffer_to_surface_matrix);
613 weston_matrix_init(&surface->surface_to_buffer_matrix);
614
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400615 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500616}
617
Alex Wu8811bf92012-02-28 18:07:54 +0800618WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500619weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200620 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500621{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100622 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500623}
624
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400625WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500626weston_view_to_global_float(struct weston_view *view,
627 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200628{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500629 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200630 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
631
Jason Ekstranda7af7042013-10-12 22:38:11 -0500632 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200633
634 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200635 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700636 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200637 v.f[3]);
638 *x = 0;
639 *y = 0;
640 return;
641 }
642
643 *x = v.f[0] / v.f[3];
644 *y = v.f[1] / v.f[3];
645 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500646 *x = sx + view->geometry.x;
647 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200648 }
649}
650
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500651WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200652weston_transformed_coord(int width, int height,
653 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200654 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200655 float sx, float sy, float *bx, float *by)
656{
657 switch (transform) {
658 case WL_OUTPUT_TRANSFORM_NORMAL:
659 default:
660 *bx = sx;
661 *by = sy;
662 break;
663 case WL_OUTPUT_TRANSFORM_FLIPPED:
664 *bx = width - sx;
665 *by = sy;
666 break;
667 case WL_OUTPUT_TRANSFORM_90:
668 *bx = height - sy;
669 *by = sx;
670 break;
671 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
672 *bx = height - sy;
673 *by = width - sx;
674 break;
675 case WL_OUTPUT_TRANSFORM_180:
676 *bx = width - sx;
677 *by = height - sy;
678 break;
679 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
680 *bx = sx;
681 *by = height - sy;
682 break;
683 case WL_OUTPUT_TRANSFORM_270:
684 *bx = sy;
685 *by = width - sx;
686 break;
687 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
688 *bx = sy;
689 *by = sx;
690 break;
691 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200692
693 *bx *= scale;
694 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200695}
696
697WL_EXPORT pixman_box32_t
698weston_transformed_rect(int width, int height,
699 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200700 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200701 pixman_box32_t rect)
702{
703 float x1, x2, y1, y2;
704
705 pixman_box32_t ret;
706
Alexander Larsson4ea95522013-05-22 14:41:37 +0200707 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200708 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200709 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200710 rect.x2, rect.y2, &x2, &y2);
711
712 if (x1 <= x2) {
713 ret.x1 = x1;
714 ret.x2 = x2;
715 } else {
716 ret.x1 = x2;
717 ret.x2 = x1;
718 }
719
720 if (y1 <= y2) {
721 ret.y1 = y1;
722 ret.y2 = y2;
723 } else {
724 ret.y1 = y2;
725 ret.y2 = y1;
726 }
727
728 return ret;
729}
730
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600731/** Transform a region by a matrix, restricted to axis-aligned transformations
732 *
733 * Warning: This function does not work for projective, affine, or matrices
734 * that encode arbitrary rotations. Only 90-degree step rotations are
735 * supported.
736 */
737WL_EXPORT void
738weston_matrix_transform_region(pixman_region32_t *dest,
739 struct weston_matrix *matrix,
740 pixman_region32_t *src)
741{
742 pixman_box32_t *src_rects, *dest_rects;
743 int nrects, i;
744
745 src_rects = pixman_region32_rectangles(src, &nrects);
746 dest_rects = malloc(nrects * sizeof(*dest_rects));
747 if (!dest_rects)
748 return;
749
750 for (i = 0; i < nrects; i++) {
751 struct weston_vector vec1 = {{
752 src_rects[i].x1, src_rects[i].y1, 0, 1
753 }};
754 weston_matrix_transform(matrix, &vec1);
755 vec1.f[0] /= vec1.f[3];
756 vec1.f[1] /= vec1.f[3];
757
758 struct weston_vector vec2 = {{
759 src_rects[i].x2, src_rects[i].y2, 0, 1
760 }};
761 weston_matrix_transform(matrix, &vec2);
762 vec2.f[0] /= vec2.f[3];
763 vec2.f[1] /= vec2.f[3];
764
765 if (vec1.f[0] < vec2.f[0]) {
766 dest_rects[i].x1 = floor(vec1.f[0]);
767 dest_rects[i].x2 = ceil(vec2.f[0]);
768 } else {
769 dest_rects[i].x1 = floor(vec2.f[0]);
770 dest_rects[i].x2 = ceil(vec1.f[0]);
771 }
772
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600773 if (vec1.f[1] < vec2.f[1]) {
774 dest_rects[i].y1 = floor(vec1.f[1]);
775 dest_rects[i].y2 = ceil(vec2.f[1]);
776 } else {
777 dest_rects[i].y1 = floor(vec2.f[1]);
778 dest_rects[i].y2 = ceil(vec1.f[1]);
779 }
780 }
781
782 pixman_region32_clear(dest);
783 pixman_region32_init_rects(dest, dest_rects, nrects);
784 free(dest_rects);
785}
786
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200787WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500788weston_transformed_region(int width, int height,
789 enum wl_output_transform transform,
790 int32_t scale,
791 pixman_region32_t *src, pixman_region32_t *dest)
792{
793 pixman_box32_t *src_rects, *dest_rects;
794 int nrects, i;
795
796 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
797 if (src != dest)
798 pixman_region32_copy(dest, src);
799 return;
800 }
801
802 src_rects = pixman_region32_rectangles(src, &nrects);
803 dest_rects = malloc(nrects * sizeof(*dest_rects));
804 if (!dest_rects)
805 return;
806
807 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
808 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
809 } else {
810 for (i = 0; i < nrects; i++) {
811 switch (transform) {
812 default:
813 case WL_OUTPUT_TRANSFORM_NORMAL:
814 dest_rects[i].x1 = src_rects[i].x1;
815 dest_rects[i].y1 = src_rects[i].y1;
816 dest_rects[i].x2 = src_rects[i].x2;
817 dest_rects[i].y2 = src_rects[i].y2;
818 break;
819 case WL_OUTPUT_TRANSFORM_90:
820 dest_rects[i].x1 = height - src_rects[i].y2;
821 dest_rects[i].y1 = src_rects[i].x1;
822 dest_rects[i].x2 = height - src_rects[i].y1;
823 dest_rects[i].y2 = src_rects[i].x2;
824 break;
825 case WL_OUTPUT_TRANSFORM_180:
826 dest_rects[i].x1 = width - src_rects[i].x2;
827 dest_rects[i].y1 = height - src_rects[i].y2;
828 dest_rects[i].x2 = width - src_rects[i].x1;
829 dest_rects[i].y2 = height - src_rects[i].y1;
830 break;
831 case WL_OUTPUT_TRANSFORM_270:
832 dest_rects[i].x1 = src_rects[i].y1;
833 dest_rects[i].y1 = width - src_rects[i].x2;
834 dest_rects[i].x2 = src_rects[i].y2;
835 dest_rects[i].y2 = width - src_rects[i].x1;
836 break;
837 case WL_OUTPUT_TRANSFORM_FLIPPED:
838 dest_rects[i].x1 = width - src_rects[i].x2;
839 dest_rects[i].y1 = src_rects[i].y1;
840 dest_rects[i].x2 = width - src_rects[i].x1;
841 dest_rects[i].y2 = src_rects[i].y2;
842 break;
843 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
844 dest_rects[i].x1 = height - src_rects[i].y2;
845 dest_rects[i].y1 = width - src_rects[i].x2;
846 dest_rects[i].x2 = height - src_rects[i].y1;
847 dest_rects[i].y2 = width - src_rects[i].x1;
848 break;
849 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
850 dest_rects[i].x1 = src_rects[i].x1;
851 dest_rects[i].y1 = height - src_rects[i].y2;
852 dest_rects[i].x2 = src_rects[i].x2;
853 dest_rects[i].y2 = height - src_rects[i].y1;
854 break;
855 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
856 dest_rects[i].x1 = src_rects[i].y1;
857 dest_rects[i].y1 = src_rects[i].x1;
858 dest_rects[i].x2 = src_rects[i].y2;
859 dest_rects[i].y2 = src_rects[i].x2;
860 break;
861 }
862 }
863 }
864
865 if (scale != 1) {
866 for (i = 0; i < nrects; i++) {
867 dest_rects[i].x1 *= scale;
868 dest_rects[i].x2 *= scale;
869 dest_rects[i].y1 *= scale;
870 dest_rects[i].y2 *= scale;
871 }
872 }
873
874 pixman_region32_clear(dest);
875 pixman_region32_init_rects(dest, dest_rects, nrects);
876 free(dest_rects);
877}
878
Jonny Lamb74130762013-11-26 18:19:46 +0100879static void
880scaler_surface_to_buffer(struct weston_surface *surface,
881 float sx, float sy, float *bx, float *by)
882{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200883 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200884 double src_width, src_height;
885 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200886
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200887 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
888 if (vp->surface.width == -1) {
889 *bx = sx;
890 *by = sy;
891 return;
892 }
Jonny Lamb74130762013-11-26 18:19:46 +0100893
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200894 src_x = 0.0;
895 src_y = 0.0;
896 src_width = surface->width_from_buffer;
897 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100898 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200899 src_x = wl_fixed_to_double(vp->buffer.src_x);
900 src_y = wl_fixed_to_double(vp->buffer.src_y);
901 src_width = wl_fixed_to_double(vp->buffer.src_width);
902 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100903 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200904
905 *bx = sx * src_width / surface->width + src_x;
906 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100907}
908
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500909WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200910weston_surface_to_buffer_float(struct weston_surface *surface,
911 float sx, float sy, float *bx, float *by)
912{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200913 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
914
Jonny Lamb74130762013-11-26 18:19:46 +0100915 /* first transform coordinates if the scaler is set */
916 scaler_surface_to_buffer(surface, sx, sy, bx, by);
917
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500918 weston_transformed_coord(surface->width_from_buffer,
919 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200920 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100921 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200922}
923
Derek Foreman99739672015-12-03 16:38:11 -0600924/** Transform a rectangle from surface coordinates to buffer coordinates
925 *
926 * \param surface The surface to fetch wl_viewport and buffer transformation
927 * from.
928 * \param rect The rectangle to transform.
929 * \return The transformed rectangle.
930 *
931 * Viewport and buffer transformations can only do translation, scaling,
932 * and rotations in 90-degree steps. Therefore the only loss in the
933 * conversion is coordinate rounding.
934 *
935 * However, some coordinate rounding takes place as an intermediate
936 * step before the buffer scale factor is applied, so the rectangle
937 * boundary may not be exactly as expected.
938 *
939 * This is OK for damage tracking since a little extra coverage is
940 * not a problem.
941 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200942WL_EXPORT pixman_box32_t
943weston_surface_to_buffer_rect(struct weston_surface *surface,
944 pixman_box32_t rect)
945{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200946 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100947 float xf, yf;
948
949 /* first transform box coordinates if the scaler is set */
950 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
951 rect.x1 = floorf(xf);
952 rect.y1 = floorf(yf);
953
954 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600955 rect.x2 = ceilf(xf);
956 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100957
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500958 return weston_transformed_rect(surface->width_from_buffer,
959 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200960 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200961 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200962}
963
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200964/** Transform a region from surface coordinates to buffer coordinates
965 *
966 * \param surface The surface to fetch wl_viewport and buffer transformation
967 * from.
968 * \param surface_region[in] The region in surface coordinates.
969 * \param buffer_region[out] The region converted to buffer coordinates.
970 *
971 * Buffer_region must be init'd, but will be completely overwritten.
972 *
973 * Viewport and buffer transformations can only do translation, scaling,
974 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600975 * conversion is from the coordinate rounding that takes place in
976 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200977 */
978WL_EXPORT void
979weston_surface_to_buffer_region(struct weston_surface *surface,
980 pixman_region32_t *surface_region,
981 pixman_region32_t *buffer_region)
982{
983 pixman_box32_t *src_rects, *dest_rects;
984 int nrects, i;
985
986 src_rects = pixman_region32_rectangles(surface_region, &nrects);
987 dest_rects = malloc(nrects * sizeof(*dest_rects));
988 if (!dest_rects)
989 return;
990
991 for (i = 0; i < nrects; i++) {
992 dest_rects[i] = weston_surface_to_buffer_rect(surface,
993 src_rects[i]);
994 }
995
996 pixman_region32_fini(buffer_region);
997 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
998 free(dest_rects);
999}
1000
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001001WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001003 struct weston_plane *plane)
1004{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001006 return;
1007
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 weston_view_damage_below(view);
1009 view->plane = plane;
1010 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001011}
1012
Pekka Paalanen51723d52015-02-17 13:10:01 +02001013/** Inflict damage on the plane where the view is visible.
1014 *
1015 * \param view The view that causes the damage.
1016 *
1017 * If the view is currently on a plane (including the primary plane),
1018 * take the view's boundingbox, subtract all the opaque views that cover it,
1019 * and add the remaining region as damage to the plane. This corresponds
1020 * to the damage inflicted to the plane if this view disappeared.
1021 *
1022 * A repaint is scheduled for this view.
1023 *
1024 * The region of all opaque views covering this view is stored in
1025 * weston_view::clip and updated by view_accumulate_damage() during
1026 * weston_output_repaint(). Specifically, that region matches the
1027 * scenegraph as it was last painted.
1028 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001029WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001030weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001031{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001032 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001033
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001034 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001035 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001037 if (view->plane)
1038 pixman_region32_union(&view->plane->damage,
1039 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001040 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001041 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001042}
1043
Bryce Harrington3f650b82015-12-23 11:01:58 -08001044/**
1045 * \param es The surface
1046 * \param mask The new set of outputs for the surface
1047 *
1048 * Sets the surface's set of outputs to the ones specified by
1049 * the new output mask provided. Identifies the outputs that
1050 * have changed, the posts enter and leave events for these
1051 * outputs as appropriate.
1052 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001053static void
1054weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1055{
1056 uint32_t different = es->output_mask ^ mask;
1057 uint32_t entered = mask & different;
1058 uint32_t left = es->output_mask & different;
1059 struct weston_output *output;
1060 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001061 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001062
1063 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001064 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001065 return;
1066 if (different == 0)
1067 return;
1068
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001069 client = wl_resource_get_client(es->resource);
1070
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001071 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08001072 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001073 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001074 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001075 client);
1076 if (resource == NULL)
1077 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -08001078 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001079 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -08001080 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001081 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001082 }
1083}
1084
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001085
Bryce Harrington3f650b82015-12-23 11:01:58 -08001086/** Recalculate which output(s) the surface has views displayed on
1087 *
1088 * \param es The surface to remap to outputs
1089 *
1090 * Finds the output that is showing the largest amount of one
1091 * of the surface's various views. This output becomes the
1092 * surface's primary output for vsync and frame event purposes.
1093 *
1094 * Also notes the primary outputs of all of the surface's views
1095 * in the output_mask for the surface.
1096 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001097static void
1098weston_surface_assign_output(struct weston_surface *es)
1099{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100 struct weston_output *new_output;
1101 struct weston_view *view;
1102 pixman_region32_t region;
1103 uint32_t max, area, mask;
1104 pixman_box32_t *e;
1105
1106 new_output = NULL;
1107 max = 0;
1108 mask = 0;
1109 pixman_region32_init(&region);
1110 wl_list_for_each(view, &es->views, surface_link) {
1111 if (!view->output)
1112 continue;
1113
1114 pixman_region32_intersect(&region, &view->transform.boundingbox,
1115 &view->output->region);
1116
1117 e = pixman_region32_extents(&region);
1118 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1119
1120 mask |= view->output_mask;
1121
1122 if (area >= max) {
1123 new_output = view->output;
1124 max = area;
1125 }
1126 }
1127 pixman_region32_fini(&region);
1128
1129 es->output = new_output;
1130 weston_surface_update_output_mask(es, mask);
1131}
1132
Bryce Harrington3f650b82015-12-23 11:01:58 -08001133/** Recalculate which output(s) the view is displayed on
1134 *
1135 * \param ev The view to remap to outputs
1136 *
1137 * Identifies the set of outputs that the view is visible on,
1138 * noting them into the output_mask. The output that the view
1139 * is most visible on is set as the view's primary output for
1140 * vsync and frame event purposes.
1141 *
1142 * Also does the same for the view's surface. See
1143 * weston_surface_assign_output().
1144 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145static void
1146weston_view_assign_output(struct weston_view *ev)
1147{
1148 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001149 struct weston_output *output, *new_output;
1150 pixman_region32_t region;
1151 uint32_t max, area, mask;
1152 pixman_box32_t *e;
1153
1154 new_output = NULL;
1155 max = 0;
1156 mask = 0;
1157 pixman_region32_init(&region);
1158 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001159 if (output->destroying)
1160 continue;
1161
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001163 &output->region);
1164
1165 e = pixman_region32_extents(&region);
1166 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1167
1168 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001169 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001170
1171 if (area >= max) {
1172 new_output = output;
1173 max = area;
1174 }
1175 }
1176 pixman_region32_fini(&region);
1177
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 ev->output = new_output;
1179 ev->output_mask = mask;
1180
1181 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001182}
1183
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001184static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001185weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1186 int from_x, int from_y, int *to_x, int *to_y)
1187{
1188 float x, y;
1189
1190 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1191 weston_view_from_global_float(to, x, y, &x, &y);
1192
1193 *to_x = round(x);
1194 *to_y = round(y);
1195}
1196
1197static void
1198weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1199{
1200 pixman_box32_t *a;
1201 pixman_box32_t b;
1202
1203 a = pixman_region32_extents(&from->geometry.scissor);
1204
1205 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1206 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1207
1208 pixman_region32_fini(&to->geometry.scissor);
1209 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1210}
1211
1212static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001213view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001215{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001216 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1217 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001218 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001219 { inbox->x1, inbox->y1 },
1220 { inbox->x1, inbox->y2 },
1221 { inbox->x2, inbox->y1 },
1222 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001223 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001224 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001225 int i;
1226
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001227 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001228 /* avoid rounding empty bbox to 1x1 */
1229 pixman_region32_init(bbox);
1230 return;
1231 }
1232
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001233 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001234 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001236 if (x < min_x)
1237 min_x = x;
1238 if (x > max_x)
1239 max_x = x;
1240 if (y < min_y)
1241 min_y = y;
1242 if (y > max_y)
1243 max_y = y;
1244 }
1245
Pekka Paalanen219b9822012-02-08 15:38:37 +02001246 int_x = floorf(min_x);
1247 int_y = floorf(min_y);
1248 pixman_region32_init_rect(bbox, int_x, int_y,
1249 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001250}
1251
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001252static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001254{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001256
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001257 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 view->geometry.x = roundf(view->geometry.x);
1259 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001260
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001261 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1263 view->transform.position.matrix.d[12] = view->geometry.x;
1264 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001265
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001267
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 view->transform.inverse = view->transform.position.matrix;
1269 view->transform.inverse.d[12] = -view->geometry.x;
1270 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001271
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001273 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001274 view->surface->width,
1275 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001276 if (view->geometry.scissor_enabled)
1277 pixman_region32_intersect(&view->transform.boundingbox,
1278 &view->transform.boundingbox,
1279 &view->geometry.scissor);
1280
1281 pixman_region32_translate(&view->transform.boundingbox,
1282 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001283
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 if (view->alpha == 1.0) {
1285 pixman_region32_copy(&view->transform.opaque,
1286 &view->surface->opaque);
1287 pixman_region32_translate(&view->transform.opaque,
1288 view->geometry.x,
1289 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001290 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001291}
1292
1293static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001295{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001296 struct weston_view *parent = view->geometry.parent;
1297 struct weston_matrix *matrix = &view->transform.matrix;
1298 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001299 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001300 pixman_region32_t surfregion;
1301 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001302
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001304
1305 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1307 view->transform.position.matrix.d[12] = view->geometry.x;
1308 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001309
1310 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001312 weston_matrix_multiply(matrix, &tform->matrix);
1313
Pekka Paalanen483243f2013-03-08 14:56:50 +02001314 if (parent)
1315 weston_matrix_multiply(matrix, &parent->transform.matrix);
1316
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001317 if (weston_matrix_invert(inverse, matrix) < 0) {
1318 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 weston_log("error: weston_view %p"
1320 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001321 return -1;
1322 }
1323
Pekka Paalanen380adf52015-02-16 14:39:11 +02001324 pixman_region32_init_rect(&surfregion, 0, 0,
1325 view->surface->width, view->surface->height);
1326 if (view->geometry.scissor_enabled)
1327 pixman_region32_intersect(&surfregion, &surfregion,
1328 &view->geometry.scissor);
1329 surfbox = pixman_region32_extents(&surfregion);
1330
1331 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1332 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001333
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001334 return 0;
1335}
1336
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001337static struct weston_layer *
1338get_view_layer(struct weston_view *view)
1339{
1340 if (view->parent_view)
1341 return get_view_layer(view->parent_view);
1342 return view->layer_link.layer;
1343}
1344
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001345WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001347{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001349 struct weston_layer *layer;
1350 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001351
Jason Ekstranda7af7042013-10-12 22:38:11 -05001352 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001353 return;
1354
Pekka Paalanen483243f2013-03-08 14:56:50 +02001355 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001356 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001357
Jason Ekstranda7af7042013-10-12 22:38:11 -05001358 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001359
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001361
Jason Ekstranda7af7042013-10-12 22:38:11 -05001362 pixman_region32_fini(&view->transform.boundingbox);
1363 pixman_region32_fini(&view->transform.opaque);
1364 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001365
Pekka Paalanencd403622012-01-25 13:37:39 +02001366 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 if (view->geometry.transformation_list.next ==
1368 &view->transform.position.link &&
1369 view->geometry.transformation_list.prev ==
1370 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001371 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001373 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374 if (weston_view_update_transform_enable(view) < 0)
1375 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001376 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001377
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001378 layer = get_view_layer(view);
1379 if (layer) {
1380 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001381 pixman_region32_intersect(&view->transform.boundingbox,
1382 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001383 pixman_region32_intersect(&view->transform.opaque,
1384 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001385 pixman_region32_fini(&mask);
1386 }
1387
Pekka Paalanen380adf52015-02-16 14:39:11 +02001388 if (parent) {
1389 if (parent->geometry.scissor_enabled) {
1390 view->geometry.scissor_enabled = true;
1391 weston_view_transfer_scissor(parent, view);
1392 } else {
1393 view->geometry.scissor_enabled = false;
1394 }
1395 }
1396
Jason Ekstranda7af7042013-10-12 22:38:11 -05001397 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001398
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001400
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 wl_signal_emit(&view->surface->compositor->transform_signal,
1402 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001403}
1404
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001405WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001407{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001409
1410 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 * The invariant: if view->geometry.dirty, then all views
1412 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001413 * Corollary: if not parent->geometry.dirty, then all ancestors
1414 * are not dirty.
1415 */
1416
Jason Ekstranda7af7042013-10-12 22:38:11 -05001417 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001418 return;
1419
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001421
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001423 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001425}
1426
1427WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428weston_view_to_global_fixed(struct weston_view *view,
1429 wl_fixed_t vx, wl_fixed_t vy,
1430 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001431{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001432 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001433
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 weston_view_to_global_float(view,
1435 wl_fixed_to_double(vx),
1436 wl_fixed_to_double(vy),
1437 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001438 *x = wl_fixed_from_double(xf);
1439 *y = wl_fixed_from_double(yf);
1440}
1441
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001442WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001443weston_view_from_global_float(struct weston_view *view,
1444 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001445{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001447 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1448
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001450
1451 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001452 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001454 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 *vx = 0;
1456 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001457 return;
1458 }
1459
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460 *vx = v.f[0] / v.f[3];
1461 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001462 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 *vx = x - view->geometry.x;
1464 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001465 }
1466}
1467
1468WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469weston_view_from_global_fixed(struct weston_view *view,
1470 wl_fixed_t x, wl_fixed_t y,
1471 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001472{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001474
Jason Ekstranda7af7042013-10-12 22:38:11 -05001475 weston_view_from_global_float(view,
1476 wl_fixed_to_double(x),
1477 wl_fixed_to_double(y),
1478 &vxf, &vyf);
1479 *vx = wl_fixed_from_double(vxf);
1480 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001481}
1482
1483WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001484weston_view_from_global(struct weston_view *view,
1485 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001486{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001487 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001488
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1490 *vx = floorf(vxf);
1491 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001492}
1493
Bryce Harrington3f650b82015-12-23 11:01:58 -08001494/**
1495 * \param surface The surface to be repainted
1496 *
1497 * Marks the output(s) that the surface is shown on as needing to be
1498 * repainted. See weston_output_schedule_repaint().
1499 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001500WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001501weston_surface_schedule_repaint(struct weston_surface *surface)
1502{
1503 struct weston_output *output;
1504
1505 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001506 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001507 weston_output_schedule_repaint(output);
1508}
1509
Bryce Harrington3f650b82015-12-23 11:01:58 -08001510/**
1511 * \param view The view to be repainted
1512 *
1513 * Marks the output(s) that the view is shown on as needing to be
1514 * repainted. See weston_output_schedule_repaint().
1515 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001516WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001517weston_view_schedule_repaint(struct weston_view *view)
1518{
1519 struct weston_output *output;
1520
1521 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001522 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 weston_output_schedule_repaint(output);
1524}
1525
Pekka Paalanene508ce62015-02-19 13:59:55 +02001526/**
1527 * XXX: This function does it the wrong way.
1528 * surface->damage is the damage from the client, and causes
1529 * surface_flush_damage() to copy pixels. No window management action can
1530 * cause damage to the client-provided content, warranting re-upload!
1531 *
1532 * Instead of surface->damage, this function should record the damage
1533 * with all the views for this surface to avoid extraneous texture
1534 * uploads.
1535 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001536WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001537weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001538{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001539 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001540 0, 0, surface->width,
1541 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001542
Kristian Høgsberg98238702012-08-03 16:29:12 -04001543 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001544}
1545
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001546WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001548{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001549 if (view->geometry.x == x && view->geometry.y == y)
1550 return;
1551
Jason Ekstranda7af7042013-10-12 22:38:11 -05001552 view->geometry.x = x;
1553 view->geometry.y = y;
1554 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001555}
1556
Pekka Paalanen483243f2013-03-08 14:56:50 +02001557static void
1558transform_parent_handle_parent_destroy(struct wl_listener *listener,
1559 void *data)
1560{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001561 struct weston_view *view =
1562 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001563 geometry.parent_destroy_listener);
1564
Jason Ekstranda7af7042013-10-12 22:38:11 -05001565 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001566}
1567
1568WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001569weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001570 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001571{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572 if (view->geometry.parent) {
1573 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1574 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001575
1576 if (!parent)
1577 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001578 }
1579
Jason Ekstranda7af7042013-10-12 22:38:11 -05001580 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001581
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001583 transform_parent_handle_parent_destroy;
1584 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001585 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001587 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001589 }
1590
Jason Ekstranda7af7042013-10-12 22:38:11 -05001591 weston_view_geometry_dirty(view);
1592}
1593
Pekka Paalanen380adf52015-02-16 14:39:11 +02001594/** Set a clip mask rectangle on a view
1595 *
1596 * \param view The view to set the clip mask on.
1597 * \param x Top-left corner X coordinate of the clip rectangle.
1598 * \param y Top-left corner Y coordinate of the clip rectangle.
1599 * \param width Width of the clip rectangle, non-negative.
1600 * \param height Height of the clip rectangle, non-negative.
1601 *
1602 * A shell may set a clip mask rectangle on a view. Everything outside
1603 * the rectangle is cut away for input and output purposes: it is
1604 * not drawn and cannot be hit by hit-test based input like pointer
1605 * motion or touch-downs. Everything inside the rectangle will behave
1606 * normally. Clients are unaware of clipping.
1607 *
1608 * The rectangle is set in the surface local coordinates. Setting a clip
1609 * mask rectangle does not affect the view position, the view is positioned
1610 * as it would be without a clip. The clip also does not change
1611 * weston_surface::width,height.
1612 *
1613 * The clip mask rectangle is part of transformation inheritance
1614 * (weston_view_set_transform_parent()). A clip set in the root of the
1615 * transformation inheritance tree will affect all views in the tree.
1616 * A clip can be set only on the root view. Attempting to set a clip
1617 * on view that has a transformation parent will fail. Assigning a parent
1618 * to a view that has a clip set will cause the clip to be forgotten.
1619 *
1620 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1621 * on the additional transformations in the child views. These transformations
1622 * may not rotate the coordinate axes, i.e., only translation and scaling
1623 * are allowed. Violating this restriction causes the clipping to malfunction.
1624 * Furthermore, using scaling may cause rounding errors in child clipping.
1625 *
1626 * The clip mask rectangle is not automatically adjusted based on
1627 * wl_surface.attach dx and dy arguments.
1628 *
1629 * A clip mask rectangle can be set only if the compositor capability
1630 * WESTON_CAP_VIEW_CLIP_MASK is present.
1631 *
1632 * This function sets the clip mask rectangle and schedules a repaint for
1633 * the view.
1634 */
1635WL_EXPORT void
1636weston_view_set_mask(struct weston_view *view,
1637 int x, int y, int width, int height)
1638{
1639 struct weston_compositor *compositor = view->surface->compositor;
1640
1641 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1642 weston_log("%s not allowed without capability!\n", __func__);
1643 return;
1644 }
1645
1646 if (view->geometry.parent) {
1647 weston_log("view %p has a parent, clip forbidden!\n", view);
1648 return;
1649 }
1650
1651 if (width < 0 || height < 0) {
1652 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1653 x, y, width, height);
1654 return;
1655 }
1656
1657 pixman_region32_fini(&view->geometry.scissor);
1658 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1659 view->geometry.scissor_enabled = true;
1660 weston_view_geometry_dirty(view);
1661 weston_view_schedule_repaint(view);
1662}
1663
1664/** Remove the clip mask from a view
1665 *
1666 * \param view The view to remove the clip mask from.
1667 *
1668 * Removed the clip mask rectangle and schedules a repaint.
1669 *
1670 * \sa weston_view_set_mask
1671 */
1672WL_EXPORT void
1673weston_view_set_mask_infinite(struct weston_view *view)
1674{
1675 view->geometry.scissor_enabled = false;
1676 weston_view_geometry_dirty(view);
1677 weston_view_schedule_repaint(view);
1678}
1679
Derek Foreman280e7dd2014-10-03 13:13:42 -05001680WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001681weston_view_is_mapped(struct weston_view *view)
1682{
1683 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001684 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001685 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001686 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001687}
1688
Derek Foreman280e7dd2014-10-03 13:13:42 -05001689WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001690weston_surface_is_mapped(struct weston_surface *surface)
1691{
1692 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001693 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001694 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001695 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001696}
1697
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001698static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001699surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001700{
1701 struct weston_view *view;
1702
1703 if (surface->width == width && surface->height == height)
1704 return;
1705
1706 surface->width = width;
1707 surface->height = height;
1708
1709 wl_list_for_each(view, &surface->views, surface_link)
1710 weston_view_geometry_dirty(view);
1711}
1712
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001713WL_EXPORT void
1714weston_surface_set_size(struct weston_surface *surface,
1715 int32_t width, int32_t height)
1716{
1717 assert(!surface->resource);
1718 surface_set_size(surface, width, height);
1719}
1720
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001721static int
1722fixed_round_up_to_int(wl_fixed_t f)
1723{
1724 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1725}
1726
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001727static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001728weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001729{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001730 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001731 int32_t width, height;
1732
1733 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001734 surface->width_from_buffer = 0;
1735 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001736 return;
1737 }
1738
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001739 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001740 case WL_OUTPUT_TRANSFORM_90:
1741 case WL_OUTPUT_TRANSFORM_270:
1742 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1743 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001744 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1745 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001746 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001747 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001748 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1749 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001750 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001751 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001752
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001753 surface->width_from_buffer = width;
1754 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001755}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001756
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001757static void
1758weston_surface_update_size(struct weston_surface *surface)
1759{
1760 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1761 int32_t width, height;
1762
1763 width = surface->width_from_buffer;
1764 height = surface->height_from_buffer;
1765
1766 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001767 surface_set_size(surface,
1768 vp->surface.width, vp->surface.height);
1769 return;
1770 }
1771
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001772 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001773 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1774 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1775
1776 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001777 return;
1778 }
1779
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001780 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001781}
1782
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001783WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001784weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001785{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001786 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001787
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001788 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001789
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001790 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001791}
1792
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793WL_EXPORT struct weston_view *
1794weston_compositor_pick_view(struct weston_compositor *compositor,
1795 wl_fixed_t x, wl_fixed_t y,
1796 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001797{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001798 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001799 wl_fixed_t view_x, view_y;
1800 int view_ix, view_iy;
1801 int ix = wl_fixed_to_int(x);
1802 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001803
Jason Ekstranda7af7042013-10-12 22:38:11 -05001804 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001805 if (!pixman_region32_contains_point(
1806 &view->transform.boundingbox, ix, iy, NULL))
1807 continue;
1808
1809 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1810 view_ix = wl_fixed_to_int(view_x);
1811 view_iy = wl_fixed_to_int(view_y);
1812
1813 if (!pixman_region32_contains_point(&view->surface->input,
1814 view_ix, view_iy, NULL))
1815 continue;
1816
Pekka Paalanen380adf52015-02-16 14:39:11 +02001817 if (view->geometry.scissor_enabled &&
1818 !pixman_region32_contains_point(&view->geometry.scissor,
1819 view_ix, view_iy, NULL))
1820 continue;
1821
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001822 *vx = view_x;
1823 *vy = view_y;
1824 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001825 }
1826
Derek Foremanf9318d12015-05-11 15:40:11 -05001827 *vx = wl_fixed_from_int(-1000000);
1828 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001829 return NULL;
1830}
1831
1832static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001833weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001834{
Daniel Stone37816df2012-05-16 18:45:18 +01001835 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001836
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001837 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001838 return;
1839
Daniel Stone37816df2012-05-16 18:45:18 +01001840 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001841 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001842}
1843
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001844WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001845weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001846{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001847 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001848
Jason Ekstranda7af7042013-10-12 22:38:11 -05001849 if (!weston_view_is_mapped(view))
1850 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001851
Jason Ekstranda7af7042013-10-12 22:38:11 -05001852 weston_view_damage_below(view);
1853 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001854 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001855 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001856 wl_list_remove(&view->link);
1857 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001858 view->output_mask = 0;
1859 weston_surface_assign_output(view->surface);
1860
1861 if (weston_surface_is_mapped(view->surface))
1862 return;
1863
1864 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001865 struct weston_touch *touch = weston_seat_get_touch(seat);
1866 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1867 struct weston_keyboard *keyboard =
1868 weston_seat_get_keyboard(seat);
1869
1870 if (keyboard && keyboard->focus == view->surface)
1871 weston_keyboard_set_focus(keyboard, NULL);
1872 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001873 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001874 if (touch && touch->focus == view)
1875 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001876 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001877}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001878
Jason Ekstranda7af7042013-10-12 22:38:11 -05001879WL_EXPORT void
1880weston_surface_unmap(struct weston_surface *surface)
1881{
1882 struct weston_view *view;
1883
1884 wl_list_for_each(view, &surface->views, surface_link)
1885 weston_view_unmap(view);
1886 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001887}
1888
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001889static void
1890weston_surface_reset_pending_buffer(struct weston_surface *surface)
1891{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001892 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001893 surface->pending.sx = 0;
1894 surface->pending.sy = 0;
1895 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001896 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001897}
1898
Jason Ekstranda7af7042013-10-12 22:38:11 -05001899WL_EXPORT void
1900weston_view_destroy(struct weston_view *view)
1901{
1902 wl_signal_emit(&view->destroy_signal, view);
1903
1904 assert(wl_list_empty(&view->geometry.child_list));
1905
1906 if (weston_view_is_mapped(view)) {
1907 weston_view_unmap(view);
1908 weston_compositor_build_view_list(view->surface->compositor);
1909 }
1910
1911 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001912 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001913
1914 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001915 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001916 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001917 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001918
1919 weston_view_set_transform_parent(view, NULL);
1920
Jason Ekstranda7af7042013-10-12 22:38:11 -05001921 wl_list_remove(&view->surface_link);
1922
1923 free(view);
1924}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001925
1926WL_EXPORT void
1927weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001928{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001929 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001930 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001931
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001932 if (--surface->ref_count > 0)
1933 return;
1934
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001935 assert(surface->resource == NULL);
1936
Pekka Paalanenca790762015-04-17 14:23:38 +03001937 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001938
Pekka Paalanene67858b2013-04-25 13:57:42 +03001939 assert(wl_list_empty(&surface->subsurface_list_pending));
1940 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001941
Jason Ekstranda7af7042013-10-12 22:38:11 -05001942 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1943 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001944
Jason Ekstrand7b982072014-05-20 14:33:03 -05001945 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001946
Pekka Paalanende685b82012-12-04 15:58:12 +02001947 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001948
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001949 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001950 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001951 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001952
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001953 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001954 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001955
Pekka Paalanen133e4392014-09-23 22:08:46 -04001956 weston_presentation_feedback_discard_list(&surface->feedback_list);
1957
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001958 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001959}
1960
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001961static void
1962destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001963{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001964 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001965
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001966 assert(surface);
1967
Giulio Camuffo0d379742013-11-15 22:06:15 +01001968 /* Set the resource to NULL, since we don't want to leave a
1969 * dangling pointer if the surface was refcounted and survives
1970 * the weston_surface_destroy() call. */
1971 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001972 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001973}
1974
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001975static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001976weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1977{
1978 struct weston_buffer *buffer =
1979 container_of(listener, struct weston_buffer, destroy_listener);
1980
1981 wl_signal_emit(&buffer->destroy_signal, buffer);
1982 free(buffer);
1983}
1984
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001985WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001986weston_buffer_from_resource(struct wl_resource *resource)
1987{
1988 struct weston_buffer *buffer;
1989 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001990
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001991 listener = wl_resource_get_destroy_listener(resource,
1992 weston_buffer_destroy_handler);
1993
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001994 if (listener)
1995 return container_of(listener, struct weston_buffer,
1996 destroy_listener);
1997
1998 buffer = zalloc(sizeof *buffer);
1999 if (buffer == NULL)
2000 return NULL;
2001
2002 buffer->resource = resource;
2003 wl_signal_init(&buffer->destroy_signal);
2004 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002005 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002006 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002007
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002008 return buffer;
2009}
2010
2011static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002012weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2013 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002014{
Pekka Paalanende685b82012-12-04 15:58:12 +02002015 struct weston_buffer_reference *ref =
2016 container_of(listener, struct weston_buffer_reference,
2017 destroy_listener);
2018
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002019 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002020 ref->buffer = NULL;
2021}
2022
2023WL_EXPORT void
2024weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002025 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002026{
2027 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002028 ref->buffer->busy_count--;
2029 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002030 assert(wl_resource_get_client(ref->buffer->resource));
2031 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05002032 WL_BUFFER_RELEASE);
2033 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002034 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002035 }
2036
Pekka Paalanende685b82012-12-04 15:58:12 +02002037 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002038 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002039 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002040 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002041 }
2042
Pekka Paalanende685b82012-12-04 15:58:12 +02002043 ref->buffer = buffer;
2044 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2045}
2046
2047static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002048weston_surface_attach(struct weston_surface *surface,
2049 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002050{
2051 weston_buffer_reference(&surface->buffer_ref, buffer);
2052
Pekka Paalanena6421c42012-12-04 15:58:10 +02002053 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002054 if (weston_surface_is_mapped(surface))
2055 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002056 }
2057
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002058 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002059
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002060 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002061 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002062}
2063
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002064WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002065weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002066{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002067 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002068
2069 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002070 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002071}
2072
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002073WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002074weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002075{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002076 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002077
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002078 pixman_region32_union(&compositor->primary_plane.damage,
2079 &compositor->primary_plane.damage,
2080 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002081 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002082}
2083
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002084static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002085surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002086{
Pekka Paalanende685b82012-12-04 15:58:12 +02002087 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002088 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002089 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002090
Pekka Paalanenb5026542014-11-12 15:09:24 +02002091 if (weston_timeline_enabled_ &&
2092 pixman_region32_not_empty(&surface->damage))
2093 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2094 TLP_OUTPUT(surface->output), TLP_END);
2095
Jason Ekstrandef540082014-06-26 10:37:36 -07002096 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002097}
2098
2099static void
2100view_accumulate_damage(struct weston_view *view,
2101 pixman_region32_t *opaque)
2102{
2103 pixman_region32_t damage;
2104
2105 pixman_region32_init(&damage);
2106 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002107 pixman_box32_t *extents;
2108
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002110 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002111 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112 pixman_region32_copy(&damage, &view->surface->damage);
2113 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002114 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002115 }
2116
Pekka Paalanen380adf52015-02-16 14:39:11 +02002117 pixman_region32_intersect(&damage, &damage,
2118 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002119 pixman_region32_subtract(&damage, &damage, opaque);
2120 pixman_region32_union(&view->plane->damage,
2121 &view->plane->damage, &damage);
2122 pixman_region32_fini(&damage);
2123 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002124 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002125}
2126
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002127static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002128compositor_accumulate_damage(struct weston_compositor *ec)
2129{
2130 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002132 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002133
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002134 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002135
2136 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002137 pixman_region32_copy(&plane->clip, &clip);
2138
2139 pixman_region32_init(&opaque);
2140
Jason Ekstranda7af7042013-10-12 22:38:11 -05002141 wl_list_for_each(ev, &ec->view_list, link) {
2142 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002143 continue;
2144
Jason Ekstranda7af7042013-10-12 22:38:11 -05002145 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002146 }
2147
2148 pixman_region32_union(&clip, &clip, &opaque);
2149 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002150 }
2151
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002152 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002153
Jason Ekstranda7af7042013-10-12 22:38:11 -05002154 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002155 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002156
2157 wl_list_for_each(ev, &ec->view_list, link) {
2158 if (ev->surface->touched)
2159 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002160 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161
2162 surface_flush_damage(ev->surface);
2163
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002164 /* Both the renderer and the backend have seen the buffer
2165 * by now. If renderer needs the buffer, it has its own
2166 * reference set. If the backend wants to keep the buffer
2167 * around for migrating the surface into a non-primary plane
2168 * later, keep_buffer is true. Otherwise, drop the core
2169 * reference now, and allow early buffer release. This enables
2170 * clients to use single-buffering.
2171 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002172 if (!ev->surface->keep_buffer)
2173 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002174 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002175}
2176
2177static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002179{
2180 struct weston_subsurface *sub;
2181
Pekka Paalanene67858b2013-04-25 13:57:42 +03002182 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002183 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002184 continue;
2185
Jason Ekstranda7af7042013-10-12 22:38:11 -05002186 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2187 wl_list_init(&sub->surface->views);
2188
2189 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002190 }
2191}
2192
2193static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002194surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002195{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002196 struct weston_subsurface *sub;
2197 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002198
Jason Ekstranda7af7042013-10-12 22:38:11 -05002199 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2200 if (sub->surface == surface)
2201 continue;
2202
George Kiagiadakised04d382014-06-13 18:10:26 +02002203 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2204 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002205 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002206 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002207
2208 surface_free_unused_subsurface_views(sub->surface);
2209 }
2210}
2211
2212static void
2213view_list_add_subsurface_view(struct weston_compositor *compositor,
2214 struct weston_subsurface *sub,
2215 struct weston_view *parent)
2216{
2217 struct weston_subsurface *child;
2218 struct weston_view *view = NULL, *iv;
2219
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002220 if (!weston_surface_is_mapped(sub->surface))
2221 return;
2222
Jason Ekstranda7af7042013-10-12 22:38:11 -05002223 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2224 if (iv->geometry.parent == parent) {
2225 view = iv;
2226 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002227 }
2228 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002229
2230 if (view) {
2231 /* Put it back in the surface's list of views */
2232 wl_list_remove(&view->surface_link);
2233 wl_list_insert(&sub->surface->views, &view->surface_link);
2234 } else {
2235 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002236 weston_view_set_position(view,
2237 sub->position.x,
2238 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002239 weston_view_set_transform_parent(view, parent);
2240 }
2241
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002242 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002243 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002244
Pekka Paalanenb188e912013-11-19 14:03:35 +02002245 if (wl_list_empty(&sub->surface->subsurface_list)) {
2246 wl_list_insert(compositor->view_list.prev, &view->link);
2247 return;
2248 }
2249
2250 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2251 if (child->surface == sub->surface)
2252 wl_list_insert(compositor->view_list.prev, &view->link);
2253 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002254 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002255 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002256}
2257
2258static void
2259view_list_add(struct weston_compositor *compositor,
2260 struct weston_view *view)
2261{
2262 struct weston_subsurface *sub;
2263
2264 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002265
Pekka Paalanenb188e912013-11-19 14:03:35 +02002266 if (wl_list_empty(&view->surface->subsurface_list)) {
2267 wl_list_insert(compositor->view_list.prev, &view->link);
2268 return;
2269 }
2270
2271 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2272 if (sub->surface == view->surface)
2273 wl_list_insert(compositor->view_list.prev, &view->link);
2274 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002275 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002276 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002277}
2278
2279static void
2280weston_compositor_build_view_list(struct weston_compositor *compositor)
2281{
2282 struct weston_view *view;
2283 struct weston_layer *layer;
2284
2285 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002286 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002287 surface_stash_subsurface_views(view->surface);
2288
2289 wl_list_init(&compositor->view_list);
2290 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002291 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002292 view_list_add(compositor, view);
2293 }
2294 }
2295
2296 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002297 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002298 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002299}
2300
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002301static void
2302weston_output_take_feedback_list(struct weston_output *output,
2303 struct weston_surface *surface)
2304{
2305 struct weston_view *view;
2306 struct weston_presentation_feedback *feedback;
2307 uint32_t flags = 0xffffffff;
2308
2309 if (wl_list_empty(&surface->feedback_list))
2310 return;
2311
2312 /* All views must have the flag for the flag to survive. */
2313 wl_list_for_each(view, &surface->views, surface_link) {
2314 /* ignore views that are not on this output at all */
2315 if (view->output_mask & (1u << output->id))
2316 flags &= view->psf_flags;
2317 }
2318
2319 wl_list_for_each(feedback, &surface->feedback_list, link)
2320 feedback->psf_flags = flags;
2321
2322 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2323 wl_list_init(&surface->feedback_list);
2324}
2325
David Herrmann1edf44c2013-10-22 17:11:26 +02002326static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002327weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002328{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002329 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002330 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002331 struct weston_animation *animation, *next;
2332 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002333 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002334 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002335 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002336
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002337 if (output->destroying)
2338 return 0;
2339
Pekka Paalanenb5026542014-11-12 15:09:24 +02002340 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2341
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002342 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002343 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002344
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002345 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002346 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002347 } else {
2348 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002349 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002350 ev->psf_flags = 0;
2351 }
2352 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002353
Pekka Paalanene67858b2013-04-25 13:57:42 +03002354 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002355 wl_list_for_each(ev, &ec->view_list, link) {
2356 /* Note: This operation is safe to do multiple times on the
2357 * same surface.
2358 */
2359 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002360 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002361 &ev->surface->frame_callback_list);
2362 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002363
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002364 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002365 }
2366 }
2367
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002368 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002369
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002370 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002371 pixman_region32_intersect(&output_damage,
2372 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002373 pixman_region32_subtract(&output_damage,
2374 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002375
Scott Moreauccbf29d2012-02-22 14:21:41 -07002376 if (output->dirty)
2377 weston_output_update_matrix(output);
2378
David Herrmann1edf44c2013-10-22 17:11:26 +02002379 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002380
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002381 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002382
Kristian Høgsbergef044142011-06-21 15:02:12 -04002383 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002384
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002385 weston_compositor_repick(ec);
2386 wl_event_loop_dispatch(ec->input_loop, 0);
2387
Jonas Ådahldb773762012-06-13 00:01:21 +02002388 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002389 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002390 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002391 }
2392
Scott Moreaud64cf212012-06-08 19:40:54 -06002393 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002394 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002395 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002396 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002397
Pekka Paalanenb5026542014-11-12 15:09:24 +02002398 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2399
David Herrmann1edf44c2013-10-22 17:11:26 +02002400 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002401}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002402
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002403static int
2404weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002405{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002406 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002407
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002408 wl_event_loop_dispatch(compositor->input_loop, 0);
2409
2410 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002411}
2412
Pekka Paalanen82919792014-05-21 13:51:49 +03002413static void
2414weston_output_schedule_repaint_reset(struct weston_output *output)
2415{
2416 struct weston_compositor *compositor = output->compositor;
2417 struct wl_event_loop *loop;
2418 int fd;
2419
2420 output->repaint_scheduled = 0;
2421 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2422
2423 if (compositor->input_loop_source)
2424 return;
2425
2426 loop = wl_display_get_event_loop(compositor->wl_display);
2427 fd = wl_event_loop_get_fd(compositor->input_loop);
2428 compositor->input_loop_source =
2429 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2430 weston_compositor_read_input, compositor);
2431}
2432
Pekka Paalanen0513a952014-05-21 16:17:27 +03002433static int
2434output_repaint_timer_handler(void *data)
2435{
2436 struct weston_output *output = data;
2437 struct weston_compositor *compositor = output->compositor;
2438
2439 if (output->repaint_needed &&
2440 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2441 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2442 weston_output_repaint(output) == 0)
2443 return 0;
2444
2445 weston_output_schedule_repaint_reset(output);
2446
2447 return 0;
2448}
2449
Kristian Høgsbergef044142011-06-21 15:02:12 -04002450WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002451weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002452 const struct timespec *stamp,
2453 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002454{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002455 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002456 int32_t refresh_nsec;
2457 struct timespec now;
2458 struct timespec gone;
2459 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002460
Pekka Paalanenb5026542014-11-12 15:09:24 +02002461 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2462 TLP_VBLANK(stamp), TLP_END);
2463
Pekka Paalanend7894d02015-07-03 15:08:53 +03002464 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002465 weston_presentation_feedback_present_list(&output->feedback_list,
2466 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002467 output->msc,
2468 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002469
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002470 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002471
Pekka Paalanen0513a952014-05-21 16:17:27 +03002472 weston_compositor_read_presentation_clock(compositor, &now);
2473 timespec_sub(&gone, &now, stamp);
2474 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2475 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002476
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002477 if (msec < -1000 || msec > 1000) {
2478 static bool warned;
2479
2480 if (!warned)
2481 weston_log("Warning: computed repaint delay is "
2482 "insane: %d msec\n", msec);
2483 warned = true;
2484
2485 msec = 0;
2486 }
2487
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002488 /* Called from restart_repaint_loop and restart happens already after
2489 * the deadline given by repaint_msec? In that case we delay until
2490 * the deadline of the next frame, to give clients a more predictable
2491 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002492 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002493 msec += refresh_nsec / 1000000;
2494
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002495 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002496 output_repaint_timer_handler(output);
2497 else
2498 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002499}
2500
2501static void
2502idle_repaint(void *data)
2503{
2504 struct weston_output *output = data;
2505
Jonas Ådahle5a12252013-04-05 23:07:11 +02002506 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002507}
2508
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002509WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002510weston_layer_entry_insert(struct weston_layer_entry *list,
2511 struct weston_layer_entry *entry)
2512{
2513 wl_list_insert(&list->link, &entry->link);
2514 entry->layer = list->layer;
2515}
2516
2517WL_EXPORT void
2518weston_layer_entry_remove(struct weston_layer_entry *entry)
2519{
2520 wl_list_remove(&entry->link);
2521 wl_list_init(&entry->link);
2522 entry->layer = NULL;
2523}
2524
2525WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002526weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2527{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002528 wl_list_init(&layer->view_list.link);
2529 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002530 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002531 if (below != NULL)
2532 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002533}
2534
2535WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002536weston_layer_set_mask(struct weston_layer *layer,
2537 int x, int y, int width, int height)
2538{
2539 struct weston_view *view;
2540
2541 layer->mask.x1 = x;
2542 layer->mask.x2 = x + width;
2543 layer->mask.y1 = y;
2544 layer->mask.y2 = y + height;
2545
2546 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2547 weston_view_geometry_dirty(view);
2548 }
2549}
2550
2551WL_EXPORT void
2552weston_layer_set_mask_infinite(struct weston_layer *layer)
2553{
2554 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2555 UINT32_MAX, UINT32_MAX);
2556}
2557
2558WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002559weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002560{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002561 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002562 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002563
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002564 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2565 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002566 return;
2567
Pekka Paalanenb5026542014-11-12 15:09:24 +02002568 if (!output->repaint_needed)
2569 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2570
Kristian Høgsbergef044142011-06-21 15:02:12 -04002571 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002572 output->repaint_needed = 1;
2573 if (output->repaint_scheduled)
2574 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002575
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002576 wl_event_loop_add_idle(loop, idle_repaint, output);
2577 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002578 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2579
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002580
2581 if (compositor->input_loop_source) {
2582 wl_event_source_remove(compositor->input_loop_source);
2583 compositor->input_loop_source = NULL;
2584 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002585}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002586
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002587WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002588weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2589{
2590 struct weston_output *output;
2591
2592 wl_list_for_each(output, &compositor->output_list, link)
2593 weston_output_schedule_repaint(output);
2594}
2595
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002596static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002597surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002598{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002599 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002600}
2601
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002602static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002603surface_attach(struct wl_client *client,
2604 struct wl_resource *resource,
2605 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2606{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002607 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002608 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002609
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002610 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002611 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002612 if (buffer == NULL) {
2613 wl_client_post_no_memory(client);
2614 return;
2615 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002616 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002617
Pekka Paalanende685b82012-12-04 15:58:12 +02002618 /* Attach, attach, without commit in between does not send
2619 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002620 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002621
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002622 surface->pending.sx = sx;
2623 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002624 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002625}
2626
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002627static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002628surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002629 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002630 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002631{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002632 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002633
Derek Foreman57e92ed2015-11-17 14:11:35 -06002634 if (width <= 0 || height <= 0)
2635 return;
2636
Derek Foreman152254b2015-11-26 14:17:48 -06002637 pixman_region32_union_rect(&surface->pending.damage_surface,
2638 &surface->pending.damage_surface,
2639 x, y, width, height);
2640}
2641
2642static void
2643surface_damage_buffer(struct wl_client *client,
2644 struct wl_resource *resource,
2645 int32_t x, int32_t y, int32_t width, int32_t height)
2646{
2647 struct weston_surface *surface = wl_resource_get_user_data(resource);
2648
2649 if (width <= 0 || height <= 0)
2650 return;
2651
2652 pixman_region32_union_rect(&surface->pending.damage_buffer,
2653 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002654 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002655}
2656
Kristian Høgsberg33418202011-08-16 23:01:28 -04002657static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002658destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002659{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002660 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002661
2662 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002663 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002664}
2665
2666static void
2667surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002668 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002669{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002670 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002671 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002672
2673 cb = malloc(sizeof *cb);
2674 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002675 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002676 return;
2677 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002678
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002679 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2680 callback);
2681 if (cb->resource == NULL) {
2682 free(cb);
2683 wl_resource_post_no_memory(resource);
2684 return;
2685 }
2686
Jason Ekstranda85118c2013-06-27 20:17:02 -05002687 wl_resource_set_implementation(cb->resource, NULL, cb,
2688 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002689
Pekka Paalanenbc106382012-10-10 12:49:31 +03002690 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002691}
2692
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002693static void
2694surface_set_opaque_region(struct wl_client *client,
2695 struct wl_resource *resource,
2696 struct wl_resource *region_resource)
2697{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002698 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002699 struct weston_region *region;
2700
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002701 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002702 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002703 pixman_region32_copy(&surface->pending.opaque,
2704 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002705 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002706 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002707 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002708}
2709
2710static void
2711surface_set_input_region(struct wl_client *client,
2712 struct wl_resource *resource,
2713 struct wl_resource *region_resource)
2714{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002715 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002716 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002717
2718 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002719 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002720 pixman_region32_copy(&surface->pending.input,
2721 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002722 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002723 pixman_region32_fini(&surface->pending.input);
2724 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002725 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002726}
2727
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002728static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002729weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002730{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002731 struct weston_subsurface *sub;
2732
2733 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2734 parent_link_pending) {
2735 wl_list_remove(&sub->parent_link);
2736 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2737 }
2738}
2739
2740static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002741weston_surface_build_buffer_matrix(struct weston_surface *surface,
2742 struct weston_matrix *matrix)
2743{
2744 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2745 double src_width, src_height, dest_width, dest_height;
2746
2747 weston_matrix_init(matrix);
2748
2749 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2750 src_width = surface->width_from_buffer;
2751 src_height = surface->height_from_buffer;
2752 } else {
2753 src_width = wl_fixed_to_double(vp->buffer.src_width);
2754 src_height = wl_fixed_to_double(vp->buffer.src_height);
2755 }
2756
2757 if (vp->surface.width == -1) {
2758 dest_width = src_width;
2759 dest_height = src_height;
2760 } else {
2761 dest_width = vp->surface.width;
2762 dest_height = vp->surface.height;
2763 }
2764
2765 if (src_width != dest_width || src_height != dest_height)
2766 weston_matrix_scale(matrix,
2767 src_width / dest_width,
2768 src_height / dest_height, 1);
2769
2770 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2771 weston_matrix_translate(matrix,
2772 wl_fixed_to_double(vp->buffer.src_x),
2773 wl_fixed_to_double(vp->buffer.src_y),
2774 0);
2775
2776 switch (vp->buffer.transform) {
2777 case WL_OUTPUT_TRANSFORM_FLIPPED:
2778 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2779 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2780 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2781 weston_matrix_scale(matrix, -1, 1, 1);
2782 weston_matrix_translate(matrix,
2783 surface->width_from_buffer, 0, 0);
2784 break;
2785 }
2786
2787 switch (vp->buffer.transform) {
2788 default:
2789 case WL_OUTPUT_TRANSFORM_NORMAL:
2790 case WL_OUTPUT_TRANSFORM_FLIPPED:
2791 break;
2792 case WL_OUTPUT_TRANSFORM_90:
2793 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2794 weston_matrix_rotate_xy(matrix, 0, 1);
2795 weston_matrix_translate(matrix,
2796 surface->height_from_buffer, 0, 0);
2797 break;
2798 case WL_OUTPUT_TRANSFORM_180:
2799 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2800 weston_matrix_rotate_xy(matrix, -1, 0);
2801 weston_matrix_translate(matrix,
2802 surface->width_from_buffer,
2803 surface->height_from_buffer, 0);
2804 break;
2805 case WL_OUTPUT_TRANSFORM_270:
2806 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2807 weston_matrix_rotate_xy(matrix, 0, -1);
2808 weston_matrix_translate(matrix,
2809 0, surface->width_from_buffer, 0);
2810 break;
2811 }
2812
2813 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2814}
2815
Derek Foreman152254b2015-11-26 14:17:48 -06002816/* Translate pending damage in buffer co-ordinates to surface
2817 * co-ordinates and union it with a pixman_region32_t.
2818 * This should only be called after the buffer is attached.
2819 */
2820static void
2821apply_damage_buffer(pixman_region32_t *dest,
2822 struct weston_surface *surface,
2823 struct weston_surface_state *state)
2824{
2825 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2826
2827 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2828 * translated into surface co-ordinates and unioned with
2829 * any other surface damage.
2830 * None of this makes sense if there is no buffer though.
2831 */
2832 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2833 pixman_region32_t buffer_damage;
2834
2835 pixman_region32_intersect_rect(&state->damage_buffer,
2836 &state->damage_buffer,
2837 0, 0, buffer->width,
2838 buffer->height);
2839 pixman_region32_init(&buffer_damage);
2840 weston_matrix_transform_region(&buffer_damage,
2841 &surface->buffer_to_surface_matrix,
2842 &state->damage_buffer);
2843 pixman_region32_union(dest, dest, &buffer_damage);
2844 pixman_region32_fini(&buffer_damage);
2845 }
2846 /* We should clear this on commit even if there was no buffer */
2847 pixman_region32_clear(&state->damage_buffer);
2848}
2849
Jason Ekstrand1e059042014-10-16 10:55:19 -05002850static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002851weston_surface_commit_state(struct weston_surface *surface,
2852 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002853{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002854 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002855 pixman_region32_t opaque;
2856
Alexander Larsson4ea95522013-05-22 14:41:37 +02002857 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002858 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002859 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002860 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002861
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002862 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002863 if (state->newly_attached)
2864 weston_surface_attach(surface, state->buffer);
2865 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002866
Jason Ekstrand1e059042014-10-16 10:55:19 -05002867 weston_surface_build_buffer_matrix(surface,
2868 &surface->surface_to_buffer_matrix);
2869 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2870 &surface->surface_to_buffer_matrix);
2871
Jason Ekstrand7b982072014-05-20 14:33:03 -05002872 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002873 weston_surface_update_size(surface);
2874 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002875 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002876 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002877
Jason Ekstrand7b982072014-05-20 14:33:03 -05002878 state->sx = 0;
2879 state->sy = 0;
2880 state->newly_attached = 0;
2881 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002882
Derek Foreman152254b2015-11-26 14:17:48 -06002883 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002884 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002885 (pixman_region32_not_empty(&state->damage_surface) ||
2886 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002887 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002888
Pekka Paalanen8e159182012-10-10 12:49:25 +03002889 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002890 &state->damage_surface);
2891
2892 apply_damage_buffer(&surface->damage, surface, state);
2893
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002894 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002895 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002896 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002897
2898 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002899 pixman_region32_init(&opaque);
2900 pixman_region32_intersect_rect(&opaque, &state->opaque,
2901 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002902
2903 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2904 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002905 wl_list_for_each(view, &surface->views, surface_link)
2906 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002907 }
2908
2909 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002910
2911 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002912 pixman_region32_intersect_rect(&surface->input, &state->input,
2913 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002914
Pekka Paalanenbc106382012-10-10 12:49:31 +03002915 /* wl_surface.frame */
2916 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002917 &state->frame_callback_list);
2918 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002919
2920 /* XXX:
2921 * What should happen with a feedback request, if there
2922 * is no wl_buffer attached for this commit?
2923 */
2924
2925 /* presentation.feedback */
2926 wl_list_insert_list(&surface->feedback_list,
2927 &state->feedback_list);
2928 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002929}
2930
2931static void
2932weston_surface_commit(struct weston_surface *surface)
2933{
2934 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002935
Pekka Paalanene67858b2013-04-25 13:57:42 +03002936 weston_surface_commit_subsurface_order(surface);
2937
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002938 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002939}
2940
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002941static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002942weston_subsurface_commit(struct weston_subsurface *sub);
2943
2944static void
2945weston_subsurface_parent_commit(struct weston_subsurface *sub,
2946 int parent_is_synchronized);
2947
2948static void
2949surface_commit(struct wl_client *client, struct wl_resource *resource)
2950{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002951 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002952 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2953
2954 if (sub) {
2955 weston_subsurface_commit(sub);
2956 return;
2957 }
2958
2959 weston_surface_commit(surface);
2960
2961 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2962 if (sub->surface != surface)
2963 weston_subsurface_parent_commit(sub, 0);
2964 }
2965}
2966
2967static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002968surface_set_buffer_transform(struct wl_client *client,
2969 struct wl_resource *resource, int transform)
2970{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002971 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002972
Jonny Lamba55f1392014-05-30 12:07:15 +02002973 /* if wl_output.transform grows more members this will need to be updated. */
2974 if (transform < 0 ||
2975 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2976 wl_resource_post_error(resource,
2977 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2978 "buffer transform must be a valid transform "
2979 "('%d' specified)", transform);
2980 return;
2981 }
2982
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002983 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002984 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002985}
2986
Alexander Larsson4ea95522013-05-22 14:41:37 +02002987static void
2988surface_set_buffer_scale(struct wl_client *client,
2989 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002990 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002991{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002992 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002993
Jonny Lamba55f1392014-05-30 12:07:15 +02002994 if (scale < 1) {
2995 wl_resource_post_error(resource,
2996 WL_SURFACE_ERROR_INVALID_SCALE,
2997 "buffer scale must be at least one "
2998 "('%d' specified)", scale);
2999 return;
3000 }
3001
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003002 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003003 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003004}
3005
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003006static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003007 surface_destroy,
3008 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003009 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003010 surface_frame,
3011 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003012 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003013 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003014 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003015 surface_set_buffer_scale,
3016 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003017};
3018
3019static void
3020compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003021 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003022{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003023 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003024 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003025
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003026 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003027 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003028 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003029 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003030 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003031
Jason Ekstranda85118c2013-06-27 20:17:02 -05003032 surface->resource =
3033 wl_resource_create(client, &wl_surface_interface,
3034 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003035 if (surface->resource == NULL) {
3036 weston_surface_destroy(surface);
3037 wl_resource_post_no_memory(resource);
3038 return;
3039 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003040 wl_resource_set_implementation(surface->resource, &surface_interface,
3041 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003042
3043 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003044}
3045
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003046static void
3047destroy_region(struct wl_resource *resource)
3048{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003049 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003050
3051 pixman_region32_fini(&region->region);
3052 free(region);
3053}
3054
3055static void
3056region_destroy(struct wl_client *client, struct wl_resource *resource)
3057{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003058 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003059}
3060
3061static void
3062region_add(struct wl_client *client, struct wl_resource *resource,
3063 int32_t x, int32_t y, int32_t width, int32_t height)
3064{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003065 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003066
3067 pixman_region32_union_rect(&region->region, &region->region,
3068 x, y, width, height);
3069}
3070
3071static void
3072region_subtract(struct wl_client *client, struct wl_resource *resource,
3073 int32_t x, int32_t y, int32_t width, int32_t height)
3074{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003075 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003076 pixman_region32_t rect;
3077
3078 pixman_region32_init_rect(&rect, x, y, width, height);
3079 pixman_region32_subtract(&region->region, &region->region, &rect);
3080 pixman_region32_fini(&rect);
3081}
3082
3083static const struct wl_region_interface region_interface = {
3084 region_destroy,
3085 region_add,
3086 region_subtract
3087};
3088
3089static void
3090compositor_create_region(struct wl_client *client,
3091 struct wl_resource *resource, uint32_t id)
3092{
3093 struct weston_region *region;
3094
3095 region = malloc(sizeof *region);
3096 if (region == NULL) {
3097 wl_resource_post_no_memory(resource);
3098 return;
3099 }
3100
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003101 pixman_region32_init(&region->region);
3102
Jason Ekstranda85118c2013-06-27 20:17:02 -05003103 region->resource =
3104 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003105 if (region->resource == NULL) {
3106 free(region);
3107 wl_resource_post_no_memory(resource);
3108 return;
3109 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003110 wl_resource_set_implementation(region->resource, &region_interface,
3111 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003112}
3113
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003114static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003115 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003116 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003117};
3118
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003119static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003120weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3121{
3122 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003123
Jason Ekstrand7b982072014-05-20 14:33:03 -05003124 weston_surface_commit_state(surface, &sub->cached);
3125 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003126
3127 weston_surface_commit_subsurface_order(surface);
3128
3129 weston_surface_schedule_repaint(surface);
3130
Jason Ekstrand7b982072014-05-20 14:33:03 -05003131 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003132}
3133
3134static void
3135weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3136{
3137 struct weston_surface *surface = sub->surface;
3138
3139 /*
3140 * If this commit would cause the surface to move by the
3141 * attach(dx, dy) parameters, the old damage region must be
3142 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003143 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003144 */
Derek Foreman152254b2015-11-26 14:17:48 -06003145 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003146 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003147 pixman_region32_union(&sub->cached.damage_surface,
3148 &sub->cached.damage_surface,
3149 &surface->pending.damage_surface);
3150 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003151
3152 if (surface->pending.newly_attached) {
3153 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003154 weston_surface_state_set_buffer(&sub->cached,
3155 surface->pending.buffer);
3156 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003157 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003158 weston_presentation_feedback_discard_list(
3159 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003160 }
3161 sub->cached.sx += surface->pending.sx;
3162 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003163
Derek Foreman152254b2015-11-26 14:17:48 -06003164 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3165
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003166 sub->cached.buffer_viewport.changed |=
3167 surface->pending.buffer_viewport.changed;
3168 sub->cached.buffer_viewport.buffer =
3169 surface->pending.buffer_viewport.buffer;
3170 sub->cached.buffer_viewport.surface =
3171 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003172
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003173 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003174
3175 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3176
3177 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3178
3179 wl_list_insert_list(&sub->cached.frame_callback_list,
3180 &surface->pending.frame_callback_list);
3181 wl_list_init(&surface->pending.frame_callback_list);
3182
Pekka Paalanen133e4392014-09-23 22:08:46 -04003183 wl_list_insert_list(&sub->cached.feedback_list,
3184 &surface->pending.feedback_list);
3185 wl_list_init(&surface->pending.feedback_list);
3186
Jason Ekstrand7b982072014-05-20 14:33:03 -05003187 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003188}
3189
Derek Foreman280e7dd2014-10-03 13:13:42 -05003190static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003191weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3192{
3193 while (sub) {
3194 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003195 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003196
3197 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003198 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003199
3200 sub = weston_surface_to_subsurface(sub->parent);
3201 }
3202
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003203 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003204}
3205
3206static void
3207weston_subsurface_commit(struct weston_subsurface *sub)
3208{
3209 struct weston_surface *surface = sub->surface;
3210 struct weston_subsurface *tmp;
3211
3212 /* Recursive check for effectively synchronized. */
3213 if (weston_subsurface_is_synchronized(sub)) {
3214 weston_subsurface_commit_to_cache(sub);
3215 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003216 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003217 /* flush accumulated state from cache */
3218 weston_subsurface_commit_to_cache(sub);
3219 weston_subsurface_commit_from_cache(sub);
3220 } else {
3221 weston_surface_commit(surface);
3222 }
3223
3224 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3225 if (tmp->surface != surface)
3226 weston_subsurface_parent_commit(tmp, 0);
3227 }
3228 }
3229}
3230
3231static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003232weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003233{
3234 struct weston_surface *surface = sub->surface;
3235 struct weston_subsurface *tmp;
3236
Pekka Paalanene67858b2013-04-25 13:57:42 +03003237 /* From now on, commit_from_cache the whole sub-tree, regardless of
3238 * the synchronized mode of each child. This sub-surface or some
3239 * of its ancestors were synchronized, so we are synchronized
3240 * all the way down.
3241 */
3242
Jason Ekstrand7b982072014-05-20 14:33:03 -05003243 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003244 weston_subsurface_commit_from_cache(sub);
3245
3246 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3247 if (tmp->surface != surface)
3248 weston_subsurface_parent_commit(tmp, 1);
3249 }
3250}
3251
3252static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003253weston_subsurface_parent_commit(struct weston_subsurface *sub,
3254 int parent_is_synchronized)
3255{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003256 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003257 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003258 wl_list_for_each(view, &sub->surface->views, surface_link)
3259 weston_view_set_position(view,
3260 sub->position.x,
3261 sub->position.y);
3262
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003263 sub->position.set = 0;
3264 }
3265
3266 if (parent_is_synchronized || sub->synchronized)
3267 weston_subsurface_synchronized_commit(sub);
3268}
3269
Pekka Paalanen8274d902014-08-06 19:36:51 +03003270static int
3271subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3272{
3273 return snprintf(buf, len, "sub-surface");
3274}
3275
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003276static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003277subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003278{
3279 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003280 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003281
Jason Ekstranda7af7042013-10-12 22:38:11 -05003282 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003283 weston_view_set_position(view,
3284 view->geometry.x + dx,
3285 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003286
3287 /* No need to check parent mappedness, because if parent is not
3288 * mapped, parent is not in a visible layer, so this sub-surface
3289 * will not be drawn either.
3290 */
3291 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003292 struct weston_output *output;
3293
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003294 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003295 * because that would call it also for the parent surface,
3296 * which might not be mapped yet. That would lead to
3297 * inconsistent state, where the window could never be
3298 * mapped.
3299 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003300 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003301 * weston_surface_is_mapped() return true, so that when the
3302 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003303 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003304 */
3305 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003306 output = container_of(compositor->output_list.next,
3307 struct weston_output, link);
3308
3309 surface->output = output;
Bryce Harrington89324ce2015-12-23 18:38:07 -08003310 weston_surface_update_output_mask(surface, 1u << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003311 }
3312}
3313
3314static struct weston_subsurface *
3315weston_surface_to_subsurface(struct weston_surface *surface)
3316{
3317 if (surface->configure == subsurface_configure)
3318 return surface->configure_private;
3319
3320 return NULL;
3321}
3322
Pekka Paalanen01388e22013-04-25 13:57:44 +03003323WL_EXPORT struct weston_surface *
3324weston_surface_get_main_surface(struct weston_surface *surface)
3325{
3326 struct weston_subsurface *sub;
3327
3328 while (surface && (sub = weston_surface_to_subsurface(surface)))
3329 surface = sub->parent;
3330
3331 return surface;
3332}
3333
Pekka Paalanen50b67472014-10-01 15:02:41 +03003334WL_EXPORT int
3335weston_surface_set_role(struct weston_surface *surface,
3336 const char *role_name,
3337 struct wl_resource *error_resource,
3338 uint32_t error_code)
3339{
3340 assert(role_name);
3341
3342 if (surface->role_name == NULL ||
3343 surface->role_name == role_name ||
3344 strcmp(surface->role_name, role_name) == 0) {
3345 surface->role_name = role_name;
3346
3347 return 0;
3348 }
3349
3350 wl_resource_post_error(error_resource, error_code,
3351 "Cannot assign role %s to wl_surface@%d,"
3352 " already has role %s\n",
3353 role_name,
3354 wl_resource_get_id(surface->resource),
3355 surface->role_name);
3356 return -1;
3357}
3358
Pekka Paalanen8274d902014-08-06 19:36:51 +03003359WL_EXPORT void
3360weston_surface_set_label_func(struct weston_surface *surface,
3361 int (*desc)(struct weston_surface *,
3362 char *, size_t))
3363{
3364 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003365 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003366}
3367
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003368/** Get the size of surface contents
3369 *
3370 * \param surface The surface to query.
3371 * \param width Returns the width of raw contents.
3372 * \param height Returns the height of raw contents.
3373 *
3374 * Retrieves the raw surface content size in pixels for the given surface.
3375 * This is the whole content size in buffer pixels. If the surface
3376 * has no content or the renderer does not implement this feature,
3377 * zeroes are returned.
3378 *
3379 * This function is used to determine the buffer size needed for
3380 * a weston_surface_copy_content() call.
3381 */
3382WL_EXPORT void
3383weston_surface_get_content_size(struct weston_surface *surface,
3384 int *width, int *height)
3385{
3386 struct weston_renderer *rer = surface->compositor->renderer;
3387
3388 if (!rer->surface_get_content_size) {
3389 *width = 0;
3390 *height = 0;
3391 return;
3392 }
3393
3394 rer->surface_get_content_size(surface, width, height);
3395}
3396
3397/** Copy surface contents to system memory.
3398 *
3399 * \param surface The surface to copy from.
3400 * \param target Pointer to the target memory buffer.
3401 * \param size Size of the target buffer in bytes.
3402 * \param src_x X location on contents to copy from.
3403 * \param src_y Y location on contents to copy from.
3404 * \param width Width in pixels of the area to copy.
3405 * \param height Height in pixels of the area to copy.
3406 * \return 0 for success, -1 for failure.
3407 *
3408 * Surface contents are maintained by the renderer. They can be in a
3409 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3410 * else.
3411 *
3412 * Surface contents are copied into memory pointed to by target,
3413 * which has size bytes of space available. The target memory
3414 * may be larger than needed, but being smaller returns an error.
3415 * The extra bytes in target may or may not be written; their content is
3416 * unspecified. Size must be large enough to hold the image.
3417 *
3418 * The image in the target memory will be arranged in rows from
3419 * top to bottom, and pixels on a row from left to right. The pixel
3420 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3421 * width * 4.
3422 *
3423 * Parameters src_x and src_y define the upper-left corner in buffer
3424 * coordinates (pixels) to copy from. Parameters width and height
3425 * define the size of the area to copy in pixels.
3426 *
3427 * The rectangle defined by src_x, src_y, width, height must fit in
3428 * the surface contents. Otherwise an error is returned.
3429 *
3430 * Use surface_get_data_size to determine the content size; the
3431 * needed target buffer size and rectangle limits.
3432 *
3433 * CURRENT IMPLEMENTATION RESTRICTIONS:
3434 * - the machine must be little-endian due to Pixman formats.
3435 *
3436 * NOTE: Pixman formats are premultiplied.
3437 */
3438WL_EXPORT int
3439weston_surface_copy_content(struct weston_surface *surface,
3440 void *target, size_t size,
3441 int src_x, int src_y,
3442 int width, int height)
3443{
3444 struct weston_renderer *rer = surface->compositor->renderer;
3445 int cw, ch;
3446 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3447
3448 if (!rer->surface_copy_content)
3449 return -1;
3450
3451 weston_surface_get_content_size(surface, &cw, &ch);
3452
3453 if (src_x < 0 || src_y < 0)
3454 return -1;
3455
3456 if (width <= 0 || height <= 0)
3457 return -1;
3458
3459 if (src_x + width > cw || src_y + height > ch)
3460 return -1;
3461
3462 if (width * bytespp * height > size)
3463 return -1;
3464
3465 return rer->surface_copy_content(surface, target, size,
3466 src_x, src_y, width, height);
3467}
3468
Pekka Paalanene67858b2013-04-25 13:57:42 +03003469static void
3470subsurface_set_position(struct wl_client *client,
3471 struct wl_resource *resource, int32_t x, int32_t y)
3472{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003473 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003474
3475 if (!sub)
3476 return;
3477
3478 sub->position.x = x;
3479 sub->position.y = y;
3480 sub->position.set = 1;
3481}
3482
3483static struct weston_subsurface *
3484subsurface_from_surface(struct weston_surface *surface)
3485{
3486 struct weston_subsurface *sub;
3487
3488 sub = weston_surface_to_subsurface(surface);
3489 if (sub)
3490 return sub;
3491
3492 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3493 if (sub->surface == surface)
3494 return sub;
3495
3496 return NULL;
3497}
3498
3499static struct weston_subsurface *
3500subsurface_sibling_check(struct weston_subsurface *sub,
3501 struct weston_surface *surface,
3502 const char *request)
3503{
3504 struct weston_subsurface *sibling;
3505
3506 sibling = subsurface_from_surface(surface);
3507
3508 if (!sibling) {
3509 wl_resource_post_error(sub->resource,
3510 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3511 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003512 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003513 return NULL;
3514 }
3515
3516 if (sibling->parent != sub->parent) {
3517 wl_resource_post_error(sub->resource,
3518 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3519 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003520 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003521 return NULL;
3522 }
3523
3524 return sibling;
3525}
3526
3527static void
3528subsurface_place_above(struct wl_client *client,
3529 struct wl_resource *resource,
3530 struct wl_resource *sibling_resource)
3531{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003532 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003533 struct weston_surface *surface =
3534 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003535 struct weston_subsurface *sibling;
3536
3537 if (!sub)
3538 return;
3539
3540 sibling = subsurface_sibling_check(sub, surface, "place_above");
3541 if (!sibling)
3542 return;
3543
3544 wl_list_remove(&sub->parent_link_pending);
3545 wl_list_insert(sibling->parent_link_pending.prev,
3546 &sub->parent_link_pending);
3547}
3548
3549static void
3550subsurface_place_below(struct wl_client *client,
3551 struct wl_resource *resource,
3552 struct wl_resource *sibling_resource)
3553{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003554 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003555 struct weston_surface *surface =
3556 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003557 struct weston_subsurface *sibling;
3558
3559 if (!sub)
3560 return;
3561
3562 sibling = subsurface_sibling_check(sub, surface, "place_below");
3563 if (!sibling)
3564 return;
3565
3566 wl_list_remove(&sub->parent_link_pending);
3567 wl_list_insert(&sibling->parent_link_pending,
3568 &sub->parent_link_pending);
3569}
3570
3571static void
3572subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3573{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003574 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003575
3576 if (sub)
3577 sub->synchronized = 1;
3578}
3579
3580static void
3581subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3582{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003583 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003584
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003585 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003586 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003587
3588 /* If sub became effectively desynchronized, flush. */
3589 if (!weston_subsurface_is_synchronized(sub))
3590 weston_subsurface_synchronized_commit(sub);
3591 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003592}
3593
3594static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003595weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3596{
3597 wl_list_remove(&sub->parent_link);
3598 wl_list_remove(&sub->parent_link_pending);
3599 wl_list_remove(&sub->parent_destroy_listener.link);
3600 sub->parent = NULL;
3601}
3602
3603static void
3604weston_subsurface_destroy(struct weston_subsurface *sub);
3605
3606static void
3607subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3608{
3609 struct weston_subsurface *sub =
3610 container_of(listener, struct weston_subsurface,
3611 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003612 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003613
3614 /* The protocol object (wl_resource) is left inert. */
3615 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003616 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003617
3618 weston_subsurface_destroy(sub);
3619}
3620
3621static void
3622subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3623{
3624 struct weston_subsurface *sub =
3625 container_of(listener, struct weston_subsurface,
3626 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003627 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003628 assert(sub->surface != sub->parent);
3629
3630 if (weston_surface_is_mapped(sub->surface))
3631 weston_surface_unmap(sub->surface);
3632
3633 weston_subsurface_unlink_parent(sub);
3634}
3635
3636static void
3637subsurface_resource_destroy(struct wl_resource *resource)
3638{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003639 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003640
3641 if (sub)
3642 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003643}
3644
3645static void
3646subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3647{
3648 wl_resource_destroy(resource);
3649}
3650
3651static void
3652weston_subsurface_link_parent(struct weston_subsurface *sub,
3653 struct weston_surface *parent)
3654{
3655 sub->parent = parent;
3656 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003657 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003658 &sub->parent_destroy_listener);
3659
3660 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3661 wl_list_insert(&parent->subsurface_list_pending,
3662 &sub->parent_link_pending);
3663}
3664
3665static void
3666weston_subsurface_link_surface(struct weston_subsurface *sub,
3667 struct weston_surface *surface)
3668{
3669 sub->surface = surface;
3670 sub->surface_destroy_listener.notify =
3671 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003672 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003673 &sub->surface_destroy_listener);
3674}
3675
3676static void
3677weston_subsurface_destroy(struct weston_subsurface *sub)
3678{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003679 struct weston_view *view, *next;
3680
Pekka Paalanene67858b2013-04-25 13:57:42 +03003681 assert(sub->surface);
3682
3683 if (sub->resource) {
3684 assert(weston_surface_to_subsurface(sub->surface) == sub);
3685 assert(sub->parent_destroy_listener.notify ==
3686 subsurface_handle_parent_destroy);
3687
George Kiagiadakised04d382014-06-13 18:10:26 +02003688 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3689 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003690 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003691 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003692
Pekka Paalanene67858b2013-04-25 13:57:42 +03003693 if (sub->parent)
3694 weston_subsurface_unlink_parent(sub);
3695
Jason Ekstrand7b982072014-05-20 14:33:03 -05003696 weston_surface_state_fini(&sub->cached);
3697 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003698
3699 sub->surface->configure = NULL;
3700 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003701 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003702 } else {
3703 /* the dummy weston_subsurface for the parent itself */
3704 assert(sub->parent_destroy_listener.notify == NULL);
3705 wl_list_remove(&sub->parent_link);
3706 wl_list_remove(&sub->parent_link_pending);
3707 }
3708
3709 wl_list_remove(&sub->surface_destroy_listener.link);
3710 free(sub);
3711}
3712
3713static const struct wl_subsurface_interface subsurface_implementation = {
3714 subsurface_destroy,
3715 subsurface_set_position,
3716 subsurface_place_above,
3717 subsurface_place_below,
3718 subsurface_set_sync,
3719 subsurface_set_desync
3720};
3721
3722static struct weston_subsurface *
3723weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3724 struct weston_surface *parent)
3725{
3726 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003727 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003728
Bryce Harringtonde16d892014-11-20 22:21:57 -08003729 sub = zalloc(sizeof *sub);
3730 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003731 return NULL;
3732
Jason Ekstranda7af7042013-10-12 22:38:11 -05003733 wl_list_init(&sub->unused_views);
3734
Jason Ekstranda85118c2013-06-27 20:17:02 -05003735 sub->resource =
3736 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003737 if (!sub->resource) {
3738 free(sub);
3739 return NULL;
3740 }
3741
Jason Ekstranda85118c2013-06-27 20:17:02 -05003742 wl_resource_set_implementation(sub->resource,
3743 &subsurface_implementation,
3744 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003745 weston_subsurface_link_surface(sub, surface);
3746 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003747 weston_surface_state_init(&sub->cached);
3748 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003749 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003750
3751 return sub;
3752}
3753
3754/* Create a dummy subsurface for having the parent itself in its
3755 * sub-surface lists. Makes stacking order manipulation easy.
3756 */
3757static struct weston_subsurface *
3758weston_subsurface_create_for_parent(struct weston_surface *parent)
3759{
3760 struct weston_subsurface *sub;
3761
Bryce Harringtonde16d892014-11-20 22:21:57 -08003762 sub = zalloc(sizeof *sub);
3763 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003764 return NULL;
3765
3766 weston_subsurface_link_surface(sub, parent);
3767 sub->parent = parent;
3768 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3769 wl_list_insert(&parent->subsurface_list_pending,
3770 &sub->parent_link_pending);
3771
3772 return sub;
3773}
3774
3775static void
3776subcompositor_get_subsurface(struct wl_client *client,
3777 struct wl_resource *resource,
3778 uint32_t id,
3779 struct wl_resource *surface_resource,
3780 struct wl_resource *parent_resource)
3781{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003782 struct weston_surface *surface =
3783 wl_resource_get_user_data(surface_resource);
3784 struct weston_surface *parent =
3785 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003786 struct weston_subsurface *sub;
3787 static const char where[] = "get_subsurface: wl_subsurface@";
3788
3789 if (surface == parent) {
3790 wl_resource_post_error(resource,
3791 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3792 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003793 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003794 return;
3795 }
3796
3797 if (weston_surface_to_subsurface(surface)) {
3798 wl_resource_post_error(resource,
3799 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3800 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003801 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003802 return;
3803 }
3804
Pekka Paalanen50b67472014-10-01 15:02:41 +03003805 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3806 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003807 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003808
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003809 if (weston_surface_get_main_surface(parent) == surface) {
3810 wl_resource_post_error(resource,
3811 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3812 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003813 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003814 return;
3815 }
3816
Pekka Paalanene67858b2013-04-25 13:57:42 +03003817 /* make sure the parent is in its own list */
3818 if (wl_list_empty(&parent->subsurface_list)) {
3819 if (!weston_subsurface_create_for_parent(parent)) {
3820 wl_resource_post_no_memory(resource);
3821 return;
3822 }
3823 }
3824
3825 sub = weston_subsurface_create(id, surface, parent);
3826 if (!sub) {
3827 wl_resource_post_no_memory(resource);
3828 return;
3829 }
3830
3831 surface->configure = subsurface_configure;
3832 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003833 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003834}
3835
3836static void
3837subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3838{
3839 wl_resource_destroy(resource);
3840}
3841
3842static const struct wl_subcompositor_interface subcompositor_interface = {
3843 subcompositor_destroy,
3844 subcompositor_get_subsurface
3845};
3846
3847static void
3848bind_subcompositor(struct wl_client *client,
3849 void *data, uint32_t version, uint32_t id)
3850{
3851 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003852 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003853
Jason Ekstranda85118c2013-06-27 20:17:02 -05003854 resource =
3855 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003856 if (resource == NULL) {
3857 wl_client_post_no_memory(client);
3858 return;
3859 }
3860 wl_resource_set_implementation(resource, &subcompositor_interface,
3861 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003862}
3863
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003864/** Set a DPMS mode on all of the compositor's outputs
3865 *
3866 * \param compositor The compositor instance
3867 * \param state The DPMS state the outputs will be set to
3868 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003869static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003870weston_compositor_dpms(struct weston_compositor *compositor,
3871 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003872{
3873 struct weston_output *output;
3874
3875 wl_list_for_each(output, &compositor->output_list, link)
3876 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003877 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003878}
3879
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003880/** Restores the compositor to active status
3881 *
3882 * \param compositor The compositor instance
3883 *
3884 * If the compositor was in a sleeping mode, all outputs are powered
3885 * back on via DPMS. Otherwise if the compositor was inactive
3886 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3887 * signal will fire.
3888 *
3889 * Restarts the idle timer.
3890 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003891WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003892weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003893{
Neil Roberts8b62e202013-09-30 13:14:47 +01003894 uint32_t old_state = compositor->state;
3895
3896 /* The state needs to be changed before emitting the wake
3897 * signal because that may try to schedule a repaint which
3898 * will not work if the compositor is still sleeping */
3899 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3900
3901 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003902 case WESTON_COMPOSITOR_SLEEPING:
3903 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3904 /* fall through */
3905 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003906 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003907 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003908 /* fall through */
3909 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003910 wl_event_source_timer_update(compositor->idle_source,
3911 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003912 }
3913}
3914
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003915/** Turns off rendering and frame events for the compositor.
3916 *
3917 * \param compositor The compositor instance
3918 *
3919 * This is used for example to prevent further rendering while the
3920 * compositor is shutting down.
3921 *
3922 * \note When offscreen state is entered, outputs will be powered
3923 * back on if they were sleeping (in DPMS off mode), even though
3924 * no rendering will be performed.
3925 *
3926 * Stops the idle timer.
3927 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003928WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003929weston_compositor_offscreen(struct weston_compositor *compositor)
3930{
3931 switch (compositor->state) {
3932 case WESTON_COMPOSITOR_OFFSCREEN:
3933 return;
3934 case WESTON_COMPOSITOR_SLEEPING:
3935 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3936 /* fall through */
3937 default:
3938 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3939 wl_event_source_timer_update(compositor->idle_source, 0);
3940 }
3941}
3942
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003943/** Powers down all attached output devices
3944 *
3945 * \param compositor The compositor instance
3946 *
3947 * Causes rendering to the outputs to cease, and no frame events to be
3948 * sent. Only powers down the outputs if the compositor is not already
3949 * in sleep mode.
3950 *
3951 * Stops the idle timer.
3952 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003953WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003954weston_compositor_sleep(struct weston_compositor *compositor)
3955{
3956 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3957 return;
3958
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003959 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003960 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3961 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3962}
3963
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003964/** Sets compositor to idle mode
3965 *
3966 * \param data The compositor instance
3967 *
3968 * This is called when the idle timer fires. Once the compositor is in
3969 * idle mode it requires a wake action (e.g. via
3970 * weston_compositor_wake()) to restore it. The compositor's
3971 * idle_signal will be triggered when the idle event occurs.
3972 *
3973 * Idleness can be inhibited by setting the compositor's idle_inhibit
3974 * property.
3975 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003976static int
3977idle_handler(void *data)
3978{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003979 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003980
3981 if (compositor->idle_inhibit)
3982 return 1;
3983
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003984 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003985 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003986
3987 return 1;
3988}
3989
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003990WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003991weston_plane_init(struct weston_plane *plane,
3992 struct weston_compositor *ec,
3993 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003994{
3995 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003996 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003997 plane->x = x;
3998 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003999 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004000
4001 /* Init the link so that the call to wl_list_remove() when releasing
4002 * the plane without ever stacking doesn't lead to a crash */
4003 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004004}
4005
4006WL_EXPORT void
4007weston_plane_release(struct weston_plane *plane)
4008{
Xiong Zhang97116532013-10-23 13:58:31 +08004009 struct weston_view *view;
4010
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004011 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004012 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004013
Xiong Zhang97116532013-10-23 13:58:31 +08004014 wl_list_for_each(view, &plane->compositor->view_list, link) {
4015 if (view->plane == plane)
4016 view->plane = NULL;
4017 }
4018
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004019 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004020}
4021
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004022WL_EXPORT void
4023weston_compositor_stack_plane(struct weston_compositor *ec,
4024 struct weston_plane *plane,
4025 struct weston_plane *above)
4026{
4027 if (above)
4028 wl_list_insert(above->link.prev, &plane->link);
4029 else
4030 wl_list_insert(&ec->plane_list, &plane->link);
4031}
4032
Casey Dahlin9074db52012-04-19 22:50:09 -04004033static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004034{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004035 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004036}
4037
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004038static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004039bind_output(struct wl_client *client,
4040 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004041{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004042 struct weston_output *output = data;
4043 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004044 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004045
Jason Ekstranda85118c2013-06-27 20:17:02 -05004046 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004047 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004048 if (resource == NULL) {
4049 wl_client_post_no_memory(client);
4050 return;
4051 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004052
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004053 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05004054 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004055
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004056 wl_output_send_geometry(resource,
4057 output->x,
4058 output->y,
4059 output->mm_width,
4060 output->mm_height,
4061 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004062 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004063 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004064 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004065 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004066 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004067
4068 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004069 wl_output_send_mode(resource,
4070 mode->flags,
4071 mode->width,
4072 mode->height,
4073 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004074 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004075
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004076 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004077 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004078}
4079
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004080/* Move other outputs when one is removed so the space remains contiguos. */
4081static void
4082weston_compositor_remove_output(struct weston_compositor *compositor,
4083 struct weston_output *remove_output)
4084{
4085 struct weston_output *output;
4086 int offset = 0;
4087
4088 wl_list_for_each(output, &compositor->output_list, link) {
4089 if (output == remove_output) {
4090 offset = output->width;
4091 continue;
4092 }
4093
4094 if (offset > 0) {
4095 weston_output_move(output,
4096 output->x - offset, output->y);
4097 output->dirty = 1;
4098 }
4099 }
4100}
4101
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004102WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004103weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004104{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004105 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004106 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004107
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02004108 output->destroying = 1;
4109
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004110 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08004111 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004112 weston_view_assign_output(view);
4113 }
4114
Pekka Paalanen0513a952014-05-21 16:17:27 +03004115 wl_event_source_remove(output->repaint_timer);
4116
Pekka Paalanen133e4392014-09-23 22:08:46 -04004117 weston_presentation_feedback_discard_list(&output->feedback_list);
4118
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004119 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004120 wl_list_remove(&output->link);
4121
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004122 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004123 wl_signal_emit(&output->destroy_signal, output);
4124
Richard Hughesafe690c2013-05-02 10:10:04 +01004125 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004126 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004127 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08004128 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004129
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004130 wl_resource_for_each(resource, &output->resource_list) {
4131 wl_resource_set_destructor(resource, NULL);
4132 }
4133
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004134 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004135}
4136
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004137WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004138weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004139{
Scott Moreau850ca422012-05-21 15:21:25 -06004140 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004141
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004142 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004143 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004144
Scott Moreauccbf29d2012-02-22 14:21:41 -07004145 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004146 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004147 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004148 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004149 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004150 weston_matrix_scale(&output->matrix, magnification,
4151 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004152 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004153
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004154 switch (output->transform) {
4155 case WL_OUTPUT_TRANSFORM_FLIPPED:
4156 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4157 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4158 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4159 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4160 weston_matrix_scale(&output->matrix, -1, 1, 1);
4161 break;
4162 }
4163
4164 switch (output->transform) {
4165 default:
4166 case WL_OUTPUT_TRANSFORM_NORMAL:
4167 case WL_OUTPUT_TRANSFORM_FLIPPED:
4168 break;
4169 case WL_OUTPUT_TRANSFORM_90:
4170 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4171 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4172 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4173 break;
4174 case WL_OUTPUT_TRANSFORM_180:
4175 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4176 weston_matrix_translate(&output->matrix,
4177 -output->width, -output->height, 0);
4178 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4179 break;
4180 case WL_OUTPUT_TRANSFORM_270:
4181 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4182 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4183 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4184 break;
4185 }
4186
4187 if (output->current_scale != 1)
4188 weston_matrix_scale(&output->matrix,
4189 output->current_scale,
4190 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004191
Scott Moreauccbf29d2012-02-22 14:21:41 -07004192 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004193
4194 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004195}
4196
Scott Moreau1bad5db2012-08-18 01:04:05 -06004197static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004198weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004199{
4200 output->transform = transform;
4201
4202 switch (transform) {
4203 case WL_OUTPUT_TRANSFORM_90:
4204 case WL_OUTPUT_TRANSFORM_270:
4205 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4206 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4207 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02004208 output->width = output->current_mode->height;
4209 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004210 break;
4211 case WL_OUTPUT_TRANSFORM_NORMAL:
4212 case WL_OUTPUT_TRANSFORM_180:
4213 case WL_OUTPUT_TRANSFORM_FLIPPED:
4214 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02004215 output->width = output->current_mode->width;
4216 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004217 break;
4218 default:
4219 break;
4220 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06004221
Hardening57388e42013-09-18 23:56:36 +02004222 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02004223 output->width /= scale;
4224 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004225}
4226
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004227static void
4228weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004229{
4230 output->x = x;
4231 output->y = y;
4232
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004233 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004234 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004235 output->width,
4236 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004237}
4238
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004239WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004240weston_output_move(struct weston_output *output, int x, int y)
4241{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004242 struct wl_resource *resource;
4243
4244 output->move_x = x - output->x;
4245 output->move_y = y - output->y;
4246
4247 if (output->move_x == 0 && output->move_y == 0)
4248 return;
4249
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004250 weston_output_init_geometry(output, x, y);
4251
4252 output->dirty = 1;
4253
4254 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004255 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004256
4257 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004258 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004259 wl_output_send_geometry(resource,
4260 output->x,
4261 output->y,
4262 output->mm_width,
4263 output->mm_height,
4264 output->subpixel,
4265 output->make,
4266 output->model,
4267 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004268
4269 if (wl_resource_get_version(resource) >= 2)
4270 wl_output_send_done(resource);
4271 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004272}
4273
Bryce Harrington3f650b82015-12-23 11:01:58 -08004274/** Initialize a weston_output object's parameters
4275 *
4276 * \param output The weston_output object to initialize
4277 * \param c The output's compositor
4278 * \param x x coordinate for the output in global coordinate space
4279 * \param y y coordinate for the output in global coordinate space
4280 * \param mm_width Physical width of the output as reported by the backend
4281 * \param mm_height Physical height of the output as reported by the backend
4282 * \param transform Rotation of the output
4283 * \param scale Native scaling factor for the output
4284 *
4285 * Sets up the transformation, zoom, and geometry of the output using
4286 * the input properties.
4287 *
4288 * Establishes a repaint timer for the output with the relevant display
4289 * object's event loop. See output_repaint_timer_handler().
4290 *
4291 * The output is assigned an ID. Weston can support up to 32 distinct
4292 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4293 * is referred to and used to find the first available ID number, and
4294 * then this ID is marked as used in output_id_pool.
4295 *
4296 * The output is also assigned a Wayland global with the wl_output
4297 * external interface.
4298 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004299WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004300weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004301 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004302 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004303{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004304 struct wl_event_loop *loop;
4305
Bryce Harrington18e45732015-12-23 20:53:53 -08004306 /* Verify we haven't reached the limit of 32 available output IDs */
4307 assert(ffs(~c->output_id_pool) > 0);
4308
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004309 output->compositor = c;
4310 output->x = x;
4311 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004312 output->mm_width = mm_width;
4313 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004314 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004315 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004316
Alexander Larsson0b135062013-05-28 16:23:36 +02004317 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004318 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004319
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004320 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004321 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004322
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004323 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004324 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004325 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004326 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004327 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004328 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004329
Pekka Paalanen0513a952014-05-21 16:17:27 +03004330 loop = wl_display_get_event_loop(c->wl_display);
4331 output->repaint_timer = wl_event_loop_add_timer(loop,
4332 output_repaint_timer_handler, output);
4333
Bryce Harrington3f650b82015-12-23 11:01:58 -08004334 /* Invert the output id pool and look for the lowest numbered
4335 * switch (the least significant bit). Take that bit's position
4336 * as our ID, and mark it used in the compositor's output_id_pool.
4337 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004338 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004339 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004340
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004341 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004342 wl_global_create(c->wl_display, &wl_output_interface, 2,
4343 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004344}
4345
4346/** Adds an output to the compositor's output list and
4347 * send the compositor's output_created signal.
4348 *
4349 * \param compositor The compositor instance.
4350 * \param output The output to be added.
4351 */
4352WL_EXPORT void
4353weston_compositor_add_output(struct weston_compositor *compositor,
4354 struct weston_output *output)
4355{
4356 wl_list_insert(compositor->output_list.prev, &output->link);
4357 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004358}
4359
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004360WL_EXPORT void
4361weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004362 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004363 wl_fixed_t *x, wl_fixed_t *y)
4364{
Derek Foreman0f679412014-10-02 13:41:17 -05004365 struct weston_vector p = { {
4366 wl_fixed_to_double(device_x),
4367 wl_fixed_to_double(device_y),
4368 0.0,
4369 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004370
Derek Foreman67a18b92015-03-24 11:36:14 -05004371 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004372
Derek Foreman0f679412014-10-02 13:41:17 -05004373 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4374 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004375}
4376
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004377static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004378destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004379{
Jonny Lamb74130762013-11-26 18:19:46 +01004380 struct weston_surface *surface =
4381 wl_resource_get_user_data(resource);
4382
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004383 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004384 surface->pending.buffer_viewport.buffer.src_width =
4385 wl_fixed_from_int(-1);
4386 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004387 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004388}
4389
4390static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004391viewport_destroy(struct wl_client *client,
4392 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004393{
4394 wl_resource_destroy(resource);
4395}
4396
4397static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004398viewport_set(struct wl_client *client,
4399 struct wl_resource *resource,
4400 wl_fixed_t src_x,
4401 wl_fixed_t src_y,
4402 wl_fixed_t src_width,
4403 wl_fixed_t src_height,
4404 int32_t dst_width,
4405 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004406{
Jonny Lamb74130762013-11-26 18:19:46 +01004407 struct weston_surface *surface =
4408 wl_resource_get_user_data(resource);
4409
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004410 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004411
Jonny Lamb8ae35902013-11-26 18:19:45 +01004412 if (wl_fixed_to_double(src_width) < 0 ||
4413 wl_fixed_to_double(src_height) < 0) {
4414 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004415 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004416 "source dimensions must be non-negative (%fx%f)",
4417 wl_fixed_to_double(src_width),
4418 wl_fixed_to_double(src_height));
4419 return;
4420 }
4421
4422 if (dst_width <= 0 || dst_height <= 0) {
4423 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004424 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004425 "destination dimensions must be positive (%dx%d)",
4426 dst_width, dst_height);
4427 return;
4428 }
4429
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004430 surface->pending.buffer_viewport.buffer.src_x = src_x;
4431 surface->pending.buffer_viewport.buffer.src_y = src_y;
4432 surface->pending.buffer_viewport.buffer.src_width = src_width;
4433 surface->pending.buffer_viewport.buffer.src_height = src_height;
4434 surface->pending.buffer_viewport.surface.width = dst_width;
4435 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004436 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004437}
4438
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004439static void
4440viewport_set_source(struct wl_client *client,
4441 struct wl_resource *resource,
4442 wl_fixed_t src_x,
4443 wl_fixed_t src_y,
4444 wl_fixed_t src_width,
4445 wl_fixed_t src_height)
4446{
4447 struct weston_surface *surface =
4448 wl_resource_get_user_data(resource);
4449
4450 assert(surface->viewport_resource != NULL);
4451
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004452 if (src_width == wl_fixed_from_int(-1) &&
4453 src_height == wl_fixed_from_int(-1)) {
4454 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004455 surface->pending.buffer_viewport.buffer.src_width =
4456 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004457 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004458 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004459 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004460
4461 if (src_width <= 0 || src_height <= 0) {
4462 wl_resource_post_error(resource,
4463 WL_VIEWPORT_ERROR_BAD_VALUE,
4464 "source size must be positive (%fx%f)",
4465 wl_fixed_to_double(src_width),
4466 wl_fixed_to_double(src_height));
4467 return;
4468 }
4469
4470 surface->pending.buffer_viewport.buffer.src_x = src_x;
4471 surface->pending.buffer_viewport.buffer.src_y = src_y;
4472 surface->pending.buffer_viewport.buffer.src_width = src_width;
4473 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004474 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004475}
4476
4477static void
4478viewport_set_destination(struct wl_client *client,
4479 struct wl_resource *resource,
4480 int32_t dst_width,
4481 int32_t dst_height)
4482{
4483 struct weston_surface *surface =
4484 wl_resource_get_user_data(resource);
4485
4486 assert(surface->viewport_resource != NULL);
4487
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004488 if (dst_width == -1 && dst_height == -1) {
4489 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004490 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004491 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004492 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004493 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004494
4495 if (dst_width <= 0 || dst_height <= 0) {
4496 wl_resource_post_error(resource,
4497 WL_VIEWPORT_ERROR_BAD_VALUE,
4498 "destination size must be positive (%dx%d)",
4499 dst_width, dst_height);
4500 return;
4501 }
4502
4503 surface->pending.buffer_viewport.surface.width = dst_width;
4504 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004505 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004506}
4507
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004508static const struct wl_viewport_interface viewport_interface = {
4509 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004510 viewport_set,
4511 viewport_set_source,
4512 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004513};
4514
4515static void
4516scaler_destroy(struct wl_client *client,
4517 struct wl_resource *resource)
4518{
4519 wl_resource_destroy(resource);
4520}
4521
4522static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004523scaler_get_viewport(struct wl_client *client,
4524 struct wl_resource *scaler,
4525 uint32_t id,
4526 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004527{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004528 int version = wl_resource_get_version(scaler);
4529 struct weston_surface *surface =
4530 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004531 struct wl_resource *resource;
4532
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004533 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004534 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004535 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4536 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004537 return;
4538 }
4539
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004540 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004541 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004542 if (resource == NULL) {
4543 wl_client_post_no_memory(client);
4544 return;
4545 }
4546
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004547 wl_resource_set_implementation(resource, &viewport_interface,
4548 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004549
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004550 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004551}
4552
4553static const struct wl_scaler_interface scaler_interface = {
4554 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004555 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004556};
4557
4558static void
4559bind_scaler(struct wl_client *client,
4560 void *data, uint32_t version, uint32_t id)
4561{
4562 struct wl_resource *resource;
4563
4564 resource = wl_resource_create(client, &wl_scaler_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004565 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004566 if (resource == NULL) {
4567 wl_client_post_no_memory(client);
4568 return;
4569 }
4570
4571 wl_resource_set_implementation(resource, &scaler_interface,
4572 NULL, NULL);
4573}
4574
4575static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004576destroy_presentation_feedback(struct wl_resource *feedback_resource)
4577{
4578 struct weston_presentation_feedback *feedback;
4579
4580 feedback = wl_resource_get_user_data(feedback_resource);
4581
4582 wl_list_remove(&feedback->link);
4583 free(feedback);
4584}
4585
4586static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004587presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4588{
4589 wl_resource_destroy(resource);
4590}
4591
4592static void
4593presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004594 struct wl_resource *presentation_resource,
4595 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004596 uint32_t callback)
4597{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004598 struct weston_surface *surface;
4599 struct weston_presentation_feedback *feedback;
4600
4601 surface = wl_resource_get_user_data(surface_resource);
4602
Bryce Harringtonde16d892014-11-20 22:21:57 -08004603 feedback = zalloc(sizeof *feedback);
4604 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004605 goto err_calloc;
4606
4607 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004608 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004609 1, callback);
4610 if (!feedback->resource)
4611 goto err_create;
4612
4613 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4614 destroy_presentation_feedback);
4615
4616 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4617
4618 return;
4619
4620err_create:
4621 free(feedback);
4622
4623err_calloc:
4624 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004625}
4626
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004627static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004628 presentation_destroy,
4629 presentation_feedback
4630};
4631
4632static void
4633bind_presentation(struct wl_client *client,
4634 void *data, uint32_t version, uint32_t id)
4635{
4636 struct weston_compositor *compositor = data;
4637 struct wl_resource *resource;
4638
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004639 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004640 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004641 if (resource == NULL) {
4642 wl_client_post_no_memory(client);
4643 return;
4644 }
4645
4646 wl_resource_set_implementation(resource, &presentation_implementation,
4647 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004648 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004649}
4650
4651static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004652compositor_bind(struct wl_client *client,
4653 void *data, uint32_t version, uint32_t id)
4654{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004655 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004656 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004657
Jason Ekstranda85118c2013-06-27 20:17:02 -05004658 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004659 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004660 if (resource == NULL) {
4661 wl_client_post_no_memory(client);
4662 return;
4663 }
4664
4665 wl_resource_set_implementation(resource, &compositor_interface,
4666 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004667}
4668
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004669WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004670weston_environment_get_fd(const char *env)
4671{
4672 char *e, *end;
4673 int fd, flags;
4674
4675 e = getenv(env);
4676 if (!e)
4677 return -1;
4678 fd = strtol(e, &end, 0);
4679 if (*end != '\0')
4680 return -1;
4681
4682 flags = fcntl(fd, F_GETFD);
4683 if (flags == -1)
4684 return -1;
4685
4686 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4687 unsetenv(env);
4688
4689 return fd;
4690}
4691
Pekka Paalanenb5026542014-11-12 15:09:24 +02004692static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004693timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004694 uint32_t key, void *data)
4695{
4696 struct weston_compositor *compositor = data;
4697
4698 if (weston_timeline_enabled_)
4699 weston_timeline_close();
4700 else
4701 weston_timeline_open(compositor);
4702}
4703
Giulio Camuffo459137b2014-10-11 23:56:24 +03004704/** Create the compositor.
4705 *
4706 * This functions creates and initializes a compositor instance.
4707 *
4708 * \param display The Wayland display to be used.
4709 * \param user_data A pointer to an object that can later be retrieved
4710 * using the \ref weston_compositor_get_user_data function.
4711 * \return The compositor instance on success or NULL on failure.
4712 */
4713WL_EXPORT struct weston_compositor *
4714weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004715{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004716 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004717 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004718
Giulio Camuffo459137b2014-10-11 23:56:24 +03004719 ec = zalloc(sizeof *ec);
4720 if (!ec)
4721 return NULL;
4722
4723 ec->wl_display = display;
4724 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004725 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004726 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004727 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004728 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004729 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004730 wl_signal_init(&ec->idle_signal);
4731 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004732 wl_signal_init(&ec->show_input_panel_signal);
4733 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004734 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004735 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004736 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004737 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004738 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004739 wl_signal_init(&ec->session_signal);
4740 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004741
Casey Dahlin58ba1372012-04-19 22:50:08 -04004742 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004743 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004744
Derek Foreman152254b2015-11-26 14:17:48 -06004745 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004746 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004747 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004748
Giulio Camuffo954f1832014-10-11 18:27:30 +03004749 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004750 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004751 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004752
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004753 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004754 ec, bind_scaler))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004755 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004756
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004757 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004758 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004759 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004760
Jason Ekstranda7af7042013-10-12 22:38:11 -05004761 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004762 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004763 wl_list_init(&ec->layer_list);
4764 wl_list_init(&ec->seat_list);
4765 wl_list_init(&ec->output_list);
4766 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004767 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004768 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004769 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004770 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004771 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004772
Xiong Zhang97116532013-10-23 13:58:31 +08004773 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004774 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004775
Giulio Camuffo459137b2014-10-11 23:56:24 +03004776 wl_data_device_manager_init(ec->wl_display);
4777
4778 wl_display_init_shm(ec->wl_display);
4779
4780 loop = wl_display_get_event_loop(ec->wl_display);
4781 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004782
4783 ec->input_loop = wl_event_loop_create();
4784
4785 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4786 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4787
4788 weston_compositor_add_debug_binding(ec, KEY_T,
4789 timeline_key_binding_handler, ec);
4790
Giulio Camuffo459137b2014-10-11 23:56:24 +03004791 return ec;
4792
4793fail:
4794 free(ec);
4795 return NULL;
4796}
4797
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004798WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004799weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004800{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004801 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004802
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004803 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004804 if (ec->input_loop_source)
4805 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004806
Matt Roper361d2ad2011-08-29 13:52:23 -07004807 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004808 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004809 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004810
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004811 if (ec->renderer)
4812 ec->renderer->destroy(ec);
4813
Daniel Stone325fc2d2012-05-30 16:31:58 +01004814 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004815 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004816 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004817 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004818 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004819 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004820
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004821 weston_plane_release(&ec->primary_plane);
4822
Jonas Ådahlc97af922012-03-28 22:36:09 +02004823 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07004824}
4825
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004826WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004827weston_compositor_exit_with_code(struct weston_compositor *compositor,
4828 int exit_code)
4829{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004830 if (compositor->exit_code == EXIT_SUCCESS)
4831 compositor->exit_code = exit_code;
4832
Giulio Camuffo459137b2014-10-11 23:56:24 +03004833 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004834}
4835
4836WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004837weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4838 const struct weston_pointer_grab_interface *interface)
4839{
4840 struct weston_seat *seat;
4841
4842 ec->default_pointer_grab = interface;
4843 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004844 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4845
4846 if (pointer)
4847 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004848 }
4849}
4850
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004851WL_EXPORT int
4852weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4853 clockid_t clk_id)
4854{
4855 struct timespec ts;
4856
4857 if (clock_gettime(clk_id, &ts) < 0)
4858 return -1;
4859
4860 compositor->presentation_clock = clk_id;
4861
4862 return 0;
4863}
4864
4865/*
4866 * For choosing the software clock, when the display hardware or API
4867 * does not expose a compatible presentation timestamp.
4868 */
4869WL_EXPORT int
4870weston_compositor_set_presentation_clock_software(
4871 struct weston_compositor *compositor)
4872{
4873 /* In order of preference */
4874 static const clockid_t clocks[] = {
4875 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4876 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4877 CLOCK_MONOTONIC, /* no jumps, may crawl */
4878 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4879 CLOCK_REALTIME /* may jump and crawl */
4880 };
4881 unsigned i;
4882
4883 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4884 if (weston_compositor_set_presentation_clock(compositor,
4885 clocks[i]) == 0)
4886 return 0;
4887
4888 weston_log("Error: no suitable presentation clock available.\n");
4889
4890 return -1;
4891}
4892
Pekka Paalanen662f3842015-03-18 12:17:26 +02004893/** Read the current time from the Presentation clock
4894 *
4895 * \param compositor
4896 * \param ts[out] The current time.
4897 *
4898 * \note Reading the current time in user space is always imprecise to some
4899 * degree.
4900 *
4901 * This function is never meant to fail. If reading the clock does fail,
4902 * an error message is logged and a zero time is returned. Callers are not
4903 * supposed to detect or react to failures.
4904 */
4905WL_EXPORT void
4906weston_compositor_read_presentation_clock(
4907 const struct weston_compositor *compositor,
4908 struct timespec *ts)
4909{
4910 static bool warned;
4911 int ret;
4912
4913 ret = clock_gettime(compositor->presentation_clock, ts);
4914 if (ret < 0) {
4915 ts->tv_sec = 0;
4916 ts->tv_nsec = 0;
4917
4918 if (!warned)
4919 weston_log("Error: failure to read "
4920 "the presentation clock %#x: '%m' (%d)\n",
4921 compositor->presentation_clock, errno);
4922 warned = true;
4923 }
4924}
4925
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004926/** Import dmabuf buffer into current renderer
4927 *
4928 * \param compositor
4929 * \param buffer the dmabuf buffer to import
4930 * \return true on usable buffers, false otherwise
4931 *
4932 * This function tests that the linux_dmabuf_buffer is usable
4933 * for the current renderer. Returns false on unusable buffers. Usually
4934 * usability is tested by importing the dmabufs for composition.
4935 *
4936 * This hook is also used for detecting if the renderer supports
4937 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4938 * supported.
4939 * */
4940WL_EXPORT bool
4941weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4942 struct linux_dmabuf_buffer *buffer)
4943{
4944 struct weston_renderer *renderer;
4945
4946 renderer = compositor->renderer;
4947
4948 if (renderer->import_dmabuf == NULL)
4949 return false;
4950
4951 return renderer->import_dmabuf(compositor, buffer);
4952}
4953
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004954WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004955weston_version(int *major, int *minor, int *micro)
4956{
4957 *major = WESTON_VERSION_MAJOR;
4958 *minor = WESTON_VERSION_MINOR;
4959 *micro = WESTON_VERSION_MICRO;
4960}
4961
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004962WL_EXPORT void *
4963weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004964{
Derek Foreman3f86e502015-06-08 11:46:54 -05004965 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004966 char path[PATH_MAX];
4967 void *module, *init;
4968
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004969 if (name == NULL)
4970 return NULL;
4971
Derek Foreman3f86e502015-06-08 11:46:54 -05004972 if (name[0] != '/') {
4973 if (builddir)
4974 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4975 else
4976 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
4977 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004978 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004979 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004980
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004981 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4982 if (module) {
4983 weston_log("Module '%s' already loaded\n", path);
4984 dlclose(module);
4985 return NULL;
4986 }
4987
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004988 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004989 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004990 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004991 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004992 return NULL;
4993 }
4994
4995 init = dlsym(module, entrypoint);
4996 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004997 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004998 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004999 return NULL;
5000 }
5001
5002 return init;
5003}
5004
Giulio Camuffo459137b2014-10-11 23:56:24 +03005005
5006/** Destroys the compositor.
5007 *
5008 * This function cleans up the compositor state and destroys it.
5009 *
5010 * \param compositor The compositor to be destroyed.
5011 */
5012WL_EXPORT void
5013weston_compositor_destroy(struct weston_compositor *compositor)
5014{
5015 /* prevent further rendering while shutting down */
5016 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5017
5018 wl_signal_emit(&compositor->destroy_signal, compositor);
5019
5020 weston_compositor_xkb_destroy(compositor);
5021
Giulio Camuffo2d24e642015-10-03 16:25:15 +03005022 if (compositor->backend)
5023 compositor->backend->destroy(compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005024 free(compositor);
5025}
5026
5027/** Instruct the compositor to exit.
5028 *
5029 * This functions does not directly destroy the compositor object, it merely
5030 * command it to start the tear down process. It is not guaranteed that the
5031 * tear down will happen immediately.
5032 *
5033 * \param compositor The compositor to tear down.
5034 */
5035WL_EXPORT void
5036weston_compositor_exit(struct weston_compositor *compositor)
5037{
5038 compositor->exit(compositor);
5039}
5040
5041/** Return the user data stored in the compositor.
5042 *
5043 * This function returns the user data pointer set with user_data parameter
5044 * to the \ref weston_compositor_create function.
5045 */
5046WL_EXPORT void *
5047weston_compositor_get_user_data(struct weston_compositor *compositor)
5048{
5049 return compositor->user_data;
5050}