blob: 150d1fb1e841fa25513298559d3dd975cef5d9a1 [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"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040061#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050062#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050063
Pekka Paalanen0513a952014-05-21 16:17:27 +030064#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
65
66#define NSEC_PER_SEC 1000000000
67
68static void
69timespec_sub(struct timespec *r,
70 const struct timespec *a, const struct timespec *b)
71{
72 r->tv_sec = a->tv_sec - b->tv_sec;
73 r->tv_nsec = a->tv_nsec - b->tv_nsec;
74 if (r->tv_nsec < 0) {
75 r->tv_sec--;
76 r->tv_nsec += NSEC_PER_SEC;
77 }
78}
79
80static int64_t
81timespec_to_nsec(const struct timespec *a)
82{
83 return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec;
84}
85
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020086static void
Alexander Larsson0b135062013-05-28 16:23:36 +020087weston_output_transform_scale_init(struct weston_output *output,
88 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020089
Rob Bradford27b17932013-06-26 18:08:46 +010090static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050091weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010092
Derek Foreman6ae7bc92014-11-04 10:47:33 -060093static void weston_mode_switch_finish(struct weston_output *output,
94 int mode_changed,
95 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080096{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020097 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020098 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020099 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -0600100 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +0200101
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200102 pixman_region32_init(&old_output_region);
103 pixman_region32_copy(&old_output_region, &output->region);
104
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200105 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200106 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200107
108 pixman_region32_init(&output->previous_damage);
109 pixman_region32_init_rect(&output->region, output->x, output->y,
110 output->width, output->height);
111
112 weston_output_update_matrix(output);
113
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200114 /* If a pointer falls outside the outputs new geometry, move it to its
115 * lower-right corner */
116 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400117 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200118 int32_t x, y;
119
120 if (!pointer)
121 continue;
122
123 x = wl_fixed_to_int(pointer->x);
124 y = wl_fixed_to_int(pointer->y);
125
126 if (!pixman_region32_contains_point(&old_output_region,
127 x, y, NULL) ||
128 pixman_region32_contains_point(&output->region,
129 x, y, NULL))
130 continue;
131
132 if (x >= output->x + output->width)
133 x = output->x + output->width - 1;
134 if (y >= output->y + output->height)
135 y = output->y + output->height - 1;
136
137 pointer->x = wl_fixed_from_int(x);
138 pointer->y = wl_fixed_from_int(y);
139 }
140
141 pixman_region32_fini(&old_output_region);
142
Derek Foremandd4cd332014-11-10 10:29:59 -0600143 if (!mode_changed && !scale_changed)
144 return;
145
Hardening57388e42013-09-18 23:56:36 +0200146 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600147 wl_resource_for_each(resource, &output->resource_list) {
148 if (mode_changed) {
149 wl_output_send_mode(resource,
150 output->current_mode->flags,
151 output->current_mode->width,
152 output->current_mode->height,
153 output->current_mode->refresh);
154 }
Hardening57388e42013-09-18 23:56:36 +0200155
Derek Foreman41bdc272014-11-05 13:26:57 -0600156 version = wl_resource_get_version(resource);
157 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600158 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200159
Derek Foreman41bdc272014-11-05 13:26:57 -0600160 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
161 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600162 }
163}
164
165WL_EXPORT int
166weston_output_mode_set_native(struct weston_output *output,
167 struct weston_mode *mode,
168 int32_t scale)
169{
170 int ret;
171 int mode_changed = 0, scale_changed = 0;
172
173 if (!output->switch_mode)
174 return -1;
175
176 if (!output->original_mode) {
177 mode_changed = 1;
178 ret = output->switch_mode(output, mode);
179 if (ret < 0)
180 return ret;
181 if (output->current_scale != scale) {
182 scale_changed = 1;
183 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200184 }
185 }
186
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600187 output->native_mode = mode;
188 output->native_scale = scale;
189
190 weston_mode_switch_finish(output, mode_changed, scale_changed);
191
192 return 0;
193}
194
195WL_EXPORT int
196weston_output_mode_switch_to_native(struct weston_output *output)
197{
198 int ret;
199 int mode_changed = 0, scale_changed = 0;
200
201 if (!output->switch_mode)
202 return -1;
203
204 if (!output->original_mode) {
205 weston_log("already in the native mode\n");
206 return -1;
207 }
208 /* the non fullscreen clients haven't seen a mode set since we
209 * switched into a temporary, so we need to notify them if the
210 * mode at that time is different from the native mode now.
211 */
212 mode_changed = (output->original_mode != output->native_mode);
213 scale_changed = (output->original_scale != output->native_scale);
214
215 ret = output->switch_mode(output, output->native_mode);
216 if (ret < 0)
217 return ret;
218
219 output->current_scale = output->native_scale;
220
221 output->original_mode = NULL;
222 output->original_scale = 0;
223
224 weston_mode_switch_finish(output, mode_changed, scale_changed);
225
226 return 0;
227}
228
229WL_EXPORT int
230weston_output_mode_switch_to_temporary(struct weston_output *output,
231 struct weston_mode *mode,
232 int32_t scale)
233{
234 int ret;
235
236 if (!output->switch_mode)
237 return -1;
238
239 /* original_mode is the last mode non full screen clients have seen,
240 * so we shouldn't change it if we already have one set.
241 */
242 if (!output->original_mode) {
243 output->original_mode = output->native_mode;
244 output->original_scale = output->native_scale;
245 }
246 ret = output->switch_mode(output, mode);
247 if (ret < 0)
248 return ret;
249
250 output->current_scale = scale;
251
252 weston_mode_switch_finish(output, 0, 0);
253
254 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800255}
256
Benjamin Franzke06286262011-05-06 19:12:33 +0200257static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200258child_client_exec(int sockfd, const char *path)
259{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500260 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200261 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200262 sigset_t allsigs;
263
264 /* do not give our signal mask to the new process */
265 sigfillset(&allsigs);
266 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200267
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100268 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
269 if (seteuid(getuid()) == -1) {
270 weston_log("compositor: failed seteuid\n");
271 return;
272 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500273
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500274 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
275 * non-CLOEXEC fd to pass through exec. */
276 clientfd = dup(sockfd);
277 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200278 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500279 return;
280 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200281
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500282 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200283 setenv("WAYLAND_SOCKET", s, 1);
284
285 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200286 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200287 path);
288}
289
290WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500291weston_client_launch(struct weston_compositor *compositor,
292 struct weston_process *proc,
293 const char *path,
294 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200295{
296 int sv[2];
297 pid_t pid;
298 struct wl_client *client;
299
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300300 weston_log("launching '%s'\n", path);
301
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300302 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200303 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200304 "socketpair failed while launching '%s': %m\n",
305 path);
306 return NULL;
307 }
308
309 pid = fork();
310 if (pid == -1) {
311 close(sv[0]);
312 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200313 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200314 "fork failed while launching '%s': %m\n", path);
315 return NULL;
316 }
317
318 if (pid == 0) {
319 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700320 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200321 }
322
323 close(sv[1]);
324
325 client = wl_client_create(compositor->wl_display, sv[0]);
326 if (!client) {
327 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200328 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200329 "wl_client_create failed while launching '%s'.\n",
330 path);
331 return NULL;
332 }
333
334 proc->pid = pid;
335 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500336 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200337
338 return client;
339}
340
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300341struct process_info {
342 struct weston_process proc;
343 char *path;
344};
345
346static void
347process_handle_sigchld(struct weston_process *process, int status)
348{
349 struct process_info *pinfo =
350 container_of(process, struct process_info, proc);
351
352 /*
353 * There are no guarantees whether this runs before or after
354 * the wl_client destructor.
355 */
356
357 if (WIFEXITED(status)) {
358 weston_log("%s exited with status %d\n", pinfo->path,
359 WEXITSTATUS(status));
360 } else if (WIFSIGNALED(status)) {
361 weston_log("%s died on signal %d\n", pinfo->path,
362 WTERMSIG(status));
363 } else {
364 weston_log("%s disappeared\n", pinfo->path);
365 }
366
367 free(pinfo->path);
368 free(pinfo);
369}
370
371WL_EXPORT struct wl_client *
372weston_client_start(struct weston_compositor *compositor, const char *path)
373{
374 struct process_info *pinfo;
375 struct wl_client *client;
376
377 pinfo = zalloc(sizeof *pinfo);
378 if (!pinfo)
379 return NULL;
380
381 pinfo->path = strdup(path);
382 if (!pinfo->path)
383 goto out_free;
384
385 client = weston_client_launch(compositor, &pinfo->proc, path,
386 process_handle_sigchld);
387 if (!client)
388 goto out_str;
389
390 return client;
391
392out_str:
393 free(pinfo->path);
394
395out_free:
396 free(pinfo);
397
398 return NULL;
399}
400
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200401static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300402region_init_infinite(pixman_region32_t *region)
403{
404 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
405 UINT32_MAX, UINT32_MAX);
406}
407
Pekka Paalanene67858b2013-04-25 13:57:42 +0300408static struct weston_subsurface *
409weston_surface_to_subsurface(struct weston_surface *surface);
410
Jason Ekstranda7af7042013-10-12 22:38:11 -0500411WL_EXPORT struct weston_view *
412weston_view_create(struct weston_surface *surface)
413{
414 struct weston_view *view;
415
Bryce Harringtonde16d892014-11-20 22:21:57 -0800416 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500417 if (view == NULL)
418 return NULL;
419
420 view->surface = surface;
421
Jason Ekstranda7af7042013-10-12 22:38:11 -0500422 /* Assign to surface */
423 wl_list_insert(&surface->views, &view->surface_link);
424
425 wl_signal_init(&view->destroy_signal);
426 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300427 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500428
Jason Ekstranda7af7042013-10-12 22:38:11 -0500429 pixman_region32_init(&view->clip);
430
431 view->alpha = 1.0;
432 pixman_region32_init(&view->transform.opaque);
433
434 wl_list_init(&view->geometry.transformation_list);
435 wl_list_insert(&view->geometry.transformation_list,
436 &view->transform.position.link);
437 weston_matrix_init(&view->transform.position.matrix);
438 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200439 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500440 pixman_region32_init(&view->transform.boundingbox);
441 view->transform.dirty = 1;
442
Jason Ekstranda7af7042013-10-12 22:38:11 -0500443 return view;
444}
445
Jason Ekstrand108865d2014-06-26 10:04:49 -0700446struct weston_frame_callback {
447 struct wl_resource *resource;
448 struct wl_list link;
449};
450
Pekka Paalanen133e4392014-09-23 22:08:46 -0400451struct weston_presentation_feedback {
452 struct wl_resource *resource;
453
454 /* XXX: could use just wl_resource_get_link() instead */
455 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200456
457 /* The per-surface feedback flags */
458 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400459};
460
461static void
462weston_presentation_feedback_discard(
463 struct weston_presentation_feedback *feedback)
464{
465 presentation_feedback_send_discarded(feedback->resource);
466 wl_resource_destroy(feedback->resource);
467}
468
469static void
470weston_presentation_feedback_discard_list(struct wl_list *list)
471{
472 struct weston_presentation_feedback *feedback, *tmp;
473
474 wl_list_for_each_safe(feedback, tmp, list, link)
475 weston_presentation_feedback_discard(feedback);
476}
477
478static void
479weston_presentation_feedback_present(
480 struct weston_presentation_feedback *feedback,
481 struct weston_output *output,
482 uint32_t refresh_nsec,
483 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200484 uint64_t seq,
485 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400486{
487 struct wl_client *client = wl_resource_get_client(feedback->resource);
488 struct wl_resource *o;
489 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400490
491 wl_resource_for_each(o, &output->resource_list) {
492 if (wl_resource_get_client(o) != client)
493 continue;
494
495 presentation_feedback_send_sync_output(feedback->resource, o);
496 }
497
498 secs = ts->tv_sec;
499 presentation_feedback_send_presented(feedback->resource,
500 secs >> 32, secs & 0xffffffff,
501 ts->tv_nsec,
502 refresh_nsec,
503 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200504 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400505 wl_resource_destroy(feedback->resource);
506}
507
508static void
509weston_presentation_feedback_present_list(struct wl_list *list,
510 struct weston_output *output,
511 uint32_t refresh_nsec,
512 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200513 uint64_t seq,
514 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400515{
516 struct weston_presentation_feedback *feedback, *tmp;
517
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200518 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
519 wl_list_empty(list));
520
Pekka Paalanen133e4392014-09-23 22:08:46 -0400521 wl_list_for_each_safe(feedback, tmp, list, link)
522 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200523 refresh_nsec, ts, seq,
524 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400525}
526
Jason Ekstrand7b982072014-05-20 14:33:03 -0500527static void
528surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
529{
530 struct weston_surface_state *state =
531 container_of(listener, struct weston_surface_state,
532 buffer_destroy_listener);
533
534 state->buffer = NULL;
535}
536
537static void
538weston_surface_state_init(struct weston_surface_state *state)
539{
540 state->newly_attached = 0;
541 state->buffer = NULL;
542 state->buffer_destroy_listener.notify =
543 surface_state_handle_buffer_destroy;
544 state->sx = 0;
545 state->sy = 0;
546
547 pixman_region32_init(&state->damage);
548 pixman_region32_init(&state->opaque);
549 region_init_infinite(&state->input);
550
551 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400552 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500553
554 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
555 state->buffer_viewport.buffer.scale = 1;
556 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
557 state->buffer_viewport.surface.width = -1;
558 state->buffer_viewport.changed = 0;
559}
560
561static void
562weston_surface_state_fini(struct weston_surface_state *state)
563{
564 struct weston_frame_callback *cb, *next;
565
566 wl_list_for_each_safe(cb, next,
567 &state->frame_callback_list, link)
568 wl_resource_destroy(cb->resource);
569
Pekka Paalanen133e4392014-09-23 22:08:46 -0400570 weston_presentation_feedback_discard_list(&state->feedback_list);
571
Jason Ekstrand7b982072014-05-20 14:33:03 -0500572 pixman_region32_fini(&state->input);
573 pixman_region32_fini(&state->opaque);
574 pixman_region32_fini(&state->damage);
575
576 if (state->buffer)
577 wl_list_remove(&state->buffer_destroy_listener.link);
578 state->buffer = NULL;
579}
580
581static void
582weston_surface_state_set_buffer(struct weston_surface_state *state,
583 struct weston_buffer *buffer)
584{
585 if (state->buffer == buffer)
586 return;
587
588 if (state->buffer)
589 wl_list_remove(&state->buffer_destroy_listener.link);
590 state->buffer = buffer;
591 if (state->buffer)
592 wl_signal_add(&state->buffer->destroy_signal,
593 &state->buffer_destroy_listener);
594}
595
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500596WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500597weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500598{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500599 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400600
Bryce Harringtonde16d892014-11-20 22:21:57 -0800601 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400602 if (surface == NULL)
603 return NULL;
604
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500605 wl_signal_init(&surface->destroy_signal);
606
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500607 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200608 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400609
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200610 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
611 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200612 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
613 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500614
615 weston_surface_state_init(&surface->pending);
616
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400617 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500618 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300619 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400620
Jason Ekstranda7af7042013-10-12 22:38:11 -0500621 wl_list_init(&surface->views);
622
623 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400624 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500625
Pekka Paalanene67858b2013-04-25 13:57:42 +0300626 wl_list_init(&surface->subsurface_list);
627 wl_list_init(&surface->subsurface_list_pending);
628
Jason Ekstrand1e059042014-10-16 10:55:19 -0500629 weston_matrix_init(&surface->buffer_to_surface_matrix);
630 weston_matrix_init(&surface->surface_to_buffer_matrix);
631
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400632 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500633}
634
Alex Wu8811bf92012-02-28 18:07:54 +0800635WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500636weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200637 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500638{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100639 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500640}
641
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400642WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500643weston_view_to_global_float(struct weston_view *view,
644 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200645{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500646 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200647 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
648
Jason Ekstranda7af7042013-10-12 22:38:11 -0500649 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200650
651 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200652 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700653 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200654 v.f[3]);
655 *x = 0;
656 *y = 0;
657 return;
658 }
659
660 *x = v.f[0] / v.f[3];
661 *y = v.f[1] / v.f[3];
662 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500663 *x = sx + view->geometry.x;
664 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200665 }
666}
667
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500668WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200669weston_transformed_coord(int width, int height,
670 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200671 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200672 float sx, float sy, float *bx, float *by)
673{
674 switch (transform) {
675 case WL_OUTPUT_TRANSFORM_NORMAL:
676 default:
677 *bx = sx;
678 *by = sy;
679 break;
680 case WL_OUTPUT_TRANSFORM_FLIPPED:
681 *bx = width - sx;
682 *by = sy;
683 break;
684 case WL_OUTPUT_TRANSFORM_90:
685 *bx = height - sy;
686 *by = sx;
687 break;
688 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
689 *bx = height - sy;
690 *by = width - sx;
691 break;
692 case WL_OUTPUT_TRANSFORM_180:
693 *bx = width - sx;
694 *by = height - sy;
695 break;
696 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
697 *bx = sx;
698 *by = height - sy;
699 break;
700 case WL_OUTPUT_TRANSFORM_270:
701 *bx = sy;
702 *by = width - sx;
703 break;
704 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
705 *bx = sy;
706 *by = sx;
707 break;
708 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200709
710 *bx *= scale;
711 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200712}
713
714WL_EXPORT pixman_box32_t
715weston_transformed_rect(int width, int height,
716 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200717 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200718 pixman_box32_t rect)
719{
720 float x1, x2, y1, y2;
721
722 pixman_box32_t ret;
723
Alexander Larsson4ea95522013-05-22 14:41:37 +0200724 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200725 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200726 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200727 rect.x2, rect.y2, &x2, &y2);
728
729 if (x1 <= x2) {
730 ret.x1 = x1;
731 ret.x2 = x2;
732 } else {
733 ret.x1 = x2;
734 ret.x2 = x1;
735 }
736
737 if (y1 <= y2) {
738 ret.y1 = y1;
739 ret.y2 = y2;
740 } else {
741 ret.y1 = y2;
742 ret.y2 = y1;
743 }
744
745 return ret;
746}
747
748WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500749weston_transformed_region(int width, int height,
750 enum wl_output_transform transform,
751 int32_t scale,
752 pixman_region32_t *src, pixman_region32_t *dest)
753{
754 pixman_box32_t *src_rects, *dest_rects;
755 int nrects, i;
756
757 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
758 if (src != dest)
759 pixman_region32_copy(dest, src);
760 return;
761 }
762
763 src_rects = pixman_region32_rectangles(src, &nrects);
764 dest_rects = malloc(nrects * sizeof(*dest_rects));
765 if (!dest_rects)
766 return;
767
768 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
769 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
770 } else {
771 for (i = 0; i < nrects; i++) {
772 switch (transform) {
773 default:
774 case WL_OUTPUT_TRANSFORM_NORMAL:
775 dest_rects[i].x1 = src_rects[i].x1;
776 dest_rects[i].y1 = src_rects[i].y1;
777 dest_rects[i].x2 = src_rects[i].x2;
778 dest_rects[i].y2 = src_rects[i].y2;
779 break;
780 case WL_OUTPUT_TRANSFORM_90:
781 dest_rects[i].x1 = height - src_rects[i].y2;
782 dest_rects[i].y1 = src_rects[i].x1;
783 dest_rects[i].x2 = height - src_rects[i].y1;
784 dest_rects[i].y2 = src_rects[i].x2;
785 break;
786 case WL_OUTPUT_TRANSFORM_180:
787 dest_rects[i].x1 = width - src_rects[i].x2;
788 dest_rects[i].y1 = height - src_rects[i].y2;
789 dest_rects[i].x2 = width - src_rects[i].x1;
790 dest_rects[i].y2 = height - src_rects[i].y1;
791 break;
792 case WL_OUTPUT_TRANSFORM_270:
793 dest_rects[i].x1 = src_rects[i].y1;
794 dest_rects[i].y1 = width - src_rects[i].x2;
795 dest_rects[i].x2 = src_rects[i].y2;
796 dest_rects[i].y2 = width - src_rects[i].x1;
797 break;
798 case WL_OUTPUT_TRANSFORM_FLIPPED:
799 dest_rects[i].x1 = width - src_rects[i].x2;
800 dest_rects[i].y1 = src_rects[i].y1;
801 dest_rects[i].x2 = width - src_rects[i].x1;
802 dest_rects[i].y2 = src_rects[i].y2;
803 break;
804 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
805 dest_rects[i].x1 = height - src_rects[i].y2;
806 dest_rects[i].y1 = width - src_rects[i].x2;
807 dest_rects[i].x2 = height - src_rects[i].y1;
808 dest_rects[i].y2 = width - src_rects[i].x1;
809 break;
810 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
811 dest_rects[i].x1 = src_rects[i].x1;
812 dest_rects[i].y1 = height - src_rects[i].y2;
813 dest_rects[i].x2 = src_rects[i].x2;
814 dest_rects[i].y2 = height - src_rects[i].y1;
815 break;
816 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
817 dest_rects[i].x1 = src_rects[i].y1;
818 dest_rects[i].y1 = src_rects[i].x1;
819 dest_rects[i].x2 = src_rects[i].y2;
820 dest_rects[i].y2 = src_rects[i].x2;
821 break;
822 }
823 }
824 }
825
826 if (scale != 1) {
827 for (i = 0; i < nrects; i++) {
828 dest_rects[i].x1 *= scale;
829 dest_rects[i].x2 *= scale;
830 dest_rects[i].y1 *= scale;
831 dest_rects[i].y2 *= scale;
832 }
833 }
834
835 pixman_region32_clear(dest);
836 pixman_region32_init_rects(dest, dest_rects, nrects);
837 free(dest_rects);
838}
839
Jonny Lamb74130762013-11-26 18:19:46 +0100840static void
841scaler_surface_to_buffer(struct weston_surface *surface,
842 float sx, float sy, float *bx, float *by)
843{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200844 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200845 double src_width, src_height;
846 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200847
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200848 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
849 if (vp->surface.width == -1) {
850 *bx = sx;
851 *by = sy;
852 return;
853 }
Jonny Lamb74130762013-11-26 18:19:46 +0100854
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200855 src_x = 0.0;
856 src_y = 0.0;
857 src_width = surface->width_from_buffer;
858 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100859 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200860 src_x = wl_fixed_to_double(vp->buffer.src_x);
861 src_y = wl_fixed_to_double(vp->buffer.src_y);
862 src_width = wl_fixed_to_double(vp->buffer.src_width);
863 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100864 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200865
866 *bx = sx * src_width / surface->width + src_x;
867 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100868}
869
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500870WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200871weston_surface_to_buffer_float(struct weston_surface *surface,
872 float sx, float sy, float *bx, float *by)
873{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200874 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
875
Jonny Lamb74130762013-11-26 18:19:46 +0100876 /* first transform coordinates if the scaler is set */
877 scaler_surface_to_buffer(surface, sx, sy, bx, by);
878
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500879 weston_transformed_coord(surface->width_from_buffer,
880 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200881 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100882 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200883}
884
Alexander Larsson4ea95522013-05-22 14:41:37 +0200885WL_EXPORT void
886weston_surface_to_buffer(struct weston_surface *surface,
887 int sx, int sy, int *bx, int *by)
888{
889 float bxf, byf;
890
Jonny Lamb74130762013-11-26 18:19:46 +0100891 weston_surface_to_buffer_float(surface,
892 sx, sy, &bxf, &byf);
893
Alexander Larsson4ea95522013-05-22 14:41:37 +0200894 *bx = floorf(bxf);
895 *by = floorf(byf);
896}
897
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200898WL_EXPORT pixman_box32_t
899weston_surface_to_buffer_rect(struct weston_surface *surface,
900 pixman_box32_t rect)
901{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200902 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100903 float xf, yf;
904
905 /* first transform box coordinates if the scaler is set */
906 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
907 rect.x1 = floorf(xf);
908 rect.y1 = floorf(yf);
909
910 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
911 rect.x2 = floorf(xf);
912 rect.y2 = floorf(yf);
913
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500914 return weston_transformed_rect(surface->width_from_buffer,
915 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200916 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200917 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200918}
919
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200920/** Transform a region from surface coordinates to buffer coordinates
921 *
922 * \param surface The surface to fetch wl_viewport and buffer transformation
923 * from.
924 * \param surface_region[in] The region in surface coordinates.
925 * \param buffer_region[out] The region converted to buffer coordinates.
926 *
927 * Buffer_region must be init'd, but will be completely overwritten.
928 *
929 * Viewport and buffer transformations can only do translation, scaling,
930 * and rotations in 90-degree steps. Therefore the only loss in the
931 * conversion is coordinate flooring (rounding).
932 */
933WL_EXPORT void
934weston_surface_to_buffer_region(struct weston_surface *surface,
935 pixman_region32_t *surface_region,
936 pixman_region32_t *buffer_region)
937{
938 pixman_box32_t *src_rects, *dest_rects;
939 int nrects, i;
940
941 src_rects = pixman_region32_rectangles(surface_region, &nrects);
942 dest_rects = malloc(nrects * sizeof(*dest_rects));
943 if (!dest_rects)
944 return;
945
946 for (i = 0; i < nrects; i++) {
947 dest_rects[i] = weston_surface_to_buffer_rect(surface,
948 src_rects[i]);
949 }
950
951 pixman_region32_fini(buffer_region);
952 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
953 free(dest_rects);
954}
955
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200956WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500957weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400958 struct weston_plane *plane)
959{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500960 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400961 return;
962
Jason Ekstranda7af7042013-10-12 22:38:11 -0500963 weston_view_damage_below(view);
964 view->plane = plane;
965 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400966}
967
Pekka Paalanen51723d52015-02-17 13:10:01 +0200968/** Inflict damage on the plane where the view is visible.
969 *
970 * \param view The view that causes the damage.
971 *
972 * If the view is currently on a plane (including the primary plane),
973 * take the view's boundingbox, subtract all the opaque views that cover it,
974 * and add the remaining region as damage to the plane. This corresponds
975 * to the damage inflicted to the plane if this view disappeared.
976 *
977 * A repaint is scheduled for this view.
978 *
979 * The region of all opaque views covering this view is stored in
980 * weston_view::clip and updated by view_accumulate_damage() during
981 * weston_output_repaint(). Specifically, that region matches the
982 * scenegraph as it was last painted.
983 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400984WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500985weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200986{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500987 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200988
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500989 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200990 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800992 if (view->plane)
993 pixman_region32_union(&view->plane->damage,
994 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500995 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800996 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200997}
998
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400999static void
1000weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1001{
1002 uint32_t different = es->output_mask ^ mask;
1003 uint32_t entered = mask & different;
1004 uint32_t left = es->output_mask & different;
1005 struct weston_output *output;
1006 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001007 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001008
1009 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001010 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001011 return;
1012 if (different == 0)
1013 return;
1014
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001015 client = wl_resource_get_client(es->resource);
1016
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001017 wl_list_for_each(output, &es->compositor->output_list, link) {
1018 if (1 << output->id & different)
1019 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001020 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001021 client);
1022 if (resource == NULL)
1023 continue;
1024 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001025 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001026 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001027 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001028 }
1029}
1030
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001031
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001032static void
1033weston_surface_assign_output(struct weston_surface *es)
1034{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001035 struct weston_output *new_output;
1036 struct weston_view *view;
1037 pixman_region32_t region;
1038 uint32_t max, area, mask;
1039 pixman_box32_t *e;
1040
1041 new_output = NULL;
1042 max = 0;
1043 mask = 0;
1044 pixman_region32_init(&region);
1045 wl_list_for_each(view, &es->views, surface_link) {
1046 if (!view->output)
1047 continue;
1048
1049 pixman_region32_intersect(&region, &view->transform.boundingbox,
1050 &view->output->region);
1051
1052 e = pixman_region32_extents(&region);
1053 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1054
1055 mask |= view->output_mask;
1056
1057 if (area >= max) {
1058 new_output = view->output;
1059 max = area;
1060 }
1061 }
1062 pixman_region32_fini(&region);
1063
1064 es->output = new_output;
1065 weston_surface_update_output_mask(es, mask);
1066}
1067
1068static void
1069weston_view_assign_output(struct weston_view *ev)
1070{
1071 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001072 struct weston_output *output, *new_output;
1073 pixman_region32_t region;
1074 uint32_t max, area, mask;
1075 pixman_box32_t *e;
1076
1077 new_output = NULL;
1078 max = 0;
1079 mask = 0;
1080 pixman_region32_init(&region);
1081 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001083 &output->region);
1084
1085 e = pixman_region32_extents(&region);
1086 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1087
1088 if (area > 0)
1089 mask |= 1 << output->id;
1090
1091 if (area >= max) {
1092 new_output = output;
1093 max = area;
1094 }
1095 }
1096 pixman_region32_fini(&region);
1097
Jason Ekstranda7af7042013-10-12 22:38:11 -05001098 ev->output = new_output;
1099 ev->output_mask = mask;
1100
1101 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001102}
1103
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001104static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001105weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1106 int from_x, int from_y, int *to_x, int *to_y)
1107{
1108 float x, y;
1109
1110 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1111 weston_view_from_global_float(to, x, y, &x, &y);
1112
1113 *to_x = round(x);
1114 *to_y = round(y);
1115}
1116
1117static void
1118weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1119{
1120 pixman_box32_t *a;
1121 pixman_box32_t b;
1122
1123 a = pixman_region32_extents(&from->geometry.scissor);
1124
1125 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1126 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1127
1128 pixman_region32_fini(&to->geometry.scissor);
1129 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1130}
1131
1132static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001133view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001134 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001135{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001136 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1137 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001138 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001139 { inbox->x1, inbox->y1 },
1140 { inbox->x1, inbox->y2 },
1141 { inbox->x2, inbox->y1 },
1142 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001143 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001144 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001145 int i;
1146
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001147 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001148 /* avoid rounding empty bbox to 1x1 */
1149 pixman_region32_init(bbox);
1150 return;
1151 }
1152
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001153 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001154 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001156 if (x < min_x)
1157 min_x = x;
1158 if (x > max_x)
1159 max_x = x;
1160 if (y < min_y)
1161 min_y = y;
1162 if (y > max_y)
1163 max_y = y;
1164 }
1165
Pekka Paalanen219b9822012-02-08 15:38:37 +02001166 int_x = floorf(min_x);
1167 int_y = floorf(min_y);
1168 pixman_region32_init_rect(bbox, int_x, int_y,
1169 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001170}
1171
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001172static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001174{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001176
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001177 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 view->geometry.x = roundf(view->geometry.x);
1179 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001180
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001181 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1183 view->transform.position.matrix.d[12] = view->geometry.x;
1184 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001185
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001187
Jason Ekstranda7af7042013-10-12 22:38:11 -05001188 view->transform.inverse = view->transform.position.matrix;
1189 view->transform.inverse.d[12] = -view->geometry.x;
1190 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001191
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001193 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001194 view->surface->width,
1195 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001196 if (view->geometry.scissor_enabled)
1197 pixman_region32_intersect(&view->transform.boundingbox,
1198 &view->transform.boundingbox,
1199 &view->geometry.scissor);
1200
1201 pixman_region32_translate(&view->transform.boundingbox,
1202 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001203
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 if (view->alpha == 1.0) {
1205 pixman_region32_copy(&view->transform.opaque,
1206 &view->surface->opaque);
1207 pixman_region32_translate(&view->transform.opaque,
1208 view->geometry.x,
1209 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001210 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001211}
1212
1213static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001215{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 struct weston_view *parent = view->geometry.parent;
1217 struct weston_matrix *matrix = &view->transform.matrix;
1218 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001219 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001220 pixman_region32_t surfregion;
1221 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001222
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001224
1225 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1227 view->transform.position.matrix.d[12] = view->geometry.x;
1228 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001229
1230 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001232 weston_matrix_multiply(matrix, &tform->matrix);
1233
Pekka Paalanen483243f2013-03-08 14:56:50 +02001234 if (parent)
1235 weston_matrix_multiply(matrix, &parent->transform.matrix);
1236
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001237 if (weston_matrix_invert(inverse, matrix) < 0) {
1238 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 weston_log("error: weston_view %p"
1240 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001241 return -1;
1242 }
1243
Pekka Paalanen380adf52015-02-16 14:39:11 +02001244 pixman_region32_init_rect(&surfregion, 0, 0,
1245 view->surface->width, view->surface->height);
1246 if (view->geometry.scissor_enabled)
1247 pixman_region32_intersect(&surfregion, &surfregion,
1248 &view->geometry.scissor);
1249 surfbox = pixman_region32_extents(&surfregion);
1250
1251 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1252 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001253
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001254 return 0;
1255}
1256
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001257static struct weston_layer *
1258get_view_layer(struct weston_view *view)
1259{
1260 if (view->parent_view)
1261 return get_view_layer(view->parent_view);
1262 return view->layer_link.layer;
1263}
1264
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001265WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001267{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001269 struct weston_layer *layer;
1270 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001271
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001273 return;
1274
Pekka Paalanen483243f2013-03-08 14:56:50 +02001275 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001277
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001279
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001281
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 pixman_region32_fini(&view->transform.boundingbox);
1283 pixman_region32_fini(&view->transform.opaque);
1284 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001285
Pekka Paalanencd403622012-01-25 13:37:39 +02001286 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 if (view->geometry.transformation_list.next ==
1288 &view->transform.position.link &&
1289 view->geometry.transformation_list.prev ==
1290 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001291 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001293 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 if (weston_view_update_transform_enable(view) < 0)
1295 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001296 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001297
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001298 layer = get_view_layer(view);
1299 if (layer) {
1300 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001301 pixman_region32_intersect(&view->transform.boundingbox,
1302 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001303 pixman_region32_intersect(&view->transform.opaque,
1304 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001305 pixman_region32_fini(&mask);
1306 }
1307
Pekka Paalanen380adf52015-02-16 14:39:11 +02001308 if (parent) {
1309 if (parent->geometry.scissor_enabled) {
1310 view->geometry.scissor_enabled = true;
1311 weston_view_transfer_scissor(parent, view);
1312 } else {
1313 view->geometry.scissor_enabled = false;
1314 }
1315 }
1316
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001318
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001320
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 wl_signal_emit(&view->surface->compositor->transform_signal,
1322 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001323}
1324
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001325WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001326weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001327{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001329
1330 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001331 * The invariant: if view->geometry.dirty, then all views
1332 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001333 * Corollary: if not parent->geometry.dirty, then all ancestors
1334 * are not dirty.
1335 */
1336
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001338 return;
1339
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001341
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001343 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001345}
1346
1347WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348weston_view_to_global_fixed(struct weston_view *view,
1349 wl_fixed_t vx, wl_fixed_t vy,
1350 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001351{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001352 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001353
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354 weston_view_to_global_float(view,
1355 wl_fixed_to_double(vx),
1356 wl_fixed_to_double(vy),
1357 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001358 *x = wl_fixed_from_double(xf);
1359 *y = wl_fixed_from_double(yf);
1360}
1361
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001362WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363weston_view_from_global_float(struct weston_view *view,
1364 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001365{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001367 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1368
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001370
1371 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001372 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001374 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 *vx = 0;
1376 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001377 return;
1378 }
1379
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380 *vx = v.f[0] / v.f[3];
1381 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001382 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383 *vx = x - view->geometry.x;
1384 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001385 }
1386}
1387
1388WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389weston_view_from_global_fixed(struct weston_view *view,
1390 wl_fixed_t x, wl_fixed_t y,
1391 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001392{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001393 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001394
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395 weston_view_from_global_float(view,
1396 wl_fixed_to_double(x),
1397 wl_fixed_to_double(y),
1398 &vxf, &vyf);
1399 *vx = wl_fixed_from_double(vxf);
1400 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001401}
1402
1403WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404weston_view_from_global(struct weston_view *view,
1405 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001406{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001408
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1410 *vx = floorf(vxf);
1411 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001412}
1413
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001414WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001415weston_surface_schedule_repaint(struct weston_surface *surface)
1416{
1417 struct weston_output *output;
1418
1419 wl_list_for_each(output, &surface->compositor->output_list, link)
1420 if (surface->output_mask & (1 << output->id))
1421 weston_output_schedule_repaint(output);
1422}
1423
1424WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001425weston_view_schedule_repaint(struct weston_view *view)
1426{
1427 struct weston_output *output;
1428
1429 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1430 if (view->output_mask & (1 << output->id))
1431 weston_output_schedule_repaint(output);
1432}
1433
Pekka Paalanene508ce62015-02-19 13:59:55 +02001434/**
1435 * XXX: This function does it the wrong way.
1436 * surface->damage is the damage from the client, and causes
1437 * surface_flush_damage() to copy pixels. No window management action can
1438 * cause damage to the client-provided content, warranting re-upload!
1439 *
1440 * Instead of surface->damage, this function should record the damage
1441 * with all the views for this surface to avoid extraneous texture
1442 * uploads.
1443 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001444WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001445weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001446{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001447 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 0, 0, surface->width,
1449 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001450
Kristian Høgsberg98238702012-08-03 16:29:12 -04001451 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001452}
1453
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001454WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001456{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001457 if (view->geometry.x == x && view->geometry.y == y)
1458 return;
1459
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460 view->geometry.x = x;
1461 view->geometry.y = y;
1462 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001463}
1464
Pekka Paalanen483243f2013-03-08 14:56:50 +02001465static void
1466transform_parent_handle_parent_destroy(struct wl_listener *listener,
1467 void *data)
1468{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469 struct weston_view *view =
1470 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001471 geometry.parent_destroy_listener);
1472
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001474}
1475
1476WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001478 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001479{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480 if (view->geometry.parent) {
1481 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1482 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001483
1484 if (!parent)
1485 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001486 }
1487
Jason Ekstranda7af7042013-10-12 22:38:11 -05001488 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001489
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001491 transform_parent_handle_parent_destroy;
1492 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001493 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001494 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001495 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001497 }
1498
Jason Ekstranda7af7042013-10-12 22:38:11 -05001499 weston_view_geometry_dirty(view);
1500}
1501
Pekka Paalanen380adf52015-02-16 14:39:11 +02001502/** Set a clip mask rectangle on a view
1503 *
1504 * \param view The view to set the clip mask on.
1505 * \param x Top-left corner X coordinate of the clip rectangle.
1506 * \param y Top-left corner Y coordinate of the clip rectangle.
1507 * \param width Width of the clip rectangle, non-negative.
1508 * \param height Height of the clip rectangle, non-negative.
1509 *
1510 * A shell may set a clip mask rectangle on a view. Everything outside
1511 * the rectangle is cut away for input and output purposes: it is
1512 * not drawn and cannot be hit by hit-test based input like pointer
1513 * motion or touch-downs. Everything inside the rectangle will behave
1514 * normally. Clients are unaware of clipping.
1515 *
1516 * The rectangle is set in the surface local coordinates. Setting a clip
1517 * mask rectangle does not affect the view position, the view is positioned
1518 * as it would be without a clip. The clip also does not change
1519 * weston_surface::width,height.
1520 *
1521 * The clip mask rectangle is part of transformation inheritance
1522 * (weston_view_set_transform_parent()). A clip set in the root of the
1523 * transformation inheritance tree will affect all views in the tree.
1524 * A clip can be set only on the root view. Attempting to set a clip
1525 * on view that has a transformation parent will fail. Assigning a parent
1526 * to a view that has a clip set will cause the clip to be forgotten.
1527 *
1528 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1529 * on the additional transformations in the child views. These transformations
1530 * may not rotate the coordinate axes, i.e., only translation and scaling
1531 * are allowed. Violating this restriction causes the clipping to malfunction.
1532 * Furthermore, using scaling may cause rounding errors in child clipping.
1533 *
1534 * The clip mask rectangle is not automatically adjusted based on
1535 * wl_surface.attach dx and dy arguments.
1536 *
1537 * A clip mask rectangle can be set only if the compositor capability
1538 * WESTON_CAP_VIEW_CLIP_MASK is present.
1539 *
1540 * This function sets the clip mask rectangle and schedules a repaint for
1541 * the view.
1542 */
1543WL_EXPORT void
1544weston_view_set_mask(struct weston_view *view,
1545 int x, int y, int width, int height)
1546{
1547 struct weston_compositor *compositor = view->surface->compositor;
1548
1549 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1550 weston_log("%s not allowed without capability!\n", __func__);
1551 return;
1552 }
1553
1554 if (view->geometry.parent) {
1555 weston_log("view %p has a parent, clip forbidden!\n", view);
1556 return;
1557 }
1558
1559 if (width < 0 || height < 0) {
1560 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1561 x, y, width, height);
1562 return;
1563 }
1564
1565 pixman_region32_fini(&view->geometry.scissor);
1566 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1567 view->geometry.scissor_enabled = true;
1568 weston_view_geometry_dirty(view);
1569 weston_view_schedule_repaint(view);
1570}
1571
1572/** Remove the clip mask from a view
1573 *
1574 * \param view The view to remove the clip mask from.
1575 *
1576 * Removed the clip mask rectangle and schedules a repaint.
1577 *
1578 * \sa weston_view_set_mask
1579 */
1580WL_EXPORT void
1581weston_view_set_mask_infinite(struct weston_view *view)
1582{
1583 view->geometry.scissor_enabled = false;
1584 weston_view_geometry_dirty(view);
1585 weston_view_schedule_repaint(view);
1586}
1587
Derek Foreman280e7dd2014-10-03 13:13:42 -05001588WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589weston_view_is_mapped(struct weston_view *view)
1590{
1591 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001592 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001593 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001594 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001595}
1596
Derek Foreman280e7dd2014-10-03 13:13:42 -05001597WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001598weston_surface_is_mapped(struct weston_surface *surface)
1599{
1600 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001601 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001602 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001603 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001604}
1605
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001606static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001607surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001608{
1609 struct weston_view *view;
1610
1611 if (surface->width == width && surface->height == height)
1612 return;
1613
1614 surface->width = width;
1615 surface->height = height;
1616
1617 wl_list_for_each(view, &surface->views, surface_link)
1618 weston_view_geometry_dirty(view);
1619}
1620
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001621WL_EXPORT void
1622weston_surface_set_size(struct weston_surface *surface,
1623 int32_t width, int32_t height)
1624{
1625 assert(!surface->resource);
1626 surface_set_size(surface, width, height);
1627}
1628
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001629static int
1630fixed_round_up_to_int(wl_fixed_t f)
1631{
1632 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1633}
1634
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001635static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001636weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001637{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001638 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001639 int32_t width, height;
1640
1641 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001642 surface->width_from_buffer = 0;
1643 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001644 return;
1645 }
1646
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001647 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001648 case WL_OUTPUT_TRANSFORM_90:
1649 case WL_OUTPUT_TRANSFORM_270:
1650 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1651 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001652 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1653 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001654 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001655 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001656 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1657 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001658 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001659 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001660
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001661 surface->width_from_buffer = width;
1662 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001663}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001664
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001665static void
1666weston_surface_update_size(struct weston_surface *surface)
1667{
1668 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1669 int32_t width, height;
1670
1671 width = surface->width_from_buffer;
1672 height = surface->height_from_buffer;
1673
1674 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001675 surface_set_size(surface,
1676 vp->surface.width, vp->surface.height);
1677 return;
1678 }
1679
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001680 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001681 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1682 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1683
1684 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001685 return;
1686 }
1687
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001688 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001689}
1690
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001691WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001692weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001693{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001694 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001695
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001696 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001697
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001698 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001699}
1700
Jason Ekstranda7af7042013-10-12 22:38:11 -05001701WL_EXPORT struct weston_view *
1702weston_compositor_pick_view(struct weston_compositor *compositor,
1703 wl_fixed_t x, wl_fixed_t y,
1704 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001705{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001707 wl_fixed_t view_x, view_y;
1708 int view_ix, view_iy;
1709 int ix = wl_fixed_to_int(x);
1710 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001711
Jason Ekstranda7af7042013-10-12 22:38:11 -05001712 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001713 if (!pixman_region32_contains_point(
1714 &view->transform.boundingbox, ix, iy, NULL))
1715 continue;
1716
1717 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1718 view_ix = wl_fixed_to_int(view_x);
1719 view_iy = wl_fixed_to_int(view_y);
1720
1721 if (!pixman_region32_contains_point(&view->surface->input,
1722 view_ix, view_iy, NULL))
1723 continue;
1724
Pekka Paalanen380adf52015-02-16 14:39:11 +02001725 if (view->geometry.scissor_enabled &&
1726 !pixman_region32_contains_point(&view->geometry.scissor,
1727 view_ix, view_iy, NULL))
1728 continue;
1729
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001730 *vx = view_x;
1731 *vy = view_y;
1732 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001733 }
1734
1735 return NULL;
1736}
1737
1738static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001739weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001740{
Daniel Stone37816df2012-05-16 18:45:18 +01001741 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001742
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001743 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001744 return;
1745
Daniel Stone37816df2012-05-16 18:45:18 +01001746 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001747 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001748}
1749
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001750WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001751weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001752{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001753 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001754
Jason Ekstranda7af7042013-10-12 22:38:11 -05001755 if (!weston_view_is_mapped(view))
1756 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001757
Jason Ekstranda7af7042013-10-12 22:38:11 -05001758 weston_view_damage_below(view);
1759 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001760 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001761 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762 wl_list_remove(&view->link);
1763 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001764 view->output_mask = 0;
1765 weston_surface_assign_output(view->surface);
1766
1767 if (weston_surface_is_mapped(view->surface))
1768 return;
1769
1770 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1771 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001772 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001773 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001774 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001775 NULL,
1776 wl_fixed_from_int(0),
1777 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001778 if (seat->touch && seat->touch->focus == view)
Derek Foreman4c93c082015-04-30 16:45:41 -05001779 weston_touch_set_focus(seat->touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001780 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001781}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001782
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783WL_EXPORT void
1784weston_surface_unmap(struct weston_surface *surface)
1785{
1786 struct weston_view *view;
1787
1788 wl_list_for_each(view, &surface->views, surface_link)
1789 weston_view_unmap(view);
1790 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001791}
1792
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001793static void
1794weston_surface_reset_pending_buffer(struct weston_surface *surface)
1795{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001796 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001797 surface->pending.sx = 0;
1798 surface->pending.sy = 0;
1799 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001800 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001801}
1802
Jason Ekstranda7af7042013-10-12 22:38:11 -05001803WL_EXPORT void
1804weston_view_destroy(struct weston_view *view)
1805{
1806 wl_signal_emit(&view->destroy_signal, view);
1807
1808 assert(wl_list_empty(&view->geometry.child_list));
1809
1810 if (weston_view_is_mapped(view)) {
1811 weston_view_unmap(view);
1812 weston_compositor_build_view_list(view->surface->compositor);
1813 }
1814
1815 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001816 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001817
1818 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001819 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001820 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001821 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001822
1823 weston_view_set_transform_parent(view, NULL);
1824
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825 wl_list_remove(&view->surface_link);
1826
1827 free(view);
1828}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001829
1830WL_EXPORT void
1831weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001832{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001833 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001835
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001836 if (--surface->ref_count > 0)
1837 return;
1838
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001839 assert(surface->resource == NULL);
1840
Pekka Paalanenca790762015-04-17 14:23:38 +03001841 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001842
Pekka Paalanene67858b2013-04-25 13:57:42 +03001843 assert(wl_list_empty(&surface->subsurface_list_pending));
1844 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001845
Jason Ekstranda7af7042013-10-12 22:38:11 -05001846 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1847 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001848
Jason Ekstrand7b982072014-05-20 14:33:03 -05001849 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001850
Pekka Paalanende685b82012-12-04 15:58:12 +02001851 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001852
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001853 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001854 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001855 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001856
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001857 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001858 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001859
Pekka Paalanen133e4392014-09-23 22:08:46 -04001860 weston_presentation_feedback_discard_list(&surface->feedback_list);
1861
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001862 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001863}
1864
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001865static void
1866destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001867{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001868 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001869
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001870 assert(surface);
1871
Giulio Camuffo0d379742013-11-15 22:06:15 +01001872 /* Set the resource to NULL, since we don't want to leave a
1873 * dangling pointer if the surface was refcounted and survives
1874 * the weston_surface_destroy() call. */
1875 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001876 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001877}
1878
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001879static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001880weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1881{
1882 struct weston_buffer *buffer =
1883 container_of(listener, struct weston_buffer, destroy_listener);
1884
1885 wl_signal_emit(&buffer->destroy_signal, buffer);
1886 free(buffer);
1887}
1888
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001889WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001890weston_buffer_from_resource(struct wl_resource *resource)
1891{
1892 struct weston_buffer *buffer;
1893 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001894
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001895 listener = wl_resource_get_destroy_listener(resource,
1896 weston_buffer_destroy_handler);
1897
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001898 if (listener)
1899 return container_of(listener, struct weston_buffer,
1900 destroy_listener);
1901
1902 buffer = zalloc(sizeof *buffer);
1903 if (buffer == NULL)
1904 return NULL;
1905
1906 buffer->resource = resource;
1907 wl_signal_init(&buffer->destroy_signal);
1908 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001909 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001910 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001911
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001912 return buffer;
1913}
1914
1915static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001916weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1917 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001918{
Pekka Paalanende685b82012-12-04 15:58:12 +02001919 struct weston_buffer_reference *ref =
1920 container_of(listener, struct weston_buffer_reference,
1921 destroy_listener);
1922
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001923 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001924 ref->buffer = NULL;
1925}
1926
1927WL_EXPORT void
1928weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001929 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001930{
1931 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001932 ref->buffer->busy_count--;
1933 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001934 assert(wl_resource_get_client(ref->buffer->resource));
1935 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001936 WL_BUFFER_RELEASE);
1937 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001938 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001939 }
1940
Pekka Paalanende685b82012-12-04 15:58:12 +02001941 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001942 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001943 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001944 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001945 }
1946
Pekka Paalanende685b82012-12-04 15:58:12 +02001947 ref->buffer = buffer;
1948 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1949}
1950
1951static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001952weston_surface_attach(struct weston_surface *surface,
1953 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001954{
1955 weston_buffer_reference(&surface->buffer_ref, buffer);
1956
Pekka Paalanena6421c42012-12-04 15:58:10 +02001957 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001958 if (weston_surface_is_mapped(surface))
1959 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001960 }
1961
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001962 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001963
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001964 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001965 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001966}
1967
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001968WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001969weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001970{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001971 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001972
1973 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001974 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001975}
1976
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001977WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001978weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001979{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001980 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001981
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001982 pixman_region32_union(&compositor->primary_plane.damage,
1983 &compositor->primary_plane.damage,
1984 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001985 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001986}
1987
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001988static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001989surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001990{
Pekka Paalanende685b82012-12-04 15:58:12 +02001991 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001992 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001993 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001994
Pekka Paalanenb5026542014-11-12 15:09:24 +02001995 if (weston_timeline_enabled_ &&
1996 pixman_region32_not_empty(&surface->damage))
1997 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1998 TLP_OUTPUT(surface->output), TLP_END);
1999
Jason Ekstrandef540082014-06-26 10:37:36 -07002000 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002001}
2002
2003static void
2004view_accumulate_damage(struct weston_view *view,
2005 pixman_region32_t *opaque)
2006{
2007 pixman_region32_t damage;
2008
2009 pixman_region32_init(&damage);
2010 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002011 pixman_box32_t *extents;
2012
Jason Ekstranda7af7042013-10-12 22:38:11 -05002013 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002014 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002015 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002016 pixman_region32_copy(&damage, &view->surface->damage);
2017 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002018 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002019 }
2020
Pekka Paalanen380adf52015-02-16 14:39:11 +02002021 pixman_region32_intersect(&damage, &damage,
2022 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023 pixman_region32_subtract(&damage, &damage, opaque);
2024 pixman_region32_union(&view->plane->damage,
2025 &view->plane->damage, &damage);
2026 pixman_region32_fini(&damage);
2027 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002028 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002029}
2030
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002031static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002032compositor_accumulate_damage(struct weston_compositor *ec)
2033{
2034 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002036 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002037
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002038 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002039
2040 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002041 pixman_region32_copy(&plane->clip, &clip);
2042
2043 pixman_region32_init(&opaque);
2044
Jason Ekstranda7af7042013-10-12 22:38:11 -05002045 wl_list_for_each(ev, &ec->view_list, link) {
2046 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002047 continue;
2048
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002050 }
2051
2052 pixman_region32_union(&clip, &clip, &opaque);
2053 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002054 }
2055
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002056 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002057
Jason Ekstranda7af7042013-10-12 22:38:11 -05002058 wl_list_for_each(ev, &ec->view_list, link)
2059 ev->surface->touched = 0;
2060
2061 wl_list_for_each(ev, &ec->view_list, link) {
2062 if (ev->surface->touched)
2063 continue;
2064 ev->surface->touched = 1;
2065
2066 surface_flush_damage(ev->surface);
2067
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002068 /* Both the renderer and the backend have seen the buffer
2069 * by now. If renderer needs the buffer, it has its own
2070 * reference set. If the backend wants to keep the buffer
2071 * around for migrating the surface into a non-primary plane
2072 * later, keep_buffer is true. Otherwise, drop the core
2073 * reference now, and allow early buffer release. This enables
2074 * clients to use single-buffering.
2075 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076 if (!ev->surface->keep_buffer)
2077 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002078 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002079}
2080
2081static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002083{
2084 struct weston_subsurface *sub;
2085
Pekka Paalanene67858b2013-04-25 13:57:42 +03002086 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002088 continue;
2089
Jason Ekstranda7af7042013-10-12 22:38:11 -05002090 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2091 wl_list_init(&sub->surface->views);
2092
2093 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002094 }
2095}
2096
2097static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002098surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002099{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002100 struct weston_subsurface *sub;
2101 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002102
Jason Ekstranda7af7042013-10-12 22:38:11 -05002103 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2104 if (sub->surface == surface)
2105 continue;
2106
George Kiagiadakised04d382014-06-13 18:10:26 +02002107 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2108 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002110 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002111
2112 surface_free_unused_subsurface_views(sub->surface);
2113 }
2114}
2115
2116static void
2117view_list_add_subsurface_view(struct weston_compositor *compositor,
2118 struct weston_subsurface *sub,
2119 struct weston_view *parent)
2120{
2121 struct weston_subsurface *child;
2122 struct weston_view *view = NULL, *iv;
2123
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002124 if (!weston_surface_is_mapped(sub->surface))
2125 return;
2126
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2128 if (iv->geometry.parent == parent) {
2129 view = iv;
2130 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002131 }
2132 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002133
2134 if (view) {
2135 /* Put it back in the surface's list of views */
2136 wl_list_remove(&view->surface_link);
2137 wl_list_insert(&sub->surface->views, &view->surface_link);
2138 } else {
2139 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002140 weston_view_set_position(view,
2141 sub->position.x,
2142 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002143 weston_view_set_transform_parent(view, parent);
2144 }
2145
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002146 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002147 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002148
Pekka Paalanenb188e912013-11-19 14:03:35 +02002149 if (wl_list_empty(&sub->surface->subsurface_list)) {
2150 wl_list_insert(compositor->view_list.prev, &view->link);
2151 return;
2152 }
2153
2154 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2155 if (child->surface == sub->surface)
2156 wl_list_insert(compositor->view_list.prev, &view->link);
2157 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002158 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002159 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002160}
2161
2162static void
2163view_list_add(struct weston_compositor *compositor,
2164 struct weston_view *view)
2165{
2166 struct weston_subsurface *sub;
2167
2168 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169
Pekka Paalanenb188e912013-11-19 14:03:35 +02002170 if (wl_list_empty(&view->surface->subsurface_list)) {
2171 wl_list_insert(compositor->view_list.prev, &view->link);
2172 return;
2173 }
2174
2175 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2176 if (sub->surface == view->surface)
2177 wl_list_insert(compositor->view_list.prev, &view->link);
2178 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002180 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181}
2182
2183static void
2184weston_compositor_build_view_list(struct weston_compositor *compositor)
2185{
2186 struct weston_view *view;
2187 struct weston_layer *layer;
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_stash_subsurface_views(view->surface);
2192
2193 wl_list_init(&compositor->view_list);
2194 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002195 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002196 view_list_add(compositor, view);
2197 }
2198 }
2199
2200 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002201 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002202 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002203}
2204
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002205static void
2206weston_output_take_feedback_list(struct weston_output *output,
2207 struct weston_surface *surface)
2208{
2209 struct weston_view *view;
2210 struct weston_presentation_feedback *feedback;
2211 uint32_t flags = 0xffffffff;
2212
2213 if (wl_list_empty(&surface->feedback_list))
2214 return;
2215
2216 /* All views must have the flag for the flag to survive. */
2217 wl_list_for_each(view, &surface->views, surface_link) {
2218 /* ignore views that are not on this output at all */
2219 if (view->output_mask & (1u << output->id))
2220 flags &= view->psf_flags;
2221 }
2222
2223 wl_list_for_each(feedback, &surface->feedback_list, link)
2224 feedback->psf_flags = flags;
2225
2226 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2227 wl_list_init(&surface->feedback_list);
2228}
2229
David Herrmann1edf44c2013-10-22 17:11:26 +02002230static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002231weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002232{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002233 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002234 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002235 struct weston_animation *animation, *next;
2236 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002237 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002238 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002239 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002240
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002241 if (output->destroying)
2242 return 0;
2243
Pekka Paalanenb5026542014-11-12 15:09:24 +02002244 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2245
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002246 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002247 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002248
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002249 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002250 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002251 } else {
2252 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002253 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002254 ev->psf_flags = 0;
2255 }
2256 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002257
Pekka Paalanene67858b2013-04-25 13:57:42 +03002258 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002259 wl_list_for_each(ev, &ec->view_list, link) {
2260 /* Note: This operation is safe to do multiple times on the
2261 * same surface.
2262 */
2263 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002264 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002265 &ev->surface->frame_callback_list);
2266 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002267
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002268 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002269 }
2270 }
2271
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002272 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002273
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002274 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002275 pixman_region32_intersect(&output_damage,
2276 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002277 pixman_region32_subtract(&output_damage,
2278 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002279
Scott Moreauccbf29d2012-02-22 14:21:41 -07002280 if (output->dirty)
2281 weston_output_update_matrix(output);
2282
David Herrmann1edf44c2013-10-22 17:11:26 +02002283 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002284
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002285 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002286
Kristian Høgsbergef044142011-06-21 15:02:12 -04002287 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002288
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002289 weston_compositor_repick(ec);
2290 wl_event_loop_dispatch(ec->input_loop, 0);
2291
Jonas Ådahldb773762012-06-13 00:01:21 +02002292 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002293 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002294 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002295 }
2296
Scott Moreaud64cf212012-06-08 19:40:54 -06002297 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002298 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002299 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002300 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002301
Pekka Paalanenb5026542014-11-12 15:09:24 +02002302 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2303
David Herrmann1edf44c2013-10-22 17:11:26 +02002304 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002305}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002306
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002307static int
2308weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002309{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002310 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002311
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002312 wl_event_loop_dispatch(compositor->input_loop, 0);
2313
2314 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002315}
2316
Pekka Paalanen82919792014-05-21 13:51:49 +03002317static void
2318weston_output_schedule_repaint_reset(struct weston_output *output)
2319{
2320 struct weston_compositor *compositor = output->compositor;
2321 struct wl_event_loop *loop;
2322 int fd;
2323
2324 output->repaint_scheduled = 0;
2325 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2326
2327 if (compositor->input_loop_source)
2328 return;
2329
2330 loop = wl_display_get_event_loop(compositor->wl_display);
2331 fd = wl_event_loop_get_fd(compositor->input_loop);
2332 compositor->input_loop_source =
2333 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2334 weston_compositor_read_input, compositor);
2335}
2336
Pekka Paalanen0513a952014-05-21 16:17:27 +03002337static int
2338output_repaint_timer_handler(void *data)
2339{
2340 struct weston_output *output = data;
2341 struct weston_compositor *compositor = output->compositor;
2342
2343 if (output->repaint_needed &&
2344 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2345 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2346 weston_output_repaint(output) == 0)
2347 return 0;
2348
2349 weston_output_schedule_repaint_reset(output);
2350
2351 return 0;
2352}
2353
Kristian Høgsbergef044142011-06-21 15:02:12 -04002354WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002355weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002356 const struct timespec *stamp,
2357 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002358{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002359 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002360 int32_t refresh_nsec;
2361 struct timespec now;
2362 struct timespec gone;
2363 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002364
Pekka Paalanenb5026542014-11-12 15:09:24 +02002365 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2366 TLP_VBLANK(stamp), TLP_END);
2367
Pekka Paalanen0513a952014-05-21 16:17:27 +03002368 refresh_nsec = 1000000000000LL / output->current_mode->refresh;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002369 weston_presentation_feedback_present_list(&output->feedback_list,
2370 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002371 output->msc,
2372 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002373
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002374 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002375
Pekka Paalanen0513a952014-05-21 16:17:27 +03002376 weston_compositor_read_presentation_clock(compositor, &now);
2377 timespec_sub(&gone, &now, stamp);
2378 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2379 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002380
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002381 if (msec < -1000 || msec > 1000) {
2382 static bool warned;
2383
2384 if (!warned)
2385 weston_log("Warning: computed repaint delay is "
2386 "insane: %d msec\n", msec);
2387 warned = true;
2388
2389 msec = 0;
2390 }
2391
2392 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002393 output_repaint_timer_handler(output);
2394 else
2395 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002396}
2397
2398static void
2399idle_repaint(void *data)
2400{
2401 struct weston_output *output = data;
2402
Jonas Ådahle5a12252013-04-05 23:07:11 +02002403 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002404}
2405
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002406WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002407weston_layer_entry_insert(struct weston_layer_entry *list,
2408 struct weston_layer_entry *entry)
2409{
2410 wl_list_insert(&list->link, &entry->link);
2411 entry->layer = list->layer;
2412}
2413
2414WL_EXPORT void
2415weston_layer_entry_remove(struct weston_layer_entry *entry)
2416{
2417 wl_list_remove(&entry->link);
2418 wl_list_init(&entry->link);
2419 entry->layer = NULL;
2420}
2421
2422WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002423weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2424{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002425 wl_list_init(&layer->view_list.link);
2426 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002427 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002428 if (below != NULL)
2429 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002430}
2431
2432WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002433weston_layer_set_mask(struct weston_layer *layer,
2434 int x, int y, int width, int height)
2435{
2436 struct weston_view *view;
2437
2438 layer->mask.x1 = x;
2439 layer->mask.x2 = x + width;
2440 layer->mask.y1 = y;
2441 layer->mask.y2 = y + height;
2442
2443 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2444 weston_view_geometry_dirty(view);
2445 }
2446}
2447
2448WL_EXPORT void
2449weston_layer_set_mask_infinite(struct weston_layer *layer)
2450{
2451 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2452 UINT32_MAX, UINT32_MAX);
2453}
2454
2455WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002456weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002457{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002458 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002459 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002460
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002461 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2462 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002463 return;
2464
Pekka Paalanenb5026542014-11-12 15:09:24 +02002465 if (!output->repaint_needed)
2466 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2467
Kristian Høgsbergef044142011-06-21 15:02:12 -04002468 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002469 output->repaint_needed = 1;
2470 if (output->repaint_scheduled)
2471 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002472
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002473 wl_event_loop_add_idle(loop, idle_repaint, output);
2474 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002475 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2476
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002477
2478 if (compositor->input_loop_source) {
2479 wl_event_source_remove(compositor->input_loop_source);
2480 compositor->input_loop_source = NULL;
2481 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002482}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002483
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002484WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002485weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2486{
2487 struct weston_output *output;
2488
2489 wl_list_for_each(output, &compositor->output_list, link)
2490 weston_output_schedule_repaint(output);
2491}
2492
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002493static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002494surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002495{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002496 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002497}
2498
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002499static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002500surface_attach(struct wl_client *client,
2501 struct wl_resource *resource,
2502 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2503{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002504 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002505 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002506
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002507 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002508 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002509 if (buffer == NULL) {
2510 wl_client_post_no_memory(client);
2511 return;
2512 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002513 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002514
Pekka Paalanende685b82012-12-04 15:58:12 +02002515 /* Attach, attach, without commit in between does not send
2516 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002517 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002518
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002519 surface->pending.sx = sx;
2520 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002521 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002522}
2523
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002524static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002525surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002526 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002527 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002528{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002529 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002530
Pekka Paalanen8e159182012-10-10 12:49:25 +03002531 pixman_region32_union_rect(&surface->pending.damage,
2532 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002533 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002534}
2535
Kristian Høgsberg33418202011-08-16 23:01:28 -04002536static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002537destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002538{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002539 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002540
2541 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002542 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002543}
2544
2545static void
2546surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002547 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002548{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002549 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002550 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002551
2552 cb = malloc(sizeof *cb);
2553 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002554 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002555 return;
2556 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002557
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002558 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2559 callback);
2560 if (cb->resource == NULL) {
2561 free(cb);
2562 wl_resource_post_no_memory(resource);
2563 return;
2564 }
2565
Jason Ekstranda85118c2013-06-27 20:17:02 -05002566 wl_resource_set_implementation(cb->resource, NULL, cb,
2567 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002568
Pekka Paalanenbc106382012-10-10 12:49:31 +03002569 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002570}
2571
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002572static void
2573surface_set_opaque_region(struct wl_client *client,
2574 struct wl_resource *resource,
2575 struct wl_resource *region_resource)
2576{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002577 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002578 struct weston_region *region;
2579
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002580 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002581 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002582 pixman_region32_copy(&surface->pending.opaque,
2583 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002584 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002585 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002586 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002587}
2588
2589static void
2590surface_set_input_region(struct wl_client *client,
2591 struct wl_resource *resource,
2592 struct wl_resource *region_resource)
2593{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002594 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002595 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002596
2597 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002598 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002599 pixman_region32_copy(&surface->pending.input,
2600 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002601 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002602 pixman_region32_fini(&surface->pending.input);
2603 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002604 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002605}
2606
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002607static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002608weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002609{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002610 struct weston_subsurface *sub;
2611
2612 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2613 parent_link_pending) {
2614 wl_list_remove(&sub->parent_link);
2615 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2616 }
2617}
2618
2619static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002620weston_surface_build_buffer_matrix(struct weston_surface *surface,
2621 struct weston_matrix *matrix)
2622{
2623 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2624 double src_width, src_height, dest_width, dest_height;
2625
2626 weston_matrix_init(matrix);
2627
2628 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2629 src_width = surface->width_from_buffer;
2630 src_height = surface->height_from_buffer;
2631 } else {
2632 src_width = wl_fixed_to_double(vp->buffer.src_width);
2633 src_height = wl_fixed_to_double(vp->buffer.src_height);
2634 }
2635
2636 if (vp->surface.width == -1) {
2637 dest_width = src_width;
2638 dest_height = src_height;
2639 } else {
2640 dest_width = vp->surface.width;
2641 dest_height = vp->surface.height;
2642 }
2643
2644 if (src_width != dest_width || src_height != dest_height)
2645 weston_matrix_scale(matrix,
2646 src_width / dest_width,
2647 src_height / dest_height, 1);
2648
2649 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2650 weston_matrix_translate(matrix,
2651 wl_fixed_to_double(vp->buffer.src_x),
2652 wl_fixed_to_double(vp->buffer.src_y),
2653 0);
2654
2655 switch (vp->buffer.transform) {
2656 case WL_OUTPUT_TRANSFORM_FLIPPED:
2657 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2658 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2659 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2660 weston_matrix_scale(matrix, -1, 1, 1);
2661 weston_matrix_translate(matrix,
2662 surface->width_from_buffer, 0, 0);
2663 break;
2664 }
2665
2666 switch (vp->buffer.transform) {
2667 default:
2668 case WL_OUTPUT_TRANSFORM_NORMAL:
2669 case WL_OUTPUT_TRANSFORM_FLIPPED:
2670 break;
2671 case WL_OUTPUT_TRANSFORM_90:
2672 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2673 weston_matrix_rotate_xy(matrix, 0, 1);
2674 weston_matrix_translate(matrix,
2675 surface->height_from_buffer, 0, 0);
2676 break;
2677 case WL_OUTPUT_TRANSFORM_180:
2678 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2679 weston_matrix_rotate_xy(matrix, -1, 0);
2680 weston_matrix_translate(matrix,
2681 surface->width_from_buffer,
2682 surface->height_from_buffer, 0);
2683 break;
2684 case WL_OUTPUT_TRANSFORM_270:
2685 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2686 weston_matrix_rotate_xy(matrix, 0, -1);
2687 weston_matrix_translate(matrix,
2688 0, surface->width_from_buffer, 0);
2689 break;
2690 }
2691
2692 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2693}
2694
2695static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002696weston_surface_commit_state(struct weston_surface *surface,
2697 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002698{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002699 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002700 pixman_region32_t opaque;
2701
Alexander Larsson4ea95522013-05-22 14:41:37 +02002702 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002703 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002704 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002705 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002706
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002707 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002708 if (state->newly_attached)
2709 weston_surface_attach(surface, state->buffer);
2710 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002711
Jason Ekstrand1e059042014-10-16 10:55:19 -05002712 weston_surface_build_buffer_matrix(surface,
2713 &surface->surface_to_buffer_matrix);
2714 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2715 &surface->surface_to_buffer_matrix);
2716
Jason Ekstrand7b982072014-05-20 14:33:03 -05002717 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002718 weston_surface_update_size(surface);
2719 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002720 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002721 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002722
Jason Ekstrand7b982072014-05-20 14:33:03 -05002723 state->sx = 0;
2724 state->sy = 0;
2725 state->newly_attached = 0;
2726 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002727
2728 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002729 if (weston_timeline_enabled_ &&
2730 pixman_region32_not_empty(&state->damage))
2731 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002732 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002733 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002734 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002735 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002736 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002737
2738 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002739 pixman_region32_init(&opaque);
2740 pixman_region32_intersect_rect(&opaque, &state->opaque,
2741 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002742
2743 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2744 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002745 wl_list_for_each(view, &surface->views, surface_link)
2746 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002747 }
2748
2749 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002750
2751 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002752 pixman_region32_intersect_rect(&surface->input, &state->input,
2753 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002754
Pekka Paalanenbc106382012-10-10 12:49:31 +03002755 /* wl_surface.frame */
2756 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002757 &state->frame_callback_list);
2758 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002759
2760 /* XXX:
2761 * What should happen with a feedback request, if there
2762 * is no wl_buffer attached for this commit?
2763 */
2764
2765 /* presentation.feedback */
2766 wl_list_insert_list(&surface->feedback_list,
2767 &state->feedback_list);
2768 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002769}
2770
2771static void
2772weston_surface_commit(struct weston_surface *surface)
2773{
2774 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002775
Pekka Paalanene67858b2013-04-25 13:57:42 +03002776 weston_surface_commit_subsurface_order(surface);
2777
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002778 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002779}
2780
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002781static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002782weston_subsurface_commit(struct weston_subsurface *sub);
2783
2784static void
2785weston_subsurface_parent_commit(struct weston_subsurface *sub,
2786 int parent_is_synchronized);
2787
2788static void
2789surface_commit(struct wl_client *client, struct wl_resource *resource)
2790{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002791 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002792 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2793
2794 if (sub) {
2795 weston_subsurface_commit(sub);
2796 return;
2797 }
2798
2799 weston_surface_commit(surface);
2800
2801 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2802 if (sub->surface != surface)
2803 weston_subsurface_parent_commit(sub, 0);
2804 }
2805}
2806
2807static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002808surface_set_buffer_transform(struct wl_client *client,
2809 struct wl_resource *resource, int transform)
2810{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002811 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002812
Jonny Lamba55f1392014-05-30 12:07:15 +02002813 /* if wl_output.transform grows more members this will need to be updated. */
2814 if (transform < 0 ||
2815 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2816 wl_resource_post_error(resource,
2817 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2818 "buffer transform must be a valid transform "
2819 "('%d' specified)", transform);
2820 return;
2821 }
2822
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002823 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002824 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002825}
2826
Alexander Larsson4ea95522013-05-22 14:41:37 +02002827static void
2828surface_set_buffer_scale(struct wl_client *client,
2829 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002830 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002831{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002832 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002833
Jonny Lamba55f1392014-05-30 12:07:15 +02002834 if (scale < 1) {
2835 wl_resource_post_error(resource,
2836 WL_SURFACE_ERROR_INVALID_SCALE,
2837 "buffer scale must be at least one "
2838 "('%d' specified)", scale);
2839 return;
2840 }
2841
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002842 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002843 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002844}
2845
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002846static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002847 surface_destroy,
2848 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002849 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002850 surface_frame,
2851 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002852 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002853 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002854 surface_set_buffer_transform,
2855 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002856};
2857
2858static void
2859compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002860 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002861{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002862 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002863 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002864
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002865 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002866 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002867 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002868 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002869 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002870
Jason Ekstranda85118c2013-06-27 20:17:02 -05002871 surface->resource =
2872 wl_resource_create(client, &wl_surface_interface,
2873 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002874 if (surface->resource == NULL) {
2875 weston_surface_destroy(surface);
2876 wl_resource_post_no_memory(resource);
2877 return;
2878 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002879 wl_resource_set_implementation(surface->resource, &surface_interface,
2880 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002881
2882 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002883}
2884
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002885static void
2886destroy_region(struct wl_resource *resource)
2887{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002888 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002889
2890 pixman_region32_fini(&region->region);
2891 free(region);
2892}
2893
2894static void
2895region_destroy(struct wl_client *client, struct wl_resource *resource)
2896{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002897 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002898}
2899
2900static void
2901region_add(struct wl_client *client, struct wl_resource *resource,
2902 int32_t x, int32_t y, int32_t width, int32_t height)
2903{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002904 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002905
2906 pixman_region32_union_rect(&region->region, &region->region,
2907 x, y, width, height);
2908}
2909
2910static void
2911region_subtract(struct wl_client *client, struct wl_resource *resource,
2912 int32_t x, int32_t y, int32_t width, int32_t height)
2913{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002914 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002915 pixman_region32_t rect;
2916
2917 pixman_region32_init_rect(&rect, x, y, width, height);
2918 pixman_region32_subtract(&region->region, &region->region, &rect);
2919 pixman_region32_fini(&rect);
2920}
2921
2922static const struct wl_region_interface region_interface = {
2923 region_destroy,
2924 region_add,
2925 region_subtract
2926};
2927
2928static void
2929compositor_create_region(struct wl_client *client,
2930 struct wl_resource *resource, uint32_t id)
2931{
2932 struct weston_region *region;
2933
2934 region = malloc(sizeof *region);
2935 if (region == NULL) {
2936 wl_resource_post_no_memory(resource);
2937 return;
2938 }
2939
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002940 pixman_region32_init(&region->region);
2941
Jason Ekstranda85118c2013-06-27 20:17:02 -05002942 region->resource =
2943 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002944 if (region->resource == NULL) {
2945 free(region);
2946 wl_resource_post_no_memory(resource);
2947 return;
2948 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002949 wl_resource_set_implementation(region->resource, &region_interface,
2950 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002951}
2952
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002953static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002954 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002955 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002956};
2957
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002958static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002959weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2960{
2961 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002962
Jason Ekstrand7b982072014-05-20 14:33:03 -05002963 weston_surface_commit_state(surface, &sub->cached);
2964 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002965
2966 weston_surface_commit_subsurface_order(surface);
2967
2968 weston_surface_schedule_repaint(surface);
2969
Jason Ekstrand7b982072014-05-20 14:33:03 -05002970 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002971}
2972
2973static void
2974weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2975{
2976 struct weston_surface *surface = sub->surface;
2977
2978 /*
2979 * If this commit would cause the surface to move by the
2980 * attach(dx, dy) parameters, the old damage region must be
2981 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002982 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002983 */
2984 pixman_region32_translate(&sub->cached.damage,
2985 -surface->pending.sx, -surface->pending.sy);
2986 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2987 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002988 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002989
2990 if (surface->pending.newly_attached) {
2991 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002992 weston_surface_state_set_buffer(&sub->cached,
2993 surface->pending.buffer);
2994 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002995 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002996 weston_presentation_feedback_discard_list(
2997 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002998 }
2999 sub->cached.sx += surface->pending.sx;
3000 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003001
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003002 sub->cached.buffer_viewport.changed |=
3003 surface->pending.buffer_viewport.changed;
3004 sub->cached.buffer_viewport.buffer =
3005 surface->pending.buffer_viewport.buffer;
3006 sub->cached.buffer_viewport.surface =
3007 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003008
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003009 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003010
3011 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3012
3013 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3014
3015 wl_list_insert_list(&sub->cached.frame_callback_list,
3016 &surface->pending.frame_callback_list);
3017 wl_list_init(&surface->pending.frame_callback_list);
3018
Pekka Paalanen133e4392014-09-23 22:08:46 -04003019 wl_list_insert_list(&sub->cached.feedback_list,
3020 &surface->pending.feedback_list);
3021 wl_list_init(&surface->pending.feedback_list);
3022
Jason Ekstrand7b982072014-05-20 14:33:03 -05003023 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003024}
3025
Derek Foreman280e7dd2014-10-03 13:13:42 -05003026static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003027weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3028{
3029 while (sub) {
3030 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003031 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003032
3033 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003034 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003035
3036 sub = weston_surface_to_subsurface(sub->parent);
3037 }
3038
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003039 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003040}
3041
3042static void
3043weston_subsurface_commit(struct weston_subsurface *sub)
3044{
3045 struct weston_surface *surface = sub->surface;
3046 struct weston_subsurface *tmp;
3047
3048 /* Recursive check for effectively synchronized. */
3049 if (weston_subsurface_is_synchronized(sub)) {
3050 weston_subsurface_commit_to_cache(sub);
3051 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003052 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003053 /* flush accumulated state from cache */
3054 weston_subsurface_commit_to_cache(sub);
3055 weston_subsurface_commit_from_cache(sub);
3056 } else {
3057 weston_surface_commit(surface);
3058 }
3059
3060 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3061 if (tmp->surface != surface)
3062 weston_subsurface_parent_commit(tmp, 0);
3063 }
3064 }
3065}
3066
3067static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003068weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003069{
3070 struct weston_surface *surface = sub->surface;
3071 struct weston_subsurface *tmp;
3072
Pekka Paalanene67858b2013-04-25 13:57:42 +03003073 /* From now on, commit_from_cache the whole sub-tree, regardless of
3074 * the synchronized mode of each child. This sub-surface or some
3075 * of its ancestors were synchronized, so we are synchronized
3076 * all the way down.
3077 */
3078
Jason Ekstrand7b982072014-05-20 14:33:03 -05003079 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003080 weston_subsurface_commit_from_cache(sub);
3081
3082 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3083 if (tmp->surface != surface)
3084 weston_subsurface_parent_commit(tmp, 1);
3085 }
3086}
3087
3088static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003089weston_subsurface_parent_commit(struct weston_subsurface *sub,
3090 int parent_is_synchronized)
3091{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003092 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003093 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003094 wl_list_for_each(view, &sub->surface->views, surface_link)
3095 weston_view_set_position(view,
3096 sub->position.x,
3097 sub->position.y);
3098
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003099 sub->position.set = 0;
3100 }
3101
3102 if (parent_is_synchronized || sub->synchronized)
3103 weston_subsurface_synchronized_commit(sub);
3104}
3105
Pekka Paalanen8274d902014-08-06 19:36:51 +03003106static int
3107subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3108{
3109 return snprintf(buf, len, "sub-surface");
3110}
3111
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003112static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003113subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003114{
3115 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003116 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003117
Jason Ekstranda7af7042013-10-12 22:38:11 -05003118 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003119 weston_view_set_position(view,
3120 view->geometry.x + dx,
3121 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003122
3123 /* No need to check parent mappedness, because if parent is not
3124 * mapped, parent is not in a visible layer, so this sub-surface
3125 * will not be drawn either.
3126 */
3127 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003128 struct weston_output *output;
3129
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003130 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003131 * because that would call it also for the parent surface,
3132 * which might not be mapped yet. That would lead to
3133 * inconsistent state, where the window could never be
3134 * mapped.
3135 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003136 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003137 * weston_surface_is_mapped() return true, so that when the
3138 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003139 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003140 */
3141 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003142 output = container_of(compositor->output_list.next,
3143 struct weston_output, link);
3144
3145 surface->output = output;
3146 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003147 }
3148}
3149
3150static struct weston_subsurface *
3151weston_surface_to_subsurface(struct weston_surface *surface)
3152{
3153 if (surface->configure == subsurface_configure)
3154 return surface->configure_private;
3155
3156 return NULL;
3157}
3158
Pekka Paalanen01388e22013-04-25 13:57:44 +03003159WL_EXPORT struct weston_surface *
3160weston_surface_get_main_surface(struct weston_surface *surface)
3161{
3162 struct weston_subsurface *sub;
3163
3164 while (surface && (sub = weston_surface_to_subsurface(surface)))
3165 surface = sub->parent;
3166
3167 return surface;
3168}
3169
Pekka Paalanen50b67472014-10-01 15:02:41 +03003170WL_EXPORT int
3171weston_surface_set_role(struct weston_surface *surface,
3172 const char *role_name,
3173 struct wl_resource *error_resource,
3174 uint32_t error_code)
3175{
3176 assert(role_name);
3177
3178 if (surface->role_name == NULL ||
3179 surface->role_name == role_name ||
3180 strcmp(surface->role_name, role_name) == 0) {
3181 surface->role_name = role_name;
3182
3183 return 0;
3184 }
3185
3186 wl_resource_post_error(error_resource, error_code,
3187 "Cannot assign role %s to wl_surface@%d,"
3188 " already has role %s\n",
3189 role_name,
3190 wl_resource_get_id(surface->resource),
3191 surface->role_name);
3192 return -1;
3193}
3194
Pekka Paalanen8274d902014-08-06 19:36:51 +03003195WL_EXPORT void
3196weston_surface_set_label_func(struct weston_surface *surface,
3197 int (*desc)(struct weston_surface *,
3198 char *, size_t))
3199{
3200 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003201 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003202}
3203
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003204/** Get the size of surface contents
3205 *
3206 * \param surface The surface to query.
3207 * \param width Returns the width of raw contents.
3208 * \param height Returns the height of raw contents.
3209 *
3210 * Retrieves the raw surface content size in pixels for the given surface.
3211 * This is the whole content size in buffer pixels. If the surface
3212 * has no content or the renderer does not implement this feature,
3213 * zeroes are returned.
3214 *
3215 * This function is used to determine the buffer size needed for
3216 * a weston_surface_copy_content() call.
3217 */
3218WL_EXPORT void
3219weston_surface_get_content_size(struct weston_surface *surface,
3220 int *width, int *height)
3221{
3222 struct weston_renderer *rer = surface->compositor->renderer;
3223
3224 if (!rer->surface_get_content_size) {
3225 *width = 0;
3226 *height = 0;
3227 return;
3228 }
3229
3230 rer->surface_get_content_size(surface, width, height);
3231}
3232
3233/** Copy surface contents to system memory.
3234 *
3235 * \param surface The surface to copy from.
3236 * \param target Pointer to the target memory buffer.
3237 * \param size Size of the target buffer in bytes.
3238 * \param src_x X location on contents to copy from.
3239 * \param src_y Y location on contents to copy from.
3240 * \param width Width in pixels of the area to copy.
3241 * \param height Height in pixels of the area to copy.
3242 * \return 0 for success, -1 for failure.
3243 *
3244 * Surface contents are maintained by the renderer. They can be in a
3245 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3246 * else.
3247 *
3248 * Surface contents are copied into memory pointed to by target,
3249 * which has size bytes of space available. The target memory
3250 * may be larger than needed, but being smaller returns an error.
3251 * The extra bytes in target may or may not be written; their content is
3252 * unspecified. Size must be large enough to hold the image.
3253 *
3254 * The image in the target memory will be arranged in rows from
3255 * top to bottom, and pixels on a row from left to right. The pixel
3256 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3257 * width * 4.
3258 *
3259 * Parameters src_x and src_y define the upper-left corner in buffer
3260 * coordinates (pixels) to copy from. Parameters width and height
3261 * define the size of the area to copy in pixels.
3262 *
3263 * The rectangle defined by src_x, src_y, width, height must fit in
3264 * the surface contents. Otherwise an error is returned.
3265 *
3266 * Use surface_get_data_size to determine the content size; the
3267 * needed target buffer size and rectangle limits.
3268 *
3269 * CURRENT IMPLEMENTATION RESTRICTIONS:
3270 * - the machine must be little-endian due to Pixman formats.
3271 *
3272 * NOTE: Pixman formats are premultiplied.
3273 */
3274WL_EXPORT int
3275weston_surface_copy_content(struct weston_surface *surface,
3276 void *target, size_t size,
3277 int src_x, int src_y,
3278 int width, int height)
3279{
3280 struct weston_renderer *rer = surface->compositor->renderer;
3281 int cw, ch;
3282 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3283
3284 if (!rer->surface_copy_content)
3285 return -1;
3286
3287 weston_surface_get_content_size(surface, &cw, &ch);
3288
3289 if (src_x < 0 || src_y < 0)
3290 return -1;
3291
3292 if (width <= 0 || height <= 0)
3293 return -1;
3294
3295 if (src_x + width > cw || src_y + height > ch)
3296 return -1;
3297
3298 if (width * bytespp * height > size)
3299 return -1;
3300
3301 return rer->surface_copy_content(surface, target, size,
3302 src_x, src_y, width, height);
3303}
3304
Pekka Paalanene67858b2013-04-25 13:57:42 +03003305static void
3306subsurface_set_position(struct wl_client *client,
3307 struct wl_resource *resource, int32_t x, int32_t y)
3308{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003309 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003310
3311 if (!sub)
3312 return;
3313
3314 sub->position.x = x;
3315 sub->position.y = y;
3316 sub->position.set = 1;
3317}
3318
3319static struct weston_subsurface *
3320subsurface_from_surface(struct weston_surface *surface)
3321{
3322 struct weston_subsurface *sub;
3323
3324 sub = weston_surface_to_subsurface(surface);
3325 if (sub)
3326 return sub;
3327
3328 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3329 if (sub->surface == surface)
3330 return sub;
3331
3332 return NULL;
3333}
3334
3335static struct weston_subsurface *
3336subsurface_sibling_check(struct weston_subsurface *sub,
3337 struct weston_surface *surface,
3338 const char *request)
3339{
3340 struct weston_subsurface *sibling;
3341
3342 sibling = subsurface_from_surface(surface);
3343
3344 if (!sibling) {
3345 wl_resource_post_error(sub->resource,
3346 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3347 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003348 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003349 return NULL;
3350 }
3351
3352 if (sibling->parent != sub->parent) {
3353 wl_resource_post_error(sub->resource,
3354 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3355 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003356 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003357 return NULL;
3358 }
3359
3360 return sibling;
3361}
3362
3363static void
3364subsurface_place_above(struct wl_client *client,
3365 struct wl_resource *resource,
3366 struct wl_resource *sibling_resource)
3367{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003368 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003369 struct weston_surface *surface =
3370 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003371 struct weston_subsurface *sibling;
3372
3373 if (!sub)
3374 return;
3375
3376 sibling = subsurface_sibling_check(sub, surface, "place_above");
3377 if (!sibling)
3378 return;
3379
3380 wl_list_remove(&sub->parent_link_pending);
3381 wl_list_insert(sibling->parent_link_pending.prev,
3382 &sub->parent_link_pending);
3383}
3384
3385static void
3386subsurface_place_below(struct wl_client *client,
3387 struct wl_resource *resource,
3388 struct wl_resource *sibling_resource)
3389{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003390 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003391 struct weston_surface *surface =
3392 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003393 struct weston_subsurface *sibling;
3394
3395 if (!sub)
3396 return;
3397
3398 sibling = subsurface_sibling_check(sub, surface, "place_below");
3399 if (!sibling)
3400 return;
3401
3402 wl_list_remove(&sub->parent_link_pending);
3403 wl_list_insert(&sibling->parent_link_pending,
3404 &sub->parent_link_pending);
3405}
3406
3407static void
3408subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3409{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003410 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003411
3412 if (sub)
3413 sub->synchronized = 1;
3414}
3415
3416static void
3417subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3418{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003419 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003420
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003421 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003422 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003423
3424 /* If sub became effectively desynchronized, flush. */
3425 if (!weston_subsurface_is_synchronized(sub))
3426 weston_subsurface_synchronized_commit(sub);
3427 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003428}
3429
3430static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003431weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3432{
3433 wl_list_remove(&sub->parent_link);
3434 wl_list_remove(&sub->parent_link_pending);
3435 wl_list_remove(&sub->parent_destroy_listener.link);
3436 sub->parent = NULL;
3437}
3438
3439static void
3440weston_subsurface_destroy(struct weston_subsurface *sub);
3441
3442static void
3443subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3444{
3445 struct weston_subsurface *sub =
3446 container_of(listener, struct weston_subsurface,
3447 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003448 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003449
3450 /* The protocol object (wl_resource) is left inert. */
3451 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003452 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003453
3454 weston_subsurface_destroy(sub);
3455}
3456
3457static void
3458subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3459{
3460 struct weston_subsurface *sub =
3461 container_of(listener, struct weston_subsurface,
3462 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003463 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003464 assert(sub->surface != sub->parent);
3465
3466 if (weston_surface_is_mapped(sub->surface))
3467 weston_surface_unmap(sub->surface);
3468
3469 weston_subsurface_unlink_parent(sub);
3470}
3471
3472static void
3473subsurface_resource_destroy(struct wl_resource *resource)
3474{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003475 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003476
3477 if (sub)
3478 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003479}
3480
3481static void
3482subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3483{
3484 wl_resource_destroy(resource);
3485}
3486
3487static void
3488weston_subsurface_link_parent(struct weston_subsurface *sub,
3489 struct weston_surface *parent)
3490{
3491 sub->parent = parent;
3492 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003493 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003494 &sub->parent_destroy_listener);
3495
3496 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3497 wl_list_insert(&parent->subsurface_list_pending,
3498 &sub->parent_link_pending);
3499}
3500
3501static void
3502weston_subsurface_link_surface(struct weston_subsurface *sub,
3503 struct weston_surface *surface)
3504{
3505 sub->surface = surface;
3506 sub->surface_destroy_listener.notify =
3507 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003508 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003509 &sub->surface_destroy_listener);
3510}
3511
3512static void
3513weston_subsurface_destroy(struct weston_subsurface *sub)
3514{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003515 struct weston_view *view, *next;
3516
Pekka Paalanene67858b2013-04-25 13:57:42 +03003517 assert(sub->surface);
3518
3519 if (sub->resource) {
3520 assert(weston_surface_to_subsurface(sub->surface) == sub);
3521 assert(sub->parent_destroy_listener.notify ==
3522 subsurface_handle_parent_destroy);
3523
George Kiagiadakised04d382014-06-13 18:10:26 +02003524 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3525 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003526 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003527 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003528
Pekka Paalanene67858b2013-04-25 13:57:42 +03003529 if (sub->parent)
3530 weston_subsurface_unlink_parent(sub);
3531
Jason Ekstrand7b982072014-05-20 14:33:03 -05003532 weston_surface_state_fini(&sub->cached);
3533 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003534
3535 sub->surface->configure = NULL;
3536 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003537 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003538 } else {
3539 /* the dummy weston_subsurface for the parent itself */
3540 assert(sub->parent_destroy_listener.notify == NULL);
3541 wl_list_remove(&sub->parent_link);
3542 wl_list_remove(&sub->parent_link_pending);
3543 }
3544
3545 wl_list_remove(&sub->surface_destroy_listener.link);
3546 free(sub);
3547}
3548
3549static const struct wl_subsurface_interface subsurface_implementation = {
3550 subsurface_destroy,
3551 subsurface_set_position,
3552 subsurface_place_above,
3553 subsurface_place_below,
3554 subsurface_set_sync,
3555 subsurface_set_desync
3556};
3557
3558static struct weston_subsurface *
3559weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3560 struct weston_surface *parent)
3561{
3562 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003563 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003564
Bryce Harringtonde16d892014-11-20 22:21:57 -08003565 sub = zalloc(sizeof *sub);
3566 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003567 return NULL;
3568
Jason Ekstranda7af7042013-10-12 22:38:11 -05003569 wl_list_init(&sub->unused_views);
3570
Jason Ekstranda85118c2013-06-27 20:17:02 -05003571 sub->resource =
3572 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003573 if (!sub->resource) {
3574 free(sub);
3575 return NULL;
3576 }
3577
Jason Ekstranda85118c2013-06-27 20:17:02 -05003578 wl_resource_set_implementation(sub->resource,
3579 &subsurface_implementation,
3580 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003581 weston_subsurface_link_surface(sub, surface);
3582 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003583 weston_surface_state_init(&sub->cached);
3584 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003585 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003586
3587 return sub;
3588}
3589
3590/* Create a dummy subsurface for having the parent itself in its
3591 * sub-surface lists. Makes stacking order manipulation easy.
3592 */
3593static struct weston_subsurface *
3594weston_subsurface_create_for_parent(struct weston_surface *parent)
3595{
3596 struct weston_subsurface *sub;
3597
Bryce Harringtonde16d892014-11-20 22:21:57 -08003598 sub = zalloc(sizeof *sub);
3599 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003600 return NULL;
3601
3602 weston_subsurface_link_surface(sub, parent);
3603 sub->parent = parent;
3604 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3605 wl_list_insert(&parent->subsurface_list_pending,
3606 &sub->parent_link_pending);
3607
3608 return sub;
3609}
3610
3611static void
3612subcompositor_get_subsurface(struct wl_client *client,
3613 struct wl_resource *resource,
3614 uint32_t id,
3615 struct wl_resource *surface_resource,
3616 struct wl_resource *parent_resource)
3617{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003618 struct weston_surface *surface =
3619 wl_resource_get_user_data(surface_resource);
3620 struct weston_surface *parent =
3621 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003622 struct weston_subsurface *sub;
3623 static const char where[] = "get_subsurface: wl_subsurface@";
3624
3625 if (surface == parent) {
3626 wl_resource_post_error(resource,
3627 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3628 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003629 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003630 return;
3631 }
3632
3633 if (weston_surface_to_subsurface(surface)) {
3634 wl_resource_post_error(resource,
3635 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3636 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003637 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003638 return;
3639 }
3640
Pekka Paalanen50b67472014-10-01 15:02:41 +03003641 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3642 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003643 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003644
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003645 if (weston_surface_get_main_surface(parent) == surface) {
3646 wl_resource_post_error(resource,
3647 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3648 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003649 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003650 return;
3651 }
3652
Pekka Paalanene67858b2013-04-25 13:57:42 +03003653 /* make sure the parent is in its own list */
3654 if (wl_list_empty(&parent->subsurface_list)) {
3655 if (!weston_subsurface_create_for_parent(parent)) {
3656 wl_resource_post_no_memory(resource);
3657 return;
3658 }
3659 }
3660
3661 sub = weston_subsurface_create(id, surface, parent);
3662 if (!sub) {
3663 wl_resource_post_no_memory(resource);
3664 return;
3665 }
3666
3667 surface->configure = subsurface_configure;
3668 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003669 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003670}
3671
3672static void
3673subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3674{
3675 wl_resource_destroy(resource);
3676}
3677
3678static const struct wl_subcompositor_interface subcompositor_interface = {
3679 subcompositor_destroy,
3680 subcompositor_get_subsurface
3681};
3682
3683static void
3684bind_subcompositor(struct wl_client *client,
3685 void *data, uint32_t version, uint32_t id)
3686{
3687 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003688 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003689
Jason Ekstranda85118c2013-06-27 20:17:02 -05003690 resource =
3691 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003692 if (resource == NULL) {
3693 wl_client_post_no_memory(client);
3694 return;
3695 }
3696 wl_resource_set_implementation(resource, &subcompositor_interface,
3697 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003698}
3699
3700static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003701weston_compositor_dpms(struct weston_compositor *compositor,
3702 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003703{
3704 struct weston_output *output;
3705
3706 wl_list_for_each(output, &compositor->output_list, link)
3707 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003708 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003709}
3710
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003711WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003712weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003713{
Neil Roberts8b62e202013-09-30 13:14:47 +01003714 uint32_t old_state = compositor->state;
3715
3716 /* The state needs to be changed before emitting the wake
3717 * signal because that may try to schedule a repaint which
3718 * will not work if the compositor is still sleeping */
3719 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3720
3721 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003722 case WESTON_COMPOSITOR_SLEEPING:
3723 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3724 /* fall through */
3725 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003726 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003727 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003728 /* fall through */
3729 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003730 wl_event_source_timer_update(compositor->idle_source,
3731 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003732 }
3733}
3734
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003735WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003736weston_compositor_offscreen(struct weston_compositor *compositor)
3737{
3738 switch (compositor->state) {
3739 case WESTON_COMPOSITOR_OFFSCREEN:
3740 return;
3741 case WESTON_COMPOSITOR_SLEEPING:
3742 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3743 /* fall through */
3744 default:
3745 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3746 wl_event_source_timer_update(compositor->idle_source, 0);
3747 }
3748}
3749
3750WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003751weston_compositor_sleep(struct weston_compositor *compositor)
3752{
3753 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3754 return;
3755
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003756 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003757 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3758 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3759}
3760
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003761static int
3762idle_handler(void *data)
3763{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003764 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003765
3766 if (compositor->idle_inhibit)
3767 return 1;
3768
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003769 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003770 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003771
3772 return 1;
3773}
3774
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003775WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003776weston_plane_init(struct weston_plane *plane,
3777 struct weston_compositor *ec,
3778 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003779{
3780 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003781 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003782 plane->x = x;
3783 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003784 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003785
3786 /* Init the link so that the call to wl_list_remove() when releasing
3787 * the plane without ever stacking doesn't lead to a crash */
3788 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003789}
3790
3791WL_EXPORT void
3792weston_plane_release(struct weston_plane *plane)
3793{
Xiong Zhang97116532013-10-23 13:58:31 +08003794 struct weston_view *view;
3795
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003796 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003797 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003798
Xiong Zhang97116532013-10-23 13:58:31 +08003799 wl_list_for_each(view, &plane->compositor->view_list, link) {
3800 if (view->plane == plane)
3801 view->plane = NULL;
3802 }
3803
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003804 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003805}
3806
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003807WL_EXPORT void
3808weston_compositor_stack_plane(struct weston_compositor *ec,
3809 struct weston_plane *plane,
3810 struct weston_plane *above)
3811{
3812 if (above)
3813 wl_list_insert(above->link.prev, &plane->link);
3814 else
3815 wl_list_insert(&ec->plane_list, &plane->link);
3816}
3817
Casey Dahlin9074db52012-04-19 22:50:09 -04003818static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003819{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003820 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003821}
3822
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003823static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003824bind_output(struct wl_client *client,
3825 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003826{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003827 struct weston_output *output = data;
3828 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003829 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003830
Jason Ekstranda85118c2013-06-27 20:17:02 -05003831 resource = wl_resource_create(client, &wl_output_interface,
3832 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003833 if (resource == NULL) {
3834 wl_client_post_no_memory(client);
3835 return;
3836 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003837
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003838 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003839 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003840
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003841 wl_output_send_geometry(resource,
3842 output->x,
3843 output->y,
3844 output->mm_width,
3845 output->mm_height,
3846 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003847 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003848 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003849 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003850 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003851 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003852
3853 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003854 wl_output_send_mode(resource,
3855 mode->flags,
3856 mode->width,
3857 mode->height,
3858 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003859 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003860
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003861 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003862 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003863}
3864
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003865/* Move other outputs when one is removed so the space remains contiguos. */
3866static void
3867weston_compositor_remove_output(struct weston_compositor *compositor,
3868 struct weston_output *remove_output)
3869{
3870 struct weston_output *output;
3871 int offset = 0;
3872
3873 wl_list_for_each(output, &compositor->output_list, link) {
3874 if (output == remove_output) {
3875 offset = output->width;
3876 continue;
3877 }
3878
3879 if (offset > 0) {
3880 weston_output_move(output,
3881 output->x - offset, output->y);
3882 output->dirty = 1;
3883 }
3884 }
3885}
3886
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003887WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003888weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003889{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003890 struct wl_resource *resource;
3891
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003892 output->destroying = 1;
3893
Pekka Paalanen0513a952014-05-21 16:17:27 +03003894 wl_event_source_remove(output->repaint_timer);
3895
Pekka Paalanen133e4392014-09-23 22:08:46 -04003896 weston_presentation_feedback_discard_list(&output->feedback_list);
3897
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003898 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003899 wl_list_remove(&output->link);
3900
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003901 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003902 wl_signal_emit(&output->destroy_signal, output);
3903
Richard Hughesafe690c2013-05-02 10:10:04 +01003904 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003905 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003906 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003907 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003908
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003909 wl_resource_for_each(resource, &output->resource_list) {
3910 wl_resource_set_destructor(resource, NULL);
3911 }
3912
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003913 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003914}
3915
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003916WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003917weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003918{
Scott Moreau850ca422012-05-21 15:21:25 -06003919 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003920
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003921 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003922 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003923
Scott Moreauccbf29d2012-02-22 14:21:41 -07003924 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003925 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003926 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003927 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003928 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003929 weston_matrix_scale(&output->matrix, magnification,
3930 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003931 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003932
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003933 switch (output->transform) {
3934 case WL_OUTPUT_TRANSFORM_FLIPPED:
3935 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3936 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3937 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3938 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3939 weston_matrix_scale(&output->matrix, -1, 1, 1);
3940 break;
3941 }
3942
3943 switch (output->transform) {
3944 default:
3945 case WL_OUTPUT_TRANSFORM_NORMAL:
3946 case WL_OUTPUT_TRANSFORM_FLIPPED:
3947 break;
3948 case WL_OUTPUT_TRANSFORM_90:
3949 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3950 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
3951 weston_matrix_rotate_xy(&output->matrix, 0, 1);
3952 break;
3953 case WL_OUTPUT_TRANSFORM_180:
3954 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3955 weston_matrix_translate(&output->matrix,
3956 -output->width, -output->height, 0);
3957 weston_matrix_rotate_xy(&output->matrix, -1, 0);
3958 break;
3959 case WL_OUTPUT_TRANSFORM_270:
3960 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3961 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3962 weston_matrix_rotate_xy(&output->matrix, 0, -1);
3963 break;
3964 }
3965
3966 if (output->current_scale != 1)
3967 weston_matrix_scale(&output->matrix,
3968 output->current_scale,
3969 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003970
Scott Moreauccbf29d2012-02-22 14:21:41 -07003971 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05003972
3973 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003974}
3975
Scott Moreau1bad5db2012-08-18 01:04:05 -06003976static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003977weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003978{
3979 output->transform = transform;
3980
3981 switch (transform) {
3982 case WL_OUTPUT_TRANSFORM_90:
3983 case WL_OUTPUT_TRANSFORM_270:
3984 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3985 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3986 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003987 output->width = output->current_mode->height;
3988 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003989 break;
3990 case WL_OUTPUT_TRANSFORM_NORMAL:
3991 case WL_OUTPUT_TRANSFORM_180:
3992 case WL_OUTPUT_TRANSFORM_FLIPPED:
3993 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003994 output->width = output->current_mode->width;
3995 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003996 break;
3997 default:
3998 break;
3999 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06004000
Hardening57388e42013-09-18 23:56:36 +02004001 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02004002 output->width /= scale;
4003 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004004}
4005
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004006static void
4007weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004008{
4009 output->x = x;
4010 output->y = y;
4011
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004012 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004013 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004014 output->width,
4015 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004016}
4017
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004018WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004019weston_output_move(struct weston_output *output, int x, int y)
4020{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004021 struct wl_resource *resource;
4022
4023 output->move_x = x - output->x;
4024 output->move_y = y - output->y;
4025
4026 if (output->move_x == 0 && output->move_y == 0)
4027 return;
4028
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004029 weston_output_init_geometry(output, x, y);
4030
4031 output->dirty = 1;
4032
4033 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004034 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004035
4036 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004037 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004038 wl_output_send_geometry(resource,
4039 output->x,
4040 output->y,
4041 output->mm_width,
4042 output->mm_height,
4043 output->subpixel,
4044 output->make,
4045 output->model,
4046 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004047
4048 if (wl_resource_get_version(resource) >= 2)
4049 wl_output_send_done(resource);
4050 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004051}
4052
4053WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004054weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004055 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004056 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004057{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004058 struct wl_event_loop *loop;
4059
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004060 output->compositor = c;
4061 output->x = x;
4062 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004063 output->mm_width = mm_width;
4064 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004065 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004066 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004067
Alexander Larsson0b135062013-05-28 16:23:36 +02004068 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004069 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004070
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004071 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004072 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004073
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004074 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004075 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004076 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004077 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004078 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02004079
Pekka Paalanen0513a952014-05-21 16:17:27 +03004080 loop = wl_display_get_event_loop(c->wl_display);
4081 output->repaint_timer = wl_event_loop_add_timer(loop,
4082 output_repaint_timer_handler, output);
4083
Casey Dahlin58ba1372012-04-19 22:50:08 -04004084 output->id = ffs(~output->compositor->output_id_pool) - 1;
4085 output->compositor->output_id_pool |= 1 << output->id;
4086
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004087 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004088 wl_global_create(c->wl_display, &wl_output_interface, 2,
4089 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004090}
4091
4092/** Adds an output to the compositor's output list and
4093 * send the compositor's output_created signal.
4094 *
4095 * \param compositor The compositor instance.
4096 * \param output The output to be added.
4097 */
4098WL_EXPORT void
4099weston_compositor_add_output(struct weston_compositor *compositor,
4100 struct weston_output *output)
4101{
4102 wl_list_insert(compositor->output_list.prev, &output->link);
4103 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004104}
4105
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004106WL_EXPORT void
4107weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004108 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004109 wl_fixed_t *x, wl_fixed_t *y)
4110{
Derek Foreman0f679412014-10-02 13:41:17 -05004111 struct weston_vector p = { {
4112 wl_fixed_to_double(device_x),
4113 wl_fixed_to_double(device_y),
4114 0.0,
4115 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004116
Derek Foreman67a18b92015-03-24 11:36:14 -05004117 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004118
Derek Foreman0f679412014-10-02 13:41:17 -05004119 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4120 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004121}
4122
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004123static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004124destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004125{
Jonny Lamb74130762013-11-26 18:19:46 +01004126 struct weston_surface *surface =
4127 wl_resource_get_user_data(resource);
4128
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004129 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004130 surface->pending.buffer_viewport.buffer.src_width =
4131 wl_fixed_from_int(-1);
4132 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004133 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004134}
4135
4136static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004137viewport_destroy(struct wl_client *client,
4138 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004139{
4140 wl_resource_destroy(resource);
4141}
4142
4143static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004144viewport_set(struct wl_client *client,
4145 struct wl_resource *resource,
4146 wl_fixed_t src_x,
4147 wl_fixed_t src_y,
4148 wl_fixed_t src_width,
4149 wl_fixed_t src_height,
4150 int32_t dst_width,
4151 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004152{
Jonny Lamb74130762013-11-26 18:19:46 +01004153 struct weston_surface *surface =
4154 wl_resource_get_user_data(resource);
4155
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004156 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004157
Jonny Lamb8ae35902013-11-26 18:19:45 +01004158 if (wl_fixed_to_double(src_width) < 0 ||
4159 wl_fixed_to_double(src_height) < 0) {
4160 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004161 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004162 "source dimensions must be non-negative (%fx%f)",
4163 wl_fixed_to_double(src_width),
4164 wl_fixed_to_double(src_height));
4165 return;
4166 }
4167
4168 if (dst_width <= 0 || dst_height <= 0) {
4169 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004170 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004171 "destination dimensions must be positive (%dx%d)",
4172 dst_width, dst_height);
4173 return;
4174 }
4175
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004176 surface->pending.buffer_viewport.buffer.src_x = src_x;
4177 surface->pending.buffer_viewport.buffer.src_y = src_y;
4178 surface->pending.buffer_viewport.buffer.src_width = src_width;
4179 surface->pending.buffer_viewport.buffer.src_height = src_height;
4180 surface->pending.buffer_viewport.surface.width = dst_width;
4181 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004182 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004183}
4184
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004185static void
4186viewport_set_source(struct wl_client *client,
4187 struct wl_resource *resource,
4188 wl_fixed_t src_x,
4189 wl_fixed_t src_y,
4190 wl_fixed_t src_width,
4191 wl_fixed_t src_height)
4192{
4193 struct weston_surface *surface =
4194 wl_resource_get_user_data(resource);
4195
4196 assert(surface->viewport_resource != NULL);
4197
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004198 if (src_width == wl_fixed_from_int(-1) &&
4199 src_height == wl_fixed_from_int(-1)) {
4200 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004201 surface->pending.buffer_viewport.buffer.src_width =
4202 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004203 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004204 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004205 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004206
4207 if (src_width <= 0 || src_height <= 0) {
4208 wl_resource_post_error(resource,
4209 WL_VIEWPORT_ERROR_BAD_VALUE,
4210 "source size must be positive (%fx%f)",
4211 wl_fixed_to_double(src_width),
4212 wl_fixed_to_double(src_height));
4213 return;
4214 }
4215
4216 surface->pending.buffer_viewport.buffer.src_x = src_x;
4217 surface->pending.buffer_viewport.buffer.src_y = src_y;
4218 surface->pending.buffer_viewport.buffer.src_width = src_width;
4219 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004220 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004221}
4222
4223static void
4224viewport_set_destination(struct wl_client *client,
4225 struct wl_resource *resource,
4226 int32_t dst_width,
4227 int32_t dst_height)
4228{
4229 struct weston_surface *surface =
4230 wl_resource_get_user_data(resource);
4231
4232 assert(surface->viewport_resource != NULL);
4233
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004234 if (dst_width == -1 && dst_height == -1) {
4235 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004236 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004237 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004238 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004239 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004240
4241 if (dst_width <= 0 || dst_height <= 0) {
4242 wl_resource_post_error(resource,
4243 WL_VIEWPORT_ERROR_BAD_VALUE,
4244 "destination size must be positive (%dx%d)",
4245 dst_width, dst_height);
4246 return;
4247 }
4248
4249 surface->pending.buffer_viewport.surface.width = dst_width;
4250 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004251 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004252}
4253
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004254static const struct wl_viewport_interface viewport_interface = {
4255 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004256 viewport_set,
4257 viewport_set_source,
4258 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004259};
4260
4261static void
4262scaler_destroy(struct wl_client *client,
4263 struct wl_resource *resource)
4264{
4265 wl_resource_destroy(resource);
4266}
4267
4268static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004269scaler_get_viewport(struct wl_client *client,
4270 struct wl_resource *scaler,
4271 uint32_t id,
4272 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004273{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004274 int version = wl_resource_get_version(scaler);
4275 struct weston_surface *surface =
4276 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004277 struct wl_resource *resource;
4278
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004279 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004280 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004281 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4282 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004283 return;
4284 }
4285
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004286 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004287 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004288 if (resource == NULL) {
4289 wl_client_post_no_memory(client);
4290 return;
4291 }
4292
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004293 wl_resource_set_implementation(resource, &viewport_interface,
4294 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004295
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004296 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004297}
4298
4299static const struct wl_scaler_interface scaler_interface = {
4300 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004301 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004302};
4303
4304static void
4305bind_scaler(struct wl_client *client,
4306 void *data, uint32_t version, uint32_t id)
4307{
4308 struct wl_resource *resource;
4309
4310 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004311 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004312 if (resource == NULL) {
4313 wl_client_post_no_memory(client);
4314 return;
4315 }
4316
4317 wl_resource_set_implementation(resource, &scaler_interface,
4318 NULL, NULL);
4319}
4320
4321static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004322destroy_presentation_feedback(struct wl_resource *feedback_resource)
4323{
4324 struct weston_presentation_feedback *feedback;
4325
4326 feedback = wl_resource_get_user_data(feedback_resource);
4327
4328 wl_list_remove(&feedback->link);
4329 free(feedback);
4330}
4331
4332static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004333presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4334{
4335 wl_resource_destroy(resource);
4336}
4337
4338static void
4339presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004340 struct wl_resource *presentation_resource,
4341 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004342 uint32_t callback)
4343{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004344 struct weston_surface *surface;
4345 struct weston_presentation_feedback *feedback;
4346
4347 surface = wl_resource_get_user_data(surface_resource);
4348
Bryce Harringtonde16d892014-11-20 22:21:57 -08004349 feedback = zalloc(sizeof *feedback);
4350 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004351 goto err_calloc;
4352
4353 feedback->resource = wl_resource_create(client,
4354 &presentation_feedback_interface,
4355 1, callback);
4356 if (!feedback->resource)
4357 goto err_create;
4358
4359 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4360 destroy_presentation_feedback);
4361
4362 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4363
4364 return;
4365
4366err_create:
4367 free(feedback);
4368
4369err_calloc:
4370 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004371}
4372
4373static const struct presentation_interface presentation_implementation = {
4374 presentation_destroy,
4375 presentation_feedback
4376};
4377
4378static void
4379bind_presentation(struct wl_client *client,
4380 void *data, uint32_t version, uint32_t id)
4381{
4382 struct weston_compositor *compositor = data;
4383 struct wl_resource *resource;
4384
4385 resource = wl_resource_create(client, &presentation_interface,
4386 MIN(version, 1), id);
4387 if (resource == NULL) {
4388 wl_client_post_no_memory(client);
4389 return;
4390 }
4391
4392 wl_resource_set_implementation(resource, &presentation_implementation,
4393 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004394 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004395}
4396
4397static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004398compositor_bind(struct wl_client *client,
4399 void *data, uint32_t version, uint32_t id)
4400{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004401 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004402 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004403
Jason Ekstranda85118c2013-06-27 20:17:02 -05004404 resource = wl_resource_create(client, &wl_compositor_interface,
4405 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004406 if (resource == NULL) {
4407 wl_client_post_no_memory(client);
4408 return;
4409 }
4410
4411 wl_resource_set_implementation(resource, &compositor_interface,
4412 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004413}
4414
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004415WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004416weston_environment_get_fd(const char *env)
4417{
4418 char *e, *end;
4419 int fd, flags;
4420
4421 e = getenv(env);
4422 if (!e)
4423 return -1;
4424 fd = strtol(e, &end, 0);
4425 if (*end != '\0')
4426 return -1;
4427
4428 flags = fcntl(fd, F_GETFD);
4429 if (flags == -1)
4430 return -1;
4431
4432 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4433 unsetenv(env);
4434
4435 return fd;
4436}
4437
Pekka Paalanenb5026542014-11-12 15:09:24 +02004438static void
4439timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4440 uint32_t key, void *data)
4441{
4442 struct weston_compositor *compositor = data;
4443
4444 if (weston_timeline_enabled_)
4445 weston_timeline_close();
4446 else
4447 weston_timeline_open(compositor);
4448}
4449
Giulio Camuffo459137b2014-10-11 23:56:24 +03004450/** Create the compositor.
4451 *
4452 * This functions creates and initializes a compositor instance.
4453 *
4454 * \param display The Wayland display to be used.
4455 * \param user_data A pointer to an object that can later be retrieved
4456 * using the \ref weston_compositor_get_user_data function.
4457 * \return The compositor instance on success or NULL on failure.
4458 */
4459WL_EXPORT struct weston_compositor *
4460weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004461{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004462 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004463 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004464
Giulio Camuffo459137b2014-10-11 23:56:24 +03004465 ec = zalloc(sizeof *ec);
4466 if (!ec)
4467 return NULL;
4468
4469 ec->wl_display = display;
4470 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004471 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004472 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004473 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004474 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004475 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004476 wl_signal_init(&ec->idle_signal);
4477 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004478 wl_signal_init(&ec->show_input_panel_signal);
4479 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004480 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004481 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004482 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004483 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004484 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004485 wl_signal_init(&ec->session_signal);
4486 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004487
Casey Dahlin58ba1372012-04-19 22:50:08 -04004488 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004489 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004490
Giulio Camuffo954f1832014-10-11 18:27:30 +03004491 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004492 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004493 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004494
Giulio Camuffo954f1832014-10-11 18:27:30 +03004495 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004496 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004497 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004498
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004499 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004500 ec, bind_scaler))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004501 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004502
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004503 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4504 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004505 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004506
Jason Ekstranda7af7042013-10-12 22:38:11 -05004507 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004508 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004509 wl_list_init(&ec->layer_list);
4510 wl_list_init(&ec->seat_list);
4511 wl_list_init(&ec->output_list);
4512 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004513 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004514 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004515 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004516 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004517 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004518
Xiong Zhang97116532013-10-23 13:58:31 +08004519 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004520 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004521
Giulio Camuffo459137b2014-10-11 23:56:24 +03004522 wl_data_device_manager_init(ec->wl_display);
4523
4524 wl_display_init_shm(ec->wl_display);
4525
4526 loop = wl_display_get_event_loop(ec->wl_display);
4527 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4528 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4529
4530 ec->input_loop = wl_event_loop_create();
4531
4532 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4533 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4534
4535 weston_compositor_add_debug_binding(ec, KEY_T,
4536 timeline_key_binding_handler, ec);
4537
4538 weston_compositor_schedule_repaint(ec);
4539
4540 return ec;
4541
4542fail:
4543 free(ec);
4544 return NULL;
4545}
4546
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004547WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004548weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004549{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004550 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004551
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004552 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004553 if (ec->input_loop_source)
4554 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004555
Matt Roper361d2ad2011-08-29 13:52:23 -07004556 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004557 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004558 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004559
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004560 if (ec->renderer)
4561 ec->renderer->destroy(ec);
4562
Daniel Stone325fc2d2012-05-30 16:31:58 +01004563 weston_binding_list_destroy_all(&ec->key_binding_list);
4564 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004565 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004566 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004567 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004568
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004569 weston_plane_release(&ec->primary_plane);
4570
Jonas Ådahlc97af922012-03-28 22:36:09 +02004571 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07004572}
4573
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004574WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004575weston_compositor_exit_with_code(struct weston_compositor *compositor,
4576 int exit_code)
4577{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004578 if (compositor->exit_code == EXIT_SUCCESS)
4579 compositor->exit_code = exit_code;
4580
Giulio Camuffo459137b2014-10-11 23:56:24 +03004581 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004582}
4583
4584WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004585weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4586 const struct weston_pointer_grab_interface *interface)
4587{
4588 struct weston_seat *seat;
4589
4590 ec->default_pointer_grab = interface;
4591 wl_list_for_each(seat, &ec->seat_list, link) {
4592 if (seat->pointer) {
4593 weston_pointer_set_default_grab(seat->pointer,
4594 interface);
4595 }
4596 }
4597}
4598
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004599WL_EXPORT int
4600weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4601 clockid_t clk_id)
4602{
4603 struct timespec ts;
4604
4605 if (clock_gettime(clk_id, &ts) < 0)
4606 return -1;
4607
4608 compositor->presentation_clock = clk_id;
4609
4610 return 0;
4611}
4612
4613/*
4614 * For choosing the software clock, when the display hardware or API
4615 * does not expose a compatible presentation timestamp.
4616 */
4617WL_EXPORT int
4618weston_compositor_set_presentation_clock_software(
4619 struct weston_compositor *compositor)
4620{
4621 /* In order of preference */
4622 static const clockid_t clocks[] = {
4623 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4624 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4625 CLOCK_MONOTONIC, /* no jumps, may crawl */
4626 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4627 CLOCK_REALTIME /* may jump and crawl */
4628 };
4629 unsigned i;
4630
4631 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4632 if (weston_compositor_set_presentation_clock(compositor,
4633 clocks[i]) == 0)
4634 return 0;
4635
4636 weston_log("Error: no suitable presentation clock available.\n");
4637
4638 return -1;
4639}
4640
Pekka Paalanen662f3842015-03-18 12:17:26 +02004641/** Read the current time from the Presentation clock
4642 *
4643 * \param compositor
4644 * \param ts[out] The current time.
4645 *
4646 * \note Reading the current time in user space is always imprecise to some
4647 * degree.
4648 *
4649 * This function is never meant to fail. If reading the clock does fail,
4650 * an error message is logged and a zero time is returned. Callers are not
4651 * supposed to detect or react to failures.
4652 */
4653WL_EXPORT void
4654weston_compositor_read_presentation_clock(
4655 const struct weston_compositor *compositor,
4656 struct timespec *ts)
4657{
4658 static bool warned;
4659 int ret;
4660
4661 ret = clock_gettime(compositor->presentation_clock, ts);
4662 if (ret < 0) {
4663 ts->tv_sec = 0;
4664 ts->tv_nsec = 0;
4665
4666 if (!warned)
4667 weston_log("Error: failure to read "
4668 "the presentation clock %#x: '%m' (%d)\n",
4669 compositor->presentation_clock, errno);
4670 warned = true;
4671 }
4672}
4673
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004674WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004675weston_version(int *major, int *minor, int *micro)
4676{
4677 *major = WESTON_VERSION_MAJOR;
4678 *minor = WESTON_VERSION_MINOR;
4679 *micro = WESTON_VERSION_MICRO;
4680}
4681
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004682WL_EXPORT void *
4683weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004684{
4685 char path[PATH_MAX];
4686 void *module, *init;
4687
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004688 if (name == NULL)
4689 return NULL;
4690
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004691 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004692 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004693 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004694 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004695
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004696 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4697 if (module) {
4698 weston_log("Module '%s' already loaded\n", path);
4699 dlclose(module);
4700 return NULL;
4701 }
4702
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004703 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004704 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004705 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004706 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004707 return NULL;
4708 }
4709
4710 init = dlsym(module, entrypoint);
4711 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004712 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004713 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004714 return NULL;
4715 }
4716
4717 return init;
4718}
4719
Giulio Camuffo459137b2014-10-11 23:56:24 +03004720
4721/** Destroys the compositor.
4722 *
4723 * This function cleans up the compositor state and destroys it.
4724 *
4725 * \param compositor The compositor to be destroyed.
4726 */
4727WL_EXPORT void
4728weston_compositor_destroy(struct weston_compositor *compositor)
4729{
4730 /* prevent further rendering while shutting down */
4731 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4732
4733 wl_signal_emit(&compositor->destroy_signal, compositor);
4734
4735 weston_compositor_xkb_destroy(compositor);
4736
4737 compositor->backend->destroy(compositor);
4738 free(compositor);
4739}
4740
4741/** Instruct the compositor to exit.
4742 *
4743 * This functions does not directly destroy the compositor object, it merely
4744 * command it to start the tear down process. It is not guaranteed that the
4745 * tear down will happen immediately.
4746 *
4747 * \param compositor The compositor to tear down.
4748 */
4749WL_EXPORT void
4750weston_compositor_exit(struct weston_compositor *compositor)
4751{
4752 compositor->exit(compositor);
4753}
4754
4755/** Return the user data stored in the compositor.
4756 *
4757 * This function returns the user data pointer set with user_data parameter
4758 * to the \ref weston_compositor_create function.
4759 */
4760WL_EXPORT void *
4761weston_compositor_get_user_data(struct weston_compositor *compositor)
4762{
4763 return compositor->user_data;
4764}