blob: 2a117d80f70cc7579acef3b5c213941bec6d01a0 [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) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001082 if (output->destroying)
1083 continue;
1084
Jason Ekstranda7af7042013-10-12 22:38:11 -05001085 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001086 &output->region);
1087
1088 e = pixman_region32_extents(&region);
1089 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1090
1091 if (area > 0)
1092 mask |= 1 << output->id;
1093
1094 if (area >= max) {
1095 new_output = output;
1096 max = area;
1097 }
1098 }
1099 pixman_region32_fini(&region);
1100
Jason Ekstranda7af7042013-10-12 22:38:11 -05001101 ev->output = new_output;
1102 ev->output_mask = mask;
1103
1104 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001105}
1106
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001107static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001108weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1109 int from_x, int from_y, int *to_x, int *to_y)
1110{
1111 float x, y;
1112
1113 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1114 weston_view_from_global_float(to, x, y, &x, &y);
1115
1116 *to_x = round(x);
1117 *to_y = round(y);
1118}
1119
1120static void
1121weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1122{
1123 pixman_box32_t *a;
1124 pixman_box32_t b;
1125
1126 a = pixman_region32_extents(&from->geometry.scissor);
1127
1128 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1129 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1130
1131 pixman_region32_fini(&to->geometry.scissor);
1132 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1133}
1134
1135static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001136view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001138{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001139 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1140 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001141 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001142 { inbox->x1, inbox->y1 },
1143 { inbox->x1, inbox->y2 },
1144 { inbox->x2, inbox->y1 },
1145 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001146 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001147 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001148 int i;
1149
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001150 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001151 /* avoid rounding empty bbox to 1x1 */
1152 pixman_region32_init(bbox);
1153 return;
1154 }
1155
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001156 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001157 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001159 if (x < min_x)
1160 min_x = x;
1161 if (x > max_x)
1162 max_x = x;
1163 if (y < min_y)
1164 min_y = y;
1165 if (y > max_y)
1166 max_y = y;
1167 }
1168
Pekka Paalanen219b9822012-02-08 15:38:37 +02001169 int_x = floorf(min_x);
1170 int_y = floorf(min_y);
1171 pixman_region32_init_rect(bbox, int_x, int_y,
1172 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001173}
1174
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001175static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001176weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001177{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001179
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001180 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 view->geometry.x = roundf(view->geometry.x);
1182 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001183
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001184 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001185 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1186 view->transform.position.matrix.d[12] = view->geometry.x;
1187 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001188
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001190
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 view->transform.inverse = view->transform.position.matrix;
1192 view->transform.inverse.d[12] = -view->geometry.x;
1193 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001194
Jason Ekstranda7af7042013-10-12 22:38:11 -05001195 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001196 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001197 view->surface->width,
1198 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001199 if (view->geometry.scissor_enabled)
1200 pixman_region32_intersect(&view->transform.boundingbox,
1201 &view->transform.boundingbox,
1202 &view->geometry.scissor);
1203
1204 pixman_region32_translate(&view->transform.boundingbox,
1205 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001206
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 if (view->alpha == 1.0) {
1208 pixman_region32_copy(&view->transform.opaque,
1209 &view->surface->opaque);
1210 pixman_region32_translate(&view->transform.opaque,
1211 view->geometry.x,
1212 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001213 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001214}
1215
1216static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001218{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 struct weston_view *parent = view->geometry.parent;
1220 struct weston_matrix *matrix = &view->transform.matrix;
1221 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001222 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001223 pixman_region32_t surfregion;
1224 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001225
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001227
1228 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1230 view->transform.position.matrix.d[12] = view->geometry.x;
1231 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001232
1233 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001235 weston_matrix_multiply(matrix, &tform->matrix);
1236
Pekka Paalanen483243f2013-03-08 14:56:50 +02001237 if (parent)
1238 weston_matrix_multiply(matrix, &parent->transform.matrix);
1239
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001240 if (weston_matrix_invert(inverse, matrix) < 0) {
1241 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 weston_log("error: weston_view %p"
1243 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001244 return -1;
1245 }
1246
Pekka Paalanen380adf52015-02-16 14:39:11 +02001247 pixman_region32_init_rect(&surfregion, 0, 0,
1248 view->surface->width, view->surface->height);
1249 if (view->geometry.scissor_enabled)
1250 pixman_region32_intersect(&surfregion, &surfregion,
1251 &view->geometry.scissor);
1252 surfbox = pixman_region32_extents(&surfregion);
1253
1254 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1255 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001256
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001257 return 0;
1258}
1259
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001260static struct weston_layer *
1261get_view_layer(struct weston_view *view)
1262{
1263 if (view->parent_view)
1264 return get_view_layer(view->parent_view);
1265 return view->layer_link.layer;
1266}
1267
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001268WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001269weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001270{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001272 struct weston_layer *layer;
1273 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001274
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001276 return;
1277
Pekka Paalanen483243f2013-03-08 14:56:50 +02001278 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001280
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001282
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001284
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 pixman_region32_fini(&view->transform.boundingbox);
1286 pixman_region32_fini(&view->transform.opaque);
1287 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001288
Pekka Paalanencd403622012-01-25 13:37:39 +02001289 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 if (view->geometry.transformation_list.next ==
1291 &view->transform.position.link &&
1292 view->geometry.transformation_list.prev ==
1293 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001294 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001296 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 if (weston_view_update_transform_enable(view) < 0)
1298 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001299 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001300
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001301 layer = get_view_layer(view);
1302 if (layer) {
1303 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001304 pixman_region32_intersect(&view->transform.boundingbox,
1305 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001306 pixman_region32_intersect(&view->transform.opaque,
1307 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001308 pixman_region32_fini(&mask);
1309 }
1310
Pekka Paalanen380adf52015-02-16 14:39:11 +02001311 if (parent) {
1312 if (parent->geometry.scissor_enabled) {
1313 view->geometry.scissor_enabled = true;
1314 weston_view_transfer_scissor(parent, view);
1315 } else {
1316 view->geometry.scissor_enabled = false;
1317 }
1318 }
1319
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001321
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001323
Jason Ekstranda7af7042013-10-12 22:38:11 -05001324 wl_signal_emit(&view->surface->compositor->transform_signal,
1325 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001326}
1327
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001328WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001329weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001330{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001331 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001332
1333 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334 * The invariant: if view->geometry.dirty, then all views
1335 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001336 * Corollary: if not parent->geometry.dirty, then all ancestors
1337 * are not dirty.
1338 */
1339
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001341 return;
1342
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001344
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001346 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001348}
1349
1350WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001351weston_view_to_global_fixed(struct weston_view *view,
1352 wl_fixed_t vx, wl_fixed_t vy,
1353 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001354{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001355 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001356
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 weston_view_to_global_float(view,
1358 wl_fixed_to_double(vx),
1359 wl_fixed_to_double(vy),
1360 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001361 *x = wl_fixed_from_double(xf);
1362 *y = wl_fixed_from_double(yf);
1363}
1364
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001365WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366weston_view_from_global_float(struct weston_view *view,
1367 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001368{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001370 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1371
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001373
1374 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001375 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001376 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001377 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001378 *vx = 0;
1379 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001380 return;
1381 }
1382
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383 *vx = v.f[0] / v.f[3];
1384 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001385 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 *vx = x - view->geometry.x;
1387 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001388 }
1389}
1390
1391WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392weston_view_from_global_fixed(struct weston_view *view,
1393 wl_fixed_t x, wl_fixed_t y,
1394 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001395{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001397
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 weston_view_from_global_float(view,
1399 wl_fixed_to_double(x),
1400 wl_fixed_to_double(y),
1401 &vxf, &vyf);
1402 *vx = wl_fixed_from_double(vxf);
1403 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001404}
1405
1406WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407weston_view_from_global(struct weston_view *view,
1408 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001409{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001411
Jason Ekstranda7af7042013-10-12 22:38:11 -05001412 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1413 *vx = floorf(vxf);
1414 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001415}
1416
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001417WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001418weston_surface_schedule_repaint(struct weston_surface *surface)
1419{
1420 struct weston_output *output;
1421
1422 wl_list_for_each(output, &surface->compositor->output_list, link)
1423 if (surface->output_mask & (1 << output->id))
1424 weston_output_schedule_repaint(output);
1425}
1426
1427WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428weston_view_schedule_repaint(struct weston_view *view)
1429{
1430 struct weston_output *output;
1431
1432 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1433 if (view->output_mask & (1 << output->id))
1434 weston_output_schedule_repaint(output);
1435}
1436
Pekka Paalanene508ce62015-02-19 13:59:55 +02001437/**
1438 * XXX: This function does it the wrong way.
1439 * surface->damage is the damage from the client, and causes
1440 * surface_flush_damage() to copy pixels. No window management action can
1441 * cause damage to the client-provided content, warranting re-upload!
1442 *
1443 * Instead of surface->damage, this function should record the damage
1444 * with all the views for this surface to avoid extraneous texture
1445 * uploads.
1446 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001448weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001449{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001450 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 0, 0, surface->width,
1452 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001453
Kristian Høgsberg98238702012-08-03 16:29:12 -04001454 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001455}
1456
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001457WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001458weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001459{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001460 if (view->geometry.x == x && view->geometry.y == y)
1461 return;
1462
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 view->geometry.x = x;
1464 view->geometry.y = y;
1465 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001466}
1467
Pekka Paalanen483243f2013-03-08 14:56:50 +02001468static void
1469transform_parent_handle_parent_destroy(struct wl_listener *listener,
1470 void *data)
1471{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 struct weston_view *view =
1473 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001474 geometry.parent_destroy_listener);
1475
Jason Ekstranda7af7042013-10-12 22:38:11 -05001476 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001477}
1478
1479WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001481 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001482{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 if (view->geometry.parent) {
1484 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1485 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001486
1487 if (!parent)
1488 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001489 }
1490
Jason Ekstranda7af7042013-10-12 22:38:11 -05001491 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001492
Jason Ekstranda7af7042013-10-12 22:38:11 -05001493 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001494 transform_parent_handle_parent_destroy;
1495 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001496 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001497 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001498 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001499 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001500 }
1501
Jason Ekstranda7af7042013-10-12 22:38:11 -05001502 weston_view_geometry_dirty(view);
1503}
1504
Pekka Paalanen380adf52015-02-16 14:39:11 +02001505/** Set a clip mask rectangle on a view
1506 *
1507 * \param view The view to set the clip mask on.
1508 * \param x Top-left corner X coordinate of the clip rectangle.
1509 * \param y Top-left corner Y coordinate of the clip rectangle.
1510 * \param width Width of the clip rectangle, non-negative.
1511 * \param height Height of the clip rectangle, non-negative.
1512 *
1513 * A shell may set a clip mask rectangle on a view. Everything outside
1514 * the rectangle is cut away for input and output purposes: it is
1515 * not drawn and cannot be hit by hit-test based input like pointer
1516 * motion or touch-downs. Everything inside the rectangle will behave
1517 * normally. Clients are unaware of clipping.
1518 *
1519 * The rectangle is set in the surface local coordinates. Setting a clip
1520 * mask rectangle does not affect the view position, the view is positioned
1521 * as it would be without a clip. The clip also does not change
1522 * weston_surface::width,height.
1523 *
1524 * The clip mask rectangle is part of transformation inheritance
1525 * (weston_view_set_transform_parent()). A clip set in the root of the
1526 * transformation inheritance tree will affect all views in the tree.
1527 * A clip can be set only on the root view. Attempting to set a clip
1528 * on view that has a transformation parent will fail. Assigning a parent
1529 * to a view that has a clip set will cause the clip to be forgotten.
1530 *
1531 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1532 * on the additional transformations in the child views. These transformations
1533 * may not rotate the coordinate axes, i.e., only translation and scaling
1534 * are allowed. Violating this restriction causes the clipping to malfunction.
1535 * Furthermore, using scaling may cause rounding errors in child clipping.
1536 *
1537 * The clip mask rectangle is not automatically adjusted based on
1538 * wl_surface.attach dx and dy arguments.
1539 *
1540 * A clip mask rectangle can be set only if the compositor capability
1541 * WESTON_CAP_VIEW_CLIP_MASK is present.
1542 *
1543 * This function sets the clip mask rectangle and schedules a repaint for
1544 * the view.
1545 */
1546WL_EXPORT void
1547weston_view_set_mask(struct weston_view *view,
1548 int x, int y, int width, int height)
1549{
1550 struct weston_compositor *compositor = view->surface->compositor;
1551
1552 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1553 weston_log("%s not allowed without capability!\n", __func__);
1554 return;
1555 }
1556
1557 if (view->geometry.parent) {
1558 weston_log("view %p has a parent, clip forbidden!\n", view);
1559 return;
1560 }
1561
1562 if (width < 0 || height < 0) {
1563 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1564 x, y, width, height);
1565 return;
1566 }
1567
1568 pixman_region32_fini(&view->geometry.scissor);
1569 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1570 view->geometry.scissor_enabled = true;
1571 weston_view_geometry_dirty(view);
1572 weston_view_schedule_repaint(view);
1573}
1574
1575/** Remove the clip mask from a view
1576 *
1577 * \param view The view to remove the clip mask from.
1578 *
1579 * Removed the clip mask rectangle and schedules a repaint.
1580 *
1581 * \sa weston_view_set_mask
1582 */
1583WL_EXPORT void
1584weston_view_set_mask_infinite(struct weston_view *view)
1585{
1586 view->geometry.scissor_enabled = false;
1587 weston_view_geometry_dirty(view);
1588 weston_view_schedule_repaint(view);
1589}
1590
Derek Foreman280e7dd2014-10-03 13:13:42 -05001591WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592weston_view_is_mapped(struct weston_view *view)
1593{
1594 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001595 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001596 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001597 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001598}
1599
Derek Foreman280e7dd2014-10-03 13:13:42 -05001600WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001601weston_surface_is_mapped(struct weston_surface *surface)
1602{
1603 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001604 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001605 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001606 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001607}
1608
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001609static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001610surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001611{
1612 struct weston_view *view;
1613
1614 if (surface->width == width && surface->height == height)
1615 return;
1616
1617 surface->width = width;
1618 surface->height = height;
1619
1620 wl_list_for_each(view, &surface->views, surface_link)
1621 weston_view_geometry_dirty(view);
1622}
1623
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001624WL_EXPORT void
1625weston_surface_set_size(struct weston_surface *surface,
1626 int32_t width, int32_t height)
1627{
1628 assert(!surface->resource);
1629 surface_set_size(surface, width, height);
1630}
1631
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001632static int
1633fixed_round_up_to_int(wl_fixed_t f)
1634{
1635 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1636}
1637
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001638static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001639weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001640{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001641 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001642 int32_t width, height;
1643
1644 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001645 surface->width_from_buffer = 0;
1646 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001647 return;
1648 }
1649
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001650 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001651 case WL_OUTPUT_TRANSFORM_90:
1652 case WL_OUTPUT_TRANSFORM_270:
1653 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1654 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001655 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1656 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001657 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001658 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001659 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1660 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001661 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001662 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001663
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001664 surface->width_from_buffer = width;
1665 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001666}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001667
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001668static void
1669weston_surface_update_size(struct weston_surface *surface)
1670{
1671 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1672 int32_t width, height;
1673
1674 width = surface->width_from_buffer;
1675 height = surface->height_from_buffer;
1676
1677 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001678 surface_set_size(surface,
1679 vp->surface.width, vp->surface.height);
1680 return;
1681 }
1682
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001683 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001684 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1685 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1686
1687 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001688 return;
1689 }
1690
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001691 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001692}
1693
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001694WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001695weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001696{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001697 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001698
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001699 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001700
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001701 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001702}
1703
Jason Ekstranda7af7042013-10-12 22:38:11 -05001704WL_EXPORT struct weston_view *
1705weston_compositor_pick_view(struct weston_compositor *compositor,
1706 wl_fixed_t x, wl_fixed_t y,
1707 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001708{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001710 wl_fixed_t view_x, view_y;
1711 int view_ix, view_iy;
1712 int ix = wl_fixed_to_int(x);
1713 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001714
Jason Ekstranda7af7042013-10-12 22:38:11 -05001715 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001716 if (!pixman_region32_contains_point(
1717 &view->transform.boundingbox, ix, iy, NULL))
1718 continue;
1719
1720 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1721 view_ix = wl_fixed_to_int(view_x);
1722 view_iy = wl_fixed_to_int(view_y);
1723
1724 if (!pixman_region32_contains_point(&view->surface->input,
1725 view_ix, view_iy, NULL))
1726 continue;
1727
Pekka Paalanen380adf52015-02-16 14:39:11 +02001728 if (view->geometry.scissor_enabled &&
1729 !pixman_region32_contains_point(&view->geometry.scissor,
1730 view_ix, view_iy, NULL))
1731 continue;
1732
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001733 *vx = view_x;
1734 *vy = view_y;
1735 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001736 }
1737
1738 return NULL;
1739}
1740
1741static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001742weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001743{
Daniel Stone37816df2012-05-16 18:45:18 +01001744 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001745
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001746 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001747 return;
1748
Daniel Stone37816df2012-05-16 18:45:18 +01001749 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001750 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001751}
1752
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001753WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001754weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001755{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001756 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001757
Jason Ekstranda7af7042013-10-12 22:38:11 -05001758 if (!weston_view_is_mapped(view))
1759 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001760
Jason Ekstranda7af7042013-10-12 22:38:11 -05001761 weston_view_damage_below(view);
1762 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001763 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001764 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001765 wl_list_remove(&view->link);
1766 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001767 view->output_mask = 0;
1768 weston_surface_assign_output(view->surface);
1769
1770 if (weston_surface_is_mapped(view->surface))
1771 return;
1772
1773 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1774 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001775 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001776 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001777 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001778 NULL,
1779 wl_fixed_from_int(0),
1780 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001781 if (seat->touch && seat->touch->focus == view)
Derek Foreman4c93c082015-04-30 16:45:41 -05001782 weston_touch_set_focus(seat->touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001783 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001784}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001785
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786WL_EXPORT void
1787weston_surface_unmap(struct weston_surface *surface)
1788{
1789 struct weston_view *view;
1790
1791 wl_list_for_each(view, &surface->views, surface_link)
1792 weston_view_unmap(view);
1793 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001794}
1795
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001796static void
1797weston_surface_reset_pending_buffer(struct weston_surface *surface)
1798{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001799 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001800 surface->pending.sx = 0;
1801 surface->pending.sy = 0;
1802 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001803 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001804}
1805
Jason Ekstranda7af7042013-10-12 22:38:11 -05001806WL_EXPORT void
1807weston_view_destroy(struct weston_view *view)
1808{
1809 wl_signal_emit(&view->destroy_signal, view);
1810
1811 assert(wl_list_empty(&view->geometry.child_list));
1812
1813 if (weston_view_is_mapped(view)) {
1814 weston_view_unmap(view);
1815 weston_compositor_build_view_list(view->surface->compositor);
1816 }
1817
1818 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001819 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001820
1821 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001822 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001823 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001824 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825
1826 weston_view_set_transform_parent(view, NULL);
1827
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828 wl_list_remove(&view->surface_link);
1829
1830 free(view);
1831}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001832
1833WL_EXPORT void
1834weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001835{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001836 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001837 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001838
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001839 if (--surface->ref_count > 0)
1840 return;
1841
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001842 assert(surface->resource == NULL);
1843
Pekka Paalanenca790762015-04-17 14:23:38 +03001844 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001845
Pekka Paalanene67858b2013-04-25 13:57:42 +03001846 assert(wl_list_empty(&surface->subsurface_list_pending));
1847 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001848
Jason Ekstranda7af7042013-10-12 22:38:11 -05001849 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1850 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001851
Jason Ekstrand7b982072014-05-20 14:33:03 -05001852 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001853
Pekka Paalanende685b82012-12-04 15:58:12 +02001854 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001855
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001856 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001857 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001858 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001859
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001860 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001861 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001862
Pekka Paalanen133e4392014-09-23 22:08:46 -04001863 weston_presentation_feedback_discard_list(&surface->feedback_list);
1864
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001865 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001866}
1867
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001868static void
1869destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001870{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001871 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001872
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001873 assert(surface);
1874
Giulio Camuffo0d379742013-11-15 22:06:15 +01001875 /* Set the resource to NULL, since we don't want to leave a
1876 * dangling pointer if the surface was refcounted and survives
1877 * the weston_surface_destroy() call. */
1878 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001879 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001880}
1881
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001882static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001883weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1884{
1885 struct weston_buffer *buffer =
1886 container_of(listener, struct weston_buffer, destroy_listener);
1887
1888 wl_signal_emit(&buffer->destroy_signal, buffer);
1889 free(buffer);
1890}
1891
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001892WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001893weston_buffer_from_resource(struct wl_resource *resource)
1894{
1895 struct weston_buffer *buffer;
1896 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001897
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001898 listener = wl_resource_get_destroy_listener(resource,
1899 weston_buffer_destroy_handler);
1900
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001901 if (listener)
1902 return container_of(listener, struct weston_buffer,
1903 destroy_listener);
1904
1905 buffer = zalloc(sizeof *buffer);
1906 if (buffer == NULL)
1907 return NULL;
1908
1909 buffer->resource = resource;
1910 wl_signal_init(&buffer->destroy_signal);
1911 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001912 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001913 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001914
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001915 return buffer;
1916}
1917
1918static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001919weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1920 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001921{
Pekka Paalanende685b82012-12-04 15:58:12 +02001922 struct weston_buffer_reference *ref =
1923 container_of(listener, struct weston_buffer_reference,
1924 destroy_listener);
1925
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001926 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001927 ref->buffer = NULL;
1928}
1929
1930WL_EXPORT void
1931weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001932 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001933{
1934 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001935 ref->buffer->busy_count--;
1936 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001937 assert(wl_resource_get_client(ref->buffer->resource));
1938 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001939 WL_BUFFER_RELEASE);
1940 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001941 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001942 }
1943
Pekka Paalanende685b82012-12-04 15:58:12 +02001944 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001945 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001946 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001947 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001948 }
1949
Pekka Paalanende685b82012-12-04 15:58:12 +02001950 ref->buffer = buffer;
1951 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1952}
1953
1954static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001955weston_surface_attach(struct weston_surface *surface,
1956 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001957{
1958 weston_buffer_reference(&surface->buffer_ref, buffer);
1959
Pekka Paalanena6421c42012-12-04 15:58:10 +02001960 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001961 if (weston_surface_is_mapped(surface))
1962 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001963 }
1964
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001965 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001966
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001967 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001968 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001969}
1970
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001971WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001972weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001973{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001974 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001975
1976 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001977 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001978}
1979
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001980WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001981weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001982{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001983 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001984
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001985 pixman_region32_union(&compositor->primary_plane.damage,
1986 &compositor->primary_plane.damage,
1987 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001988 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001989}
1990
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001991static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001992surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001993{
Pekka Paalanende685b82012-12-04 15:58:12 +02001994 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001995 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001996 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001997
Pekka Paalanenb5026542014-11-12 15:09:24 +02001998 if (weston_timeline_enabled_ &&
1999 pixman_region32_not_empty(&surface->damage))
2000 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2001 TLP_OUTPUT(surface->output), TLP_END);
2002
Jason Ekstrandef540082014-06-26 10:37:36 -07002003 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002004}
2005
2006static void
2007view_accumulate_damage(struct weston_view *view,
2008 pixman_region32_t *opaque)
2009{
2010 pixman_region32_t damage;
2011
2012 pixman_region32_init(&damage);
2013 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002014 pixman_box32_t *extents;
2015
Jason Ekstranda7af7042013-10-12 22:38:11 -05002016 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002017 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002018 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002019 pixman_region32_copy(&damage, &view->surface->damage);
2020 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002021 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002022 }
2023
Pekka Paalanen380adf52015-02-16 14:39:11 +02002024 pixman_region32_intersect(&damage, &damage,
2025 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002026 pixman_region32_subtract(&damage, &damage, opaque);
2027 pixman_region32_union(&view->plane->damage,
2028 &view->plane->damage, &damage);
2029 pixman_region32_fini(&damage);
2030 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002031 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002032}
2033
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002034static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002035compositor_accumulate_damage(struct weston_compositor *ec)
2036{
2037 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002038 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002039 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002040
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002041 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002042
2043 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002044 pixman_region32_copy(&plane->clip, &clip);
2045
2046 pixman_region32_init(&opaque);
2047
Jason Ekstranda7af7042013-10-12 22:38:11 -05002048 wl_list_for_each(ev, &ec->view_list, link) {
2049 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002050 continue;
2051
Jason Ekstranda7af7042013-10-12 22:38:11 -05002052 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002053 }
2054
2055 pixman_region32_union(&clip, &clip, &opaque);
2056 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002057 }
2058
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002059 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002060
Jason Ekstranda7af7042013-10-12 22:38:11 -05002061 wl_list_for_each(ev, &ec->view_list, link)
2062 ev->surface->touched = 0;
2063
2064 wl_list_for_each(ev, &ec->view_list, link) {
2065 if (ev->surface->touched)
2066 continue;
2067 ev->surface->touched = 1;
2068
2069 surface_flush_damage(ev->surface);
2070
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002071 /* Both the renderer and the backend have seen the buffer
2072 * by now. If renderer needs the buffer, it has its own
2073 * reference set. If the backend wants to keep the buffer
2074 * around for migrating the surface into a non-primary plane
2075 * later, keep_buffer is true. Otherwise, drop the core
2076 * reference now, and allow early buffer release. This enables
2077 * clients to use single-buffering.
2078 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079 if (!ev->surface->keep_buffer)
2080 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002081 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002082}
2083
2084static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002085surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002086{
2087 struct weston_subsurface *sub;
2088
Pekka Paalanene67858b2013-04-25 13:57:42 +03002089 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002090 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002091 continue;
2092
Jason Ekstranda7af7042013-10-12 22:38:11 -05002093 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2094 wl_list_init(&sub->surface->views);
2095
2096 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002097 }
2098}
2099
2100static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002101surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002102{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002103 struct weston_subsurface *sub;
2104 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002105
Jason Ekstranda7af7042013-10-12 22:38:11 -05002106 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2107 if (sub->surface == surface)
2108 continue;
2109
George Kiagiadakised04d382014-06-13 18:10:26 +02002110 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2111 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002113 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002114
2115 surface_free_unused_subsurface_views(sub->surface);
2116 }
2117}
2118
2119static void
2120view_list_add_subsurface_view(struct weston_compositor *compositor,
2121 struct weston_subsurface *sub,
2122 struct weston_view *parent)
2123{
2124 struct weston_subsurface *child;
2125 struct weston_view *view = NULL, *iv;
2126
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002127 if (!weston_surface_is_mapped(sub->surface))
2128 return;
2129
Jason Ekstranda7af7042013-10-12 22:38:11 -05002130 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2131 if (iv->geometry.parent == parent) {
2132 view = iv;
2133 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002134 }
2135 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136
2137 if (view) {
2138 /* Put it back in the surface's list of views */
2139 wl_list_remove(&view->surface_link);
2140 wl_list_insert(&sub->surface->views, &view->surface_link);
2141 } else {
2142 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002143 weston_view_set_position(view,
2144 sub->position.x,
2145 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002146 weston_view_set_transform_parent(view, parent);
2147 }
2148
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002149 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002150 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002151
Pekka Paalanenb188e912013-11-19 14:03:35 +02002152 if (wl_list_empty(&sub->surface->subsurface_list)) {
2153 wl_list_insert(compositor->view_list.prev, &view->link);
2154 return;
2155 }
2156
2157 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2158 if (child->surface == sub->surface)
2159 wl_list_insert(compositor->view_list.prev, &view->link);
2160 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002162 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002163}
2164
2165static void
2166view_list_add(struct weston_compositor *compositor,
2167 struct weston_view *view)
2168{
2169 struct weston_subsurface *sub;
2170
2171 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002172
Pekka Paalanenb188e912013-11-19 14:03:35 +02002173 if (wl_list_empty(&view->surface->subsurface_list)) {
2174 wl_list_insert(compositor->view_list.prev, &view->link);
2175 return;
2176 }
2177
2178 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2179 if (sub->surface == view->surface)
2180 wl_list_insert(compositor->view_list.prev, &view->link);
2181 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002182 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002183 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002184}
2185
2186static void
2187weston_compositor_build_view_list(struct weston_compositor *compositor)
2188{
2189 struct weston_view *view;
2190 struct weston_layer *layer;
2191
2192 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002193 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002194 surface_stash_subsurface_views(view->surface);
2195
2196 wl_list_init(&compositor->view_list);
2197 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002198 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002199 view_list_add(compositor, view);
2200 }
2201 }
2202
2203 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002204 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002205 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002206}
2207
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002208static void
2209weston_output_take_feedback_list(struct weston_output *output,
2210 struct weston_surface *surface)
2211{
2212 struct weston_view *view;
2213 struct weston_presentation_feedback *feedback;
2214 uint32_t flags = 0xffffffff;
2215
2216 if (wl_list_empty(&surface->feedback_list))
2217 return;
2218
2219 /* All views must have the flag for the flag to survive. */
2220 wl_list_for_each(view, &surface->views, surface_link) {
2221 /* ignore views that are not on this output at all */
2222 if (view->output_mask & (1u << output->id))
2223 flags &= view->psf_flags;
2224 }
2225
2226 wl_list_for_each(feedback, &surface->feedback_list, link)
2227 feedback->psf_flags = flags;
2228
2229 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2230 wl_list_init(&surface->feedback_list);
2231}
2232
David Herrmann1edf44c2013-10-22 17:11:26 +02002233static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002234weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002235{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002236 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002237 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002238 struct weston_animation *animation, *next;
2239 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002240 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002241 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002242 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002243
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002244 if (output->destroying)
2245 return 0;
2246
Pekka Paalanenb5026542014-11-12 15:09:24 +02002247 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2248
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002249 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002250 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002251
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002252 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002253 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002254 } else {
2255 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002256 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002257 ev->psf_flags = 0;
2258 }
2259 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002260
Pekka Paalanene67858b2013-04-25 13:57:42 +03002261 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002262 wl_list_for_each(ev, &ec->view_list, link) {
2263 /* Note: This operation is safe to do multiple times on the
2264 * same surface.
2265 */
2266 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002267 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002268 &ev->surface->frame_callback_list);
2269 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002270
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002271 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002272 }
2273 }
2274
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002275 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002276
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002277 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002278 pixman_region32_intersect(&output_damage,
2279 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002280 pixman_region32_subtract(&output_damage,
2281 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002282
Scott Moreauccbf29d2012-02-22 14:21:41 -07002283 if (output->dirty)
2284 weston_output_update_matrix(output);
2285
David Herrmann1edf44c2013-10-22 17:11:26 +02002286 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002287
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002288 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002289
Kristian Høgsbergef044142011-06-21 15:02:12 -04002290 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002291
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002292 weston_compositor_repick(ec);
2293 wl_event_loop_dispatch(ec->input_loop, 0);
2294
Jonas Ådahldb773762012-06-13 00:01:21 +02002295 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002296 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002297 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002298 }
2299
Scott Moreaud64cf212012-06-08 19:40:54 -06002300 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002301 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002302 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002303 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002304
Pekka Paalanenb5026542014-11-12 15:09:24 +02002305 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2306
David Herrmann1edf44c2013-10-22 17:11:26 +02002307 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002308}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002309
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002310static int
2311weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002312{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002313 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002314
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002315 wl_event_loop_dispatch(compositor->input_loop, 0);
2316
2317 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002318}
2319
Pekka Paalanen82919792014-05-21 13:51:49 +03002320static void
2321weston_output_schedule_repaint_reset(struct weston_output *output)
2322{
2323 struct weston_compositor *compositor = output->compositor;
2324 struct wl_event_loop *loop;
2325 int fd;
2326
2327 output->repaint_scheduled = 0;
2328 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2329
2330 if (compositor->input_loop_source)
2331 return;
2332
2333 loop = wl_display_get_event_loop(compositor->wl_display);
2334 fd = wl_event_loop_get_fd(compositor->input_loop);
2335 compositor->input_loop_source =
2336 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2337 weston_compositor_read_input, compositor);
2338}
2339
Pekka Paalanen0513a952014-05-21 16:17:27 +03002340static int
2341output_repaint_timer_handler(void *data)
2342{
2343 struct weston_output *output = data;
2344 struct weston_compositor *compositor = output->compositor;
2345
2346 if (output->repaint_needed &&
2347 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2348 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2349 weston_output_repaint(output) == 0)
2350 return 0;
2351
2352 weston_output_schedule_repaint_reset(output);
2353
2354 return 0;
2355}
2356
Kristian Høgsbergef044142011-06-21 15:02:12 -04002357WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002358weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002359 const struct timespec *stamp,
2360 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002361{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002362 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002363 int32_t refresh_nsec;
2364 struct timespec now;
2365 struct timespec gone;
2366 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002367
Pekka Paalanenb5026542014-11-12 15:09:24 +02002368 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2369 TLP_VBLANK(stamp), TLP_END);
2370
Pekka Paalanen0513a952014-05-21 16:17:27 +03002371 refresh_nsec = 1000000000000LL / output->current_mode->refresh;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002372 weston_presentation_feedback_present_list(&output->feedback_list,
2373 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002374 output->msc,
2375 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002376
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002377 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002378
Pekka Paalanen0513a952014-05-21 16:17:27 +03002379 weston_compositor_read_presentation_clock(compositor, &now);
2380 timespec_sub(&gone, &now, stamp);
2381 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2382 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002383
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002384 if (msec < -1000 || msec > 1000) {
2385 static bool warned;
2386
2387 if (!warned)
2388 weston_log("Warning: computed repaint delay is "
2389 "insane: %d msec\n", msec);
2390 warned = true;
2391
2392 msec = 0;
2393 }
2394
2395 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002396 output_repaint_timer_handler(output);
2397 else
2398 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002399}
2400
2401static void
2402idle_repaint(void *data)
2403{
2404 struct weston_output *output = data;
2405
Jonas Ådahle5a12252013-04-05 23:07:11 +02002406 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002407}
2408
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002409WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002410weston_layer_entry_insert(struct weston_layer_entry *list,
2411 struct weston_layer_entry *entry)
2412{
2413 wl_list_insert(&list->link, &entry->link);
2414 entry->layer = list->layer;
2415}
2416
2417WL_EXPORT void
2418weston_layer_entry_remove(struct weston_layer_entry *entry)
2419{
2420 wl_list_remove(&entry->link);
2421 wl_list_init(&entry->link);
2422 entry->layer = NULL;
2423}
2424
2425WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002426weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2427{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002428 wl_list_init(&layer->view_list.link);
2429 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002430 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002431 if (below != NULL)
2432 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002433}
2434
2435WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002436weston_layer_set_mask(struct weston_layer *layer,
2437 int x, int y, int width, int height)
2438{
2439 struct weston_view *view;
2440
2441 layer->mask.x1 = x;
2442 layer->mask.x2 = x + width;
2443 layer->mask.y1 = y;
2444 layer->mask.y2 = y + height;
2445
2446 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2447 weston_view_geometry_dirty(view);
2448 }
2449}
2450
2451WL_EXPORT void
2452weston_layer_set_mask_infinite(struct weston_layer *layer)
2453{
2454 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2455 UINT32_MAX, UINT32_MAX);
2456}
2457
2458WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002459weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002460{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002461 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002462 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002463
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002464 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2465 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002466 return;
2467
Pekka Paalanenb5026542014-11-12 15:09:24 +02002468 if (!output->repaint_needed)
2469 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2470
Kristian Høgsbergef044142011-06-21 15:02:12 -04002471 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002472 output->repaint_needed = 1;
2473 if (output->repaint_scheduled)
2474 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002475
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002476 wl_event_loop_add_idle(loop, idle_repaint, output);
2477 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002478 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2479
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002480
2481 if (compositor->input_loop_source) {
2482 wl_event_source_remove(compositor->input_loop_source);
2483 compositor->input_loop_source = NULL;
2484 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002485}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002486
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002487WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002488weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2489{
2490 struct weston_output *output;
2491
2492 wl_list_for_each(output, &compositor->output_list, link)
2493 weston_output_schedule_repaint(output);
2494}
2495
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002496static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002497surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002498{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002499 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002500}
2501
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002502static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002503surface_attach(struct wl_client *client,
2504 struct wl_resource *resource,
2505 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2506{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002507 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002508 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002509
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002510 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002511 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002512 if (buffer == NULL) {
2513 wl_client_post_no_memory(client);
2514 return;
2515 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002516 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002517
Pekka Paalanende685b82012-12-04 15:58:12 +02002518 /* Attach, attach, without commit in between does not send
2519 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002520 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002521
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002522 surface->pending.sx = sx;
2523 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002524 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002525}
2526
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002527static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002528surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002529 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002530 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002531{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002532 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002533
Pekka Paalanen8e159182012-10-10 12:49:25 +03002534 pixman_region32_union_rect(&surface->pending.damage,
2535 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002536 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002537}
2538
Kristian Høgsberg33418202011-08-16 23:01:28 -04002539static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002540destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002541{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002542 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002543
2544 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002545 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002546}
2547
2548static void
2549surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002550 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002551{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002552 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002553 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002554
2555 cb = malloc(sizeof *cb);
2556 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002557 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002558 return;
2559 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002560
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002561 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2562 callback);
2563 if (cb->resource == NULL) {
2564 free(cb);
2565 wl_resource_post_no_memory(resource);
2566 return;
2567 }
2568
Jason Ekstranda85118c2013-06-27 20:17:02 -05002569 wl_resource_set_implementation(cb->resource, NULL, cb,
2570 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002571
Pekka Paalanenbc106382012-10-10 12:49:31 +03002572 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002573}
2574
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002575static void
2576surface_set_opaque_region(struct wl_client *client,
2577 struct wl_resource *resource,
2578 struct wl_resource *region_resource)
2579{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002580 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002581 struct weston_region *region;
2582
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002583 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002584 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002585 pixman_region32_copy(&surface->pending.opaque,
2586 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002587 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002588 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002589 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002590}
2591
2592static void
2593surface_set_input_region(struct wl_client *client,
2594 struct wl_resource *resource,
2595 struct wl_resource *region_resource)
2596{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002597 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002598 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002599
2600 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002601 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002602 pixman_region32_copy(&surface->pending.input,
2603 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002604 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002605 pixman_region32_fini(&surface->pending.input);
2606 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002607 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002608}
2609
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002610static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002611weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002612{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002613 struct weston_subsurface *sub;
2614
2615 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2616 parent_link_pending) {
2617 wl_list_remove(&sub->parent_link);
2618 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2619 }
2620}
2621
2622static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002623weston_surface_build_buffer_matrix(struct weston_surface *surface,
2624 struct weston_matrix *matrix)
2625{
2626 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2627 double src_width, src_height, dest_width, dest_height;
2628
2629 weston_matrix_init(matrix);
2630
2631 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2632 src_width = surface->width_from_buffer;
2633 src_height = surface->height_from_buffer;
2634 } else {
2635 src_width = wl_fixed_to_double(vp->buffer.src_width);
2636 src_height = wl_fixed_to_double(vp->buffer.src_height);
2637 }
2638
2639 if (vp->surface.width == -1) {
2640 dest_width = src_width;
2641 dest_height = src_height;
2642 } else {
2643 dest_width = vp->surface.width;
2644 dest_height = vp->surface.height;
2645 }
2646
2647 if (src_width != dest_width || src_height != dest_height)
2648 weston_matrix_scale(matrix,
2649 src_width / dest_width,
2650 src_height / dest_height, 1);
2651
2652 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2653 weston_matrix_translate(matrix,
2654 wl_fixed_to_double(vp->buffer.src_x),
2655 wl_fixed_to_double(vp->buffer.src_y),
2656 0);
2657
2658 switch (vp->buffer.transform) {
2659 case WL_OUTPUT_TRANSFORM_FLIPPED:
2660 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2661 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2662 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2663 weston_matrix_scale(matrix, -1, 1, 1);
2664 weston_matrix_translate(matrix,
2665 surface->width_from_buffer, 0, 0);
2666 break;
2667 }
2668
2669 switch (vp->buffer.transform) {
2670 default:
2671 case WL_OUTPUT_TRANSFORM_NORMAL:
2672 case WL_OUTPUT_TRANSFORM_FLIPPED:
2673 break;
2674 case WL_OUTPUT_TRANSFORM_90:
2675 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2676 weston_matrix_rotate_xy(matrix, 0, 1);
2677 weston_matrix_translate(matrix,
2678 surface->height_from_buffer, 0, 0);
2679 break;
2680 case WL_OUTPUT_TRANSFORM_180:
2681 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2682 weston_matrix_rotate_xy(matrix, -1, 0);
2683 weston_matrix_translate(matrix,
2684 surface->width_from_buffer,
2685 surface->height_from_buffer, 0);
2686 break;
2687 case WL_OUTPUT_TRANSFORM_270:
2688 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2689 weston_matrix_rotate_xy(matrix, 0, -1);
2690 weston_matrix_translate(matrix,
2691 0, surface->width_from_buffer, 0);
2692 break;
2693 }
2694
2695 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2696}
2697
2698static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002699weston_surface_commit_state(struct weston_surface *surface,
2700 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002701{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002702 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002703 pixman_region32_t opaque;
2704
Alexander Larsson4ea95522013-05-22 14:41:37 +02002705 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002706 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002707 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002708 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002709
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002710 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002711 if (state->newly_attached)
2712 weston_surface_attach(surface, state->buffer);
2713 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002714
Jason Ekstrand1e059042014-10-16 10:55:19 -05002715 weston_surface_build_buffer_matrix(surface,
2716 &surface->surface_to_buffer_matrix);
2717 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2718 &surface->surface_to_buffer_matrix);
2719
Jason Ekstrand7b982072014-05-20 14:33:03 -05002720 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002721 weston_surface_update_size(surface);
2722 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002723 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002724 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002725
Jason Ekstrand7b982072014-05-20 14:33:03 -05002726 state->sx = 0;
2727 state->sy = 0;
2728 state->newly_attached = 0;
2729 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002730
2731 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002732 if (weston_timeline_enabled_ &&
2733 pixman_region32_not_empty(&state->damage))
2734 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002735 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002736 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002737 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002738 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002739 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002740
2741 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002742 pixman_region32_init(&opaque);
2743 pixman_region32_intersect_rect(&opaque, &state->opaque,
2744 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002745
2746 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2747 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002748 wl_list_for_each(view, &surface->views, surface_link)
2749 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002750 }
2751
2752 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002753
2754 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002755 pixman_region32_intersect_rect(&surface->input, &state->input,
2756 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002757
Pekka Paalanenbc106382012-10-10 12:49:31 +03002758 /* wl_surface.frame */
2759 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002760 &state->frame_callback_list);
2761 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002762
2763 /* XXX:
2764 * What should happen with a feedback request, if there
2765 * is no wl_buffer attached for this commit?
2766 */
2767
2768 /* presentation.feedback */
2769 wl_list_insert_list(&surface->feedback_list,
2770 &state->feedback_list);
2771 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002772}
2773
2774static void
2775weston_surface_commit(struct weston_surface *surface)
2776{
2777 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002778
Pekka Paalanene67858b2013-04-25 13:57:42 +03002779 weston_surface_commit_subsurface_order(surface);
2780
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002781 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002782}
2783
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002784static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002785weston_subsurface_commit(struct weston_subsurface *sub);
2786
2787static void
2788weston_subsurface_parent_commit(struct weston_subsurface *sub,
2789 int parent_is_synchronized);
2790
2791static void
2792surface_commit(struct wl_client *client, struct wl_resource *resource)
2793{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002794 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002795 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2796
2797 if (sub) {
2798 weston_subsurface_commit(sub);
2799 return;
2800 }
2801
2802 weston_surface_commit(surface);
2803
2804 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2805 if (sub->surface != surface)
2806 weston_subsurface_parent_commit(sub, 0);
2807 }
2808}
2809
2810static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002811surface_set_buffer_transform(struct wl_client *client,
2812 struct wl_resource *resource, int transform)
2813{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002814 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002815
Jonny Lamba55f1392014-05-30 12:07:15 +02002816 /* if wl_output.transform grows more members this will need to be updated. */
2817 if (transform < 0 ||
2818 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2819 wl_resource_post_error(resource,
2820 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2821 "buffer transform must be a valid transform "
2822 "('%d' specified)", transform);
2823 return;
2824 }
2825
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002826 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002827 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002828}
2829
Alexander Larsson4ea95522013-05-22 14:41:37 +02002830static void
2831surface_set_buffer_scale(struct wl_client *client,
2832 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002833 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002834{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002835 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002836
Jonny Lamba55f1392014-05-30 12:07:15 +02002837 if (scale < 1) {
2838 wl_resource_post_error(resource,
2839 WL_SURFACE_ERROR_INVALID_SCALE,
2840 "buffer scale must be at least one "
2841 "('%d' specified)", scale);
2842 return;
2843 }
2844
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002845 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002846 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002847}
2848
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002849static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002850 surface_destroy,
2851 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002852 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002853 surface_frame,
2854 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002855 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002856 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002857 surface_set_buffer_transform,
2858 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002859};
2860
2861static void
2862compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002863 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002864{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002865 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002866 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002867
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002868 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002869 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002870 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002871 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002872 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002873
Jason Ekstranda85118c2013-06-27 20:17:02 -05002874 surface->resource =
2875 wl_resource_create(client, &wl_surface_interface,
2876 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002877 if (surface->resource == NULL) {
2878 weston_surface_destroy(surface);
2879 wl_resource_post_no_memory(resource);
2880 return;
2881 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002882 wl_resource_set_implementation(surface->resource, &surface_interface,
2883 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002884
2885 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002886}
2887
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002888static void
2889destroy_region(struct wl_resource *resource)
2890{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002891 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002892
2893 pixman_region32_fini(&region->region);
2894 free(region);
2895}
2896
2897static void
2898region_destroy(struct wl_client *client, struct wl_resource *resource)
2899{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002900 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002901}
2902
2903static void
2904region_add(struct wl_client *client, struct wl_resource *resource,
2905 int32_t x, int32_t y, int32_t width, int32_t height)
2906{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002907 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002908
2909 pixman_region32_union_rect(&region->region, &region->region,
2910 x, y, width, height);
2911}
2912
2913static void
2914region_subtract(struct wl_client *client, struct wl_resource *resource,
2915 int32_t x, int32_t y, int32_t width, int32_t height)
2916{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002917 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002918 pixman_region32_t rect;
2919
2920 pixman_region32_init_rect(&rect, x, y, width, height);
2921 pixman_region32_subtract(&region->region, &region->region, &rect);
2922 pixman_region32_fini(&rect);
2923}
2924
2925static const struct wl_region_interface region_interface = {
2926 region_destroy,
2927 region_add,
2928 region_subtract
2929};
2930
2931static void
2932compositor_create_region(struct wl_client *client,
2933 struct wl_resource *resource, uint32_t id)
2934{
2935 struct weston_region *region;
2936
2937 region = malloc(sizeof *region);
2938 if (region == NULL) {
2939 wl_resource_post_no_memory(resource);
2940 return;
2941 }
2942
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002943 pixman_region32_init(&region->region);
2944
Jason Ekstranda85118c2013-06-27 20:17:02 -05002945 region->resource =
2946 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002947 if (region->resource == NULL) {
2948 free(region);
2949 wl_resource_post_no_memory(resource);
2950 return;
2951 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002952 wl_resource_set_implementation(region->resource, &region_interface,
2953 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002954}
2955
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002956static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002957 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002958 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002959};
2960
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002961static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002962weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2963{
2964 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002965
Jason Ekstrand7b982072014-05-20 14:33:03 -05002966 weston_surface_commit_state(surface, &sub->cached);
2967 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002968
2969 weston_surface_commit_subsurface_order(surface);
2970
2971 weston_surface_schedule_repaint(surface);
2972
Jason Ekstrand7b982072014-05-20 14:33:03 -05002973 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002974}
2975
2976static void
2977weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2978{
2979 struct weston_surface *surface = sub->surface;
2980
2981 /*
2982 * If this commit would cause the surface to move by the
2983 * attach(dx, dy) parameters, the old damage region must be
2984 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002985 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002986 */
2987 pixman_region32_translate(&sub->cached.damage,
2988 -surface->pending.sx, -surface->pending.sy);
2989 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2990 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002991 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002992
2993 if (surface->pending.newly_attached) {
2994 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002995 weston_surface_state_set_buffer(&sub->cached,
2996 surface->pending.buffer);
2997 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002998 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002999 weston_presentation_feedback_discard_list(
3000 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003001 }
3002 sub->cached.sx += surface->pending.sx;
3003 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003004
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003005 sub->cached.buffer_viewport.changed |=
3006 surface->pending.buffer_viewport.changed;
3007 sub->cached.buffer_viewport.buffer =
3008 surface->pending.buffer_viewport.buffer;
3009 sub->cached.buffer_viewport.surface =
3010 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003011
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003012 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003013
3014 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3015
3016 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3017
3018 wl_list_insert_list(&sub->cached.frame_callback_list,
3019 &surface->pending.frame_callback_list);
3020 wl_list_init(&surface->pending.frame_callback_list);
3021
Pekka Paalanen133e4392014-09-23 22:08:46 -04003022 wl_list_insert_list(&sub->cached.feedback_list,
3023 &surface->pending.feedback_list);
3024 wl_list_init(&surface->pending.feedback_list);
3025
Jason Ekstrand7b982072014-05-20 14:33:03 -05003026 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003027}
3028
Derek Foreman280e7dd2014-10-03 13:13:42 -05003029static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003030weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3031{
3032 while (sub) {
3033 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003034 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003035
3036 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003037 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003038
3039 sub = weston_surface_to_subsurface(sub->parent);
3040 }
3041
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003042 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003043}
3044
3045static void
3046weston_subsurface_commit(struct weston_subsurface *sub)
3047{
3048 struct weston_surface *surface = sub->surface;
3049 struct weston_subsurface *tmp;
3050
3051 /* Recursive check for effectively synchronized. */
3052 if (weston_subsurface_is_synchronized(sub)) {
3053 weston_subsurface_commit_to_cache(sub);
3054 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003055 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003056 /* flush accumulated state from cache */
3057 weston_subsurface_commit_to_cache(sub);
3058 weston_subsurface_commit_from_cache(sub);
3059 } else {
3060 weston_surface_commit(surface);
3061 }
3062
3063 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3064 if (tmp->surface != surface)
3065 weston_subsurface_parent_commit(tmp, 0);
3066 }
3067 }
3068}
3069
3070static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003071weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003072{
3073 struct weston_surface *surface = sub->surface;
3074 struct weston_subsurface *tmp;
3075
Pekka Paalanene67858b2013-04-25 13:57:42 +03003076 /* From now on, commit_from_cache the whole sub-tree, regardless of
3077 * the synchronized mode of each child. This sub-surface or some
3078 * of its ancestors were synchronized, so we are synchronized
3079 * all the way down.
3080 */
3081
Jason Ekstrand7b982072014-05-20 14:33:03 -05003082 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003083 weston_subsurface_commit_from_cache(sub);
3084
3085 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3086 if (tmp->surface != surface)
3087 weston_subsurface_parent_commit(tmp, 1);
3088 }
3089}
3090
3091static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003092weston_subsurface_parent_commit(struct weston_subsurface *sub,
3093 int parent_is_synchronized)
3094{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003095 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003096 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003097 wl_list_for_each(view, &sub->surface->views, surface_link)
3098 weston_view_set_position(view,
3099 sub->position.x,
3100 sub->position.y);
3101
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003102 sub->position.set = 0;
3103 }
3104
3105 if (parent_is_synchronized || sub->synchronized)
3106 weston_subsurface_synchronized_commit(sub);
3107}
3108
Pekka Paalanen8274d902014-08-06 19:36:51 +03003109static int
3110subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3111{
3112 return snprintf(buf, len, "sub-surface");
3113}
3114
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003115static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003116subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003117{
3118 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003119 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003120
Jason Ekstranda7af7042013-10-12 22:38:11 -05003121 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003122 weston_view_set_position(view,
3123 view->geometry.x + dx,
3124 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003125
3126 /* No need to check parent mappedness, because if parent is not
3127 * mapped, parent is not in a visible layer, so this sub-surface
3128 * will not be drawn either.
3129 */
3130 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003131 struct weston_output *output;
3132
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003133 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003134 * because that would call it also for the parent surface,
3135 * which might not be mapped yet. That would lead to
3136 * inconsistent state, where the window could never be
3137 * mapped.
3138 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003139 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003140 * weston_surface_is_mapped() return true, so that when the
3141 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003142 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003143 */
3144 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003145 output = container_of(compositor->output_list.next,
3146 struct weston_output, link);
3147
3148 surface->output = output;
3149 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003150 }
3151}
3152
3153static struct weston_subsurface *
3154weston_surface_to_subsurface(struct weston_surface *surface)
3155{
3156 if (surface->configure == subsurface_configure)
3157 return surface->configure_private;
3158
3159 return NULL;
3160}
3161
Pekka Paalanen01388e22013-04-25 13:57:44 +03003162WL_EXPORT struct weston_surface *
3163weston_surface_get_main_surface(struct weston_surface *surface)
3164{
3165 struct weston_subsurface *sub;
3166
3167 while (surface && (sub = weston_surface_to_subsurface(surface)))
3168 surface = sub->parent;
3169
3170 return surface;
3171}
3172
Pekka Paalanen50b67472014-10-01 15:02:41 +03003173WL_EXPORT int
3174weston_surface_set_role(struct weston_surface *surface,
3175 const char *role_name,
3176 struct wl_resource *error_resource,
3177 uint32_t error_code)
3178{
3179 assert(role_name);
3180
3181 if (surface->role_name == NULL ||
3182 surface->role_name == role_name ||
3183 strcmp(surface->role_name, role_name) == 0) {
3184 surface->role_name = role_name;
3185
3186 return 0;
3187 }
3188
3189 wl_resource_post_error(error_resource, error_code,
3190 "Cannot assign role %s to wl_surface@%d,"
3191 " already has role %s\n",
3192 role_name,
3193 wl_resource_get_id(surface->resource),
3194 surface->role_name);
3195 return -1;
3196}
3197
Pekka Paalanen8274d902014-08-06 19:36:51 +03003198WL_EXPORT void
3199weston_surface_set_label_func(struct weston_surface *surface,
3200 int (*desc)(struct weston_surface *,
3201 char *, size_t))
3202{
3203 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003204 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003205}
3206
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003207/** Get the size of surface contents
3208 *
3209 * \param surface The surface to query.
3210 * \param width Returns the width of raw contents.
3211 * \param height Returns the height of raw contents.
3212 *
3213 * Retrieves the raw surface content size in pixels for the given surface.
3214 * This is the whole content size in buffer pixels. If the surface
3215 * has no content or the renderer does not implement this feature,
3216 * zeroes are returned.
3217 *
3218 * This function is used to determine the buffer size needed for
3219 * a weston_surface_copy_content() call.
3220 */
3221WL_EXPORT void
3222weston_surface_get_content_size(struct weston_surface *surface,
3223 int *width, int *height)
3224{
3225 struct weston_renderer *rer = surface->compositor->renderer;
3226
3227 if (!rer->surface_get_content_size) {
3228 *width = 0;
3229 *height = 0;
3230 return;
3231 }
3232
3233 rer->surface_get_content_size(surface, width, height);
3234}
3235
3236/** Copy surface contents to system memory.
3237 *
3238 * \param surface The surface to copy from.
3239 * \param target Pointer to the target memory buffer.
3240 * \param size Size of the target buffer in bytes.
3241 * \param src_x X location on contents to copy from.
3242 * \param src_y Y location on contents to copy from.
3243 * \param width Width in pixels of the area to copy.
3244 * \param height Height in pixels of the area to copy.
3245 * \return 0 for success, -1 for failure.
3246 *
3247 * Surface contents are maintained by the renderer. They can be in a
3248 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3249 * else.
3250 *
3251 * Surface contents are copied into memory pointed to by target,
3252 * which has size bytes of space available. The target memory
3253 * may be larger than needed, but being smaller returns an error.
3254 * The extra bytes in target may or may not be written; their content is
3255 * unspecified. Size must be large enough to hold the image.
3256 *
3257 * The image in the target memory will be arranged in rows from
3258 * top to bottom, and pixels on a row from left to right. The pixel
3259 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3260 * width * 4.
3261 *
3262 * Parameters src_x and src_y define the upper-left corner in buffer
3263 * coordinates (pixels) to copy from. Parameters width and height
3264 * define the size of the area to copy in pixels.
3265 *
3266 * The rectangle defined by src_x, src_y, width, height must fit in
3267 * the surface contents. Otherwise an error is returned.
3268 *
3269 * Use surface_get_data_size to determine the content size; the
3270 * needed target buffer size and rectangle limits.
3271 *
3272 * CURRENT IMPLEMENTATION RESTRICTIONS:
3273 * - the machine must be little-endian due to Pixman formats.
3274 *
3275 * NOTE: Pixman formats are premultiplied.
3276 */
3277WL_EXPORT int
3278weston_surface_copy_content(struct weston_surface *surface,
3279 void *target, size_t size,
3280 int src_x, int src_y,
3281 int width, int height)
3282{
3283 struct weston_renderer *rer = surface->compositor->renderer;
3284 int cw, ch;
3285 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3286
3287 if (!rer->surface_copy_content)
3288 return -1;
3289
3290 weston_surface_get_content_size(surface, &cw, &ch);
3291
3292 if (src_x < 0 || src_y < 0)
3293 return -1;
3294
3295 if (width <= 0 || height <= 0)
3296 return -1;
3297
3298 if (src_x + width > cw || src_y + height > ch)
3299 return -1;
3300
3301 if (width * bytespp * height > size)
3302 return -1;
3303
3304 return rer->surface_copy_content(surface, target, size,
3305 src_x, src_y, width, height);
3306}
3307
Pekka Paalanene67858b2013-04-25 13:57:42 +03003308static void
3309subsurface_set_position(struct wl_client *client,
3310 struct wl_resource *resource, int32_t x, int32_t y)
3311{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003312 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003313
3314 if (!sub)
3315 return;
3316
3317 sub->position.x = x;
3318 sub->position.y = y;
3319 sub->position.set = 1;
3320}
3321
3322static struct weston_subsurface *
3323subsurface_from_surface(struct weston_surface *surface)
3324{
3325 struct weston_subsurface *sub;
3326
3327 sub = weston_surface_to_subsurface(surface);
3328 if (sub)
3329 return sub;
3330
3331 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3332 if (sub->surface == surface)
3333 return sub;
3334
3335 return NULL;
3336}
3337
3338static struct weston_subsurface *
3339subsurface_sibling_check(struct weston_subsurface *sub,
3340 struct weston_surface *surface,
3341 const char *request)
3342{
3343 struct weston_subsurface *sibling;
3344
3345 sibling = subsurface_from_surface(surface);
3346
3347 if (!sibling) {
3348 wl_resource_post_error(sub->resource,
3349 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3350 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003351 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003352 return NULL;
3353 }
3354
3355 if (sibling->parent != sub->parent) {
3356 wl_resource_post_error(sub->resource,
3357 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3358 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003359 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003360 return NULL;
3361 }
3362
3363 return sibling;
3364}
3365
3366static void
3367subsurface_place_above(struct wl_client *client,
3368 struct wl_resource *resource,
3369 struct wl_resource *sibling_resource)
3370{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003371 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003372 struct weston_surface *surface =
3373 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003374 struct weston_subsurface *sibling;
3375
3376 if (!sub)
3377 return;
3378
3379 sibling = subsurface_sibling_check(sub, surface, "place_above");
3380 if (!sibling)
3381 return;
3382
3383 wl_list_remove(&sub->parent_link_pending);
3384 wl_list_insert(sibling->parent_link_pending.prev,
3385 &sub->parent_link_pending);
3386}
3387
3388static void
3389subsurface_place_below(struct wl_client *client,
3390 struct wl_resource *resource,
3391 struct wl_resource *sibling_resource)
3392{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003393 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003394 struct weston_surface *surface =
3395 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003396 struct weston_subsurface *sibling;
3397
3398 if (!sub)
3399 return;
3400
3401 sibling = subsurface_sibling_check(sub, surface, "place_below");
3402 if (!sibling)
3403 return;
3404
3405 wl_list_remove(&sub->parent_link_pending);
3406 wl_list_insert(&sibling->parent_link_pending,
3407 &sub->parent_link_pending);
3408}
3409
3410static void
3411subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3412{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003413 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003414
3415 if (sub)
3416 sub->synchronized = 1;
3417}
3418
3419static void
3420subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3421{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003422 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003423
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003424 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003425 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003426
3427 /* If sub became effectively desynchronized, flush. */
3428 if (!weston_subsurface_is_synchronized(sub))
3429 weston_subsurface_synchronized_commit(sub);
3430 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003431}
3432
3433static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003434weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3435{
3436 wl_list_remove(&sub->parent_link);
3437 wl_list_remove(&sub->parent_link_pending);
3438 wl_list_remove(&sub->parent_destroy_listener.link);
3439 sub->parent = NULL;
3440}
3441
3442static void
3443weston_subsurface_destroy(struct weston_subsurface *sub);
3444
3445static void
3446subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3447{
3448 struct weston_subsurface *sub =
3449 container_of(listener, struct weston_subsurface,
3450 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003451 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003452
3453 /* The protocol object (wl_resource) is left inert. */
3454 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003455 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003456
3457 weston_subsurface_destroy(sub);
3458}
3459
3460static void
3461subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3462{
3463 struct weston_subsurface *sub =
3464 container_of(listener, struct weston_subsurface,
3465 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003466 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003467 assert(sub->surface != sub->parent);
3468
3469 if (weston_surface_is_mapped(sub->surface))
3470 weston_surface_unmap(sub->surface);
3471
3472 weston_subsurface_unlink_parent(sub);
3473}
3474
3475static void
3476subsurface_resource_destroy(struct wl_resource *resource)
3477{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003478 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003479
3480 if (sub)
3481 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003482}
3483
3484static void
3485subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3486{
3487 wl_resource_destroy(resource);
3488}
3489
3490static void
3491weston_subsurface_link_parent(struct weston_subsurface *sub,
3492 struct weston_surface *parent)
3493{
3494 sub->parent = parent;
3495 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003496 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003497 &sub->parent_destroy_listener);
3498
3499 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3500 wl_list_insert(&parent->subsurface_list_pending,
3501 &sub->parent_link_pending);
3502}
3503
3504static void
3505weston_subsurface_link_surface(struct weston_subsurface *sub,
3506 struct weston_surface *surface)
3507{
3508 sub->surface = surface;
3509 sub->surface_destroy_listener.notify =
3510 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003511 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003512 &sub->surface_destroy_listener);
3513}
3514
3515static void
3516weston_subsurface_destroy(struct weston_subsurface *sub)
3517{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003518 struct weston_view *view, *next;
3519
Pekka Paalanene67858b2013-04-25 13:57:42 +03003520 assert(sub->surface);
3521
3522 if (sub->resource) {
3523 assert(weston_surface_to_subsurface(sub->surface) == sub);
3524 assert(sub->parent_destroy_listener.notify ==
3525 subsurface_handle_parent_destroy);
3526
George Kiagiadakised04d382014-06-13 18:10:26 +02003527 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3528 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003529 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003530 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003531
Pekka Paalanene67858b2013-04-25 13:57:42 +03003532 if (sub->parent)
3533 weston_subsurface_unlink_parent(sub);
3534
Jason Ekstrand7b982072014-05-20 14:33:03 -05003535 weston_surface_state_fini(&sub->cached);
3536 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003537
3538 sub->surface->configure = NULL;
3539 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003540 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003541 } else {
3542 /* the dummy weston_subsurface for the parent itself */
3543 assert(sub->parent_destroy_listener.notify == NULL);
3544 wl_list_remove(&sub->parent_link);
3545 wl_list_remove(&sub->parent_link_pending);
3546 }
3547
3548 wl_list_remove(&sub->surface_destroy_listener.link);
3549 free(sub);
3550}
3551
3552static const struct wl_subsurface_interface subsurface_implementation = {
3553 subsurface_destroy,
3554 subsurface_set_position,
3555 subsurface_place_above,
3556 subsurface_place_below,
3557 subsurface_set_sync,
3558 subsurface_set_desync
3559};
3560
3561static struct weston_subsurface *
3562weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3563 struct weston_surface *parent)
3564{
3565 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003566 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003567
Bryce Harringtonde16d892014-11-20 22:21:57 -08003568 sub = zalloc(sizeof *sub);
3569 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003570 return NULL;
3571
Jason Ekstranda7af7042013-10-12 22:38:11 -05003572 wl_list_init(&sub->unused_views);
3573
Jason Ekstranda85118c2013-06-27 20:17:02 -05003574 sub->resource =
3575 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003576 if (!sub->resource) {
3577 free(sub);
3578 return NULL;
3579 }
3580
Jason Ekstranda85118c2013-06-27 20:17:02 -05003581 wl_resource_set_implementation(sub->resource,
3582 &subsurface_implementation,
3583 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003584 weston_subsurface_link_surface(sub, surface);
3585 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003586 weston_surface_state_init(&sub->cached);
3587 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003588 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003589
3590 return sub;
3591}
3592
3593/* Create a dummy subsurface for having the parent itself in its
3594 * sub-surface lists. Makes stacking order manipulation easy.
3595 */
3596static struct weston_subsurface *
3597weston_subsurface_create_for_parent(struct weston_surface *parent)
3598{
3599 struct weston_subsurface *sub;
3600
Bryce Harringtonde16d892014-11-20 22:21:57 -08003601 sub = zalloc(sizeof *sub);
3602 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003603 return NULL;
3604
3605 weston_subsurface_link_surface(sub, parent);
3606 sub->parent = parent;
3607 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3608 wl_list_insert(&parent->subsurface_list_pending,
3609 &sub->parent_link_pending);
3610
3611 return sub;
3612}
3613
3614static void
3615subcompositor_get_subsurface(struct wl_client *client,
3616 struct wl_resource *resource,
3617 uint32_t id,
3618 struct wl_resource *surface_resource,
3619 struct wl_resource *parent_resource)
3620{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003621 struct weston_surface *surface =
3622 wl_resource_get_user_data(surface_resource);
3623 struct weston_surface *parent =
3624 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003625 struct weston_subsurface *sub;
3626 static const char where[] = "get_subsurface: wl_subsurface@";
3627
3628 if (surface == parent) {
3629 wl_resource_post_error(resource,
3630 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3631 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003632 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003633 return;
3634 }
3635
3636 if (weston_surface_to_subsurface(surface)) {
3637 wl_resource_post_error(resource,
3638 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3639 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003640 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003641 return;
3642 }
3643
Pekka Paalanen50b67472014-10-01 15:02:41 +03003644 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3645 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003646 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003647
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003648 if (weston_surface_get_main_surface(parent) == surface) {
3649 wl_resource_post_error(resource,
3650 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3651 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003652 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003653 return;
3654 }
3655
Pekka Paalanene67858b2013-04-25 13:57:42 +03003656 /* make sure the parent is in its own list */
3657 if (wl_list_empty(&parent->subsurface_list)) {
3658 if (!weston_subsurface_create_for_parent(parent)) {
3659 wl_resource_post_no_memory(resource);
3660 return;
3661 }
3662 }
3663
3664 sub = weston_subsurface_create(id, surface, parent);
3665 if (!sub) {
3666 wl_resource_post_no_memory(resource);
3667 return;
3668 }
3669
3670 surface->configure = subsurface_configure;
3671 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003672 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003673}
3674
3675static void
3676subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3677{
3678 wl_resource_destroy(resource);
3679}
3680
3681static const struct wl_subcompositor_interface subcompositor_interface = {
3682 subcompositor_destroy,
3683 subcompositor_get_subsurface
3684};
3685
3686static void
3687bind_subcompositor(struct wl_client *client,
3688 void *data, uint32_t version, uint32_t id)
3689{
3690 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003691 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003692
Jason Ekstranda85118c2013-06-27 20:17:02 -05003693 resource =
3694 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003695 if (resource == NULL) {
3696 wl_client_post_no_memory(client);
3697 return;
3698 }
3699 wl_resource_set_implementation(resource, &subcompositor_interface,
3700 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003701}
3702
3703static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003704weston_compositor_dpms(struct weston_compositor *compositor,
3705 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003706{
3707 struct weston_output *output;
3708
3709 wl_list_for_each(output, &compositor->output_list, link)
3710 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003711 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003712}
3713
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003714WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003715weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003716{
Neil Roberts8b62e202013-09-30 13:14:47 +01003717 uint32_t old_state = compositor->state;
3718
3719 /* The state needs to be changed before emitting the wake
3720 * signal because that may try to schedule a repaint which
3721 * will not work if the compositor is still sleeping */
3722 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3723
3724 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003725 case WESTON_COMPOSITOR_SLEEPING:
3726 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3727 /* fall through */
3728 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003729 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003730 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003731 /* fall through */
3732 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003733 wl_event_source_timer_update(compositor->idle_source,
3734 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003735 }
3736}
3737
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003738WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003739weston_compositor_offscreen(struct weston_compositor *compositor)
3740{
3741 switch (compositor->state) {
3742 case WESTON_COMPOSITOR_OFFSCREEN:
3743 return;
3744 case WESTON_COMPOSITOR_SLEEPING:
3745 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3746 /* fall through */
3747 default:
3748 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3749 wl_event_source_timer_update(compositor->idle_source, 0);
3750 }
3751}
3752
3753WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003754weston_compositor_sleep(struct weston_compositor *compositor)
3755{
3756 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3757 return;
3758
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003759 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003760 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3761 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3762}
3763
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003764static int
3765idle_handler(void *data)
3766{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003767 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003768
3769 if (compositor->idle_inhibit)
3770 return 1;
3771
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003772 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003773 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003774
3775 return 1;
3776}
3777
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003778WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003779weston_plane_init(struct weston_plane *plane,
3780 struct weston_compositor *ec,
3781 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003782{
3783 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003784 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003785 plane->x = x;
3786 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003787 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003788
3789 /* Init the link so that the call to wl_list_remove() when releasing
3790 * the plane without ever stacking doesn't lead to a crash */
3791 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003792}
3793
3794WL_EXPORT void
3795weston_plane_release(struct weston_plane *plane)
3796{
Xiong Zhang97116532013-10-23 13:58:31 +08003797 struct weston_view *view;
3798
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003799 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003800 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003801
Xiong Zhang97116532013-10-23 13:58:31 +08003802 wl_list_for_each(view, &plane->compositor->view_list, link) {
3803 if (view->plane == plane)
3804 view->plane = NULL;
3805 }
3806
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003807 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003808}
3809
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003810WL_EXPORT void
3811weston_compositor_stack_plane(struct weston_compositor *ec,
3812 struct weston_plane *plane,
3813 struct weston_plane *above)
3814{
3815 if (above)
3816 wl_list_insert(above->link.prev, &plane->link);
3817 else
3818 wl_list_insert(&ec->plane_list, &plane->link);
3819}
3820
Casey Dahlin9074db52012-04-19 22:50:09 -04003821static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003822{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003823 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003824}
3825
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003826static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003827bind_output(struct wl_client *client,
3828 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003829{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003830 struct weston_output *output = data;
3831 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003832 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003833
Jason Ekstranda85118c2013-06-27 20:17:02 -05003834 resource = wl_resource_create(client, &wl_output_interface,
3835 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003836 if (resource == NULL) {
3837 wl_client_post_no_memory(client);
3838 return;
3839 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003840
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003841 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003842 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003843
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003844 wl_output_send_geometry(resource,
3845 output->x,
3846 output->y,
3847 output->mm_width,
3848 output->mm_height,
3849 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003850 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003851 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003852 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003853 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003854 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003855
3856 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003857 wl_output_send_mode(resource,
3858 mode->flags,
3859 mode->width,
3860 mode->height,
3861 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003862 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003863
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003864 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003865 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003866}
3867
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003868/* Move other outputs when one is removed so the space remains contiguos. */
3869static void
3870weston_compositor_remove_output(struct weston_compositor *compositor,
3871 struct weston_output *remove_output)
3872{
3873 struct weston_output *output;
3874 int offset = 0;
3875
3876 wl_list_for_each(output, &compositor->output_list, link) {
3877 if (output == remove_output) {
3878 offset = output->width;
3879 continue;
3880 }
3881
3882 if (offset > 0) {
3883 weston_output_move(output,
3884 output->x - offset, output->y);
3885 output->dirty = 1;
3886 }
3887 }
3888}
3889
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003890WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003891weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003892{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003893 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003894 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003895
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003896 output->destroying = 1;
3897
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003898 wl_list_for_each(view, &output->compositor->view_list, link) {
3899 if (view->output_mask & (1 << output->id))
3900 weston_view_assign_output(view);
3901 }
3902
Pekka Paalanen0513a952014-05-21 16:17:27 +03003903 wl_event_source_remove(output->repaint_timer);
3904
Pekka Paalanen133e4392014-09-23 22:08:46 -04003905 weston_presentation_feedback_discard_list(&output->feedback_list);
3906
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003907 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003908 wl_list_remove(&output->link);
3909
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003910 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003911 wl_signal_emit(&output->destroy_signal, output);
3912
Richard Hughesafe690c2013-05-02 10:10:04 +01003913 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003914 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003915 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003916 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003917
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003918 wl_resource_for_each(resource, &output->resource_list) {
3919 wl_resource_set_destructor(resource, NULL);
3920 }
3921
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003922 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003923}
3924
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003925WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003926weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003927{
Scott Moreau850ca422012-05-21 15:21:25 -06003928 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003929
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003930 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003931 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003932
Scott Moreauccbf29d2012-02-22 14:21:41 -07003933 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003934 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003935 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003936 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003937 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003938 weston_matrix_scale(&output->matrix, magnification,
3939 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003940 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003941
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003942 switch (output->transform) {
3943 case WL_OUTPUT_TRANSFORM_FLIPPED:
3944 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3945 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3946 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3947 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3948 weston_matrix_scale(&output->matrix, -1, 1, 1);
3949 break;
3950 }
3951
3952 switch (output->transform) {
3953 default:
3954 case WL_OUTPUT_TRANSFORM_NORMAL:
3955 case WL_OUTPUT_TRANSFORM_FLIPPED:
3956 break;
3957 case WL_OUTPUT_TRANSFORM_90:
3958 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3959 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
3960 weston_matrix_rotate_xy(&output->matrix, 0, 1);
3961 break;
3962 case WL_OUTPUT_TRANSFORM_180:
3963 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3964 weston_matrix_translate(&output->matrix,
3965 -output->width, -output->height, 0);
3966 weston_matrix_rotate_xy(&output->matrix, -1, 0);
3967 break;
3968 case WL_OUTPUT_TRANSFORM_270:
3969 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3970 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3971 weston_matrix_rotate_xy(&output->matrix, 0, -1);
3972 break;
3973 }
3974
3975 if (output->current_scale != 1)
3976 weston_matrix_scale(&output->matrix,
3977 output->current_scale,
3978 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003979
Scott Moreauccbf29d2012-02-22 14:21:41 -07003980 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05003981
3982 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003983}
3984
Scott Moreau1bad5db2012-08-18 01:04:05 -06003985static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003986weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003987{
3988 output->transform = transform;
3989
3990 switch (transform) {
3991 case WL_OUTPUT_TRANSFORM_90:
3992 case WL_OUTPUT_TRANSFORM_270:
3993 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3994 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3995 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003996 output->width = output->current_mode->height;
3997 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003998 break;
3999 case WL_OUTPUT_TRANSFORM_NORMAL:
4000 case WL_OUTPUT_TRANSFORM_180:
4001 case WL_OUTPUT_TRANSFORM_FLIPPED:
4002 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02004003 output->width = output->current_mode->width;
4004 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004005 break;
4006 default:
4007 break;
4008 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06004009
Hardening57388e42013-09-18 23:56:36 +02004010 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02004011 output->width /= scale;
4012 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004013}
4014
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004015static void
4016weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004017{
4018 output->x = x;
4019 output->y = y;
4020
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004021 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004022 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004023 output->width,
4024 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004025}
4026
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004027WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004028weston_output_move(struct weston_output *output, int x, int y)
4029{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004030 struct wl_resource *resource;
4031
4032 output->move_x = x - output->x;
4033 output->move_y = y - output->y;
4034
4035 if (output->move_x == 0 && output->move_y == 0)
4036 return;
4037
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004038 weston_output_init_geometry(output, x, y);
4039
4040 output->dirty = 1;
4041
4042 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004043 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004044
4045 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004046 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004047 wl_output_send_geometry(resource,
4048 output->x,
4049 output->y,
4050 output->mm_width,
4051 output->mm_height,
4052 output->subpixel,
4053 output->make,
4054 output->model,
4055 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004056
4057 if (wl_resource_get_version(resource) >= 2)
4058 wl_output_send_done(resource);
4059 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004060}
4061
4062WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004063weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004064 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004065 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004066{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004067 struct wl_event_loop *loop;
4068
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004069 output->compositor = c;
4070 output->x = x;
4071 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004072 output->mm_width = mm_width;
4073 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004074 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004075 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004076
Alexander Larsson0b135062013-05-28 16:23:36 +02004077 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004078 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004079
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004080 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004081 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004082
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004083 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004084 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004085 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004086 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004087 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02004088
Pekka Paalanen0513a952014-05-21 16:17:27 +03004089 loop = wl_display_get_event_loop(c->wl_display);
4090 output->repaint_timer = wl_event_loop_add_timer(loop,
4091 output_repaint_timer_handler, output);
4092
Casey Dahlin58ba1372012-04-19 22:50:08 -04004093 output->id = ffs(~output->compositor->output_id_pool) - 1;
4094 output->compositor->output_id_pool |= 1 << output->id;
4095
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004096 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004097 wl_global_create(c->wl_display, &wl_output_interface, 2,
4098 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004099}
4100
4101/** Adds an output to the compositor's output list and
4102 * send the compositor's output_created signal.
4103 *
4104 * \param compositor The compositor instance.
4105 * \param output The output to be added.
4106 */
4107WL_EXPORT void
4108weston_compositor_add_output(struct weston_compositor *compositor,
4109 struct weston_output *output)
4110{
4111 wl_list_insert(compositor->output_list.prev, &output->link);
4112 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004113}
4114
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004115WL_EXPORT void
4116weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004117 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004118 wl_fixed_t *x, wl_fixed_t *y)
4119{
Derek Foreman0f679412014-10-02 13:41:17 -05004120 struct weston_vector p = { {
4121 wl_fixed_to_double(device_x),
4122 wl_fixed_to_double(device_y),
4123 0.0,
4124 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004125
Derek Foreman67a18b92015-03-24 11:36:14 -05004126 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004127
Derek Foreman0f679412014-10-02 13:41:17 -05004128 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4129 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004130}
4131
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004132static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004133destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004134{
Jonny Lamb74130762013-11-26 18:19:46 +01004135 struct weston_surface *surface =
4136 wl_resource_get_user_data(resource);
4137
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004138 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004139 surface->pending.buffer_viewport.buffer.src_width =
4140 wl_fixed_from_int(-1);
4141 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004142 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004143}
4144
4145static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004146viewport_destroy(struct wl_client *client,
4147 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004148{
4149 wl_resource_destroy(resource);
4150}
4151
4152static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004153viewport_set(struct wl_client *client,
4154 struct wl_resource *resource,
4155 wl_fixed_t src_x,
4156 wl_fixed_t src_y,
4157 wl_fixed_t src_width,
4158 wl_fixed_t src_height,
4159 int32_t dst_width,
4160 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004161{
Jonny Lamb74130762013-11-26 18:19:46 +01004162 struct weston_surface *surface =
4163 wl_resource_get_user_data(resource);
4164
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004165 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004166
Jonny Lamb8ae35902013-11-26 18:19:45 +01004167 if (wl_fixed_to_double(src_width) < 0 ||
4168 wl_fixed_to_double(src_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 "source dimensions must be non-negative (%fx%f)",
4172 wl_fixed_to_double(src_width),
4173 wl_fixed_to_double(src_height));
4174 return;
4175 }
4176
4177 if (dst_width <= 0 || dst_height <= 0) {
4178 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004179 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004180 "destination dimensions must be positive (%dx%d)",
4181 dst_width, dst_height);
4182 return;
4183 }
4184
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004185 surface->pending.buffer_viewport.buffer.src_x = src_x;
4186 surface->pending.buffer_viewport.buffer.src_y = src_y;
4187 surface->pending.buffer_viewport.buffer.src_width = src_width;
4188 surface->pending.buffer_viewport.buffer.src_height = src_height;
4189 surface->pending.buffer_viewport.surface.width = dst_width;
4190 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004191 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004192}
4193
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004194static void
4195viewport_set_source(struct wl_client *client,
4196 struct wl_resource *resource,
4197 wl_fixed_t src_x,
4198 wl_fixed_t src_y,
4199 wl_fixed_t src_width,
4200 wl_fixed_t src_height)
4201{
4202 struct weston_surface *surface =
4203 wl_resource_get_user_data(resource);
4204
4205 assert(surface->viewport_resource != NULL);
4206
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004207 if (src_width == wl_fixed_from_int(-1) &&
4208 src_height == wl_fixed_from_int(-1)) {
4209 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004210 surface->pending.buffer_viewport.buffer.src_width =
4211 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004212 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004213 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004214 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004215
4216 if (src_width <= 0 || src_height <= 0) {
4217 wl_resource_post_error(resource,
4218 WL_VIEWPORT_ERROR_BAD_VALUE,
4219 "source size must be positive (%fx%f)",
4220 wl_fixed_to_double(src_width),
4221 wl_fixed_to_double(src_height));
4222 return;
4223 }
4224
4225 surface->pending.buffer_viewport.buffer.src_x = src_x;
4226 surface->pending.buffer_viewport.buffer.src_y = src_y;
4227 surface->pending.buffer_viewport.buffer.src_width = src_width;
4228 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004229 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004230}
4231
4232static void
4233viewport_set_destination(struct wl_client *client,
4234 struct wl_resource *resource,
4235 int32_t dst_width,
4236 int32_t dst_height)
4237{
4238 struct weston_surface *surface =
4239 wl_resource_get_user_data(resource);
4240
4241 assert(surface->viewport_resource != NULL);
4242
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004243 if (dst_width == -1 && dst_height == -1) {
4244 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004245 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004246 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004247 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004248 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004249
4250 if (dst_width <= 0 || dst_height <= 0) {
4251 wl_resource_post_error(resource,
4252 WL_VIEWPORT_ERROR_BAD_VALUE,
4253 "destination size must be positive (%dx%d)",
4254 dst_width, dst_height);
4255 return;
4256 }
4257
4258 surface->pending.buffer_viewport.surface.width = dst_width;
4259 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004260 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004261}
4262
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004263static const struct wl_viewport_interface viewport_interface = {
4264 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004265 viewport_set,
4266 viewport_set_source,
4267 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004268};
4269
4270static void
4271scaler_destroy(struct wl_client *client,
4272 struct wl_resource *resource)
4273{
4274 wl_resource_destroy(resource);
4275}
4276
4277static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004278scaler_get_viewport(struct wl_client *client,
4279 struct wl_resource *scaler,
4280 uint32_t id,
4281 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004282{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004283 int version = wl_resource_get_version(scaler);
4284 struct weston_surface *surface =
4285 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004286 struct wl_resource *resource;
4287
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004288 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004289 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004290 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4291 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004292 return;
4293 }
4294
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004295 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004296 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004297 if (resource == NULL) {
4298 wl_client_post_no_memory(client);
4299 return;
4300 }
4301
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004302 wl_resource_set_implementation(resource, &viewport_interface,
4303 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004304
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004305 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004306}
4307
4308static const struct wl_scaler_interface scaler_interface = {
4309 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004310 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004311};
4312
4313static void
4314bind_scaler(struct wl_client *client,
4315 void *data, uint32_t version, uint32_t id)
4316{
4317 struct wl_resource *resource;
4318
4319 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004320 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004321 if (resource == NULL) {
4322 wl_client_post_no_memory(client);
4323 return;
4324 }
4325
4326 wl_resource_set_implementation(resource, &scaler_interface,
4327 NULL, NULL);
4328}
4329
4330static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004331destroy_presentation_feedback(struct wl_resource *feedback_resource)
4332{
4333 struct weston_presentation_feedback *feedback;
4334
4335 feedback = wl_resource_get_user_data(feedback_resource);
4336
4337 wl_list_remove(&feedback->link);
4338 free(feedback);
4339}
4340
4341static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004342presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4343{
4344 wl_resource_destroy(resource);
4345}
4346
4347static void
4348presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004349 struct wl_resource *presentation_resource,
4350 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004351 uint32_t callback)
4352{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004353 struct weston_surface *surface;
4354 struct weston_presentation_feedback *feedback;
4355
4356 surface = wl_resource_get_user_data(surface_resource);
4357
Bryce Harringtonde16d892014-11-20 22:21:57 -08004358 feedback = zalloc(sizeof *feedback);
4359 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004360 goto err_calloc;
4361
4362 feedback->resource = wl_resource_create(client,
4363 &presentation_feedback_interface,
4364 1, callback);
4365 if (!feedback->resource)
4366 goto err_create;
4367
4368 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4369 destroy_presentation_feedback);
4370
4371 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4372
4373 return;
4374
4375err_create:
4376 free(feedback);
4377
4378err_calloc:
4379 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004380}
4381
4382static const struct presentation_interface presentation_implementation = {
4383 presentation_destroy,
4384 presentation_feedback
4385};
4386
4387static void
4388bind_presentation(struct wl_client *client,
4389 void *data, uint32_t version, uint32_t id)
4390{
4391 struct weston_compositor *compositor = data;
4392 struct wl_resource *resource;
4393
4394 resource = wl_resource_create(client, &presentation_interface,
4395 MIN(version, 1), id);
4396 if (resource == NULL) {
4397 wl_client_post_no_memory(client);
4398 return;
4399 }
4400
4401 wl_resource_set_implementation(resource, &presentation_implementation,
4402 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004403 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004404}
4405
4406static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004407compositor_bind(struct wl_client *client,
4408 void *data, uint32_t version, uint32_t id)
4409{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004410 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004411 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004412
Jason Ekstranda85118c2013-06-27 20:17:02 -05004413 resource = wl_resource_create(client, &wl_compositor_interface,
4414 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004415 if (resource == NULL) {
4416 wl_client_post_no_memory(client);
4417 return;
4418 }
4419
4420 wl_resource_set_implementation(resource, &compositor_interface,
4421 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004422}
4423
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004424WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004425weston_environment_get_fd(const char *env)
4426{
4427 char *e, *end;
4428 int fd, flags;
4429
4430 e = getenv(env);
4431 if (!e)
4432 return -1;
4433 fd = strtol(e, &end, 0);
4434 if (*end != '\0')
4435 return -1;
4436
4437 flags = fcntl(fd, F_GETFD);
4438 if (flags == -1)
4439 return -1;
4440
4441 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4442 unsetenv(env);
4443
4444 return fd;
4445}
4446
Pekka Paalanenb5026542014-11-12 15:09:24 +02004447static void
4448timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4449 uint32_t key, void *data)
4450{
4451 struct weston_compositor *compositor = data;
4452
4453 if (weston_timeline_enabled_)
4454 weston_timeline_close();
4455 else
4456 weston_timeline_open(compositor);
4457}
4458
Giulio Camuffo459137b2014-10-11 23:56:24 +03004459/** Create the compositor.
4460 *
4461 * This functions creates and initializes a compositor instance.
4462 *
4463 * \param display The Wayland display to be used.
4464 * \param user_data A pointer to an object that can later be retrieved
4465 * using the \ref weston_compositor_get_user_data function.
4466 * \return The compositor instance on success or NULL on failure.
4467 */
4468WL_EXPORT struct weston_compositor *
4469weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004470{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004471 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004472 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004473
Giulio Camuffo459137b2014-10-11 23:56:24 +03004474 ec = zalloc(sizeof *ec);
4475 if (!ec)
4476 return NULL;
4477
4478 ec->wl_display = display;
4479 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004480 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004481 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004482 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004483 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004484 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004485 wl_signal_init(&ec->idle_signal);
4486 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004487 wl_signal_init(&ec->show_input_panel_signal);
4488 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004489 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004490 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004491 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004492 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004493 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004494 wl_signal_init(&ec->session_signal);
4495 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004496
Casey Dahlin58ba1372012-04-19 22:50:08 -04004497 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004498 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004499
Giulio Camuffo954f1832014-10-11 18:27:30 +03004500 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004501 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004502 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004503
Giulio Camuffo954f1832014-10-11 18:27:30 +03004504 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004505 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004506 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004507
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004508 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004509 ec, bind_scaler))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004510 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004511
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004512 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4513 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004514 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004515
Jason Ekstranda7af7042013-10-12 22:38:11 -05004516 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004517 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004518 wl_list_init(&ec->layer_list);
4519 wl_list_init(&ec->seat_list);
4520 wl_list_init(&ec->output_list);
4521 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004522 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004523 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004524 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004525 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004526 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004527
Xiong Zhang97116532013-10-23 13:58:31 +08004528 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004529 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004530
Giulio Camuffo459137b2014-10-11 23:56:24 +03004531 wl_data_device_manager_init(ec->wl_display);
4532
4533 wl_display_init_shm(ec->wl_display);
4534
4535 loop = wl_display_get_event_loop(ec->wl_display);
4536 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4537 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4538
4539 ec->input_loop = wl_event_loop_create();
4540
4541 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4542 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4543
4544 weston_compositor_add_debug_binding(ec, KEY_T,
4545 timeline_key_binding_handler, ec);
4546
4547 weston_compositor_schedule_repaint(ec);
4548
4549 return ec;
4550
4551fail:
4552 free(ec);
4553 return NULL;
4554}
4555
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004556WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004557weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004558{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004559 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004560
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004561 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004562 if (ec->input_loop_source)
4563 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004564
Matt Roper361d2ad2011-08-29 13:52:23 -07004565 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004566 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004567 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004568
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004569 if (ec->renderer)
4570 ec->renderer->destroy(ec);
4571
Daniel Stone325fc2d2012-05-30 16:31:58 +01004572 weston_binding_list_destroy_all(&ec->key_binding_list);
4573 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004574 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004575 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004576 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004577
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004578 weston_plane_release(&ec->primary_plane);
4579
Jonas Ådahlc97af922012-03-28 22:36:09 +02004580 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07004581}
4582
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004583WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004584weston_compositor_exit_with_code(struct weston_compositor *compositor,
4585 int exit_code)
4586{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004587 if (compositor->exit_code == EXIT_SUCCESS)
4588 compositor->exit_code = exit_code;
4589
Giulio Camuffo459137b2014-10-11 23:56:24 +03004590 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004591}
4592
4593WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004594weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4595 const struct weston_pointer_grab_interface *interface)
4596{
4597 struct weston_seat *seat;
4598
4599 ec->default_pointer_grab = interface;
4600 wl_list_for_each(seat, &ec->seat_list, link) {
4601 if (seat->pointer) {
4602 weston_pointer_set_default_grab(seat->pointer,
4603 interface);
4604 }
4605 }
4606}
4607
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004608WL_EXPORT int
4609weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4610 clockid_t clk_id)
4611{
4612 struct timespec ts;
4613
4614 if (clock_gettime(clk_id, &ts) < 0)
4615 return -1;
4616
4617 compositor->presentation_clock = clk_id;
4618
4619 return 0;
4620}
4621
4622/*
4623 * For choosing the software clock, when the display hardware or API
4624 * does not expose a compatible presentation timestamp.
4625 */
4626WL_EXPORT int
4627weston_compositor_set_presentation_clock_software(
4628 struct weston_compositor *compositor)
4629{
4630 /* In order of preference */
4631 static const clockid_t clocks[] = {
4632 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4633 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4634 CLOCK_MONOTONIC, /* no jumps, may crawl */
4635 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4636 CLOCK_REALTIME /* may jump and crawl */
4637 };
4638 unsigned i;
4639
4640 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4641 if (weston_compositor_set_presentation_clock(compositor,
4642 clocks[i]) == 0)
4643 return 0;
4644
4645 weston_log("Error: no suitable presentation clock available.\n");
4646
4647 return -1;
4648}
4649
Pekka Paalanen662f3842015-03-18 12:17:26 +02004650/** Read the current time from the Presentation clock
4651 *
4652 * \param compositor
4653 * \param ts[out] The current time.
4654 *
4655 * \note Reading the current time in user space is always imprecise to some
4656 * degree.
4657 *
4658 * This function is never meant to fail. If reading the clock does fail,
4659 * an error message is logged and a zero time is returned. Callers are not
4660 * supposed to detect or react to failures.
4661 */
4662WL_EXPORT void
4663weston_compositor_read_presentation_clock(
4664 const struct weston_compositor *compositor,
4665 struct timespec *ts)
4666{
4667 static bool warned;
4668 int ret;
4669
4670 ret = clock_gettime(compositor->presentation_clock, ts);
4671 if (ret < 0) {
4672 ts->tv_sec = 0;
4673 ts->tv_nsec = 0;
4674
4675 if (!warned)
4676 weston_log("Error: failure to read "
4677 "the presentation clock %#x: '%m' (%d)\n",
4678 compositor->presentation_clock, errno);
4679 warned = true;
4680 }
4681}
4682
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004683WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004684weston_version(int *major, int *minor, int *micro)
4685{
4686 *major = WESTON_VERSION_MAJOR;
4687 *minor = WESTON_VERSION_MINOR;
4688 *micro = WESTON_VERSION_MICRO;
4689}
4690
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004691WL_EXPORT void *
4692weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004693{
4694 char path[PATH_MAX];
4695 void *module, *init;
4696
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004697 if (name == NULL)
4698 return NULL;
4699
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004700 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004701 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004702 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004703 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004704
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004705 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4706 if (module) {
4707 weston_log("Module '%s' already loaded\n", path);
4708 dlclose(module);
4709 return NULL;
4710 }
4711
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004712 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004713 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004714 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004715 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004716 return NULL;
4717 }
4718
4719 init = dlsym(module, entrypoint);
4720 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004721 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004722 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004723 return NULL;
4724 }
4725
4726 return init;
4727}
4728
Giulio Camuffo459137b2014-10-11 23:56:24 +03004729
4730/** Destroys the compositor.
4731 *
4732 * This function cleans up the compositor state and destroys it.
4733 *
4734 * \param compositor The compositor to be destroyed.
4735 */
4736WL_EXPORT void
4737weston_compositor_destroy(struct weston_compositor *compositor)
4738{
4739 /* prevent further rendering while shutting down */
4740 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4741
4742 wl_signal_emit(&compositor->destroy_signal, compositor);
4743
4744 weston_compositor_xkb_destroy(compositor);
4745
4746 compositor->backend->destroy(compositor);
4747 free(compositor);
4748}
4749
4750/** Instruct the compositor to exit.
4751 *
4752 * This functions does not directly destroy the compositor object, it merely
4753 * command it to start the tear down process. It is not guaranteed that the
4754 * tear down will happen immediately.
4755 *
4756 * \param compositor The compositor to tear down.
4757 */
4758WL_EXPORT void
4759weston_compositor_exit(struct weston_compositor *compositor)
4760{
4761 compositor->exit(compositor);
4762}
4763
4764/** Return the user data stored in the compositor.
4765 *
4766 * This function returns the user data pointer set with user_data parameter
4767 * to the \ref weston_compositor_create function.
4768 */
4769WL_EXPORT void *
4770weston_compositor_get_user_data(struct weston_compositor *compositor)
4771{
4772 return compositor->user_data;
4773}