blob: 541829caf9aa7f4d553d81bfd18bac1bf138505a [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004 * Copyright © 2012-2015 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050026 */
27
Kristian Høgsberga9410222011-01-14 17:22:35 -050028#include "config.h"
29
Daniel Stoneb7452fe2012-06-01 12:14:06 +010030#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010035#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020037#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040038#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010039#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040040#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020041#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020042#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020043#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050045#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040046#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040047#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040048#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050049#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040050#include <sys/time.h>
51#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030052#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050053
Pekka Paalanenb5026542014-11-12 15:09:24 +020054#include "timeline.h"
55
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010057#include "scaler-server-protocol.h"
Pekka Paalanen31f7d782014-09-23 22:08:43 -040058#include "presentation_timing-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070059#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070060#include "shared/os-compatibility.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030061#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040062#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050063#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050064
Pekka Paalanen0513a952014-05-21 16:17:27 +030065#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
66
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020067static void
Alexander Larsson0b135062013-05-28 16:23:36 +020068weston_output_transform_scale_init(struct weston_output *output,
69 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020070
Rob Bradford27b17932013-06-26 18:08:46 +010071static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050072weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010073
Derek Foreman6ae7bc92014-11-04 10:47:33 -060074static void weston_mode_switch_finish(struct weston_output *output,
75 int mode_changed,
76 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080077{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020078 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020079 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020080 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060081 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020082
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020083 pixman_region32_init(&old_output_region);
84 pixman_region32_copy(&old_output_region, &output->region);
85
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020086 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020087 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020088
89 pixman_region32_init(&output->previous_damage);
90 pixman_region32_init_rect(&output->region, output->x, output->y,
91 output->width, output->height);
92
93 weston_output_update_matrix(output);
94
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020095 /* If a pointer falls outside the outputs new geometry, move it to its
96 * lower-right corner */
97 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -050098 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020099 int32_t x, y;
100
101 if (!pointer)
102 continue;
103
104 x = wl_fixed_to_int(pointer->x);
105 y = wl_fixed_to_int(pointer->y);
106
107 if (!pixman_region32_contains_point(&old_output_region,
108 x, y, NULL) ||
109 pixman_region32_contains_point(&output->region,
110 x, y, NULL))
111 continue;
112
113 if (x >= output->x + output->width)
114 x = output->x + output->width - 1;
115 if (y >= output->y + output->height)
116 y = output->y + output->height - 1;
117
118 pointer->x = wl_fixed_from_int(x);
119 pointer->y = wl_fixed_from_int(y);
120 }
121
122 pixman_region32_fini(&old_output_region);
123
Derek Foremandd4cd332014-11-10 10:29:59 -0600124 if (!mode_changed && !scale_changed)
125 return;
126
Hardening57388e42013-09-18 23:56:36 +0200127 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600128 wl_resource_for_each(resource, &output->resource_list) {
129 if (mode_changed) {
130 wl_output_send_mode(resource,
131 output->current_mode->flags,
132 output->current_mode->width,
133 output->current_mode->height,
134 output->current_mode->refresh);
135 }
Hardening57388e42013-09-18 23:56:36 +0200136
Derek Foreman41bdc272014-11-05 13:26:57 -0600137 version = wl_resource_get_version(resource);
138 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600139 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200140
Derek Foreman41bdc272014-11-05 13:26:57 -0600141 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
142 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600143 }
144}
145
146WL_EXPORT int
147weston_output_mode_set_native(struct weston_output *output,
148 struct weston_mode *mode,
149 int32_t scale)
150{
151 int ret;
152 int mode_changed = 0, scale_changed = 0;
153
154 if (!output->switch_mode)
155 return -1;
156
157 if (!output->original_mode) {
158 mode_changed = 1;
159 ret = output->switch_mode(output, mode);
160 if (ret < 0)
161 return ret;
162 if (output->current_scale != scale) {
163 scale_changed = 1;
164 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200165 }
166 }
167
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600168 output->native_mode = mode;
169 output->native_scale = scale;
170
171 weston_mode_switch_finish(output, mode_changed, scale_changed);
172
173 return 0;
174}
175
176WL_EXPORT int
177weston_output_mode_switch_to_native(struct weston_output *output)
178{
179 int ret;
180 int mode_changed = 0, scale_changed = 0;
181
182 if (!output->switch_mode)
183 return -1;
184
185 if (!output->original_mode) {
186 weston_log("already in the native mode\n");
187 return -1;
188 }
189 /* the non fullscreen clients haven't seen a mode set since we
190 * switched into a temporary, so we need to notify them if the
191 * mode at that time is different from the native mode now.
192 */
193 mode_changed = (output->original_mode != output->native_mode);
194 scale_changed = (output->original_scale != output->native_scale);
195
196 ret = output->switch_mode(output, output->native_mode);
197 if (ret < 0)
198 return ret;
199
200 output->current_scale = output->native_scale;
201
202 output->original_mode = NULL;
203 output->original_scale = 0;
204
205 weston_mode_switch_finish(output, mode_changed, scale_changed);
206
207 return 0;
208}
209
210WL_EXPORT int
211weston_output_mode_switch_to_temporary(struct weston_output *output,
212 struct weston_mode *mode,
213 int32_t scale)
214{
215 int ret;
216
217 if (!output->switch_mode)
218 return -1;
219
220 /* original_mode is the last mode non full screen clients have seen,
221 * so we shouldn't change it if we already have one set.
222 */
223 if (!output->original_mode) {
224 output->original_mode = output->native_mode;
225 output->original_scale = output->native_scale;
226 }
227 ret = output->switch_mode(output, mode);
228 if (ret < 0)
229 return ret;
230
231 output->current_scale = scale;
232
233 weston_mode_switch_finish(output, 0, 0);
234
235 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800236}
237
Benjamin Franzke06286262011-05-06 19:12:33 +0200238static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200239child_client_exec(int sockfd, const char *path)
240{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500241 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200243 sigset_t allsigs;
244
245 /* do not give our signal mask to the new process */
246 sigfillset(&allsigs);
247 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200248
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100249 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
250 if (seteuid(getuid()) == -1) {
251 weston_log("compositor: failed seteuid\n");
252 return;
253 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500254
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500255 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
256 * non-CLOEXEC fd to pass through exec. */
257 clientfd = dup(sockfd);
258 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200259 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500260 return;
261 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200262
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500263 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200264 setenv("WAYLAND_SOCKET", s, 1);
265
266 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200267 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200268 path);
269}
270
271WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272weston_client_launch(struct weston_compositor *compositor,
273 struct weston_process *proc,
274 const char *path,
275 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200276{
277 int sv[2];
278 pid_t pid;
279 struct wl_client *client;
280
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300281 weston_log("launching '%s'\n", path);
282
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300283 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200284 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200285 "socketpair failed while launching '%s': %m\n",
286 path);
287 return NULL;
288 }
289
290 pid = fork();
291 if (pid == -1) {
292 close(sv[0]);
293 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200295 "fork failed while launching '%s': %m\n", path);
296 return NULL;
297 }
298
299 if (pid == 0) {
300 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700301 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200302 }
303
304 close(sv[1]);
305
306 client = wl_client_create(compositor->wl_display, sv[0]);
307 if (!client) {
308 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200310 "wl_client_create failed while launching '%s'.\n",
311 path);
312 return NULL;
313 }
314
315 proc->pid = pid;
316 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200318
319 return client;
320}
321
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300322struct process_info {
323 struct weston_process proc;
324 char *path;
325};
326
327static void
328process_handle_sigchld(struct weston_process *process, int status)
329{
330 struct process_info *pinfo =
331 container_of(process, struct process_info, proc);
332
333 /*
334 * There are no guarantees whether this runs before or after
335 * the wl_client destructor.
336 */
337
338 if (WIFEXITED(status)) {
339 weston_log("%s exited with status %d\n", pinfo->path,
340 WEXITSTATUS(status));
341 } else if (WIFSIGNALED(status)) {
342 weston_log("%s died on signal %d\n", pinfo->path,
343 WTERMSIG(status));
344 } else {
345 weston_log("%s disappeared\n", pinfo->path);
346 }
347
348 free(pinfo->path);
349 free(pinfo);
350}
351
352WL_EXPORT struct wl_client *
353weston_client_start(struct weston_compositor *compositor, const char *path)
354{
355 struct process_info *pinfo;
356 struct wl_client *client;
357
358 pinfo = zalloc(sizeof *pinfo);
359 if (!pinfo)
360 return NULL;
361
362 pinfo->path = strdup(path);
363 if (!pinfo->path)
364 goto out_free;
365
366 client = weston_client_launch(compositor, &pinfo->proc, path,
367 process_handle_sigchld);
368 if (!client)
369 goto out_str;
370
371 return client;
372
373out_str:
374 free(pinfo->path);
375
376out_free:
377 free(pinfo);
378
379 return NULL;
380}
381
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200382static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300383region_init_infinite(pixman_region32_t *region)
384{
385 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
386 UINT32_MAX, UINT32_MAX);
387}
388
Pekka Paalanene67858b2013-04-25 13:57:42 +0300389static struct weston_subsurface *
390weston_surface_to_subsurface(struct weston_surface *surface);
391
Jason Ekstranda7af7042013-10-12 22:38:11 -0500392WL_EXPORT struct weston_view *
393weston_view_create(struct weston_surface *surface)
394{
395 struct weston_view *view;
396
Bryce Harringtonde16d892014-11-20 22:21:57 -0800397 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500398 if (view == NULL)
399 return NULL;
400
401 view->surface = surface;
402
Jason Ekstranda7af7042013-10-12 22:38:11 -0500403 /* Assign to surface */
404 wl_list_insert(&surface->views, &view->surface_link);
405
406 wl_signal_init(&view->destroy_signal);
407 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300408 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500409
Jason Ekstranda7af7042013-10-12 22:38:11 -0500410 pixman_region32_init(&view->clip);
411
412 view->alpha = 1.0;
413 pixman_region32_init(&view->transform.opaque);
414
415 wl_list_init(&view->geometry.transformation_list);
416 wl_list_insert(&view->geometry.transformation_list,
417 &view->transform.position.link);
418 weston_matrix_init(&view->transform.position.matrix);
419 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200420 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500421 pixman_region32_init(&view->transform.boundingbox);
422 view->transform.dirty = 1;
423
Jason Ekstranda7af7042013-10-12 22:38:11 -0500424 return view;
425}
426
Jason Ekstrand108865d2014-06-26 10:04:49 -0700427struct weston_frame_callback {
428 struct wl_resource *resource;
429 struct wl_list link;
430};
431
Pekka Paalanen133e4392014-09-23 22:08:46 -0400432struct weston_presentation_feedback {
433 struct wl_resource *resource;
434
435 /* XXX: could use just wl_resource_get_link() instead */
436 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200437
438 /* The per-surface feedback flags */
439 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400440};
441
442static void
443weston_presentation_feedback_discard(
444 struct weston_presentation_feedback *feedback)
445{
446 presentation_feedback_send_discarded(feedback->resource);
447 wl_resource_destroy(feedback->resource);
448}
449
450static void
451weston_presentation_feedback_discard_list(struct wl_list *list)
452{
453 struct weston_presentation_feedback *feedback, *tmp;
454
455 wl_list_for_each_safe(feedback, tmp, list, link)
456 weston_presentation_feedback_discard(feedback);
457}
458
459static void
460weston_presentation_feedback_present(
461 struct weston_presentation_feedback *feedback,
462 struct weston_output *output,
463 uint32_t refresh_nsec,
464 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200465 uint64_t seq,
466 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400467{
468 struct wl_client *client = wl_resource_get_client(feedback->resource);
469 struct wl_resource *o;
470 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400471
472 wl_resource_for_each(o, &output->resource_list) {
473 if (wl_resource_get_client(o) != client)
474 continue;
475
476 presentation_feedback_send_sync_output(feedback->resource, o);
477 }
478
479 secs = ts->tv_sec;
480 presentation_feedback_send_presented(feedback->resource,
481 secs >> 32, secs & 0xffffffff,
482 ts->tv_nsec,
483 refresh_nsec,
484 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200485 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400486 wl_resource_destroy(feedback->resource);
487}
488
489static void
490weston_presentation_feedback_present_list(struct wl_list *list,
491 struct weston_output *output,
492 uint32_t refresh_nsec,
493 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200494 uint64_t seq,
495 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400496{
497 struct weston_presentation_feedback *feedback, *tmp;
498
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200499 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
500 wl_list_empty(list));
501
Pekka Paalanen133e4392014-09-23 22:08:46 -0400502 wl_list_for_each_safe(feedback, tmp, list, link)
503 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200504 refresh_nsec, ts, seq,
505 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400506}
507
Jason Ekstrand7b982072014-05-20 14:33:03 -0500508static void
509surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
510{
511 struct weston_surface_state *state =
512 container_of(listener, struct weston_surface_state,
513 buffer_destroy_listener);
514
515 state->buffer = NULL;
516}
517
518static void
519weston_surface_state_init(struct weston_surface_state *state)
520{
521 state->newly_attached = 0;
522 state->buffer = NULL;
523 state->buffer_destroy_listener.notify =
524 surface_state_handle_buffer_destroy;
525 state->sx = 0;
526 state->sy = 0;
527
528 pixman_region32_init(&state->damage);
529 pixman_region32_init(&state->opaque);
530 region_init_infinite(&state->input);
531
532 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400533 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500534
535 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
536 state->buffer_viewport.buffer.scale = 1;
537 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
538 state->buffer_viewport.surface.width = -1;
539 state->buffer_viewport.changed = 0;
540}
541
542static void
543weston_surface_state_fini(struct weston_surface_state *state)
544{
545 struct weston_frame_callback *cb, *next;
546
547 wl_list_for_each_safe(cb, next,
548 &state->frame_callback_list, link)
549 wl_resource_destroy(cb->resource);
550
Pekka Paalanen133e4392014-09-23 22:08:46 -0400551 weston_presentation_feedback_discard_list(&state->feedback_list);
552
Jason Ekstrand7b982072014-05-20 14:33:03 -0500553 pixman_region32_fini(&state->input);
554 pixman_region32_fini(&state->opaque);
555 pixman_region32_fini(&state->damage);
556
557 if (state->buffer)
558 wl_list_remove(&state->buffer_destroy_listener.link);
559 state->buffer = NULL;
560}
561
562static void
563weston_surface_state_set_buffer(struct weston_surface_state *state,
564 struct weston_buffer *buffer)
565{
566 if (state->buffer == buffer)
567 return;
568
569 if (state->buffer)
570 wl_list_remove(&state->buffer_destroy_listener.link);
571 state->buffer = buffer;
572 if (state->buffer)
573 wl_signal_add(&state->buffer->destroy_signal,
574 &state->buffer_destroy_listener);
575}
576
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500577WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500578weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500579{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500580 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400581
Bryce Harringtonde16d892014-11-20 22:21:57 -0800582 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400583 if (surface == NULL)
584 return NULL;
585
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500586 wl_signal_init(&surface->destroy_signal);
587
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500588 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200589 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400590
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200591 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
592 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200593 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
594 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500595
596 weston_surface_state_init(&surface->pending);
597
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400598 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500599 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300600 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400601
Jason Ekstranda7af7042013-10-12 22:38:11 -0500602 wl_list_init(&surface->views);
603
604 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400605 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500606
Pekka Paalanene67858b2013-04-25 13:57:42 +0300607 wl_list_init(&surface->subsurface_list);
608 wl_list_init(&surface->subsurface_list_pending);
609
Jason Ekstrand1e059042014-10-16 10:55:19 -0500610 weston_matrix_init(&surface->buffer_to_surface_matrix);
611 weston_matrix_init(&surface->surface_to_buffer_matrix);
612
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400613 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500614}
615
Alex Wu8811bf92012-02-28 18:07:54 +0800616WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500617weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200618 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500619{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100620 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500621}
622
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400623WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500624weston_view_to_global_float(struct weston_view *view,
625 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200626{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500627 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200628 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
629
Jason Ekstranda7af7042013-10-12 22:38:11 -0500630 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200631
632 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200633 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700634 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200635 v.f[3]);
636 *x = 0;
637 *y = 0;
638 return;
639 }
640
641 *x = v.f[0] / v.f[3];
642 *y = v.f[1] / v.f[3];
643 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500644 *x = sx + view->geometry.x;
645 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200646 }
647}
648
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500649WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200650weston_transformed_coord(int width, int height,
651 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200652 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200653 float sx, float sy, float *bx, float *by)
654{
655 switch (transform) {
656 case WL_OUTPUT_TRANSFORM_NORMAL:
657 default:
658 *bx = sx;
659 *by = sy;
660 break;
661 case WL_OUTPUT_TRANSFORM_FLIPPED:
662 *bx = width - sx;
663 *by = sy;
664 break;
665 case WL_OUTPUT_TRANSFORM_90:
666 *bx = height - sy;
667 *by = sx;
668 break;
669 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
670 *bx = height - sy;
671 *by = width - sx;
672 break;
673 case WL_OUTPUT_TRANSFORM_180:
674 *bx = width - sx;
675 *by = height - sy;
676 break;
677 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
678 *bx = sx;
679 *by = height - sy;
680 break;
681 case WL_OUTPUT_TRANSFORM_270:
682 *bx = sy;
683 *by = width - sx;
684 break;
685 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
686 *bx = sy;
687 *by = sx;
688 break;
689 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200690
691 *bx *= scale;
692 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200693}
694
695WL_EXPORT pixman_box32_t
696weston_transformed_rect(int width, int height,
697 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200698 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200699 pixman_box32_t rect)
700{
701 float x1, x2, y1, y2;
702
703 pixman_box32_t ret;
704
Alexander Larsson4ea95522013-05-22 14:41:37 +0200705 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200706 rect.x1, rect.y1, &x1, &y1);
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.x2, rect.y2, &x2, &y2);
709
710 if (x1 <= x2) {
711 ret.x1 = x1;
712 ret.x2 = x2;
713 } else {
714 ret.x1 = x2;
715 ret.x2 = x1;
716 }
717
718 if (y1 <= y2) {
719 ret.y1 = y1;
720 ret.y2 = y2;
721 } else {
722 ret.y1 = y2;
723 ret.y2 = y1;
724 }
725
726 return ret;
727}
728
729WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500730weston_transformed_region(int width, int height,
731 enum wl_output_transform transform,
732 int32_t scale,
733 pixman_region32_t *src, pixman_region32_t *dest)
734{
735 pixman_box32_t *src_rects, *dest_rects;
736 int nrects, i;
737
738 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
739 if (src != dest)
740 pixman_region32_copy(dest, src);
741 return;
742 }
743
744 src_rects = pixman_region32_rectangles(src, &nrects);
745 dest_rects = malloc(nrects * sizeof(*dest_rects));
746 if (!dest_rects)
747 return;
748
749 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
750 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
751 } else {
752 for (i = 0; i < nrects; i++) {
753 switch (transform) {
754 default:
755 case WL_OUTPUT_TRANSFORM_NORMAL:
756 dest_rects[i].x1 = src_rects[i].x1;
757 dest_rects[i].y1 = src_rects[i].y1;
758 dest_rects[i].x2 = src_rects[i].x2;
759 dest_rects[i].y2 = src_rects[i].y2;
760 break;
761 case WL_OUTPUT_TRANSFORM_90:
762 dest_rects[i].x1 = height - src_rects[i].y2;
763 dest_rects[i].y1 = src_rects[i].x1;
764 dest_rects[i].x2 = height - src_rects[i].y1;
765 dest_rects[i].y2 = src_rects[i].x2;
766 break;
767 case WL_OUTPUT_TRANSFORM_180:
768 dest_rects[i].x1 = width - src_rects[i].x2;
769 dest_rects[i].y1 = height - src_rects[i].y2;
770 dest_rects[i].x2 = width - src_rects[i].x1;
771 dest_rects[i].y2 = height - src_rects[i].y1;
772 break;
773 case WL_OUTPUT_TRANSFORM_270:
774 dest_rects[i].x1 = src_rects[i].y1;
775 dest_rects[i].y1 = width - src_rects[i].x2;
776 dest_rects[i].x2 = src_rects[i].y2;
777 dest_rects[i].y2 = width - src_rects[i].x1;
778 break;
779 case WL_OUTPUT_TRANSFORM_FLIPPED:
780 dest_rects[i].x1 = width - src_rects[i].x2;
781 dest_rects[i].y1 = src_rects[i].y1;
782 dest_rects[i].x2 = width - src_rects[i].x1;
783 dest_rects[i].y2 = src_rects[i].y2;
784 break;
785 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
786 dest_rects[i].x1 = height - src_rects[i].y2;
787 dest_rects[i].y1 = width - src_rects[i].x2;
788 dest_rects[i].x2 = height - src_rects[i].y1;
789 dest_rects[i].y2 = width - src_rects[i].x1;
790 break;
791 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
792 dest_rects[i].x1 = src_rects[i].x1;
793 dest_rects[i].y1 = height - src_rects[i].y2;
794 dest_rects[i].x2 = src_rects[i].x2;
795 dest_rects[i].y2 = height - src_rects[i].y1;
796 break;
797 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
798 dest_rects[i].x1 = src_rects[i].y1;
799 dest_rects[i].y1 = src_rects[i].x1;
800 dest_rects[i].x2 = src_rects[i].y2;
801 dest_rects[i].y2 = src_rects[i].x2;
802 break;
803 }
804 }
805 }
806
807 if (scale != 1) {
808 for (i = 0; i < nrects; i++) {
809 dest_rects[i].x1 *= scale;
810 dest_rects[i].x2 *= scale;
811 dest_rects[i].y1 *= scale;
812 dest_rects[i].y2 *= scale;
813 }
814 }
815
816 pixman_region32_clear(dest);
817 pixman_region32_init_rects(dest, dest_rects, nrects);
818 free(dest_rects);
819}
820
Jonny Lamb74130762013-11-26 18:19:46 +0100821static void
822scaler_surface_to_buffer(struct weston_surface *surface,
823 float sx, float sy, float *bx, float *by)
824{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200825 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200826 double src_width, src_height;
827 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200828
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200829 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
830 if (vp->surface.width == -1) {
831 *bx = sx;
832 *by = sy;
833 return;
834 }
Jonny Lamb74130762013-11-26 18:19:46 +0100835
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200836 src_x = 0.0;
837 src_y = 0.0;
838 src_width = surface->width_from_buffer;
839 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100840 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200841 src_x = wl_fixed_to_double(vp->buffer.src_x);
842 src_y = wl_fixed_to_double(vp->buffer.src_y);
843 src_width = wl_fixed_to_double(vp->buffer.src_width);
844 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100845 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200846
847 *bx = sx * src_width / surface->width + src_x;
848 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100849}
850
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500851WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200852weston_surface_to_buffer_float(struct weston_surface *surface,
853 float sx, float sy, float *bx, float *by)
854{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200855 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
856
Jonny Lamb74130762013-11-26 18:19:46 +0100857 /* first transform coordinates if the scaler is set */
858 scaler_surface_to_buffer(surface, sx, sy, bx, by);
859
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500860 weston_transformed_coord(surface->width_from_buffer,
861 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200862 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100863 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200864}
865
Alexander Larsson4ea95522013-05-22 14:41:37 +0200866WL_EXPORT void
867weston_surface_to_buffer(struct weston_surface *surface,
868 int sx, int sy, int *bx, int *by)
869{
870 float bxf, byf;
871
Jonny Lamb74130762013-11-26 18:19:46 +0100872 weston_surface_to_buffer_float(surface,
873 sx, sy, &bxf, &byf);
874
Alexander Larsson4ea95522013-05-22 14:41:37 +0200875 *bx = floorf(bxf);
876 *by = floorf(byf);
877}
878
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200879WL_EXPORT pixman_box32_t
880weston_surface_to_buffer_rect(struct weston_surface *surface,
881 pixman_box32_t rect)
882{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200883 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100884 float xf, yf;
885
886 /* first transform box coordinates if the scaler is set */
887 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
888 rect.x1 = floorf(xf);
889 rect.y1 = floorf(yf);
890
891 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
892 rect.x2 = floorf(xf);
893 rect.y2 = floorf(yf);
894
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500895 return weston_transformed_rect(surface->width_from_buffer,
896 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200897 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200898 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200899}
900
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200901/** Transform a region from surface coordinates to buffer coordinates
902 *
903 * \param surface The surface to fetch wl_viewport and buffer transformation
904 * from.
905 * \param surface_region[in] The region in surface coordinates.
906 * \param buffer_region[out] The region converted to buffer coordinates.
907 *
908 * Buffer_region must be init'd, but will be completely overwritten.
909 *
910 * Viewport and buffer transformations can only do translation, scaling,
911 * and rotations in 90-degree steps. Therefore the only loss in the
912 * conversion is coordinate flooring (rounding).
913 */
914WL_EXPORT void
915weston_surface_to_buffer_region(struct weston_surface *surface,
916 pixman_region32_t *surface_region,
917 pixman_region32_t *buffer_region)
918{
919 pixman_box32_t *src_rects, *dest_rects;
920 int nrects, i;
921
922 src_rects = pixman_region32_rectangles(surface_region, &nrects);
923 dest_rects = malloc(nrects * sizeof(*dest_rects));
924 if (!dest_rects)
925 return;
926
927 for (i = 0; i < nrects; i++) {
928 dest_rects[i] = weston_surface_to_buffer_rect(surface,
929 src_rects[i]);
930 }
931
932 pixman_region32_fini(buffer_region);
933 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
934 free(dest_rects);
935}
936
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200937WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400939 struct weston_plane *plane)
940{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500941 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400942 return;
943
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944 weston_view_damage_below(view);
945 view->plane = plane;
946 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400947}
948
Pekka Paalanen51723d52015-02-17 13:10:01 +0200949/** Inflict damage on the plane where the view is visible.
950 *
951 * \param view The view that causes the damage.
952 *
953 * If the view is currently on a plane (including the primary plane),
954 * take the view's boundingbox, subtract all the opaque views that cover it,
955 * and add the remaining region as damage to the plane. This corresponds
956 * to the damage inflicted to the plane if this view disappeared.
957 *
958 * A repaint is scheduled for this view.
959 *
960 * The region of all opaque views covering this view is stored in
961 * weston_view::clip and updated by view_accumulate_damage() during
962 * weston_output_repaint(). Specifically, that region matches the
963 * scenegraph as it was last painted.
964 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400965WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500966weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200967{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500968 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200969
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500970 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200971 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800973 if (view->plane)
974 pixman_region32_union(&view->plane->damage,
975 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500976 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800977 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200978}
979
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400980static void
981weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
982{
983 uint32_t different = es->output_mask ^ mask;
984 uint32_t entered = mask & different;
985 uint32_t left = es->output_mask & different;
986 struct weston_output *output;
987 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500988 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400989
990 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500991 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400992 return;
993 if (different == 0)
994 return;
995
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500996 client = wl_resource_get_client(es->resource);
997
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400998 wl_list_for_each(output, &es->compositor->output_list, link) {
999 if (1 << output->id & different)
1000 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001001 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001002 client);
1003 if (resource == NULL)
1004 continue;
1005 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001006 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001007 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001008 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001009 }
1010}
1011
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001012
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001013static void
1014weston_surface_assign_output(struct weston_surface *es)
1015{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016 struct weston_output *new_output;
1017 struct weston_view *view;
1018 pixman_region32_t region;
1019 uint32_t max, area, mask;
1020 pixman_box32_t *e;
1021
1022 new_output = NULL;
1023 max = 0;
1024 mask = 0;
1025 pixman_region32_init(&region);
1026 wl_list_for_each(view, &es->views, surface_link) {
1027 if (!view->output)
1028 continue;
1029
1030 pixman_region32_intersect(&region, &view->transform.boundingbox,
1031 &view->output->region);
1032
1033 e = pixman_region32_extents(&region);
1034 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1035
1036 mask |= view->output_mask;
1037
1038 if (area >= max) {
1039 new_output = view->output;
1040 max = area;
1041 }
1042 }
1043 pixman_region32_fini(&region);
1044
1045 es->output = new_output;
1046 weston_surface_update_output_mask(es, mask);
1047}
1048
1049static void
1050weston_view_assign_output(struct weston_view *ev)
1051{
1052 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001053 struct weston_output *output, *new_output;
1054 pixman_region32_t region;
1055 uint32_t max, area, mask;
1056 pixman_box32_t *e;
1057
1058 new_output = NULL;
1059 max = 0;
1060 mask = 0;
1061 pixman_region32_init(&region);
1062 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001063 if (output->destroying)
1064 continue;
1065
Jason Ekstranda7af7042013-10-12 22:38:11 -05001066 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001067 &output->region);
1068
1069 e = pixman_region32_extents(&region);
1070 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1071
1072 if (area > 0)
1073 mask |= 1 << output->id;
1074
1075 if (area >= max) {
1076 new_output = output;
1077 max = area;
1078 }
1079 }
1080 pixman_region32_fini(&region);
1081
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 ev->output = new_output;
1083 ev->output_mask = mask;
1084
1085 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001086}
1087
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001088static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001089weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1090 int from_x, int from_y, int *to_x, int *to_y)
1091{
1092 float x, y;
1093
1094 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1095 weston_view_from_global_float(to, x, y, &x, &y);
1096
1097 *to_x = round(x);
1098 *to_y = round(y);
1099}
1100
1101static void
1102weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1103{
1104 pixman_box32_t *a;
1105 pixman_box32_t b;
1106
1107 a = pixman_region32_extents(&from->geometry.scissor);
1108
1109 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1110 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1111
1112 pixman_region32_fini(&to->geometry.scissor);
1113 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1114}
1115
1116static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001117view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001118 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001119{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001120 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1121 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001122 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001123 { inbox->x1, inbox->y1 },
1124 { inbox->x1, inbox->y2 },
1125 { inbox->x2, inbox->y1 },
1126 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001127 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001128 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001129 int i;
1130
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001131 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001132 /* avoid rounding empty bbox to 1x1 */
1133 pixman_region32_init(bbox);
1134 return;
1135 }
1136
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001137 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001138 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001140 if (x < min_x)
1141 min_x = x;
1142 if (x > max_x)
1143 max_x = x;
1144 if (y < min_y)
1145 min_y = y;
1146 if (y > max_y)
1147 max_y = y;
1148 }
1149
Pekka Paalanen219b9822012-02-08 15:38:37 +02001150 int_x = floorf(min_x);
1151 int_y = floorf(min_y);
1152 pixman_region32_init_rect(bbox, int_x, int_y,
1153 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001154}
1155
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001156static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001158{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001160
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001161 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162 view->geometry.x = roundf(view->geometry.x);
1163 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001164
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001165 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1167 view->transform.position.matrix.d[12] = view->geometry.x;
1168 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001169
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001171
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 view->transform.inverse = view->transform.position.matrix;
1173 view->transform.inverse.d[12] = -view->geometry.x;
1174 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001175
Jason Ekstranda7af7042013-10-12 22:38:11 -05001176 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001177 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001178 view->surface->width,
1179 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001180 if (view->geometry.scissor_enabled)
1181 pixman_region32_intersect(&view->transform.boundingbox,
1182 &view->transform.boundingbox,
1183 &view->geometry.scissor);
1184
1185 pixman_region32_translate(&view->transform.boundingbox,
1186 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001187
Jason Ekstranda7af7042013-10-12 22:38:11 -05001188 if (view->alpha == 1.0) {
1189 pixman_region32_copy(&view->transform.opaque,
1190 &view->surface->opaque);
1191 pixman_region32_translate(&view->transform.opaque,
1192 view->geometry.x,
1193 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001194 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001195}
1196
1197static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001199{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 struct weston_view *parent = view->geometry.parent;
1201 struct weston_matrix *matrix = &view->transform.matrix;
1202 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001203 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001204 pixman_region32_t surfregion;
1205 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001206
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001208
1209 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1211 view->transform.position.matrix.d[12] = view->geometry.x;
1212 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001213
1214 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001216 weston_matrix_multiply(matrix, &tform->matrix);
1217
Pekka Paalanen483243f2013-03-08 14:56:50 +02001218 if (parent)
1219 weston_matrix_multiply(matrix, &parent->transform.matrix);
1220
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001221 if (weston_matrix_invert(inverse, matrix) < 0) {
1222 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 weston_log("error: weston_view %p"
1224 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001225 return -1;
1226 }
1227
Pekka Paalanen380adf52015-02-16 14:39:11 +02001228 pixman_region32_init_rect(&surfregion, 0, 0,
1229 view->surface->width, view->surface->height);
1230 if (view->geometry.scissor_enabled)
1231 pixman_region32_intersect(&surfregion, &surfregion,
1232 &view->geometry.scissor);
1233 surfbox = pixman_region32_extents(&surfregion);
1234
1235 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1236 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001237
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001238 return 0;
1239}
1240
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001241static struct weston_layer *
1242get_view_layer(struct weston_view *view)
1243{
1244 if (view->parent_view)
1245 return get_view_layer(view->parent_view);
1246 return view->layer_link.layer;
1247}
1248
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001249WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001251{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001253 struct weston_layer *layer;
1254 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001255
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001257 return;
1258
Pekka Paalanen483243f2013-03-08 14:56:50 +02001259 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001261
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001263
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001265
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 pixman_region32_fini(&view->transform.boundingbox);
1267 pixman_region32_fini(&view->transform.opaque);
1268 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001269
Pekka Paalanencd403622012-01-25 13:37:39 +02001270 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 if (view->geometry.transformation_list.next ==
1272 &view->transform.position.link &&
1273 view->geometry.transformation_list.prev ==
1274 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001275 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001277 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 if (weston_view_update_transform_enable(view) < 0)
1279 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001280 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001281
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001282 layer = get_view_layer(view);
1283 if (layer) {
1284 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001285 pixman_region32_intersect(&view->transform.boundingbox,
1286 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001287 pixman_region32_intersect(&view->transform.opaque,
1288 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001289 pixman_region32_fini(&mask);
1290 }
1291
Pekka Paalanen380adf52015-02-16 14:39:11 +02001292 if (parent) {
1293 if (parent->geometry.scissor_enabled) {
1294 view->geometry.scissor_enabled = true;
1295 weston_view_transfer_scissor(parent, view);
1296 } else {
1297 view->geometry.scissor_enabled = false;
1298 }
1299 }
1300
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001302
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001304
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 wl_signal_emit(&view->surface->compositor->transform_signal,
1306 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001307}
1308
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001309WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001311{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001313
1314 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 * The invariant: if view->geometry.dirty, then all views
1316 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001317 * Corollary: if not parent->geometry.dirty, then all ancestors
1318 * are not dirty.
1319 */
1320
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001322 return;
1323
Jason Ekstranda7af7042013-10-12 22:38:11 -05001324 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001325
Jason Ekstranda7af7042013-10-12 22:38:11 -05001326 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001327 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001329}
1330
1331WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332weston_view_to_global_fixed(struct weston_view *view,
1333 wl_fixed_t vx, wl_fixed_t vy,
1334 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001335{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001336 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001337
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338 weston_view_to_global_float(view,
1339 wl_fixed_to_double(vx),
1340 wl_fixed_to_double(vy),
1341 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001342 *x = wl_fixed_from_double(xf);
1343 *y = wl_fixed_from_double(yf);
1344}
1345
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001346WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347weston_view_from_global_float(struct weston_view *view,
1348 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001349{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001351 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1352
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001354
1355 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001356 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001358 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 *vx = 0;
1360 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001361 return;
1362 }
1363
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364 *vx = v.f[0] / v.f[3];
1365 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001366 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 *vx = x - view->geometry.x;
1368 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001369 }
1370}
1371
1372WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373weston_view_from_global_fixed(struct weston_view *view,
1374 wl_fixed_t x, wl_fixed_t y,
1375 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001376{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001378
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 weston_view_from_global_float(view,
1380 wl_fixed_to_double(x),
1381 wl_fixed_to_double(y),
1382 &vxf, &vyf);
1383 *vx = wl_fixed_from_double(vxf);
1384 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001385}
1386
1387WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001388weston_view_from_global(struct weston_view *view,
1389 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001390{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001392
Jason Ekstranda7af7042013-10-12 22:38:11 -05001393 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1394 *vx = floorf(vxf);
1395 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001396}
1397
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001398WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001399weston_surface_schedule_repaint(struct weston_surface *surface)
1400{
1401 struct weston_output *output;
1402
1403 wl_list_for_each(output, &surface->compositor->output_list, link)
1404 if (surface->output_mask & (1 << output->id))
1405 weston_output_schedule_repaint(output);
1406}
1407
1408WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409weston_view_schedule_repaint(struct weston_view *view)
1410{
1411 struct weston_output *output;
1412
1413 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1414 if (view->output_mask & (1 << output->id))
1415 weston_output_schedule_repaint(output);
1416}
1417
Pekka Paalanene508ce62015-02-19 13:59:55 +02001418/**
1419 * XXX: This function does it the wrong way.
1420 * surface->damage is the damage from the client, and causes
1421 * surface_flush_damage() to copy pixels. No window management action can
1422 * cause damage to the client-provided content, warranting re-upload!
1423 *
1424 * Instead of surface->damage, this function should record the damage
1425 * with all the views for this surface to avoid extraneous texture
1426 * uploads.
1427 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001429weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001430{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001431 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 0, 0, surface->width,
1433 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001434
Kristian Høgsberg98238702012-08-03 16:29:12 -04001435 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001436}
1437
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001438WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001440{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001441 if (view->geometry.x == x && view->geometry.y == y)
1442 return;
1443
Jason Ekstranda7af7042013-10-12 22:38:11 -05001444 view->geometry.x = x;
1445 view->geometry.y = y;
1446 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001447}
1448
Pekka Paalanen483243f2013-03-08 14:56:50 +02001449static void
1450transform_parent_handle_parent_destroy(struct wl_listener *listener,
1451 void *data)
1452{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 struct weston_view *view =
1454 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001455 geometry.parent_destroy_listener);
1456
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001458}
1459
1460WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001462 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001463{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 if (view->geometry.parent) {
1465 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1466 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001467
1468 if (!parent)
1469 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001470 }
1471
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001473
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001475 transform_parent_handle_parent_destroy;
1476 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001477 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001478 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001479 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001481 }
1482
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 weston_view_geometry_dirty(view);
1484}
1485
Pekka Paalanen380adf52015-02-16 14:39:11 +02001486/** Set a clip mask rectangle on a view
1487 *
1488 * \param view The view to set the clip mask on.
1489 * \param x Top-left corner X coordinate of the clip rectangle.
1490 * \param y Top-left corner Y coordinate of the clip rectangle.
1491 * \param width Width of the clip rectangle, non-negative.
1492 * \param height Height of the clip rectangle, non-negative.
1493 *
1494 * A shell may set a clip mask rectangle on a view. Everything outside
1495 * the rectangle is cut away for input and output purposes: it is
1496 * not drawn and cannot be hit by hit-test based input like pointer
1497 * motion or touch-downs. Everything inside the rectangle will behave
1498 * normally. Clients are unaware of clipping.
1499 *
1500 * The rectangle is set in the surface local coordinates. Setting a clip
1501 * mask rectangle does not affect the view position, the view is positioned
1502 * as it would be without a clip. The clip also does not change
1503 * weston_surface::width,height.
1504 *
1505 * The clip mask rectangle is part of transformation inheritance
1506 * (weston_view_set_transform_parent()). A clip set in the root of the
1507 * transformation inheritance tree will affect all views in the tree.
1508 * A clip can be set only on the root view. Attempting to set a clip
1509 * on view that has a transformation parent will fail. Assigning a parent
1510 * to a view that has a clip set will cause the clip to be forgotten.
1511 *
1512 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1513 * on the additional transformations in the child views. These transformations
1514 * may not rotate the coordinate axes, i.e., only translation and scaling
1515 * are allowed. Violating this restriction causes the clipping to malfunction.
1516 * Furthermore, using scaling may cause rounding errors in child clipping.
1517 *
1518 * The clip mask rectangle is not automatically adjusted based on
1519 * wl_surface.attach dx and dy arguments.
1520 *
1521 * A clip mask rectangle can be set only if the compositor capability
1522 * WESTON_CAP_VIEW_CLIP_MASK is present.
1523 *
1524 * This function sets the clip mask rectangle and schedules a repaint for
1525 * the view.
1526 */
1527WL_EXPORT void
1528weston_view_set_mask(struct weston_view *view,
1529 int x, int y, int width, int height)
1530{
1531 struct weston_compositor *compositor = view->surface->compositor;
1532
1533 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1534 weston_log("%s not allowed without capability!\n", __func__);
1535 return;
1536 }
1537
1538 if (view->geometry.parent) {
1539 weston_log("view %p has a parent, clip forbidden!\n", view);
1540 return;
1541 }
1542
1543 if (width < 0 || height < 0) {
1544 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1545 x, y, width, height);
1546 return;
1547 }
1548
1549 pixman_region32_fini(&view->geometry.scissor);
1550 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1551 view->geometry.scissor_enabled = true;
1552 weston_view_geometry_dirty(view);
1553 weston_view_schedule_repaint(view);
1554}
1555
1556/** Remove the clip mask from a view
1557 *
1558 * \param view The view to remove the clip mask from.
1559 *
1560 * Removed the clip mask rectangle and schedules a repaint.
1561 *
1562 * \sa weston_view_set_mask
1563 */
1564WL_EXPORT void
1565weston_view_set_mask_infinite(struct weston_view *view)
1566{
1567 view->geometry.scissor_enabled = false;
1568 weston_view_geometry_dirty(view);
1569 weston_view_schedule_repaint(view);
1570}
1571
Derek Foreman280e7dd2014-10-03 13:13:42 -05001572WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573weston_view_is_mapped(struct weston_view *view)
1574{
1575 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001576 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001578 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001579}
1580
Derek Foreman280e7dd2014-10-03 13:13:42 -05001581WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001582weston_surface_is_mapped(struct weston_surface *surface)
1583{
1584 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001585 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001586 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001587 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001588}
1589
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001590static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001591surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001592{
1593 struct weston_view *view;
1594
1595 if (surface->width == width && surface->height == height)
1596 return;
1597
1598 surface->width = width;
1599 surface->height = height;
1600
1601 wl_list_for_each(view, &surface->views, surface_link)
1602 weston_view_geometry_dirty(view);
1603}
1604
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001605WL_EXPORT void
1606weston_surface_set_size(struct weston_surface *surface,
1607 int32_t width, int32_t height)
1608{
1609 assert(!surface->resource);
1610 surface_set_size(surface, width, height);
1611}
1612
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001613static int
1614fixed_round_up_to_int(wl_fixed_t f)
1615{
1616 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1617}
1618
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001619static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001620weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001621{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001622 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001623 int32_t width, height;
1624
1625 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001626 surface->width_from_buffer = 0;
1627 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001628 return;
1629 }
1630
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001631 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001632 case WL_OUTPUT_TRANSFORM_90:
1633 case WL_OUTPUT_TRANSFORM_270:
1634 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1635 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001636 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1637 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001638 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001639 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001640 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1641 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001642 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001643 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001644
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001645 surface->width_from_buffer = width;
1646 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001647}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001648
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001649static void
1650weston_surface_update_size(struct weston_surface *surface)
1651{
1652 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1653 int32_t width, height;
1654
1655 width = surface->width_from_buffer;
1656 height = surface->height_from_buffer;
1657
1658 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001659 surface_set_size(surface,
1660 vp->surface.width, vp->surface.height);
1661 return;
1662 }
1663
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001664 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001665 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1666 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1667
1668 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001669 return;
1670 }
1671
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001672 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001673}
1674
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001675WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001676weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001677{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001678 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001679
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001680 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001681
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001682 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001683}
1684
Jason Ekstranda7af7042013-10-12 22:38:11 -05001685WL_EXPORT struct weston_view *
1686weston_compositor_pick_view(struct weston_compositor *compositor,
1687 wl_fixed_t x, wl_fixed_t y,
1688 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001689{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001691 wl_fixed_t view_x, view_y;
1692 int view_ix, view_iy;
1693 int ix = wl_fixed_to_int(x);
1694 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001695
Jason Ekstranda7af7042013-10-12 22:38:11 -05001696 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001697 if (!pixman_region32_contains_point(
1698 &view->transform.boundingbox, ix, iy, NULL))
1699 continue;
1700
1701 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1702 view_ix = wl_fixed_to_int(view_x);
1703 view_iy = wl_fixed_to_int(view_y);
1704
1705 if (!pixman_region32_contains_point(&view->surface->input,
1706 view_ix, view_iy, NULL))
1707 continue;
1708
Pekka Paalanen380adf52015-02-16 14:39:11 +02001709 if (view->geometry.scissor_enabled &&
1710 !pixman_region32_contains_point(&view->geometry.scissor,
1711 view_ix, view_iy, NULL))
1712 continue;
1713
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001714 *vx = view_x;
1715 *vy = view_y;
1716 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001717 }
1718
1719 return NULL;
1720}
1721
1722static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001723weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001724{
Daniel Stone37816df2012-05-16 18:45:18 +01001725 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001726
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001727 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001728 return;
1729
Daniel Stone37816df2012-05-16 18:45:18 +01001730 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001731 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001732}
1733
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001734WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001736{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001737 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001738
Jason Ekstranda7af7042013-10-12 22:38:11 -05001739 if (!weston_view_is_mapped(view))
1740 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001741
Jason Ekstranda7af7042013-10-12 22:38:11 -05001742 weston_view_damage_below(view);
1743 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001744 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001745 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 wl_list_remove(&view->link);
1747 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001748 view->output_mask = 0;
1749 weston_surface_assign_output(view->surface);
1750
1751 if (weston_surface_is_mapped(view->surface))
1752 return;
1753
1754 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001755 struct weston_touch *touch = weston_seat_get_touch(seat);
1756 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1757 struct weston_keyboard *keyboard =
1758 weston_seat_get_keyboard(seat);
1759
1760 if (keyboard && keyboard->focus == view->surface)
1761 weston_keyboard_set_focus(keyboard, NULL);
1762 if (pointer && pointer->focus == view)
1763 weston_pointer_set_focus(pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001764 NULL,
1765 wl_fixed_from_int(0),
1766 wl_fixed_from_int(0));
Derek Foreman1281a362015-07-31 16:55:32 -05001767 if (touch && touch->focus == view)
1768 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001769 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001771
Jason Ekstranda7af7042013-10-12 22:38:11 -05001772WL_EXPORT void
1773weston_surface_unmap(struct weston_surface *surface)
1774{
1775 struct weston_view *view;
1776
1777 wl_list_for_each(view, &surface->views, surface_link)
1778 weston_view_unmap(view);
1779 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001780}
1781
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001782static void
1783weston_surface_reset_pending_buffer(struct weston_surface *surface)
1784{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001785 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001786 surface->pending.sx = 0;
1787 surface->pending.sy = 0;
1788 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001789 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001790}
1791
Jason Ekstranda7af7042013-10-12 22:38:11 -05001792WL_EXPORT void
1793weston_view_destroy(struct weston_view *view)
1794{
1795 wl_signal_emit(&view->destroy_signal, view);
1796
1797 assert(wl_list_empty(&view->geometry.child_list));
1798
1799 if (weston_view_is_mapped(view)) {
1800 weston_view_unmap(view);
1801 weston_compositor_build_view_list(view->surface->compositor);
1802 }
1803
1804 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001805 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001806
1807 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001808 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001809 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001810 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001811
1812 weston_view_set_transform_parent(view, NULL);
1813
Jason Ekstranda7af7042013-10-12 22:38:11 -05001814 wl_list_remove(&view->surface_link);
1815
1816 free(view);
1817}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001818
1819WL_EXPORT void
1820weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001821{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001822 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001823 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001824
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001825 if (--surface->ref_count > 0)
1826 return;
1827
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001828 assert(surface->resource == NULL);
1829
Pekka Paalanenca790762015-04-17 14:23:38 +03001830 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001831
Pekka Paalanene67858b2013-04-25 13:57:42 +03001832 assert(wl_list_empty(&surface->subsurface_list_pending));
1833 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001834
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1836 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001837
Jason Ekstrand7b982072014-05-20 14:33:03 -05001838 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001839
Pekka Paalanende685b82012-12-04 15:58:12 +02001840 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001841
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001842 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001843 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001844 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001845
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001846 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001847 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001848
Pekka Paalanen133e4392014-09-23 22:08:46 -04001849 weston_presentation_feedback_discard_list(&surface->feedback_list);
1850
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001851 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001852}
1853
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001854static void
1855destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001856{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001857 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001858
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001859 assert(surface);
1860
Giulio Camuffo0d379742013-11-15 22:06:15 +01001861 /* Set the resource to NULL, since we don't want to leave a
1862 * dangling pointer if the surface was refcounted and survives
1863 * the weston_surface_destroy() call. */
1864 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001865 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001866}
1867
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001868static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001869weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1870{
1871 struct weston_buffer *buffer =
1872 container_of(listener, struct weston_buffer, destroy_listener);
1873
1874 wl_signal_emit(&buffer->destroy_signal, buffer);
1875 free(buffer);
1876}
1877
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001878WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001879weston_buffer_from_resource(struct wl_resource *resource)
1880{
1881 struct weston_buffer *buffer;
1882 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001883
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001884 listener = wl_resource_get_destroy_listener(resource,
1885 weston_buffer_destroy_handler);
1886
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001887 if (listener)
1888 return container_of(listener, struct weston_buffer,
1889 destroy_listener);
1890
1891 buffer = zalloc(sizeof *buffer);
1892 if (buffer == NULL)
1893 return NULL;
1894
1895 buffer->resource = resource;
1896 wl_signal_init(&buffer->destroy_signal);
1897 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001898 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001899 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001900
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001901 return buffer;
1902}
1903
1904static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001905weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1906 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001907{
Pekka Paalanende685b82012-12-04 15:58:12 +02001908 struct weston_buffer_reference *ref =
1909 container_of(listener, struct weston_buffer_reference,
1910 destroy_listener);
1911
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001912 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001913 ref->buffer = NULL;
1914}
1915
1916WL_EXPORT void
1917weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001918 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001919{
1920 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001921 ref->buffer->busy_count--;
1922 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001923 assert(wl_resource_get_client(ref->buffer->resource));
1924 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001925 WL_BUFFER_RELEASE);
1926 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001927 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001928 }
1929
Pekka Paalanende685b82012-12-04 15:58:12 +02001930 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001931 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001932 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001933 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001934 }
1935
Pekka Paalanende685b82012-12-04 15:58:12 +02001936 ref->buffer = buffer;
1937 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1938}
1939
1940static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001941weston_surface_attach(struct weston_surface *surface,
1942 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001943{
1944 weston_buffer_reference(&surface->buffer_ref, buffer);
1945
Pekka Paalanena6421c42012-12-04 15:58:10 +02001946 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001947 if (weston_surface_is_mapped(surface))
1948 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001949 }
1950
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001951 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001952
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001953 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001954 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001955}
1956
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001957WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001958weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001959{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001960 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001961
1962 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001963 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001964}
1965
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001966WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001967weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001968{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001969 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001970
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001971 pixman_region32_union(&compositor->primary_plane.damage,
1972 &compositor->primary_plane.damage,
1973 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001974 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001975}
1976
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001977static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001978surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001979{
Pekka Paalanende685b82012-12-04 15:58:12 +02001980 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001981 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001982 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001983
Pekka Paalanenb5026542014-11-12 15:09:24 +02001984 if (weston_timeline_enabled_ &&
1985 pixman_region32_not_empty(&surface->damage))
1986 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1987 TLP_OUTPUT(surface->output), TLP_END);
1988
Jason Ekstrandef540082014-06-26 10:37:36 -07001989 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001990}
1991
1992static void
1993view_accumulate_damage(struct weston_view *view,
1994 pixman_region32_t *opaque)
1995{
1996 pixman_region32_t damage;
1997
1998 pixman_region32_init(&damage);
1999 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002000 pixman_box32_t *extents;
2001
Jason Ekstranda7af7042013-10-12 22:38:11 -05002002 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002003 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002004 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002005 pixman_region32_copy(&damage, &view->surface->damage);
2006 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002007 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002008 }
2009
Pekka Paalanen380adf52015-02-16 14:39:11 +02002010 pixman_region32_intersect(&damage, &damage,
2011 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002012 pixman_region32_subtract(&damage, &damage, opaque);
2013 pixman_region32_union(&view->plane->damage,
2014 &view->plane->damage, &damage);
2015 pixman_region32_fini(&damage);
2016 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002017 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002018}
2019
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002020static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002021compositor_accumulate_damage(struct weston_compositor *ec)
2022{
2023 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002024 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002025 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002026
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002027 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002028
2029 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002030 pixman_region32_copy(&plane->clip, &clip);
2031
2032 pixman_region32_init(&opaque);
2033
Jason Ekstranda7af7042013-10-12 22:38:11 -05002034 wl_list_for_each(ev, &ec->view_list, link) {
2035 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002036 continue;
2037
Jason Ekstranda7af7042013-10-12 22:38:11 -05002038 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002039 }
2040
2041 pixman_region32_union(&clip, &clip, &opaque);
2042 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002043 }
2044
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002045 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002046
Jason Ekstranda7af7042013-10-12 22:38:11 -05002047 wl_list_for_each(ev, &ec->view_list, link)
2048 ev->surface->touched = 0;
2049
2050 wl_list_for_each(ev, &ec->view_list, link) {
2051 if (ev->surface->touched)
2052 continue;
2053 ev->surface->touched = 1;
2054
2055 surface_flush_damage(ev->surface);
2056
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002057 /* Both the renderer and the backend have seen the buffer
2058 * by now. If renderer needs the buffer, it has its own
2059 * reference set. If the backend wants to keep the buffer
2060 * around for migrating the surface into a non-primary plane
2061 * later, keep_buffer is true. Otherwise, drop the core
2062 * reference now, and allow early buffer release. This enables
2063 * clients to use single-buffering.
2064 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002065 if (!ev->surface->keep_buffer)
2066 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002067 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002068}
2069
2070static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002071surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002072{
2073 struct weston_subsurface *sub;
2074
Pekka Paalanene67858b2013-04-25 13:57:42 +03002075 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002077 continue;
2078
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2080 wl_list_init(&sub->surface->views);
2081
2082 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002083 }
2084}
2085
2086static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002088{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002089 struct weston_subsurface *sub;
2090 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002091
Jason Ekstranda7af7042013-10-12 22:38:11 -05002092 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2093 if (sub->surface == surface)
2094 continue;
2095
George Kiagiadakised04d382014-06-13 18:10:26 +02002096 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2097 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002098 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002099 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002100
2101 surface_free_unused_subsurface_views(sub->surface);
2102 }
2103}
2104
2105static void
2106view_list_add_subsurface_view(struct weston_compositor *compositor,
2107 struct weston_subsurface *sub,
2108 struct weston_view *parent)
2109{
2110 struct weston_subsurface *child;
2111 struct weston_view *view = NULL, *iv;
2112
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002113 if (!weston_surface_is_mapped(sub->surface))
2114 return;
2115
Jason Ekstranda7af7042013-10-12 22:38:11 -05002116 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2117 if (iv->geometry.parent == parent) {
2118 view = iv;
2119 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002120 }
2121 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002122
2123 if (view) {
2124 /* Put it back in the surface's list of views */
2125 wl_list_remove(&view->surface_link);
2126 wl_list_insert(&sub->surface->views, &view->surface_link);
2127 } else {
2128 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002129 weston_view_set_position(view,
2130 sub->position.x,
2131 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002132 weston_view_set_transform_parent(view, parent);
2133 }
2134
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002135 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002137
Pekka Paalanenb188e912013-11-19 14:03:35 +02002138 if (wl_list_empty(&sub->surface->subsurface_list)) {
2139 wl_list_insert(compositor->view_list.prev, &view->link);
2140 return;
2141 }
2142
2143 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2144 if (child->surface == sub->surface)
2145 wl_list_insert(compositor->view_list.prev, &view->link);
2146 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002147 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002148 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002149}
2150
2151static void
2152view_list_add(struct weston_compositor *compositor,
2153 struct weston_view *view)
2154{
2155 struct weston_subsurface *sub;
2156
2157 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002158
Pekka Paalanenb188e912013-11-19 14:03:35 +02002159 if (wl_list_empty(&view->surface->subsurface_list)) {
2160 wl_list_insert(compositor->view_list.prev, &view->link);
2161 return;
2162 }
2163
2164 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2165 if (sub->surface == view->surface)
2166 wl_list_insert(compositor->view_list.prev, &view->link);
2167 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002168 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002169 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002170}
2171
2172static void
2173weston_compositor_build_view_list(struct weston_compositor *compositor)
2174{
2175 struct weston_view *view;
2176 struct weston_layer *layer;
2177
2178 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002179 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180 surface_stash_subsurface_views(view->surface);
2181
2182 wl_list_init(&compositor->view_list);
2183 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002184 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002185 view_list_add(compositor, view);
2186 }
2187 }
2188
2189 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002190 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002192}
2193
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002194static void
2195weston_output_take_feedback_list(struct weston_output *output,
2196 struct weston_surface *surface)
2197{
2198 struct weston_view *view;
2199 struct weston_presentation_feedback *feedback;
2200 uint32_t flags = 0xffffffff;
2201
2202 if (wl_list_empty(&surface->feedback_list))
2203 return;
2204
2205 /* All views must have the flag for the flag to survive. */
2206 wl_list_for_each(view, &surface->views, surface_link) {
2207 /* ignore views that are not on this output at all */
2208 if (view->output_mask & (1u << output->id))
2209 flags &= view->psf_flags;
2210 }
2211
2212 wl_list_for_each(feedback, &surface->feedback_list, link)
2213 feedback->psf_flags = flags;
2214
2215 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2216 wl_list_init(&surface->feedback_list);
2217}
2218
David Herrmann1edf44c2013-10-22 17:11:26 +02002219static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002220weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002221{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002222 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002223 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002224 struct weston_animation *animation, *next;
2225 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002226 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002227 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002228 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002229
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002230 if (output->destroying)
2231 return 0;
2232
Pekka Paalanenb5026542014-11-12 15:09:24 +02002233 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2234
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002235 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002236 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002237
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002238 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002239 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002240 } else {
2241 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002242 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002243 ev->psf_flags = 0;
2244 }
2245 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002246
Pekka Paalanene67858b2013-04-25 13:57:42 +03002247 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002248 wl_list_for_each(ev, &ec->view_list, link) {
2249 /* Note: This operation is safe to do multiple times on the
2250 * same surface.
2251 */
2252 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002253 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002254 &ev->surface->frame_callback_list);
2255 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002256
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002257 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002258 }
2259 }
2260
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002261 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002262
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002263 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002264 pixman_region32_intersect(&output_damage,
2265 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002266 pixman_region32_subtract(&output_damage,
2267 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002268
Scott Moreauccbf29d2012-02-22 14:21:41 -07002269 if (output->dirty)
2270 weston_output_update_matrix(output);
2271
David Herrmann1edf44c2013-10-22 17:11:26 +02002272 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002273
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002274 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002275
Kristian Høgsbergef044142011-06-21 15:02:12 -04002276 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002277
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002278 weston_compositor_repick(ec);
2279 wl_event_loop_dispatch(ec->input_loop, 0);
2280
Jonas Ådahldb773762012-06-13 00:01:21 +02002281 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002282 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002283 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002284 }
2285
Scott Moreaud64cf212012-06-08 19:40:54 -06002286 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002287 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002288 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002289 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002290
Pekka Paalanenb5026542014-11-12 15:09:24 +02002291 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2292
David Herrmann1edf44c2013-10-22 17:11:26 +02002293 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002294}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002295
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002296static int
2297weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002298{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002299 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002300
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002301 wl_event_loop_dispatch(compositor->input_loop, 0);
2302
2303 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002304}
2305
Pekka Paalanen82919792014-05-21 13:51:49 +03002306static void
2307weston_output_schedule_repaint_reset(struct weston_output *output)
2308{
2309 struct weston_compositor *compositor = output->compositor;
2310 struct wl_event_loop *loop;
2311 int fd;
2312
2313 output->repaint_scheduled = 0;
2314 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2315
2316 if (compositor->input_loop_source)
2317 return;
2318
2319 loop = wl_display_get_event_loop(compositor->wl_display);
2320 fd = wl_event_loop_get_fd(compositor->input_loop);
2321 compositor->input_loop_source =
2322 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2323 weston_compositor_read_input, compositor);
2324}
2325
Pekka Paalanen0513a952014-05-21 16:17:27 +03002326static int
2327output_repaint_timer_handler(void *data)
2328{
2329 struct weston_output *output = data;
2330 struct weston_compositor *compositor = output->compositor;
2331
2332 if (output->repaint_needed &&
2333 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2334 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2335 weston_output_repaint(output) == 0)
2336 return 0;
2337
2338 weston_output_schedule_repaint_reset(output);
2339
2340 return 0;
2341}
2342
Kristian Høgsbergef044142011-06-21 15:02:12 -04002343WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002344weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002345 const struct timespec *stamp,
2346 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002347{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002348 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002349 int32_t refresh_nsec;
2350 struct timespec now;
2351 struct timespec gone;
2352 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002353
Pekka Paalanenb5026542014-11-12 15:09:24 +02002354 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2355 TLP_VBLANK(stamp), TLP_END);
2356
Pekka Paalanend7894d02015-07-03 15:08:53 +03002357 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002358 weston_presentation_feedback_present_list(&output->feedback_list,
2359 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002360 output->msc,
2361 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002362
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002363 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002364
Pekka Paalanen0513a952014-05-21 16:17:27 +03002365 weston_compositor_read_presentation_clock(compositor, &now);
2366 timespec_sub(&gone, &now, stamp);
2367 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2368 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002369
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002370 if (msec < -1000 || msec > 1000) {
2371 static bool warned;
2372
2373 if (!warned)
2374 weston_log("Warning: computed repaint delay is "
2375 "insane: %d msec\n", msec);
2376 warned = true;
2377
2378 msec = 0;
2379 }
2380
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002381 /* Called from restart_repaint_loop and restart happens already after
2382 * the deadline given by repaint_msec? In that case we delay until
2383 * the deadline of the next frame, to give clients a more predictable
2384 * timing of the repaint cycle to lock on. */
2385 if (presented_flags == PRESENTATION_FEEDBACK_INVALID && msec < 0)
2386 msec += refresh_nsec / 1000000;
2387
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002388 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002389 output_repaint_timer_handler(output);
2390 else
2391 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002392}
2393
2394static void
2395idle_repaint(void *data)
2396{
2397 struct weston_output *output = data;
2398
Jonas Ådahle5a12252013-04-05 23:07:11 +02002399 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002400}
2401
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002402WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002403weston_layer_entry_insert(struct weston_layer_entry *list,
2404 struct weston_layer_entry *entry)
2405{
2406 wl_list_insert(&list->link, &entry->link);
2407 entry->layer = list->layer;
2408}
2409
2410WL_EXPORT void
2411weston_layer_entry_remove(struct weston_layer_entry *entry)
2412{
2413 wl_list_remove(&entry->link);
2414 wl_list_init(&entry->link);
2415 entry->layer = NULL;
2416}
2417
2418WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002419weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2420{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002421 wl_list_init(&layer->view_list.link);
2422 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002423 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002424 if (below != NULL)
2425 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002426}
2427
2428WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002429weston_layer_set_mask(struct weston_layer *layer,
2430 int x, int y, int width, int height)
2431{
2432 struct weston_view *view;
2433
2434 layer->mask.x1 = x;
2435 layer->mask.x2 = x + width;
2436 layer->mask.y1 = y;
2437 layer->mask.y2 = y + height;
2438
2439 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2440 weston_view_geometry_dirty(view);
2441 }
2442}
2443
2444WL_EXPORT void
2445weston_layer_set_mask_infinite(struct weston_layer *layer)
2446{
2447 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2448 UINT32_MAX, UINT32_MAX);
2449}
2450
2451WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002452weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002453{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002454 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002455 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002456
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002457 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2458 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002459 return;
2460
Pekka Paalanenb5026542014-11-12 15:09:24 +02002461 if (!output->repaint_needed)
2462 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2463
Kristian Høgsbergef044142011-06-21 15:02:12 -04002464 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002465 output->repaint_needed = 1;
2466 if (output->repaint_scheduled)
2467 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002468
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002469 wl_event_loop_add_idle(loop, idle_repaint, output);
2470 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002471 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2472
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002473
2474 if (compositor->input_loop_source) {
2475 wl_event_source_remove(compositor->input_loop_source);
2476 compositor->input_loop_source = NULL;
2477 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002478}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002479
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002480WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002481weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2482{
2483 struct weston_output *output;
2484
2485 wl_list_for_each(output, &compositor->output_list, link)
2486 weston_output_schedule_repaint(output);
2487}
2488
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002489static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002490surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002491{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002492 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002493}
2494
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002495static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002496surface_attach(struct wl_client *client,
2497 struct wl_resource *resource,
2498 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2499{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002500 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002501 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002502
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002503 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002504 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002505 if (buffer == NULL) {
2506 wl_client_post_no_memory(client);
2507 return;
2508 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002509 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002510
Pekka Paalanende685b82012-12-04 15:58:12 +02002511 /* Attach, attach, without commit in between does not send
2512 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002513 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002514
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002515 surface->pending.sx = sx;
2516 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002517 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002518}
2519
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002520static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002521surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002522 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002523 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002524{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002525 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002526
Pekka Paalanen8e159182012-10-10 12:49:25 +03002527 pixman_region32_union_rect(&surface->pending.damage,
2528 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002529 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002530}
2531
Kristian Høgsberg33418202011-08-16 23:01:28 -04002532static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002533destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002534{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002535 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002536
2537 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002538 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002539}
2540
2541static void
2542surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002543 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002544{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002545 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002546 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002547
2548 cb = malloc(sizeof *cb);
2549 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002550 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002551 return;
2552 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002553
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002554 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2555 callback);
2556 if (cb->resource == NULL) {
2557 free(cb);
2558 wl_resource_post_no_memory(resource);
2559 return;
2560 }
2561
Jason Ekstranda85118c2013-06-27 20:17:02 -05002562 wl_resource_set_implementation(cb->resource, NULL, cb,
2563 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002564
Pekka Paalanenbc106382012-10-10 12:49:31 +03002565 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002566}
2567
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002568static void
2569surface_set_opaque_region(struct wl_client *client,
2570 struct wl_resource *resource,
2571 struct wl_resource *region_resource)
2572{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002573 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002574 struct weston_region *region;
2575
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002576 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002577 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002578 pixman_region32_copy(&surface->pending.opaque,
2579 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002580 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002581 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002582 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002583}
2584
2585static void
2586surface_set_input_region(struct wl_client *client,
2587 struct wl_resource *resource,
2588 struct wl_resource *region_resource)
2589{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002590 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002591 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002592
2593 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002594 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002595 pixman_region32_copy(&surface->pending.input,
2596 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002597 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002598 pixman_region32_fini(&surface->pending.input);
2599 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002600 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002601}
2602
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002603static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002604weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002605{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002606 struct weston_subsurface *sub;
2607
2608 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2609 parent_link_pending) {
2610 wl_list_remove(&sub->parent_link);
2611 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2612 }
2613}
2614
2615static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002616weston_surface_build_buffer_matrix(struct weston_surface *surface,
2617 struct weston_matrix *matrix)
2618{
2619 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2620 double src_width, src_height, dest_width, dest_height;
2621
2622 weston_matrix_init(matrix);
2623
2624 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2625 src_width = surface->width_from_buffer;
2626 src_height = surface->height_from_buffer;
2627 } else {
2628 src_width = wl_fixed_to_double(vp->buffer.src_width);
2629 src_height = wl_fixed_to_double(vp->buffer.src_height);
2630 }
2631
2632 if (vp->surface.width == -1) {
2633 dest_width = src_width;
2634 dest_height = src_height;
2635 } else {
2636 dest_width = vp->surface.width;
2637 dest_height = vp->surface.height;
2638 }
2639
2640 if (src_width != dest_width || src_height != dest_height)
2641 weston_matrix_scale(matrix,
2642 src_width / dest_width,
2643 src_height / dest_height, 1);
2644
2645 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2646 weston_matrix_translate(matrix,
2647 wl_fixed_to_double(vp->buffer.src_x),
2648 wl_fixed_to_double(vp->buffer.src_y),
2649 0);
2650
2651 switch (vp->buffer.transform) {
2652 case WL_OUTPUT_TRANSFORM_FLIPPED:
2653 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2654 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2655 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2656 weston_matrix_scale(matrix, -1, 1, 1);
2657 weston_matrix_translate(matrix,
2658 surface->width_from_buffer, 0, 0);
2659 break;
2660 }
2661
2662 switch (vp->buffer.transform) {
2663 default:
2664 case WL_OUTPUT_TRANSFORM_NORMAL:
2665 case WL_OUTPUT_TRANSFORM_FLIPPED:
2666 break;
2667 case WL_OUTPUT_TRANSFORM_90:
2668 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2669 weston_matrix_rotate_xy(matrix, 0, 1);
2670 weston_matrix_translate(matrix,
2671 surface->height_from_buffer, 0, 0);
2672 break;
2673 case WL_OUTPUT_TRANSFORM_180:
2674 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2675 weston_matrix_rotate_xy(matrix, -1, 0);
2676 weston_matrix_translate(matrix,
2677 surface->width_from_buffer,
2678 surface->height_from_buffer, 0);
2679 break;
2680 case WL_OUTPUT_TRANSFORM_270:
2681 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2682 weston_matrix_rotate_xy(matrix, 0, -1);
2683 weston_matrix_translate(matrix,
2684 0, surface->width_from_buffer, 0);
2685 break;
2686 }
2687
2688 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2689}
2690
2691static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002692weston_surface_commit_state(struct weston_surface *surface,
2693 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002694{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002695 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002696 pixman_region32_t opaque;
2697
Alexander Larsson4ea95522013-05-22 14:41:37 +02002698 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002699 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002700 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002701 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002702
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002703 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002704 if (state->newly_attached)
2705 weston_surface_attach(surface, state->buffer);
2706 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002707
Jason Ekstrand1e059042014-10-16 10:55:19 -05002708 weston_surface_build_buffer_matrix(surface,
2709 &surface->surface_to_buffer_matrix);
2710 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2711 &surface->surface_to_buffer_matrix);
2712
Jason Ekstrand7b982072014-05-20 14:33:03 -05002713 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002714 weston_surface_update_size(surface);
2715 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002716 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002717 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002718
Jason Ekstrand7b982072014-05-20 14:33:03 -05002719 state->sx = 0;
2720 state->sy = 0;
2721 state->newly_attached = 0;
2722 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002723
2724 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002725 if (weston_timeline_enabled_ &&
2726 pixman_region32_not_empty(&state->damage))
2727 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002728 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002729 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002730 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002731 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002732 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002733
2734 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002735 pixman_region32_init(&opaque);
2736 pixman_region32_intersect_rect(&opaque, &state->opaque,
2737 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002738
2739 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2740 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002741 wl_list_for_each(view, &surface->views, surface_link)
2742 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002743 }
2744
2745 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002746
2747 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002748 pixman_region32_intersect_rect(&surface->input, &state->input,
2749 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002750
Pekka Paalanenbc106382012-10-10 12:49:31 +03002751 /* wl_surface.frame */
2752 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002753 &state->frame_callback_list);
2754 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002755
2756 /* XXX:
2757 * What should happen with a feedback request, if there
2758 * is no wl_buffer attached for this commit?
2759 */
2760
2761 /* presentation.feedback */
2762 wl_list_insert_list(&surface->feedback_list,
2763 &state->feedback_list);
2764 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002765}
2766
2767static void
2768weston_surface_commit(struct weston_surface *surface)
2769{
2770 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002771
Pekka Paalanene67858b2013-04-25 13:57:42 +03002772 weston_surface_commit_subsurface_order(surface);
2773
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002774 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002775}
2776
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002777static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002778weston_subsurface_commit(struct weston_subsurface *sub);
2779
2780static void
2781weston_subsurface_parent_commit(struct weston_subsurface *sub,
2782 int parent_is_synchronized);
2783
2784static void
2785surface_commit(struct wl_client *client, struct wl_resource *resource)
2786{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002787 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002788 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2789
2790 if (sub) {
2791 weston_subsurface_commit(sub);
2792 return;
2793 }
2794
2795 weston_surface_commit(surface);
2796
2797 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2798 if (sub->surface != surface)
2799 weston_subsurface_parent_commit(sub, 0);
2800 }
2801}
2802
2803static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002804surface_set_buffer_transform(struct wl_client *client,
2805 struct wl_resource *resource, int transform)
2806{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002807 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002808
Jonny Lamba55f1392014-05-30 12:07:15 +02002809 /* if wl_output.transform grows more members this will need to be updated. */
2810 if (transform < 0 ||
2811 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2812 wl_resource_post_error(resource,
2813 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2814 "buffer transform must be a valid transform "
2815 "('%d' specified)", transform);
2816 return;
2817 }
2818
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002819 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002820 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002821}
2822
Alexander Larsson4ea95522013-05-22 14:41:37 +02002823static void
2824surface_set_buffer_scale(struct wl_client *client,
2825 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002826 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002827{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002828 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002829
Jonny Lamba55f1392014-05-30 12:07:15 +02002830 if (scale < 1) {
2831 wl_resource_post_error(resource,
2832 WL_SURFACE_ERROR_INVALID_SCALE,
2833 "buffer scale must be at least one "
2834 "('%d' specified)", scale);
2835 return;
2836 }
2837
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002838 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002839 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002840}
2841
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002842static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002843 surface_destroy,
2844 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002845 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002846 surface_frame,
2847 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002848 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002849 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002850 surface_set_buffer_transform,
2851 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002852};
2853
2854static void
2855compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002856 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002857{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002858 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002859 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002860
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002861 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002862 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002863 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002864 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002865 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002866
Jason Ekstranda85118c2013-06-27 20:17:02 -05002867 surface->resource =
2868 wl_resource_create(client, &wl_surface_interface,
2869 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002870 if (surface->resource == NULL) {
2871 weston_surface_destroy(surface);
2872 wl_resource_post_no_memory(resource);
2873 return;
2874 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002875 wl_resource_set_implementation(surface->resource, &surface_interface,
2876 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002877
2878 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002879}
2880
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002881static void
2882destroy_region(struct wl_resource *resource)
2883{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002884 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002885
2886 pixman_region32_fini(&region->region);
2887 free(region);
2888}
2889
2890static void
2891region_destroy(struct wl_client *client, struct wl_resource *resource)
2892{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002893 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002894}
2895
2896static void
2897region_add(struct wl_client *client, struct wl_resource *resource,
2898 int32_t x, int32_t y, int32_t width, int32_t height)
2899{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002900 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002901
2902 pixman_region32_union_rect(&region->region, &region->region,
2903 x, y, width, height);
2904}
2905
2906static void
2907region_subtract(struct wl_client *client, struct wl_resource *resource,
2908 int32_t x, int32_t y, int32_t width, int32_t height)
2909{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002910 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002911 pixman_region32_t rect;
2912
2913 pixman_region32_init_rect(&rect, x, y, width, height);
2914 pixman_region32_subtract(&region->region, &region->region, &rect);
2915 pixman_region32_fini(&rect);
2916}
2917
2918static const struct wl_region_interface region_interface = {
2919 region_destroy,
2920 region_add,
2921 region_subtract
2922};
2923
2924static void
2925compositor_create_region(struct wl_client *client,
2926 struct wl_resource *resource, uint32_t id)
2927{
2928 struct weston_region *region;
2929
2930 region = malloc(sizeof *region);
2931 if (region == NULL) {
2932 wl_resource_post_no_memory(resource);
2933 return;
2934 }
2935
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002936 pixman_region32_init(&region->region);
2937
Jason Ekstranda85118c2013-06-27 20:17:02 -05002938 region->resource =
2939 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002940 if (region->resource == NULL) {
2941 free(region);
2942 wl_resource_post_no_memory(resource);
2943 return;
2944 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002945 wl_resource_set_implementation(region->resource, &region_interface,
2946 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002947}
2948
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002949static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002950 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002951 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002952};
2953
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002954static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002955weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2956{
2957 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002958
Jason Ekstrand7b982072014-05-20 14:33:03 -05002959 weston_surface_commit_state(surface, &sub->cached);
2960 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002961
2962 weston_surface_commit_subsurface_order(surface);
2963
2964 weston_surface_schedule_repaint(surface);
2965
Jason Ekstrand7b982072014-05-20 14:33:03 -05002966 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002967}
2968
2969static void
2970weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2971{
2972 struct weston_surface *surface = sub->surface;
2973
2974 /*
2975 * If this commit would cause the surface to move by the
2976 * attach(dx, dy) parameters, the old damage region must be
2977 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002978 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002979 */
2980 pixman_region32_translate(&sub->cached.damage,
2981 -surface->pending.sx, -surface->pending.sy);
2982 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2983 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002984 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002985
2986 if (surface->pending.newly_attached) {
2987 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002988 weston_surface_state_set_buffer(&sub->cached,
2989 surface->pending.buffer);
2990 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002991 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002992 weston_presentation_feedback_discard_list(
2993 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002994 }
2995 sub->cached.sx += surface->pending.sx;
2996 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002997
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002998 sub->cached.buffer_viewport.changed |=
2999 surface->pending.buffer_viewport.changed;
3000 sub->cached.buffer_viewport.buffer =
3001 surface->pending.buffer_viewport.buffer;
3002 sub->cached.buffer_viewport.surface =
3003 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003004
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003005 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003006
3007 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3008
3009 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3010
3011 wl_list_insert_list(&sub->cached.frame_callback_list,
3012 &surface->pending.frame_callback_list);
3013 wl_list_init(&surface->pending.frame_callback_list);
3014
Pekka Paalanen133e4392014-09-23 22:08:46 -04003015 wl_list_insert_list(&sub->cached.feedback_list,
3016 &surface->pending.feedback_list);
3017 wl_list_init(&surface->pending.feedback_list);
3018
Jason Ekstrand7b982072014-05-20 14:33:03 -05003019 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003020}
3021
Derek Foreman280e7dd2014-10-03 13:13:42 -05003022static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003023weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3024{
3025 while (sub) {
3026 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003027 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003028
3029 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003030 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003031
3032 sub = weston_surface_to_subsurface(sub->parent);
3033 }
3034
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003035 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003036}
3037
3038static void
3039weston_subsurface_commit(struct weston_subsurface *sub)
3040{
3041 struct weston_surface *surface = sub->surface;
3042 struct weston_subsurface *tmp;
3043
3044 /* Recursive check for effectively synchronized. */
3045 if (weston_subsurface_is_synchronized(sub)) {
3046 weston_subsurface_commit_to_cache(sub);
3047 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003048 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003049 /* flush accumulated state from cache */
3050 weston_subsurface_commit_to_cache(sub);
3051 weston_subsurface_commit_from_cache(sub);
3052 } else {
3053 weston_surface_commit(surface);
3054 }
3055
3056 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3057 if (tmp->surface != surface)
3058 weston_subsurface_parent_commit(tmp, 0);
3059 }
3060 }
3061}
3062
3063static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003064weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003065{
3066 struct weston_surface *surface = sub->surface;
3067 struct weston_subsurface *tmp;
3068
Pekka Paalanene67858b2013-04-25 13:57:42 +03003069 /* From now on, commit_from_cache the whole sub-tree, regardless of
3070 * the synchronized mode of each child. This sub-surface or some
3071 * of its ancestors were synchronized, so we are synchronized
3072 * all the way down.
3073 */
3074
Jason Ekstrand7b982072014-05-20 14:33:03 -05003075 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003076 weston_subsurface_commit_from_cache(sub);
3077
3078 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3079 if (tmp->surface != surface)
3080 weston_subsurface_parent_commit(tmp, 1);
3081 }
3082}
3083
3084static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003085weston_subsurface_parent_commit(struct weston_subsurface *sub,
3086 int parent_is_synchronized)
3087{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003088 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003089 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003090 wl_list_for_each(view, &sub->surface->views, surface_link)
3091 weston_view_set_position(view,
3092 sub->position.x,
3093 sub->position.y);
3094
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003095 sub->position.set = 0;
3096 }
3097
3098 if (parent_is_synchronized || sub->synchronized)
3099 weston_subsurface_synchronized_commit(sub);
3100}
3101
Pekka Paalanen8274d902014-08-06 19:36:51 +03003102static int
3103subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3104{
3105 return snprintf(buf, len, "sub-surface");
3106}
3107
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003108static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003109subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003110{
3111 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003112 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003113
Jason Ekstranda7af7042013-10-12 22:38:11 -05003114 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003115 weston_view_set_position(view,
3116 view->geometry.x + dx,
3117 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003118
3119 /* No need to check parent mappedness, because if parent is not
3120 * mapped, parent is not in a visible layer, so this sub-surface
3121 * will not be drawn either.
3122 */
3123 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003124 struct weston_output *output;
3125
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003126 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003127 * because that would call it also for the parent surface,
3128 * which might not be mapped yet. That would lead to
3129 * inconsistent state, where the window could never be
3130 * mapped.
3131 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003132 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003133 * weston_surface_is_mapped() return true, so that when the
3134 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003135 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003136 */
3137 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003138 output = container_of(compositor->output_list.next,
3139 struct weston_output, link);
3140
3141 surface->output = output;
3142 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003143 }
3144}
3145
3146static struct weston_subsurface *
3147weston_surface_to_subsurface(struct weston_surface *surface)
3148{
3149 if (surface->configure == subsurface_configure)
3150 return surface->configure_private;
3151
3152 return NULL;
3153}
3154
Pekka Paalanen01388e22013-04-25 13:57:44 +03003155WL_EXPORT struct weston_surface *
3156weston_surface_get_main_surface(struct weston_surface *surface)
3157{
3158 struct weston_subsurface *sub;
3159
3160 while (surface && (sub = weston_surface_to_subsurface(surface)))
3161 surface = sub->parent;
3162
3163 return surface;
3164}
3165
Pekka Paalanen50b67472014-10-01 15:02:41 +03003166WL_EXPORT int
3167weston_surface_set_role(struct weston_surface *surface,
3168 const char *role_name,
3169 struct wl_resource *error_resource,
3170 uint32_t error_code)
3171{
3172 assert(role_name);
3173
3174 if (surface->role_name == NULL ||
3175 surface->role_name == role_name ||
3176 strcmp(surface->role_name, role_name) == 0) {
3177 surface->role_name = role_name;
3178
3179 return 0;
3180 }
3181
3182 wl_resource_post_error(error_resource, error_code,
3183 "Cannot assign role %s to wl_surface@%d,"
3184 " already has role %s\n",
3185 role_name,
3186 wl_resource_get_id(surface->resource),
3187 surface->role_name);
3188 return -1;
3189}
3190
Pekka Paalanen8274d902014-08-06 19:36:51 +03003191WL_EXPORT void
3192weston_surface_set_label_func(struct weston_surface *surface,
3193 int (*desc)(struct weston_surface *,
3194 char *, size_t))
3195{
3196 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003197 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003198}
3199
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003200/** Get the size of surface contents
3201 *
3202 * \param surface The surface to query.
3203 * \param width Returns the width of raw contents.
3204 * \param height Returns the height of raw contents.
3205 *
3206 * Retrieves the raw surface content size in pixels for the given surface.
3207 * This is the whole content size in buffer pixels. If the surface
3208 * has no content or the renderer does not implement this feature,
3209 * zeroes are returned.
3210 *
3211 * This function is used to determine the buffer size needed for
3212 * a weston_surface_copy_content() call.
3213 */
3214WL_EXPORT void
3215weston_surface_get_content_size(struct weston_surface *surface,
3216 int *width, int *height)
3217{
3218 struct weston_renderer *rer = surface->compositor->renderer;
3219
3220 if (!rer->surface_get_content_size) {
3221 *width = 0;
3222 *height = 0;
3223 return;
3224 }
3225
3226 rer->surface_get_content_size(surface, width, height);
3227}
3228
3229/** Copy surface contents to system memory.
3230 *
3231 * \param surface The surface to copy from.
3232 * \param target Pointer to the target memory buffer.
3233 * \param size Size of the target buffer in bytes.
3234 * \param src_x X location on contents to copy from.
3235 * \param src_y Y location on contents to copy from.
3236 * \param width Width in pixels of the area to copy.
3237 * \param height Height in pixels of the area to copy.
3238 * \return 0 for success, -1 for failure.
3239 *
3240 * Surface contents are maintained by the renderer. They can be in a
3241 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3242 * else.
3243 *
3244 * Surface contents are copied into memory pointed to by target,
3245 * which has size bytes of space available. The target memory
3246 * may be larger than needed, but being smaller returns an error.
3247 * The extra bytes in target may or may not be written; their content is
3248 * unspecified. Size must be large enough to hold the image.
3249 *
3250 * The image in the target memory will be arranged in rows from
3251 * top to bottom, and pixels on a row from left to right. The pixel
3252 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3253 * width * 4.
3254 *
3255 * Parameters src_x and src_y define the upper-left corner in buffer
3256 * coordinates (pixels) to copy from. Parameters width and height
3257 * define the size of the area to copy in pixels.
3258 *
3259 * The rectangle defined by src_x, src_y, width, height must fit in
3260 * the surface contents. Otherwise an error is returned.
3261 *
3262 * Use surface_get_data_size to determine the content size; the
3263 * needed target buffer size and rectangle limits.
3264 *
3265 * CURRENT IMPLEMENTATION RESTRICTIONS:
3266 * - the machine must be little-endian due to Pixman formats.
3267 *
3268 * NOTE: Pixman formats are premultiplied.
3269 */
3270WL_EXPORT int
3271weston_surface_copy_content(struct weston_surface *surface,
3272 void *target, size_t size,
3273 int src_x, int src_y,
3274 int width, int height)
3275{
3276 struct weston_renderer *rer = surface->compositor->renderer;
3277 int cw, ch;
3278 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3279
3280 if (!rer->surface_copy_content)
3281 return -1;
3282
3283 weston_surface_get_content_size(surface, &cw, &ch);
3284
3285 if (src_x < 0 || src_y < 0)
3286 return -1;
3287
3288 if (width <= 0 || height <= 0)
3289 return -1;
3290
3291 if (src_x + width > cw || src_y + height > ch)
3292 return -1;
3293
3294 if (width * bytespp * height > size)
3295 return -1;
3296
3297 return rer->surface_copy_content(surface, target, size,
3298 src_x, src_y, width, height);
3299}
3300
Pekka Paalanene67858b2013-04-25 13:57:42 +03003301static void
3302subsurface_set_position(struct wl_client *client,
3303 struct wl_resource *resource, int32_t x, int32_t y)
3304{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003305 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003306
3307 if (!sub)
3308 return;
3309
3310 sub->position.x = x;
3311 sub->position.y = y;
3312 sub->position.set = 1;
3313}
3314
3315static struct weston_subsurface *
3316subsurface_from_surface(struct weston_surface *surface)
3317{
3318 struct weston_subsurface *sub;
3319
3320 sub = weston_surface_to_subsurface(surface);
3321 if (sub)
3322 return sub;
3323
3324 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3325 if (sub->surface == surface)
3326 return sub;
3327
3328 return NULL;
3329}
3330
3331static struct weston_subsurface *
3332subsurface_sibling_check(struct weston_subsurface *sub,
3333 struct weston_surface *surface,
3334 const char *request)
3335{
3336 struct weston_subsurface *sibling;
3337
3338 sibling = subsurface_from_surface(surface);
3339
3340 if (!sibling) {
3341 wl_resource_post_error(sub->resource,
3342 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3343 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003344 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003345 return NULL;
3346 }
3347
3348 if (sibling->parent != sub->parent) {
3349 wl_resource_post_error(sub->resource,
3350 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3351 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003352 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003353 return NULL;
3354 }
3355
3356 return sibling;
3357}
3358
3359static void
3360subsurface_place_above(struct wl_client *client,
3361 struct wl_resource *resource,
3362 struct wl_resource *sibling_resource)
3363{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003364 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003365 struct weston_surface *surface =
3366 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003367 struct weston_subsurface *sibling;
3368
3369 if (!sub)
3370 return;
3371
3372 sibling = subsurface_sibling_check(sub, surface, "place_above");
3373 if (!sibling)
3374 return;
3375
3376 wl_list_remove(&sub->parent_link_pending);
3377 wl_list_insert(sibling->parent_link_pending.prev,
3378 &sub->parent_link_pending);
3379}
3380
3381static void
3382subsurface_place_below(struct wl_client *client,
3383 struct wl_resource *resource,
3384 struct wl_resource *sibling_resource)
3385{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003386 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003387 struct weston_surface *surface =
3388 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003389 struct weston_subsurface *sibling;
3390
3391 if (!sub)
3392 return;
3393
3394 sibling = subsurface_sibling_check(sub, surface, "place_below");
3395 if (!sibling)
3396 return;
3397
3398 wl_list_remove(&sub->parent_link_pending);
3399 wl_list_insert(&sibling->parent_link_pending,
3400 &sub->parent_link_pending);
3401}
3402
3403static void
3404subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3405{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003406 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003407
3408 if (sub)
3409 sub->synchronized = 1;
3410}
3411
3412static void
3413subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3414{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003415 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003416
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003417 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003418 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003419
3420 /* If sub became effectively desynchronized, flush. */
3421 if (!weston_subsurface_is_synchronized(sub))
3422 weston_subsurface_synchronized_commit(sub);
3423 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003424}
3425
3426static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003427weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3428{
3429 wl_list_remove(&sub->parent_link);
3430 wl_list_remove(&sub->parent_link_pending);
3431 wl_list_remove(&sub->parent_destroy_listener.link);
3432 sub->parent = NULL;
3433}
3434
3435static void
3436weston_subsurface_destroy(struct weston_subsurface *sub);
3437
3438static void
3439subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3440{
3441 struct weston_subsurface *sub =
3442 container_of(listener, struct weston_subsurface,
3443 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003444 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003445
3446 /* The protocol object (wl_resource) is left inert. */
3447 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003448 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003449
3450 weston_subsurface_destroy(sub);
3451}
3452
3453static void
3454subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3455{
3456 struct weston_subsurface *sub =
3457 container_of(listener, struct weston_subsurface,
3458 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003459 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003460 assert(sub->surface != sub->parent);
3461
3462 if (weston_surface_is_mapped(sub->surface))
3463 weston_surface_unmap(sub->surface);
3464
3465 weston_subsurface_unlink_parent(sub);
3466}
3467
3468static void
3469subsurface_resource_destroy(struct wl_resource *resource)
3470{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003471 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003472
3473 if (sub)
3474 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003475}
3476
3477static void
3478subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3479{
3480 wl_resource_destroy(resource);
3481}
3482
3483static void
3484weston_subsurface_link_parent(struct weston_subsurface *sub,
3485 struct weston_surface *parent)
3486{
3487 sub->parent = parent;
3488 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003489 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003490 &sub->parent_destroy_listener);
3491
3492 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3493 wl_list_insert(&parent->subsurface_list_pending,
3494 &sub->parent_link_pending);
3495}
3496
3497static void
3498weston_subsurface_link_surface(struct weston_subsurface *sub,
3499 struct weston_surface *surface)
3500{
3501 sub->surface = surface;
3502 sub->surface_destroy_listener.notify =
3503 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003504 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003505 &sub->surface_destroy_listener);
3506}
3507
3508static void
3509weston_subsurface_destroy(struct weston_subsurface *sub)
3510{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003511 struct weston_view *view, *next;
3512
Pekka Paalanene67858b2013-04-25 13:57:42 +03003513 assert(sub->surface);
3514
3515 if (sub->resource) {
3516 assert(weston_surface_to_subsurface(sub->surface) == sub);
3517 assert(sub->parent_destroy_listener.notify ==
3518 subsurface_handle_parent_destroy);
3519
George Kiagiadakised04d382014-06-13 18:10:26 +02003520 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3521 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003522 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003523 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003524
Pekka Paalanene67858b2013-04-25 13:57:42 +03003525 if (sub->parent)
3526 weston_subsurface_unlink_parent(sub);
3527
Jason Ekstrand7b982072014-05-20 14:33:03 -05003528 weston_surface_state_fini(&sub->cached);
3529 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003530
3531 sub->surface->configure = NULL;
3532 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003533 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003534 } else {
3535 /* the dummy weston_subsurface for the parent itself */
3536 assert(sub->parent_destroy_listener.notify == NULL);
3537 wl_list_remove(&sub->parent_link);
3538 wl_list_remove(&sub->parent_link_pending);
3539 }
3540
3541 wl_list_remove(&sub->surface_destroy_listener.link);
3542 free(sub);
3543}
3544
3545static const struct wl_subsurface_interface subsurface_implementation = {
3546 subsurface_destroy,
3547 subsurface_set_position,
3548 subsurface_place_above,
3549 subsurface_place_below,
3550 subsurface_set_sync,
3551 subsurface_set_desync
3552};
3553
3554static struct weston_subsurface *
3555weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3556 struct weston_surface *parent)
3557{
3558 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003559 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003560
Bryce Harringtonde16d892014-11-20 22:21:57 -08003561 sub = zalloc(sizeof *sub);
3562 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003563 return NULL;
3564
Jason Ekstranda7af7042013-10-12 22:38:11 -05003565 wl_list_init(&sub->unused_views);
3566
Jason Ekstranda85118c2013-06-27 20:17:02 -05003567 sub->resource =
3568 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003569 if (!sub->resource) {
3570 free(sub);
3571 return NULL;
3572 }
3573
Jason Ekstranda85118c2013-06-27 20:17:02 -05003574 wl_resource_set_implementation(sub->resource,
3575 &subsurface_implementation,
3576 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003577 weston_subsurface_link_surface(sub, surface);
3578 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003579 weston_surface_state_init(&sub->cached);
3580 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003581 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003582
3583 return sub;
3584}
3585
3586/* Create a dummy subsurface for having the parent itself in its
3587 * sub-surface lists. Makes stacking order manipulation easy.
3588 */
3589static struct weston_subsurface *
3590weston_subsurface_create_for_parent(struct weston_surface *parent)
3591{
3592 struct weston_subsurface *sub;
3593
Bryce Harringtonde16d892014-11-20 22:21:57 -08003594 sub = zalloc(sizeof *sub);
3595 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003596 return NULL;
3597
3598 weston_subsurface_link_surface(sub, parent);
3599 sub->parent = parent;
3600 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3601 wl_list_insert(&parent->subsurface_list_pending,
3602 &sub->parent_link_pending);
3603
3604 return sub;
3605}
3606
3607static void
3608subcompositor_get_subsurface(struct wl_client *client,
3609 struct wl_resource *resource,
3610 uint32_t id,
3611 struct wl_resource *surface_resource,
3612 struct wl_resource *parent_resource)
3613{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003614 struct weston_surface *surface =
3615 wl_resource_get_user_data(surface_resource);
3616 struct weston_surface *parent =
3617 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003618 struct weston_subsurface *sub;
3619 static const char where[] = "get_subsurface: wl_subsurface@";
3620
3621 if (surface == parent) {
3622 wl_resource_post_error(resource,
3623 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3624 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003625 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003626 return;
3627 }
3628
3629 if (weston_surface_to_subsurface(surface)) {
3630 wl_resource_post_error(resource,
3631 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3632 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003633 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003634 return;
3635 }
3636
Pekka Paalanen50b67472014-10-01 15:02:41 +03003637 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3638 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003639 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003640
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003641 if (weston_surface_get_main_surface(parent) == surface) {
3642 wl_resource_post_error(resource,
3643 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3644 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003645 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003646 return;
3647 }
3648
Pekka Paalanene67858b2013-04-25 13:57:42 +03003649 /* make sure the parent is in its own list */
3650 if (wl_list_empty(&parent->subsurface_list)) {
3651 if (!weston_subsurface_create_for_parent(parent)) {
3652 wl_resource_post_no_memory(resource);
3653 return;
3654 }
3655 }
3656
3657 sub = weston_subsurface_create(id, surface, parent);
3658 if (!sub) {
3659 wl_resource_post_no_memory(resource);
3660 return;
3661 }
3662
3663 surface->configure = subsurface_configure;
3664 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003665 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003666}
3667
3668static void
3669subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3670{
3671 wl_resource_destroy(resource);
3672}
3673
3674static const struct wl_subcompositor_interface subcompositor_interface = {
3675 subcompositor_destroy,
3676 subcompositor_get_subsurface
3677};
3678
3679static void
3680bind_subcompositor(struct wl_client *client,
3681 void *data, uint32_t version, uint32_t id)
3682{
3683 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003684 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003685
Jason Ekstranda85118c2013-06-27 20:17:02 -05003686 resource =
3687 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003688 if (resource == NULL) {
3689 wl_client_post_no_memory(client);
3690 return;
3691 }
3692 wl_resource_set_implementation(resource, &subcompositor_interface,
3693 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003694}
3695
3696static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003697weston_compositor_dpms(struct weston_compositor *compositor,
3698 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003699{
3700 struct weston_output *output;
3701
3702 wl_list_for_each(output, &compositor->output_list, link)
3703 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003704 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003705}
3706
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003707WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003708weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003709{
Neil Roberts8b62e202013-09-30 13:14:47 +01003710 uint32_t old_state = compositor->state;
3711
3712 /* The state needs to be changed before emitting the wake
3713 * signal because that may try to schedule a repaint which
3714 * will not work if the compositor is still sleeping */
3715 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3716
3717 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003718 case WESTON_COMPOSITOR_SLEEPING:
3719 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3720 /* fall through */
3721 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003722 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003723 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003724 /* fall through */
3725 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003726 wl_event_source_timer_update(compositor->idle_source,
3727 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003728 }
3729}
3730
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003731WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003732weston_compositor_offscreen(struct weston_compositor *compositor)
3733{
3734 switch (compositor->state) {
3735 case WESTON_COMPOSITOR_OFFSCREEN:
3736 return;
3737 case WESTON_COMPOSITOR_SLEEPING:
3738 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3739 /* fall through */
3740 default:
3741 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3742 wl_event_source_timer_update(compositor->idle_source, 0);
3743 }
3744}
3745
3746WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003747weston_compositor_sleep(struct weston_compositor *compositor)
3748{
3749 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3750 return;
3751
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003752 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003753 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3754 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3755}
3756
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003757static int
3758idle_handler(void *data)
3759{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003760 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003761
3762 if (compositor->idle_inhibit)
3763 return 1;
3764
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003765 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003766 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003767
3768 return 1;
3769}
3770
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003771WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003772weston_plane_init(struct weston_plane *plane,
3773 struct weston_compositor *ec,
3774 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003775{
3776 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003777 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003778 plane->x = x;
3779 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003780 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003781
3782 /* Init the link so that the call to wl_list_remove() when releasing
3783 * the plane without ever stacking doesn't lead to a crash */
3784 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003785}
3786
3787WL_EXPORT void
3788weston_plane_release(struct weston_plane *plane)
3789{
Xiong Zhang97116532013-10-23 13:58:31 +08003790 struct weston_view *view;
3791
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003792 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003793 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003794
Xiong Zhang97116532013-10-23 13:58:31 +08003795 wl_list_for_each(view, &plane->compositor->view_list, link) {
3796 if (view->plane == plane)
3797 view->plane = NULL;
3798 }
3799
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003800 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003801}
3802
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003803WL_EXPORT void
3804weston_compositor_stack_plane(struct weston_compositor *ec,
3805 struct weston_plane *plane,
3806 struct weston_plane *above)
3807{
3808 if (above)
3809 wl_list_insert(above->link.prev, &plane->link);
3810 else
3811 wl_list_insert(&ec->plane_list, &plane->link);
3812}
3813
Casey Dahlin9074db52012-04-19 22:50:09 -04003814static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003815{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003816 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003817}
3818
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003819static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003820bind_output(struct wl_client *client,
3821 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003822{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003823 struct weston_output *output = data;
3824 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003825 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003826
Jason Ekstranda85118c2013-06-27 20:17:02 -05003827 resource = wl_resource_create(client, &wl_output_interface,
3828 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003829 if (resource == NULL) {
3830 wl_client_post_no_memory(client);
3831 return;
3832 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003833
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003834 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003835 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003836
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003837 wl_output_send_geometry(resource,
3838 output->x,
3839 output->y,
3840 output->mm_width,
3841 output->mm_height,
3842 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003843 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003844 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003845 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003846 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003847 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003848
3849 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003850 wl_output_send_mode(resource,
3851 mode->flags,
3852 mode->width,
3853 mode->height,
3854 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003855 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003856
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003857 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003858 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003859}
3860
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003861/* Move other outputs when one is removed so the space remains contiguos. */
3862static void
3863weston_compositor_remove_output(struct weston_compositor *compositor,
3864 struct weston_output *remove_output)
3865{
3866 struct weston_output *output;
3867 int offset = 0;
3868
3869 wl_list_for_each(output, &compositor->output_list, link) {
3870 if (output == remove_output) {
3871 offset = output->width;
3872 continue;
3873 }
3874
3875 if (offset > 0) {
3876 weston_output_move(output,
3877 output->x - offset, output->y);
3878 output->dirty = 1;
3879 }
3880 }
3881}
3882
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003883WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003884weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003885{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003886 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003887 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003888
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003889 output->destroying = 1;
3890
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003891 wl_list_for_each(view, &output->compositor->view_list, link) {
3892 if (view->output_mask & (1 << output->id))
3893 weston_view_assign_output(view);
3894 }
3895
Pekka Paalanen0513a952014-05-21 16:17:27 +03003896 wl_event_source_remove(output->repaint_timer);
3897
Pekka Paalanen133e4392014-09-23 22:08:46 -04003898 weston_presentation_feedback_discard_list(&output->feedback_list);
3899
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003900 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003901 wl_list_remove(&output->link);
3902
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003903 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003904 wl_signal_emit(&output->destroy_signal, output);
3905
Richard Hughesafe690c2013-05-02 10:10:04 +01003906 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003907 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003908 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003909 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003910
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003911 wl_resource_for_each(resource, &output->resource_list) {
3912 wl_resource_set_destructor(resource, NULL);
3913 }
3914
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003915 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003916}
3917
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003918WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003919weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003920{
Scott Moreau850ca422012-05-21 15:21:25 -06003921 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003922
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003923 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003924 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003925
Scott Moreauccbf29d2012-02-22 14:21:41 -07003926 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003927 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003928 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003929 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003930 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003931 weston_matrix_scale(&output->matrix, magnification,
3932 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003933 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003934
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003935 switch (output->transform) {
3936 case WL_OUTPUT_TRANSFORM_FLIPPED:
3937 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3938 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3939 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3940 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3941 weston_matrix_scale(&output->matrix, -1, 1, 1);
3942 break;
3943 }
3944
3945 switch (output->transform) {
3946 default:
3947 case WL_OUTPUT_TRANSFORM_NORMAL:
3948 case WL_OUTPUT_TRANSFORM_FLIPPED:
3949 break;
3950 case WL_OUTPUT_TRANSFORM_90:
3951 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3952 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
3953 weston_matrix_rotate_xy(&output->matrix, 0, 1);
3954 break;
3955 case WL_OUTPUT_TRANSFORM_180:
3956 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3957 weston_matrix_translate(&output->matrix,
3958 -output->width, -output->height, 0);
3959 weston_matrix_rotate_xy(&output->matrix, -1, 0);
3960 break;
3961 case WL_OUTPUT_TRANSFORM_270:
3962 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3963 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3964 weston_matrix_rotate_xy(&output->matrix, 0, -1);
3965 break;
3966 }
3967
3968 if (output->current_scale != 1)
3969 weston_matrix_scale(&output->matrix,
3970 output->current_scale,
3971 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003972
Scott Moreauccbf29d2012-02-22 14:21:41 -07003973 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05003974
3975 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003976}
3977
Scott Moreau1bad5db2012-08-18 01:04:05 -06003978static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003979weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003980{
3981 output->transform = transform;
3982
3983 switch (transform) {
3984 case WL_OUTPUT_TRANSFORM_90:
3985 case WL_OUTPUT_TRANSFORM_270:
3986 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3987 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3988 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003989 output->width = output->current_mode->height;
3990 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003991 break;
3992 case WL_OUTPUT_TRANSFORM_NORMAL:
3993 case WL_OUTPUT_TRANSFORM_180:
3994 case WL_OUTPUT_TRANSFORM_FLIPPED:
3995 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003996 output->width = output->current_mode->width;
3997 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003998 break;
3999 default:
4000 break;
4001 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06004002
Hardening57388e42013-09-18 23:56:36 +02004003 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02004004 output->width /= scale;
4005 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004006}
4007
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004008static void
4009weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004010{
4011 output->x = x;
4012 output->y = y;
4013
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004014 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004015 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004016 output->width,
4017 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004018}
4019
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004020WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004021weston_output_move(struct weston_output *output, int x, int y)
4022{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004023 struct wl_resource *resource;
4024
4025 output->move_x = x - output->x;
4026 output->move_y = y - output->y;
4027
4028 if (output->move_x == 0 && output->move_y == 0)
4029 return;
4030
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004031 weston_output_init_geometry(output, x, y);
4032
4033 output->dirty = 1;
4034
4035 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004036 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004037
4038 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004039 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004040 wl_output_send_geometry(resource,
4041 output->x,
4042 output->y,
4043 output->mm_width,
4044 output->mm_height,
4045 output->subpixel,
4046 output->make,
4047 output->model,
4048 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004049
4050 if (wl_resource_get_version(resource) >= 2)
4051 wl_output_send_done(resource);
4052 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004053}
4054
4055WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004056weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004057 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004058 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004059{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004060 struct wl_event_loop *loop;
4061
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004062 output->compositor = c;
4063 output->x = x;
4064 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004065 output->mm_width = mm_width;
4066 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004067 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004068 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004069
Alexander Larsson0b135062013-05-28 16:23:36 +02004070 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004071 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004072
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004073 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004074 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004075
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004076 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004077 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004078 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004079 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004080 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02004081
Pekka Paalanen0513a952014-05-21 16:17:27 +03004082 loop = wl_display_get_event_loop(c->wl_display);
4083 output->repaint_timer = wl_event_loop_add_timer(loop,
4084 output_repaint_timer_handler, output);
4085
Casey Dahlin58ba1372012-04-19 22:50:08 -04004086 output->id = ffs(~output->compositor->output_id_pool) - 1;
4087 output->compositor->output_id_pool |= 1 << output->id;
4088
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004089 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004090 wl_global_create(c->wl_display, &wl_output_interface, 2,
4091 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004092}
4093
4094/** Adds an output to the compositor's output list and
4095 * send the compositor's output_created signal.
4096 *
4097 * \param compositor The compositor instance.
4098 * \param output The output to be added.
4099 */
4100WL_EXPORT void
4101weston_compositor_add_output(struct weston_compositor *compositor,
4102 struct weston_output *output)
4103{
4104 wl_list_insert(compositor->output_list.prev, &output->link);
4105 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004106}
4107
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004108WL_EXPORT void
4109weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004110 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004111 wl_fixed_t *x, wl_fixed_t *y)
4112{
Derek Foreman0f679412014-10-02 13:41:17 -05004113 struct weston_vector p = { {
4114 wl_fixed_to_double(device_x),
4115 wl_fixed_to_double(device_y),
4116 0.0,
4117 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004118
Derek Foreman67a18b92015-03-24 11:36:14 -05004119 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004120
Derek Foreman0f679412014-10-02 13:41:17 -05004121 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4122 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004123}
4124
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004125static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004126destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004127{
Jonny Lamb74130762013-11-26 18:19:46 +01004128 struct weston_surface *surface =
4129 wl_resource_get_user_data(resource);
4130
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004131 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004132 surface->pending.buffer_viewport.buffer.src_width =
4133 wl_fixed_from_int(-1);
4134 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004135 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004136}
4137
4138static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004139viewport_destroy(struct wl_client *client,
4140 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004141{
4142 wl_resource_destroy(resource);
4143}
4144
4145static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004146viewport_set(struct wl_client *client,
4147 struct wl_resource *resource,
4148 wl_fixed_t src_x,
4149 wl_fixed_t src_y,
4150 wl_fixed_t src_width,
4151 wl_fixed_t src_height,
4152 int32_t dst_width,
4153 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004154{
Jonny Lamb74130762013-11-26 18:19:46 +01004155 struct weston_surface *surface =
4156 wl_resource_get_user_data(resource);
4157
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004158 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004159
Jonny Lamb8ae35902013-11-26 18:19:45 +01004160 if (wl_fixed_to_double(src_width) < 0 ||
4161 wl_fixed_to_double(src_height) < 0) {
4162 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004163 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004164 "source dimensions must be non-negative (%fx%f)",
4165 wl_fixed_to_double(src_width),
4166 wl_fixed_to_double(src_height));
4167 return;
4168 }
4169
4170 if (dst_width <= 0 || dst_height <= 0) {
4171 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004172 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004173 "destination dimensions must be positive (%dx%d)",
4174 dst_width, dst_height);
4175 return;
4176 }
4177
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004178 surface->pending.buffer_viewport.buffer.src_x = src_x;
4179 surface->pending.buffer_viewport.buffer.src_y = src_y;
4180 surface->pending.buffer_viewport.buffer.src_width = src_width;
4181 surface->pending.buffer_viewport.buffer.src_height = src_height;
4182 surface->pending.buffer_viewport.surface.width = dst_width;
4183 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004184 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004185}
4186
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004187static void
4188viewport_set_source(struct wl_client *client,
4189 struct wl_resource *resource,
4190 wl_fixed_t src_x,
4191 wl_fixed_t src_y,
4192 wl_fixed_t src_width,
4193 wl_fixed_t src_height)
4194{
4195 struct weston_surface *surface =
4196 wl_resource_get_user_data(resource);
4197
4198 assert(surface->viewport_resource != NULL);
4199
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004200 if (src_width == wl_fixed_from_int(-1) &&
4201 src_height == wl_fixed_from_int(-1)) {
4202 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004203 surface->pending.buffer_viewport.buffer.src_width =
4204 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004205 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004206 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004207 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004208
4209 if (src_width <= 0 || src_height <= 0) {
4210 wl_resource_post_error(resource,
4211 WL_VIEWPORT_ERROR_BAD_VALUE,
4212 "source size must be positive (%fx%f)",
4213 wl_fixed_to_double(src_width),
4214 wl_fixed_to_double(src_height));
4215 return;
4216 }
4217
4218 surface->pending.buffer_viewport.buffer.src_x = src_x;
4219 surface->pending.buffer_viewport.buffer.src_y = src_y;
4220 surface->pending.buffer_viewport.buffer.src_width = src_width;
4221 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004222 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004223}
4224
4225static void
4226viewport_set_destination(struct wl_client *client,
4227 struct wl_resource *resource,
4228 int32_t dst_width,
4229 int32_t dst_height)
4230{
4231 struct weston_surface *surface =
4232 wl_resource_get_user_data(resource);
4233
4234 assert(surface->viewport_resource != NULL);
4235
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004236 if (dst_width == -1 && dst_height == -1) {
4237 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004238 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004239 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004240 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004241 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004242
4243 if (dst_width <= 0 || dst_height <= 0) {
4244 wl_resource_post_error(resource,
4245 WL_VIEWPORT_ERROR_BAD_VALUE,
4246 "destination size must be positive (%dx%d)",
4247 dst_width, dst_height);
4248 return;
4249 }
4250
4251 surface->pending.buffer_viewport.surface.width = dst_width;
4252 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004253 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004254}
4255
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004256static const struct wl_viewport_interface viewport_interface = {
4257 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004258 viewport_set,
4259 viewport_set_source,
4260 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004261};
4262
4263static void
4264scaler_destroy(struct wl_client *client,
4265 struct wl_resource *resource)
4266{
4267 wl_resource_destroy(resource);
4268}
4269
4270static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004271scaler_get_viewport(struct wl_client *client,
4272 struct wl_resource *scaler,
4273 uint32_t id,
4274 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004275{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004276 int version = wl_resource_get_version(scaler);
4277 struct weston_surface *surface =
4278 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004279 struct wl_resource *resource;
4280
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004281 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004282 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004283 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4284 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004285 return;
4286 }
4287
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004288 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004289 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004290 if (resource == NULL) {
4291 wl_client_post_no_memory(client);
4292 return;
4293 }
4294
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004295 wl_resource_set_implementation(resource, &viewport_interface,
4296 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004297
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004298 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004299}
4300
4301static const struct wl_scaler_interface scaler_interface = {
4302 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004303 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004304};
4305
4306static void
4307bind_scaler(struct wl_client *client,
4308 void *data, uint32_t version, uint32_t id)
4309{
4310 struct wl_resource *resource;
4311
4312 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004313 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004314 if (resource == NULL) {
4315 wl_client_post_no_memory(client);
4316 return;
4317 }
4318
4319 wl_resource_set_implementation(resource, &scaler_interface,
4320 NULL, NULL);
4321}
4322
4323static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004324destroy_presentation_feedback(struct wl_resource *feedback_resource)
4325{
4326 struct weston_presentation_feedback *feedback;
4327
4328 feedback = wl_resource_get_user_data(feedback_resource);
4329
4330 wl_list_remove(&feedback->link);
4331 free(feedback);
4332}
4333
4334static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004335presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4336{
4337 wl_resource_destroy(resource);
4338}
4339
4340static void
4341presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004342 struct wl_resource *presentation_resource,
4343 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004344 uint32_t callback)
4345{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004346 struct weston_surface *surface;
4347 struct weston_presentation_feedback *feedback;
4348
4349 surface = wl_resource_get_user_data(surface_resource);
4350
Bryce Harringtonde16d892014-11-20 22:21:57 -08004351 feedback = zalloc(sizeof *feedback);
4352 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004353 goto err_calloc;
4354
4355 feedback->resource = wl_resource_create(client,
4356 &presentation_feedback_interface,
4357 1, callback);
4358 if (!feedback->resource)
4359 goto err_create;
4360
4361 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4362 destroy_presentation_feedback);
4363
4364 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4365
4366 return;
4367
4368err_create:
4369 free(feedback);
4370
4371err_calloc:
4372 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004373}
4374
4375static const struct presentation_interface presentation_implementation = {
4376 presentation_destroy,
4377 presentation_feedback
4378};
4379
4380static void
4381bind_presentation(struct wl_client *client,
4382 void *data, uint32_t version, uint32_t id)
4383{
4384 struct weston_compositor *compositor = data;
4385 struct wl_resource *resource;
4386
4387 resource = wl_resource_create(client, &presentation_interface,
4388 MIN(version, 1), id);
4389 if (resource == NULL) {
4390 wl_client_post_no_memory(client);
4391 return;
4392 }
4393
4394 wl_resource_set_implementation(resource, &presentation_implementation,
4395 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004396 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004397}
4398
4399static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004400compositor_bind(struct wl_client *client,
4401 void *data, uint32_t version, uint32_t id)
4402{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004403 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004404 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004405
Jason Ekstranda85118c2013-06-27 20:17:02 -05004406 resource = wl_resource_create(client, &wl_compositor_interface,
4407 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004408 if (resource == NULL) {
4409 wl_client_post_no_memory(client);
4410 return;
4411 }
4412
4413 wl_resource_set_implementation(resource, &compositor_interface,
4414 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004415}
4416
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004417WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004418weston_environment_get_fd(const char *env)
4419{
4420 char *e, *end;
4421 int fd, flags;
4422
4423 e = getenv(env);
4424 if (!e)
4425 return -1;
4426 fd = strtol(e, &end, 0);
4427 if (*end != '\0')
4428 return -1;
4429
4430 flags = fcntl(fd, F_GETFD);
4431 if (flags == -1)
4432 return -1;
4433
4434 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4435 unsetenv(env);
4436
4437 return fd;
4438}
4439
Pekka Paalanenb5026542014-11-12 15:09:24 +02004440static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004441timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004442 uint32_t key, void *data)
4443{
4444 struct weston_compositor *compositor = data;
4445
4446 if (weston_timeline_enabled_)
4447 weston_timeline_close();
4448 else
4449 weston_timeline_open(compositor);
4450}
4451
Giulio Camuffo459137b2014-10-11 23:56:24 +03004452/** Create the compositor.
4453 *
4454 * This functions creates and initializes a compositor instance.
4455 *
4456 * \param display The Wayland display to be used.
4457 * \param user_data A pointer to an object that can later be retrieved
4458 * using the \ref weston_compositor_get_user_data function.
4459 * \return The compositor instance on success or NULL on failure.
4460 */
4461WL_EXPORT struct weston_compositor *
4462weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004463{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004464 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004465 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004466
Giulio Camuffo459137b2014-10-11 23:56:24 +03004467 ec = zalloc(sizeof *ec);
4468 if (!ec)
4469 return NULL;
4470
4471 ec->wl_display = display;
4472 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004473 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004474 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004475 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004476 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004477 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004478 wl_signal_init(&ec->idle_signal);
4479 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004480 wl_signal_init(&ec->show_input_panel_signal);
4481 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004482 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004483 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004484 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004485 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004486 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004487 wl_signal_init(&ec->session_signal);
4488 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004489
Casey Dahlin58ba1372012-04-19 22:50:08 -04004490 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004491 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004492
Giulio Camuffo954f1832014-10-11 18:27:30 +03004493 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004494 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004495 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004496
Giulio Camuffo954f1832014-10-11 18:27:30 +03004497 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004498 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004499 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004500
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004501 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004502 ec, bind_scaler))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004503 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004504
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004505 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4506 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004507 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004508
Jason Ekstranda7af7042013-10-12 22:38:11 -05004509 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004510 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004511 wl_list_init(&ec->layer_list);
4512 wl_list_init(&ec->seat_list);
4513 wl_list_init(&ec->output_list);
4514 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004515 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004516 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004517 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004518 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004519 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004520
Xiong Zhang97116532013-10-23 13:58:31 +08004521 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004522 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004523
Giulio Camuffo459137b2014-10-11 23:56:24 +03004524 wl_data_device_manager_init(ec->wl_display);
4525
4526 wl_display_init_shm(ec->wl_display);
4527
4528 loop = wl_display_get_event_loop(ec->wl_display);
4529 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4530 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4531
4532 ec->input_loop = wl_event_loop_create();
4533
4534 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4535 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4536
4537 weston_compositor_add_debug_binding(ec, KEY_T,
4538 timeline_key_binding_handler, ec);
4539
4540 weston_compositor_schedule_repaint(ec);
4541
4542 return ec;
4543
4544fail:
4545 free(ec);
4546 return NULL;
4547}
4548
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004549WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004550weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004551{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004552 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004553
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004554 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004555 if (ec->input_loop_source)
4556 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004557
Matt Roper361d2ad2011-08-29 13:52:23 -07004558 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004559 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004560 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004561
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004562 if (ec->renderer)
4563 ec->renderer->destroy(ec);
4564
Daniel Stone325fc2d2012-05-30 16:31:58 +01004565 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004566 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004567 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004568 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004569 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004570 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004571
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004572 weston_plane_release(&ec->primary_plane);
4573
Jonas Ådahlc97af922012-03-28 22:36:09 +02004574 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07004575}
4576
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004577WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004578weston_compositor_exit_with_code(struct weston_compositor *compositor,
4579 int exit_code)
4580{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004581 if (compositor->exit_code == EXIT_SUCCESS)
4582 compositor->exit_code = exit_code;
4583
Giulio Camuffo459137b2014-10-11 23:56:24 +03004584 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004585}
4586
4587WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004588weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4589 const struct weston_pointer_grab_interface *interface)
4590{
4591 struct weston_seat *seat;
4592
4593 ec->default_pointer_grab = interface;
4594 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004595 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4596
4597 if (pointer)
4598 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004599 }
4600}
4601
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004602WL_EXPORT int
4603weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4604 clockid_t clk_id)
4605{
4606 struct timespec ts;
4607
4608 if (clock_gettime(clk_id, &ts) < 0)
4609 return -1;
4610
4611 compositor->presentation_clock = clk_id;
4612
4613 return 0;
4614}
4615
4616/*
4617 * For choosing the software clock, when the display hardware or API
4618 * does not expose a compatible presentation timestamp.
4619 */
4620WL_EXPORT int
4621weston_compositor_set_presentation_clock_software(
4622 struct weston_compositor *compositor)
4623{
4624 /* In order of preference */
4625 static const clockid_t clocks[] = {
4626 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4627 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4628 CLOCK_MONOTONIC, /* no jumps, may crawl */
4629 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4630 CLOCK_REALTIME /* may jump and crawl */
4631 };
4632 unsigned i;
4633
4634 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4635 if (weston_compositor_set_presentation_clock(compositor,
4636 clocks[i]) == 0)
4637 return 0;
4638
4639 weston_log("Error: no suitable presentation clock available.\n");
4640
4641 return -1;
4642}
4643
Pekka Paalanen662f3842015-03-18 12:17:26 +02004644/** Read the current time from the Presentation clock
4645 *
4646 * \param compositor
4647 * \param ts[out] The current time.
4648 *
4649 * \note Reading the current time in user space is always imprecise to some
4650 * degree.
4651 *
4652 * This function is never meant to fail. If reading the clock does fail,
4653 * an error message is logged and a zero time is returned. Callers are not
4654 * supposed to detect or react to failures.
4655 */
4656WL_EXPORT void
4657weston_compositor_read_presentation_clock(
4658 const struct weston_compositor *compositor,
4659 struct timespec *ts)
4660{
4661 static bool warned;
4662 int ret;
4663
4664 ret = clock_gettime(compositor->presentation_clock, ts);
4665 if (ret < 0) {
4666 ts->tv_sec = 0;
4667 ts->tv_nsec = 0;
4668
4669 if (!warned)
4670 weston_log("Error: failure to read "
4671 "the presentation clock %#x: '%m' (%d)\n",
4672 compositor->presentation_clock, errno);
4673 warned = true;
4674 }
4675}
4676
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004677WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004678weston_version(int *major, int *minor, int *micro)
4679{
4680 *major = WESTON_VERSION_MAJOR;
4681 *minor = WESTON_VERSION_MINOR;
4682 *micro = WESTON_VERSION_MICRO;
4683}
4684
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004685WL_EXPORT void *
4686weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004687{
Derek Foreman3f86e502015-06-08 11:46:54 -05004688 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004689 char path[PATH_MAX];
4690 void *module, *init;
4691
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004692 if (name == NULL)
4693 return NULL;
4694
Derek Foreman3f86e502015-06-08 11:46:54 -05004695 if (name[0] != '/') {
4696 if (builddir)
4697 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4698 else
4699 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
4700 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004701 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004702 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004703
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004704 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4705 if (module) {
4706 weston_log("Module '%s' already loaded\n", path);
4707 dlclose(module);
4708 return NULL;
4709 }
4710
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004711 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004712 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004713 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004714 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004715 return NULL;
4716 }
4717
4718 init = dlsym(module, entrypoint);
4719 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004720 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004721 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004722 return NULL;
4723 }
4724
4725 return init;
4726}
4727
Giulio Camuffo459137b2014-10-11 23:56:24 +03004728
4729/** Destroys the compositor.
4730 *
4731 * This function cleans up the compositor state and destroys it.
4732 *
4733 * \param compositor The compositor to be destroyed.
4734 */
4735WL_EXPORT void
4736weston_compositor_destroy(struct weston_compositor *compositor)
4737{
4738 /* prevent further rendering while shutting down */
4739 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4740
4741 wl_signal_emit(&compositor->destroy_signal, compositor);
4742
4743 weston_compositor_xkb_destroy(compositor);
4744
4745 compositor->backend->destroy(compositor);
4746 free(compositor);
4747}
4748
4749/** Instruct the compositor to exit.
4750 *
4751 * This functions does not directly destroy the compositor object, it merely
4752 * command it to start the tear down process. It is not guaranteed that the
4753 * tear down will happen immediately.
4754 *
4755 * \param compositor The compositor to tear down.
4756 */
4757WL_EXPORT void
4758weston_compositor_exit(struct weston_compositor *compositor)
4759{
4760 compositor->exit(compositor);
4761}
4762
4763/** Return the user data stored in the compositor.
4764 *
4765 * This function returns the user data pointer set with user_data parameter
4766 * to the \ref weston_compositor_create function.
4767 */
4768WL_EXPORT void *
4769weston_compositor_get_user_data(struct weston_compositor *compositor)
4770{
4771 return compositor->user_data;
4772}