blob: cafac2962d664bb400cb63764d279dd4a0f79926 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004 * Copyright © 2012-2015 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050026 */
27
Kristian Høgsberga9410222011-01-14 17:22:35 -050028#include "config.h"
29
Daniel Stoneb7452fe2012-06-01 12:14:06 +010030#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010035#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020037#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040038#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010039#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040040#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020041#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020042#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020043#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050045#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040046#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040047#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040048#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050049#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040050#include <sys/time.h>
51#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030052#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050053
Pekka Paalanenb5026542014-11-12 15:09:24 +020054#include "timeline.h"
55
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010057#include "scaler-server-protocol.h"
Pekka Paalanen31f7d782014-09-23 22:08:43 -040058#include "presentation_timing-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070059#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070060#include "shared/os-compatibility.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030061#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040062#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050063#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050064
Pekka Paalanen0513a952014-05-21 16:17:27 +030065#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
66
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020067static void
Alexander Larsson0b135062013-05-28 16:23:36 +020068weston_output_transform_scale_init(struct weston_output *output,
69 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020070
Rob Bradford27b17932013-06-26 18:08:46 +010071static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050072weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010073
Derek Foreman6ae7bc92014-11-04 10:47:33 -060074static void weston_mode_switch_finish(struct weston_output *output,
75 int mode_changed,
76 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080077{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020078 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020079 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020080 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060081 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020082
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020083 pixman_region32_init(&old_output_region);
84 pixman_region32_copy(&old_output_region, &output->region);
85
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020086 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020087 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020088
89 pixman_region32_init(&output->previous_damage);
90 pixman_region32_init_rect(&output->region, output->x, output->y,
91 output->width, output->height);
92
93 weston_output_update_matrix(output);
94
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020095 /* If a pointer falls outside the outputs new geometry, move it to its
96 * lower-right corner */
97 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -040098 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020099 int32_t x, y;
100
101 if (!pointer)
102 continue;
103
104 x = wl_fixed_to_int(pointer->x);
105 y = wl_fixed_to_int(pointer->y);
106
107 if (!pixman_region32_contains_point(&old_output_region,
108 x, y, NULL) ||
109 pixman_region32_contains_point(&output->region,
110 x, y, NULL))
111 continue;
112
113 if (x >= output->x + output->width)
114 x = output->x + output->width - 1;
115 if (y >= output->y + output->height)
116 y = output->y + output->height - 1;
117
118 pointer->x = wl_fixed_from_int(x);
119 pointer->y = wl_fixed_from_int(y);
120 }
121
122 pixman_region32_fini(&old_output_region);
123
Derek Foremandd4cd332014-11-10 10:29:59 -0600124 if (!mode_changed && !scale_changed)
125 return;
126
Hardening57388e42013-09-18 23:56:36 +0200127 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600128 wl_resource_for_each(resource, &output->resource_list) {
129 if (mode_changed) {
130 wl_output_send_mode(resource,
131 output->current_mode->flags,
132 output->current_mode->width,
133 output->current_mode->height,
134 output->current_mode->refresh);
135 }
Hardening57388e42013-09-18 23:56:36 +0200136
Derek Foreman41bdc272014-11-05 13:26:57 -0600137 version = wl_resource_get_version(resource);
138 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600139 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200140
Derek Foreman41bdc272014-11-05 13:26:57 -0600141 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
142 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600143 }
144}
145
146WL_EXPORT int
147weston_output_mode_set_native(struct weston_output *output,
148 struct weston_mode *mode,
149 int32_t scale)
150{
151 int ret;
152 int mode_changed = 0, scale_changed = 0;
153
154 if (!output->switch_mode)
155 return -1;
156
157 if (!output->original_mode) {
158 mode_changed = 1;
159 ret = output->switch_mode(output, mode);
160 if (ret < 0)
161 return ret;
162 if (output->current_scale != scale) {
163 scale_changed = 1;
164 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200165 }
166 }
167
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600168 output->native_mode = mode;
169 output->native_scale = scale;
170
171 weston_mode_switch_finish(output, mode_changed, scale_changed);
172
173 return 0;
174}
175
176WL_EXPORT int
177weston_output_mode_switch_to_native(struct weston_output *output)
178{
179 int ret;
180 int mode_changed = 0, scale_changed = 0;
181
182 if (!output->switch_mode)
183 return -1;
184
185 if (!output->original_mode) {
186 weston_log("already in the native mode\n");
187 return -1;
188 }
189 /* the non fullscreen clients haven't seen a mode set since we
190 * switched into a temporary, so we need to notify them if the
191 * mode at that time is different from the native mode now.
192 */
193 mode_changed = (output->original_mode != output->native_mode);
194 scale_changed = (output->original_scale != output->native_scale);
195
196 ret = output->switch_mode(output, output->native_mode);
197 if (ret < 0)
198 return ret;
199
200 output->current_scale = output->native_scale;
201
202 output->original_mode = NULL;
203 output->original_scale = 0;
204
205 weston_mode_switch_finish(output, mode_changed, scale_changed);
206
207 return 0;
208}
209
210WL_EXPORT int
211weston_output_mode_switch_to_temporary(struct weston_output *output,
212 struct weston_mode *mode,
213 int32_t scale)
214{
215 int ret;
216
217 if (!output->switch_mode)
218 return -1;
219
220 /* original_mode is the last mode non full screen clients have seen,
221 * so we shouldn't change it if we already have one set.
222 */
223 if (!output->original_mode) {
224 output->original_mode = output->native_mode;
225 output->original_scale = output->native_scale;
226 }
227 ret = output->switch_mode(output, mode);
228 if (ret < 0)
229 return ret;
230
231 output->current_scale = scale;
232
233 weston_mode_switch_finish(output, 0, 0);
234
235 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800236}
237
Benjamin Franzke06286262011-05-06 19:12:33 +0200238static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200239child_client_exec(int sockfd, const char *path)
240{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500241 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200243 sigset_t allsigs;
244
245 /* do not give our signal mask to the new process */
246 sigfillset(&allsigs);
247 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200248
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100249 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
250 if (seteuid(getuid()) == -1) {
251 weston_log("compositor: failed seteuid\n");
252 return;
253 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500254
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500255 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
256 * non-CLOEXEC fd to pass through exec. */
257 clientfd = dup(sockfd);
258 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200259 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500260 return;
261 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200262
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500263 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200264 setenv("WAYLAND_SOCKET", s, 1);
265
266 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200267 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200268 path);
269}
270
271WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272weston_client_launch(struct weston_compositor *compositor,
273 struct weston_process *proc,
274 const char *path,
275 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200276{
277 int sv[2];
278 pid_t pid;
279 struct wl_client *client;
280
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300281 weston_log("launching '%s'\n", path);
282
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300283 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200284 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200285 "socketpair failed while launching '%s': %m\n",
286 path);
287 return NULL;
288 }
289
290 pid = fork();
291 if (pid == -1) {
292 close(sv[0]);
293 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200295 "fork failed while launching '%s': %m\n", path);
296 return NULL;
297 }
298
299 if (pid == 0) {
300 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700301 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200302 }
303
304 close(sv[1]);
305
306 client = wl_client_create(compositor->wl_display, sv[0]);
307 if (!client) {
308 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200310 "wl_client_create failed while launching '%s'.\n",
311 path);
312 return NULL;
313 }
314
315 proc->pid = pid;
316 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200318
319 return client;
320}
321
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300322struct process_info {
323 struct weston_process proc;
324 char *path;
325};
326
327static void
328process_handle_sigchld(struct weston_process *process, int status)
329{
330 struct process_info *pinfo =
331 container_of(process, struct process_info, proc);
332
333 /*
334 * There are no guarantees whether this runs before or after
335 * the wl_client destructor.
336 */
337
338 if (WIFEXITED(status)) {
339 weston_log("%s exited with status %d\n", pinfo->path,
340 WEXITSTATUS(status));
341 } else if (WIFSIGNALED(status)) {
342 weston_log("%s died on signal %d\n", pinfo->path,
343 WTERMSIG(status));
344 } else {
345 weston_log("%s disappeared\n", pinfo->path);
346 }
347
348 free(pinfo->path);
349 free(pinfo);
350}
351
352WL_EXPORT struct wl_client *
353weston_client_start(struct weston_compositor *compositor, const char *path)
354{
355 struct process_info *pinfo;
356 struct wl_client *client;
357
358 pinfo = zalloc(sizeof *pinfo);
359 if (!pinfo)
360 return NULL;
361
362 pinfo->path = strdup(path);
363 if (!pinfo->path)
364 goto out_free;
365
366 client = weston_client_launch(compositor, &pinfo->proc, path,
367 process_handle_sigchld);
368 if (!client)
369 goto out_str;
370
371 return client;
372
373out_str:
374 free(pinfo->path);
375
376out_free:
377 free(pinfo);
378
379 return NULL;
380}
381
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200382static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300383region_init_infinite(pixman_region32_t *region)
384{
385 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
386 UINT32_MAX, UINT32_MAX);
387}
388
Pekka Paalanene67858b2013-04-25 13:57:42 +0300389static struct weston_subsurface *
390weston_surface_to_subsurface(struct weston_surface *surface);
391
Jason Ekstranda7af7042013-10-12 22:38:11 -0500392WL_EXPORT struct weston_view *
393weston_view_create(struct weston_surface *surface)
394{
395 struct weston_view *view;
396
Bryce Harringtonde16d892014-11-20 22:21:57 -0800397 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500398 if (view == NULL)
399 return NULL;
400
401 view->surface = surface;
402
Jason Ekstranda7af7042013-10-12 22:38:11 -0500403 /* Assign to surface */
404 wl_list_insert(&surface->views, &view->surface_link);
405
406 wl_signal_init(&view->destroy_signal);
407 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300408 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500409
Jason Ekstranda7af7042013-10-12 22:38:11 -0500410 pixman_region32_init(&view->clip);
411
412 view->alpha = 1.0;
413 pixman_region32_init(&view->transform.opaque);
414
415 wl_list_init(&view->geometry.transformation_list);
416 wl_list_insert(&view->geometry.transformation_list,
417 &view->transform.position.link);
418 weston_matrix_init(&view->transform.position.matrix);
419 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200420 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500421 pixman_region32_init(&view->transform.boundingbox);
422 view->transform.dirty = 1;
423
Jason Ekstranda7af7042013-10-12 22:38:11 -0500424 return view;
425}
426
Jason Ekstrand108865d2014-06-26 10:04:49 -0700427struct weston_frame_callback {
428 struct wl_resource *resource;
429 struct wl_list link;
430};
431
Pekka Paalanen133e4392014-09-23 22:08:46 -0400432struct weston_presentation_feedback {
433 struct wl_resource *resource;
434
435 /* XXX: could use just wl_resource_get_link() instead */
436 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200437
438 /* The per-surface feedback flags */
439 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400440};
441
442static void
443weston_presentation_feedback_discard(
444 struct weston_presentation_feedback *feedback)
445{
446 presentation_feedback_send_discarded(feedback->resource);
447 wl_resource_destroy(feedback->resource);
448}
449
450static void
451weston_presentation_feedback_discard_list(struct wl_list *list)
452{
453 struct weston_presentation_feedback *feedback, *tmp;
454
455 wl_list_for_each_safe(feedback, tmp, list, link)
456 weston_presentation_feedback_discard(feedback);
457}
458
459static void
460weston_presentation_feedback_present(
461 struct weston_presentation_feedback *feedback,
462 struct weston_output *output,
463 uint32_t refresh_nsec,
464 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200465 uint64_t seq,
466 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400467{
468 struct wl_client *client = wl_resource_get_client(feedback->resource);
469 struct wl_resource *o;
470 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400471
472 wl_resource_for_each(o, &output->resource_list) {
473 if (wl_resource_get_client(o) != client)
474 continue;
475
476 presentation_feedback_send_sync_output(feedback->resource, o);
477 }
478
479 secs = ts->tv_sec;
480 presentation_feedback_send_presented(feedback->resource,
481 secs >> 32, secs & 0xffffffff,
482 ts->tv_nsec,
483 refresh_nsec,
484 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200485 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400486 wl_resource_destroy(feedback->resource);
487}
488
489static void
490weston_presentation_feedback_present_list(struct wl_list *list,
491 struct weston_output *output,
492 uint32_t refresh_nsec,
493 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200494 uint64_t seq,
495 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400496{
497 struct weston_presentation_feedback *feedback, *tmp;
498
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200499 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
500 wl_list_empty(list));
501
Pekka Paalanen133e4392014-09-23 22:08:46 -0400502 wl_list_for_each_safe(feedback, tmp, list, link)
503 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200504 refresh_nsec, ts, seq,
505 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400506}
507
Jason Ekstrand7b982072014-05-20 14:33:03 -0500508static void
509surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
510{
511 struct weston_surface_state *state =
512 container_of(listener, struct weston_surface_state,
513 buffer_destroy_listener);
514
515 state->buffer = NULL;
516}
517
518static void
519weston_surface_state_init(struct weston_surface_state *state)
520{
521 state->newly_attached = 0;
522 state->buffer = NULL;
523 state->buffer_destroy_listener.notify =
524 surface_state_handle_buffer_destroy;
525 state->sx = 0;
526 state->sy = 0;
527
528 pixman_region32_init(&state->damage);
529 pixman_region32_init(&state->opaque);
530 region_init_infinite(&state->input);
531
532 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400533 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500534
535 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
536 state->buffer_viewport.buffer.scale = 1;
537 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
538 state->buffer_viewport.surface.width = -1;
539 state->buffer_viewport.changed = 0;
540}
541
542static void
543weston_surface_state_fini(struct weston_surface_state *state)
544{
545 struct weston_frame_callback *cb, *next;
546
547 wl_list_for_each_safe(cb, next,
548 &state->frame_callback_list, link)
549 wl_resource_destroy(cb->resource);
550
Pekka Paalanen133e4392014-09-23 22:08:46 -0400551 weston_presentation_feedback_discard_list(&state->feedback_list);
552
Jason Ekstrand7b982072014-05-20 14:33:03 -0500553 pixman_region32_fini(&state->input);
554 pixman_region32_fini(&state->opaque);
555 pixman_region32_fini(&state->damage);
556
557 if (state->buffer)
558 wl_list_remove(&state->buffer_destroy_listener.link);
559 state->buffer = NULL;
560}
561
562static void
563weston_surface_state_set_buffer(struct weston_surface_state *state,
564 struct weston_buffer *buffer)
565{
566 if (state->buffer == buffer)
567 return;
568
569 if (state->buffer)
570 wl_list_remove(&state->buffer_destroy_listener.link);
571 state->buffer = buffer;
572 if (state->buffer)
573 wl_signal_add(&state->buffer->destroy_signal,
574 &state->buffer_destroy_listener);
575}
576
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500577WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500578weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500579{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500580 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400581
Bryce Harringtonde16d892014-11-20 22:21:57 -0800582 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400583 if (surface == NULL)
584 return NULL;
585
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500586 wl_signal_init(&surface->destroy_signal);
587
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500588 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200589 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400590
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200591 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
592 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200593 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
594 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500595
596 weston_surface_state_init(&surface->pending);
597
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400598 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500599 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300600 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400601
Jason Ekstranda7af7042013-10-12 22:38:11 -0500602 wl_list_init(&surface->views);
603
604 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400605 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500606
Pekka Paalanene67858b2013-04-25 13:57:42 +0300607 wl_list_init(&surface->subsurface_list);
608 wl_list_init(&surface->subsurface_list_pending);
609
Jason Ekstrand1e059042014-10-16 10:55:19 -0500610 weston_matrix_init(&surface->buffer_to_surface_matrix);
611 weston_matrix_init(&surface->surface_to_buffer_matrix);
612
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400613 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500614}
615
Alex Wu8811bf92012-02-28 18:07:54 +0800616WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500617weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200618 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500619{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100620 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500621}
622
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400623WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500624weston_view_to_global_float(struct weston_view *view,
625 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200626{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500627 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200628 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
629
Jason Ekstranda7af7042013-10-12 22:38:11 -0500630 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200631
632 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200633 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700634 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200635 v.f[3]);
636 *x = 0;
637 *y = 0;
638 return;
639 }
640
641 *x = v.f[0] / v.f[3];
642 *y = v.f[1] / v.f[3];
643 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500644 *x = sx + view->geometry.x;
645 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200646 }
647}
648
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500649WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200650weston_transformed_coord(int width, int height,
651 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200652 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200653 float sx, float sy, float *bx, float *by)
654{
655 switch (transform) {
656 case WL_OUTPUT_TRANSFORM_NORMAL:
657 default:
658 *bx = sx;
659 *by = sy;
660 break;
661 case WL_OUTPUT_TRANSFORM_FLIPPED:
662 *bx = width - sx;
663 *by = sy;
664 break;
665 case WL_OUTPUT_TRANSFORM_90:
666 *bx = height - sy;
667 *by = sx;
668 break;
669 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
670 *bx = height - sy;
671 *by = width - sx;
672 break;
673 case WL_OUTPUT_TRANSFORM_180:
674 *bx = width - sx;
675 *by = height - sy;
676 break;
677 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
678 *bx = sx;
679 *by = height - sy;
680 break;
681 case WL_OUTPUT_TRANSFORM_270:
682 *bx = sy;
683 *by = width - sx;
684 break;
685 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
686 *bx = sy;
687 *by = sx;
688 break;
689 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200690
691 *bx *= scale;
692 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200693}
694
695WL_EXPORT pixman_box32_t
696weston_transformed_rect(int width, int height,
697 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200698 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200699 pixman_box32_t rect)
700{
701 float x1, x2, y1, y2;
702
703 pixman_box32_t ret;
704
Alexander Larsson4ea95522013-05-22 14:41:37 +0200705 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200706 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200707 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200708 rect.x2, rect.y2, &x2, &y2);
709
710 if (x1 <= x2) {
711 ret.x1 = x1;
712 ret.x2 = x2;
713 } else {
714 ret.x1 = x2;
715 ret.x2 = x1;
716 }
717
718 if (y1 <= y2) {
719 ret.y1 = y1;
720 ret.y2 = y2;
721 } else {
722 ret.y1 = y2;
723 ret.y2 = y1;
724 }
725
726 return ret;
727}
728
729WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500730weston_transformed_region(int width, int height,
731 enum wl_output_transform transform,
732 int32_t scale,
733 pixman_region32_t *src, pixman_region32_t *dest)
734{
735 pixman_box32_t *src_rects, *dest_rects;
736 int nrects, i;
737
738 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
739 if (src != dest)
740 pixman_region32_copy(dest, src);
741 return;
742 }
743
744 src_rects = pixman_region32_rectangles(src, &nrects);
745 dest_rects = malloc(nrects * sizeof(*dest_rects));
746 if (!dest_rects)
747 return;
748
749 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
750 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
751 } else {
752 for (i = 0; i < nrects; i++) {
753 switch (transform) {
754 default:
755 case WL_OUTPUT_TRANSFORM_NORMAL:
756 dest_rects[i].x1 = src_rects[i].x1;
757 dest_rects[i].y1 = src_rects[i].y1;
758 dest_rects[i].x2 = src_rects[i].x2;
759 dest_rects[i].y2 = src_rects[i].y2;
760 break;
761 case WL_OUTPUT_TRANSFORM_90:
762 dest_rects[i].x1 = height - src_rects[i].y2;
763 dest_rects[i].y1 = src_rects[i].x1;
764 dest_rects[i].x2 = height - src_rects[i].y1;
765 dest_rects[i].y2 = src_rects[i].x2;
766 break;
767 case WL_OUTPUT_TRANSFORM_180:
768 dest_rects[i].x1 = width - src_rects[i].x2;
769 dest_rects[i].y1 = height - src_rects[i].y2;
770 dest_rects[i].x2 = width - src_rects[i].x1;
771 dest_rects[i].y2 = height - src_rects[i].y1;
772 break;
773 case WL_OUTPUT_TRANSFORM_270:
774 dest_rects[i].x1 = src_rects[i].y1;
775 dest_rects[i].y1 = width - src_rects[i].x2;
776 dest_rects[i].x2 = src_rects[i].y2;
777 dest_rects[i].y2 = width - src_rects[i].x1;
778 break;
779 case WL_OUTPUT_TRANSFORM_FLIPPED:
780 dest_rects[i].x1 = width - src_rects[i].x2;
781 dest_rects[i].y1 = src_rects[i].y1;
782 dest_rects[i].x2 = width - src_rects[i].x1;
783 dest_rects[i].y2 = src_rects[i].y2;
784 break;
785 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
786 dest_rects[i].x1 = height - src_rects[i].y2;
787 dest_rects[i].y1 = width - src_rects[i].x2;
788 dest_rects[i].x2 = height - src_rects[i].y1;
789 dest_rects[i].y2 = width - src_rects[i].x1;
790 break;
791 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
792 dest_rects[i].x1 = src_rects[i].x1;
793 dest_rects[i].y1 = height - src_rects[i].y2;
794 dest_rects[i].x2 = src_rects[i].x2;
795 dest_rects[i].y2 = height - src_rects[i].y1;
796 break;
797 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
798 dest_rects[i].x1 = src_rects[i].y1;
799 dest_rects[i].y1 = src_rects[i].x1;
800 dest_rects[i].x2 = src_rects[i].y2;
801 dest_rects[i].y2 = src_rects[i].x2;
802 break;
803 }
804 }
805 }
806
807 if (scale != 1) {
808 for (i = 0; i < nrects; i++) {
809 dest_rects[i].x1 *= scale;
810 dest_rects[i].x2 *= scale;
811 dest_rects[i].y1 *= scale;
812 dest_rects[i].y2 *= scale;
813 }
814 }
815
816 pixman_region32_clear(dest);
817 pixman_region32_init_rects(dest, dest_rects, nrects);
818 free(dest_rects);
819}
820
Jonny Lamb74130762013-11-26 18:19:46 +0100821static void
822scaler_surface_to_buffer(struct weston_surface *surface,
823 float sx, float sy, float *bx, float *by)
824{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200825 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200826 double src_width, src_height;
827 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200828
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200829 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
830 if (vp->surface.width == -1) {
831 *bx = sx;
832 *by = sy;
833 return;
834 }
Jonny Lamb74130762013-11-26 18:19:46 +0100835
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200836 src_x = 0.0;
837 src_y = 0.0;
838 src_width = surface->width_from_buffer;
839 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100840 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200841 src_x = wl_fixed_to_double(vp->buffer.src_x);
842 src_y = wl_fixed_to_double(vp->buffer.src_y);
843 src_width = wl_fixed_to_double(vp->buffer.src_width);
844 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100845 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200846
847 *bx = sx * src_width / surface->width + src_x;
848 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100849}
850
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500851WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200852weston_surface_to_buffer_float(struct weston_surface *surface,
853 float sx, float sy, float *bx, float *by)
854{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200855 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
856
Jonny Lamb74130762013-11-26 18:19:46 +0100857 /* first transform coordinates if the scaler is set */
858 scaler_surface_to_buffer(surface, sx, sy, bx, by);
859
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500860 weston_transformed_coord(surface->width_from_buffer,
861 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200862 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100863 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200864}
865
Alexander Larsson4ea95522013-05-22 14:41:37 +0200866WL_EXPORT void
867weston_surface_to_buffer(struct weston_surface *surface,
868 int sx, int sy, int *bx, int *by)
869{
870 float bxf, byf;
871
Jonny Lamb74130762013-11-26 18:19:46 +0100872 weston_surface_to_buffer_float(surface,
873 sx, sy, &bxf, &byf);
874
Alexander Larsson4ea95522013-05-22 14:41:37 +0200875 *bx = floorf(bxf);
876 *by = floorf(byf);
877}
878
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200879WL_EXPORT pixman_box32_t
880weston_surface_to_buffer_rect(struct weston_surface *surface,
881 pixman_box32_t rect)
882{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200883 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100884 float xf, yf;
885
886 /* first transform box coordinates if the scaler is set */
887 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
888 rect.x1 = floorf(xf);
889 rect.y1 = floorf(yf);
890
891 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
892 rect.x2 = floorf(xf);
893 rect.y2 = floorf(yf);
894
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500895 return weston_transformed_rect(surface->width_from_buffer,
896 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200897 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200898 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200899}
900
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200901/** Transform a region from surface coordinates to buffer coordinates
902 *
903 * \param surface The surface to fetch wl_viewport and buffer transformation
904 * from.
905 * \param surface_region[in] The region in surface coordinates.
906 * \param buffer_region[out] The region converted to buffer coordinates.
907 *
908 * Buffer_region must be init'd, but will be completely overwritten.
909 *
910 * Viewport and buffer transformations can only do translation, scaling,
911 * and rotations in 90-degree steps. Therefore the only loss in the
912 * conversion is coordinate flooring (rounding).
913 */
914WL_EXPORT void
915weston_surface_to_buffer_region(struct weston_surface *surface,
916 pixman_region32_t *surface_region,
917 pixman_region32_t *buffer_region)
918{
919 pixman_box32_t *src_rects, *dest_rects;
920 int nrects, i;
921
922 src_rects = pixman_region32_rectangles(surface_region, &nrects);
923 dest_rects = malloc(nrects * sizeof(*dest_rects));
924 if (!dest_rects)
925 return;
926
927 for (i = 0; i < nrects; i++) {
928 dest_rects[i] = weston_surface_to_buffer_rect(surface,
929 src_rects[i]);
930 }
931
932 pixman_region32_fini(buffer_region);
933 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
934 free(dest_rects);
935}
936
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200937WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400939 struct weston_plane *plane)
940{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500941 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400942 return;
943
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944 weston_view_damage_below(view);
945 view->plane = plane;
946 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400947}
948
Pekka Paalanen51723d52015-02-17 13:10:01 +0200949/** Inflict damage on the plane where the view is visible.
950 *
951 * \param view The view that causes the damage.
952 *
953 * If the view is currently on a plane (including the primary plane),
954 * take the view's boundingbox, subtract all the opaque views that cover it,
955 * and add the remaining region as damage to the plane. This corresponds
956 * to the damage inflicted to the plane if this view disappeared.
957 *
958 * A repaint is scheduled for this view.
959 *
960 * The region of all opaque views covering this view is stored in
961 * weston_view::clip and updated by view_accumulate_damage() during
962 * weston_output_repaint(). Specifically, that region matches the
963 * scenegraph as it was last painted.
964 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400965WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500966weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200967{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500968 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200969
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500970 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200971 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800973 if (view->plane)
974 pixman_region32_union(&view->plane->damage,
975 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500976 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800977 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200978}
979
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400980static void
981weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
982{
983 uint32_t different = es->output_mask ^ mask;
984 uint32_t entered = mask & different;
985 uint32_t left = es->output_mask & different;
986 struct weston_output *output;
987 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500988 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400989
990 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500991 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400992 return;
993 if (different == 0)
994 return;
995
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500996 client = wl_resource_get_client(es->resource);
997
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400998 wl_list_for_each(output, &es->compositor->output_list, link) {
999 if (1 << output->id & different)
1000 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001001 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001002 client);
1003 if (resource == NULL)
1004 continue;
1005 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001006 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001007 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001008 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001009 }
1010}
1011
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001012
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001013static void
1014weston_surface_assign_output(struct weston_surface *es)
1015{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016 struct weston_output *new_output;
1017 struct weston_view *view;
1018 pixman_region32_t region;
1019 uint32_t max, area, mask;
1020 pixman_box32_t *e;
1021
1022 new_output = NULL;
1023 max = 0;
1024 mask = 0;
1025 pixman_region32_init(&region);
1026 wl_list_for_each(view, &es->views, surface_link) {
1027 if (!view->output)
1028 continue;
1029
1030 pixman_region32_intersect(&region, &view->transform.boundingbox,
1031 &view->output->region);
1032
1033 e = pixman_region32_extents(&region);
1034 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1035
1036 mask |= view->output_mask;
1037
1038 if (area >= max) {
1039 new_output = view->output;
1040 max = area;
1041 }
1042 }
1043 pixman_region32_fini(&region);
1044
1045 es->output = new_output;
1046 weston_surface_update_output_mask(es, mask);
1047}
1048
1049static void
1050weston_view_assign_output(struct weston_view *ev)
1051{
1052 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001053 struct weston_output *output, *new_output;
1054 pixman_region32_t region;
1055 uint32_t max, area, mask;
1056 pixman_box32_t *e;
1057
1058 new_output = NULL;
1059 max = 0;
1060 mask = 0;
1061 pixman_region32_init(&region);
1062 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001063 if (output->destroying)
1064 continue;
1065
Jason Ekstranda7af7042013-10-12 22:38:11 -05001066 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001067 &output->region);
1068
1069 e = pixman_region32_extents(&region);
1070 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1071
1072 if (area > 0)
1073 mask |= 1 << output->id;
1074
1075 if (area >= max) {
1076 new_output = output;
1077 max = area;
1078 }
1079 }
1080 pixman_region32_fini(&region);
1081
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 ev->output = new_output;
1083 ev->output_mask = mask;
1084
1085 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001086}
1087
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001088static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001089weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1090 int from_x, int from_y, int *to_x, int *to_y)
1091{
1092 float x, y;
1093
1094 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1095 weston_view_from_global_float(to, x, y, &x, &y);
1096
1097 *to_x = round(x);
1098 *to_y = round(y);
1099}
1100
1101static void
1102weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1103{
1104 pixman_box32_t *a;
1105 pixman_box32_t b;
1106
1107 a = pixman_region32_extents(&from->geometry.scissor);
1108
1109 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1110 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1111
1112 pixman_region32_fini(&to->geometry.scissor);
1113 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1114}
1115
1116static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001117view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001118 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001119{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001120 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1121 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001122 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001123 { inbox->x1, inbox->y1 },
1124 { inbox->x1, inbox->y2 },
1125 { inbox->x2, inbox->y1 },
1126 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001127 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001128 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001129 int i;
1130
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001131 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001132 /* avoid rounding empty bbox to 1x1 */
1133 pixman_region32_init(bbox);
1134 return;
1135 }
1136
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001137 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001138 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001140 if (x < min_x)
1141 min_x = x;
1142 if (x > max_x)
1143 max_x = x;
1144 if (y < min_y)
1145 min_y = y;
1146 if (y > max_y)
1147 max_y = y;
1148 }
1149
Pekka Paalanen219b9822012-02-08 15:38:37 +02001150 int_x = floorf(min_x);
1151 int_y = floorf(min_y);
1152 pixman_region32_init_rect(bbox, int_x, int_y,
1153 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001154}
1155
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001156static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001158{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001160
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001161 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162 view->geometry.x = roundf(view->geometry.x);
1163 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001164
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001165 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1167 view->transform.position.matrix.d[12] = view->geometry.x;
1168 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001169
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001171
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 view->transform.inverse = view->transform.position.matrix;
1173 view->transform.inverse.d[12] = -view->geometry.x;
1174 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001175
Jason Ekstranda7af7042013-10-12 22:38:11 -05001176 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001177 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001178 view->surface->width,
1179 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001180 if (view->geometry.scissor_enabled)
1181 pixman_region32_intersect(&view->transform.boundingbox,
1182 &view->transform.boundingbox,
1183 &view->geometry.scissor);
1184
1185 pixman_region32_translate(&view->transform.boundingbox,
1186 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001187
Jason Ekstranda7af7042013-10-12 22:38:11 -05001188 if (view->alpha == 1.0) {
1189 pixman_region32_copy(&view->transform.opaque,
1190 &view->surface->opaque);
1191 pixman_region32_translate(&view->transform.opaque,
1192 view->geometry.x,
1193 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001194 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001195}
1196
1197static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001199{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 struct weston_view *parent = view->geometry.parent;
1201 struct weston_matrix *matrix = &view->transform.matrix;
1202 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001203 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001204 pixman_region32_t surfregion;
1205 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001206
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001208
1209 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1211 view->transform.position.matrix.d[12] = view->geometry.x;
1212 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001213
1214 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001216 weston_matrix_multiply(matrix, &tform->matrix);
1217
Pekka Paalanen483243f2013-03-08 14:56:50 +02001218 if (parent)
1219 weston_matrix_multiply(matrix, &parent->transform.matrix);
1220
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001221 if (weston_matrix_invert(inverse, matrix) < 0) {
1222 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 weston_log("error: weston_view %p"
1224 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001225 return -1;
1226 }
1227
Pekka Paalanen380adf52015-02-16 14:39:11 +02001228 pixman_region32_init_rect(&surfregion, 0, 0,
1229 view->surface->width, view->surface->height);
1230 if (view->geometry.scissor_enabled)
1231 pixman_region32_intersect(&surfregion, &surfregion,
1232 &view->geometry.scissor);
1233 surfbox = pixman_region32_extents(&surfregion);
1234
1235 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1236 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001237
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001238 return 0;
1239}
1240
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001241static struct weston_layer *
1242get_view_layer(struct weston_view *view)
1243{
1244 if (view->parent_view)
1245 return get_view_layer(view->parent_view);
1246 return view->layer_link.layer;
1247}
1248
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001249WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001251{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001253 struct weston_layer *layer;
1254 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001255
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001257 return;
1258
Pekka Paalanen483243f2013-03-08 14:56:50 +02001259 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001261
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001263
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001265
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 pixman_region32_fini(&view->transform.boundingbox);
1267 pixman_region32_fini(&view->transform.opaque);
1268 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001269
Pekka Paalanencd403622012-01-25 13:37:39 +02001270 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 if (view->geometry.transformation_list.next ==
1272 &view->transform.position.link &&
1273 view->geometry.transformation_list.prev ==
1274 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001275 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001277 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 if (weston_view_update_transform_enable(view) < 0)
1279 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001280 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001281
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001282 layer = get_view_layer(view);
1283 if (layer) {
1284 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001285 pixman_region32_intersect(&view->transform.boundingbox,
1286 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001287 pixman_region32_intersect(&view->transform.opaque,
1288 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001289 pixman_region32_fini(&mask);
1290 }
1291
Pekka Paalanen380adf52015-02-16 14:39:11 +02001292 if (parent) {
1293 if (parent->geometry.scissor_enabled) {
1294 view->geometry.scissor_enabled = true;
1295 weston_view_transfer_scissor(parent, view);
1296 } else {
1297 view->geometry.scissor_enabled = false;
1298 }
1299 }
1300
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001302
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001304
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 wl_signal_emit(&view->surface->compositor->transform_signal,
1306 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001307}
1308
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001309WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001311{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001313
1314 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 * The invariant: if view->geometry.dirty, then all views
1316 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001317 * Corollary: if not parent->geometry.dirty, then all ancestors
1318 * are not dirty.
1319 */
1320
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001322 return;
1323
Jason Ekstranda7af7042013-10-12 22:38:11 -05001324 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001325
Jason Ekstranda7af7042013-10-12 22:38:11 -05001326 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001327 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001329}
1330
1331WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332weston_view_to_global_fixed(struct weston_view *view,
1333 wl_fixed_t vx, wl_fixed_t vy,
1334 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001335{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001336 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001337
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338 weston_view_to_global_float(view,
1339 wl_fixed_to_double(vx),
1340 wl_fixed_to_double(vy),
1341 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001342 *x = wl_fixed_from_double(xf);
1343 *y = wl_fixed_from_double(yf);
1344}
1345
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001346WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347weston_view_from_global_float(struct weston_view *view,
1348 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001349{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001351 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1352
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001354
1355 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001356 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001358 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 *vx = 0;
1360 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001361 return;
1362 }
1363
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364 *vx = v.f[0] / v.f[3];
1365 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001366 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 *vx = x - view->geometry.x;
1368 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001369 }
1370}
1371
1372WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373weston_view_from_global_fixed(struct weston_view *view,
1374 wl_fixed_t x, wl_fixed_t y,
1375 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001376{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001378
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 weston_view_from_global_float(view,
1380 wl_fixed_to_double(x),
1381 wl_fixed_to_double(y),
1382 &vxf, &vyf);
1383 *vx = wl_fixed_from_double(vxf);
1384 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001385}
1386
1387WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001388weston_view_from_global(struct weston_view *view,
1389 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001390{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001392
Jason Ekstranda7af7042013-10-12 22:38:11 -05001393 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1394 *vx = floorf(vxf);
1395 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001396}
1397
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001398WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001399weston_surface_schedule_repaint(struct weston_surface *surface)
1400{
1401 struct weston_output *output;
1402
1403 wl_list_for_each(output, &surface->compositor->output_list, link)
1404 if (surface->output_mask & (1 << output->id))
1405 weston_output_schedule_repaint(output);
1406}
1407
1408WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409weston_view_schedule_repaint(struct weston_view *view)
1410{
1411 struct weston_output *output;
1412
1413 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1414 if (view->output_mask & (1 << output->id))
1415 weston_output_schedule_repaint(output);
1416}
1417
Pekka Paalanene508ce62015-02-19 13:59:55 +02001418/**
1419 * XXX: This function does it the wrong way.
1420 * surface->damage is the damage from the client, and causes
1421 * surface_flush_damage() to copy pixels. No window management action can
1422 * cause damage to the client-provided content, warranting re-upload!
1423 *
1424 * Instead of surface->damage, this function should record the damage
1425 * with all the views for this surface to avoid extraneous texture
1426 * uploads.
1427 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001429weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001430{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001431 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 0, 0, surface->width,
1433 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001434
Kristian Høgsberg98238702012-08-03 16:29:12 -04001435 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001436}
1437
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001438WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001440{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001441 if (view->geometry.x == x && view->geometry.y == y)
1442 return;
1443
Jason Ekstranda7af7042013-10-12 22:38:11 -05001444 view->geometry.x = x;
1445 view->geometry.y = y;
1446 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001447}
1448
Pekka Paalanen483243f2013-03-08 14:56:50 +02001449static void
1450transform_parent_handle_parent_destroy(struct wl_listener *listener,
1451 void *data)
1452{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 struct weston_view *view =
1454 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001455 geometry.parent_destroy_listener);
1456
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001458}
1459
1460WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001462 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001463{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 if (view->geometry.parent) {
1465 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1466 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001467
1468 if (!parent)
1469 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001470 }
1471
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001473
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001475 transform_parent_handle_parent_destroy;
1476 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001477 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001478 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001479 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001481 }
1482
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 weston_view_geometry_dirty(view);
1484}
1485
Pekka Paalanen380adf52015-02-16 14:39:11 +02001486/** Set a clip mask rectangle on a view
1487 *
1488 * \param view The view to set the clip mask on.
1489 * \param x Top-left corner X coordinate of the clip rectangle.
1490 * \param y Top-left corner Y coordinate of the clip rectangle.
1491 * \param width Width of the clip rectangle, non-negative.
1492 * \param height Height of the clip rectangle, non-negative.
1493 *
1494 * A shell may set a clip mask rectangle on a view. Everything outside
1495 * the rectangle is cut away for input and output purposes: it is
1496 * not drawn and cannot be hit by hit-test based input like pointer
1497 * motion or touch-downs. Everything inside the rectangle will behave
1498 * normally. Clients are unaware of clipping.
1499 *
1500 * The rectangle is set in the surface local coordinates. Setting a clip
1501 * mask rectangle does not affect the view position, the view is positioned
1502 * as it would be without a clip. The clip also does not change
1503 * weston_surface::width,height.
1504 *
1505 * The clip mask rectangle is part of transformation inheritance
1506 * (weston_view_set_transform_parent()). A clip set in the root of the
1507 * transformation inheritance tree will affect all views in the tree.
1508 * A clip can be set only on the root view. Attempting to set a clip
1509 * on view that has a transformation parent will fail. Assigning a parent
1510 * to a view that has a clip set will cause the clip to be forgotten.
1511 *
1512 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1513 * on the additional transformations in the child views. These transformations
1514 * may not rotate the coordinate axes, i.e., only translation and scaling
1515 * are allowed. Violating this restriction causes the clipping to malfunction.
1516 * Furthermore, using scaling may cause rounding errors in child clipping.
1517 *
1518 * The clip mask rectangle is not automatically adjusted based on
1519 * wl_surface.attach dx and dy arguments.
1520 *
1521 * A clip mask rectangle can be set only if the compositor capability
1522 * WESTON_CAP_VIEW_CLIP_MASK is present.
1523 *
1524 * This function sets the clip mask rectangle and schedules a repaint for
1525 * the view.
1526 */
1527WL_EXPORT void
1528weston_view_set_mask(struct weston_view *view,
1529 int x, int y, int width, int height)
1530{
1531 struct weston_compositor *compositor = view->surface->compositor;
1532
1533 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1534 weston_log("%s not allowed without capability!\n", __func__);
1535 return;
1536 }
1537
1538 if (view->geometry.parent) {
1539 weston_log("view %p has a parent, clip forbidden!\n", view);
1540 return;
1541 }
1542
1543 if (width < 0 || height < 0) {
1544 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1545 x, y, width, height);
1546 return;
1547 }
1548
1549 pixman_region32_fini(&view->geometry.scissor);
1550 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1551 view->geometry.scissor_enabled = true;
1552 weston_view_geometry_dirty(view);
1553 weston_view_schedule_repaint(view);
1554}
1555
1556/** Remove the clip mask from a view
1557 *
1558 * \param view The view to remove the clip mask from.
1559 *
1560 * Removed the clip mask rectangle and schedules a repaint.
1561 *
1562 * \sa weston_view_set_mask
1563 */
1564WL_EXPORT void
1565weston_view_set_mask_infinite(struct weston_view *view)
1566{
1567 view->geometry.scissor_enabled = false;
1568 weston_view_geometry_dirty(view);
1569 weston_view_schedule_repaint(view);
1570}
1571
Derek Foreman280e7dd2014-10-03 13:13:42 -05001572WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573weston_view_is_mapped(struct weston_view *view)
1574{
1575 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001576 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001578 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001579}
1580
Derek Foreman280e7dd2014-10-03 13:13:42 -05001581WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001582weston_surface_is_mapped(struct weston_surface *surface)
1583{
1584 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001585 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001586 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001587 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001588}
1589
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001590static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001591surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001592{
1593 struct weston_view *view;
1594
1595 if (surface->width == width && surface->height == height)
1596 return;
1597
1598 surface->width = width;
1599 surface->height = height;
1600
1601 wl_list_for_each(view, &surface->views, surface_link)
1602 weston_view_geometry_dirty(view);
1603}
1604
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001605WL_EXPORT void
1606weston_surface_set_size(struct weston_surface *surface,
1607 int32_t width, int32_t height)
1608{
1609 assert(!surface->resource);
1610 surface_set_size(surface, width, height);
1611}
1612
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001613static int
1614fixed_round_up_to_int(wl_fixed_t f)
1615{
1616 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1617}
1618
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001619static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001620weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001621{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001622 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001623 int32_t width, height;
1624
1625 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001626 surface->width_from_buffer = 0;
1627 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001628 return;
1629 }
1630
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001631 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001632 case WL_OUTPUT_TRANSFORM_90:
1633 case WL_OUTPUT_TRANSFORM_270:
1634 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1635 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001636 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1637 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001638 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001639 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001640 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1641 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001642 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001643 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001644
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001645 surface->width_from_buffer = width;
1646 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001647}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001648
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001649static void
1650weston_surface_update_size(struct weston_surface *surface)
1651{
1652 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1653 int32_t width, height;
1654
1655 width = surface->width_from_buffer;
1656 height = surface->height_from_buffer;
1657
1658 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001659 surface_set_size(surface,
1660 vp->surface.width, vp->surface.height);
1661 return;
1662 }
1663
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001664 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001665 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1666 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1667
1668 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001669 return;
1670 }
1671
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001672 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001673}
1674
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001675WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001676weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001677{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001678 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001679
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001680 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001681
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001682 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001683}
1684
Jason Ekstranda7af7042013-10-12 22:38:11 -05001685WL_EXPORT struct weston_view *
1686weston_compositor_pick_view(struct weston_compositor *compositor,
1687 wl_fixed_t x, wl_fixed_t y,
1688 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001689{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001691 wl_fixed_t view_x, view_y;
1692 int view_ix, view_iy;
1693 int ix = wl_fixed_to_int(x);
1694 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001695
Jason Ekstranda7af7042013-10-12 22:38:11 -05001696 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001697 if (!pixman_region32_contains_point(
1698 &view->transform.boundingbox, ix, iy, NULL))
1699 continue;
1700
1701 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1702 view_ix = wl_fixed_to_int(view_x);
1703 view_iy = wl_fixed_to_int(view_y);
1704
1705 if (!pixman_region32_contains_point(&view->surface->input,
1706 view_ix, view_iy, NULL))
1707 continue;
1708
Pekka Paalanen380adf52015-02-16 14:39:11 +02001709 if (view->geometry.scissor_enabled &&
1710 !pixman_region32_contains_point(&view->geometry.scissor,
1711 view_ix, view_iy, NULL))
1712 continue;
1713
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001714 *vx = view_x;
1715 *vy = view_y;
1716 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001717 }
1718
1719 return NULL;
1720}
1721
1722static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001723weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001724{
Daniel Stone37816df2012-05-16 18:45:18 +01001725 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001726
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001727 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001728 return;
1729
Daniel Stone37816df2012-05-16 18:45:18 +01001730 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001731 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001732}
1733
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001734WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001736{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001737 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001738
Jason Ekstranda7af7042013-10-12 22:38:11 -05001739 if (!weston_view_is_mapped(view))
1740 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001741
Jason Ekstranda7af7042013-10-12 22:38:11 -05001742 weston_view_damage_below(view);
1743 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001744 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001745 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 wl_list_remove(&view->link);
1747 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001748 view->output_mask = 0;
1749 weston_surface_assign_output(view->surface);
1750
1751 if (weston_surface_is_mapped(view->surface))
1752 return;
1753
1754 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1755 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001756 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001757 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001758 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001759 NULL,
1760 wl_fixed_from_int(0),
1761 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762 if (seat->touch && seat->touch->focus == view)
Derek Foreman4c93c082015-04-30 16:45:41 -05001763 weston_touch_set_focus(seat->touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001764 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001765}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001766
Jason Ekstranda7af7042013-10-12 22:38:11 -05001767WL_EXPORT void
1768weston_surface_unmap(struct weston_surface *surface)
1769{
1770 struct weston_view *view;
1771
1772 wl_list_for_each(view, &surface->views, surface_link)
1773 weston_view_unmap(view);
1774 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001775}
1776
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001777static void
1778weston_surface_reset_pending_buffer(struct weston_surface *surface)
1779{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001780 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001781 surface->pending.sx = 0;
1782 surface->pending.sy = 0;
1783 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001784 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001785}
1786
Jason Ekstranda7af7042013-10-12 22:38:11 -05001787WL_EXPORT void
1788weston_view_destroy(struct weston_view *view)
1789{
1790 wl_signal_emit(&view->destroy_signal, view);
1791
1792 assert(wl_list_empty(&view->geometry.child_list));
1793
1794 if (weston_view_is_mapped(view)) {
1795 weston_view_unmap(view);
1796 weston_compositor_build_view_list(view->surface->compositor);
1797 }
1798
1799 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001800 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801
1802 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001803 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001804 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001805 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001806
1807 weston_view_set_transform_parent(view, NULL);
1808
Jason Ekstranda7af7042013-10-12 22:38:11 -05001809 wl_list_remove(&view->surface_link);
1810
1811 free(view);
1812}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001813
1814WL_EXPORT void
1815weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001816{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001817 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001819
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001820 if (--surface->ref_count > 0)
1821 return;
1822
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001823 assert(surface->resource == NULL);
1824
Pekka Paalanenca790762015-04-17 14:23:38 +03001825 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001826
Pekka Paalanene67858b2013-04-25 13:57:42 +03001827 assert(wl_list_empty(&surface->subsurface_list_pending));
1828 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001829
Jason Ekstranda7af7042013-10-12 22:38:11 -05001830 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1831 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001832
Jason Ekstrand7b982072014-05-20 14:33:03 -05001833 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001834
Pekka Paalanende685b82012-12-04 15:58:12 +02001835 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001836
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001837 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001838 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001839 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001840
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001841 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001842 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001843
Pekka Paalanen133e4392014-09-23 22:08:46 -04001844 weston_presentation_feedback_discard_list(&surface->feedback_list);
1845
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001846 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001847}
1848
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001849static void
1850destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001851{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001852 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001853
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001854 assert(surface);
1855
Giulio Camuffo0d379742013-11-15 22:06:15 +01001856 /* Set the resource to NULL, since we don't want to leave a
1857 * dangling pointer if the surface was refcounted and survives
1858 * the weston_surface_destroy() call. */
1859 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001860 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001861}
1862
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001863static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001864weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1865{
1866 struct weston_buffer *buffer =
1867 container_of(listener, struct weston_buffer, destroy_listener);
1868
1869 wl_signal_emit(&buffer->destroy_signal, buffer);
1870 free(buffer);
1871}
1872
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001873WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001874weston_buffer_from_resource(struct wl_resource *resource)
1875{
1876 struct weston_buffer *buffer;
1877 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001878
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001879 listener = wl_resource_get_destroy_listener(resource,
1880 weston_buffer_destroy_handler);
1881
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001882 if (listener)
1883 return container_of(listener, struct weston_buffer,
1884 destroy_listener);
1885
1886 buffer = zalloc(sizeof *buffer);
1887 if (buffer == NULL)
1888 return NULL;
1889
1890 buffer->resource = resource;
1891 wl_signal_init(&buffer->destroy_signal);
1892 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001893 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001894 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001895
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001896 return buffer;
1897}
1898
1899static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001900weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1901 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001902{
Pekka Paalanende685b82012-12-04 15:58:12 +02001903 struct weston_buffer_reference *ref =
1904 container_of(listener, struct weston_buffer_reference,
1905 destroy_listener);
1906
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001907 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001908 ref->buffer = NULL;
1909}
1910
1911WL_EXPORT void
1912weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001913 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001914{
1915 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001916 ref->buffer->busy_count--;
1917 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001918 assert(wl_resource_get_client(ref->buffer->resource));
1919 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001920 WL_BUFFER_RELEASE);
1921 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001922 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001923 }
1924
Pekka Paalanende685b82012-12-04 15:58:12 +02001925 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001926 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001927 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001928 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001929 }
1930
Pekka Paalanende685b82012-12-04 15:58:12 +02001931 ref->buffer = buffer;
1932 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1933}
1934
1935static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001936weston_surface_attach(struct weston_surface *surface,
1937 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001938{
1939 weston_buffer_reference(&surface->buffer_ref, buffer);
1940
Pekka Paalanena6421c42012-12-04 15:58:10 +02001941 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001942 if (weston_surface_is_mapped(surface))
1943 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001944 }
1945
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001946 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001947
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001948 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001949 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001950}
1951
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001952WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001953weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001954{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001955 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001956
1957 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001958 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001959}
1960
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001961WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001962weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001963{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001964 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001965
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001966 pixman_region32_union(&compositor->primary_plane.damage,
1967 &compositor->primary_plane.damage,
1968 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001969 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001970}
1971
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001972static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001973surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001974{
Pekka Paalanende685b82012-12-04 15:58:12 +02001975 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001976 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001977 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001978
Pekka Paalanenb5026542014-11-12 15:09:24 +02001979 if (weston_timeline_enabled_ &&
1980 pixman_region32_not_empty(&surface->damage))
1981 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1982 TLP_OUTPUT(surface->output), TLP_END);
1983
Jason Ekstrandef540082014-06-26 10:37:36 -07001984 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001985}
1986
1987static void
1988view_accumulate_damage(struct weston_view *view,
1989 pixman_region32_t *opaque)
1990{
1991 pixman_region32_t damage;
1992
1993 pixman_region32_init(&damage);
1994 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001995 pixman_box32_t *extents;
1996
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001998 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001999 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002000 pixman_region32_copy(&damage, &view->surface->damage);
2001 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002002 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002003 }
2004
Pekka Paalanen380adf52015-02-16 14:39:11 +02002005 pixman_region32_intersect(&damage, &damage,
2006 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002007 pixman_region32_subtract(&damage, &damage, opaque);
2008 pixman_region32_union(&view->plane->damage,
2009 &view->plane->damage, &damage);
2010 pixman_region32_fini(&damage);
2011 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002012 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002013}
2014
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002015static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002016compositor_accumulate_damage(struct weston_compositor *ec)
2017{
2018 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002019 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002020 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002021
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002022 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002023
2024 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002025 pixman_region32_copy(&plane->clip, &clip);
2026
2027 pixman_region32_init(&opaque);
2028
Jason Ekstranda7af7042013-10-12 22:38:11 -05002029 wl_list_for_each(ev, &ec->view_list, link) {
2030 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002031 continue;
2032
Jason Ekstranda7af7042013-10-12 22:38:11 -05002033 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002034 }
2035
2036 pixman_region32_union(&clip, &clip, &opaque);
2037 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002038 }
2039
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002040 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002041
Jason Ekstranda7af7042013-10-12 22:38:11 -05002042 wl_list_for_each(ev, &ec->view_list, link)
2043 ev->surface->touched = 0;
2044
2045 wl_list_for_each(ev, &ec->view_list, link) {
2046 if (ev->surface->touched)
2047 continue;
2048 ev->surface->touched = 1;
2049
2050 surface_flush_damage(ev->surface);
2051
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002052 /* Both the renderer and the backend have seen the buffer
2053 * by now. If renderer needs the buffer, it has its own
2054 * reference set. If the backend wants to keep the buffer
2055 * around for migrating the surface into a non-primary plane
2056 * later, keep_buffer is true. Otherwise, drop the core
2057 * reference now, and allow early buffer release. This enables
2058 * clients to use single-buffering.
2059 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002060 if (!ev->surface->keep_buffer)
2061 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002062 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002063}
2064
2065static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002066surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002067{
2068 struct weston_subsurface *sub;
2069
Pekka Paalanene67858b2013-04-25 13:57:42 +03002070 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002071 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002072 continue;
2073
Jason Ekstranda7af7042013-10-12 22:38:11 -05002074 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2075 wl_list_init(&sub->surface->views);
2076
2077 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002078 }
2079}
2080
2081static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002083{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002084 struct weston_subsurface *sub;
2085 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002086
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2088 if (sub->surface == surface)
2089 continue;
2090
George Kiagiadakised04d382014-06-13 18:10:26 +02002091 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2092 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002093 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002094 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002095
2096 surface_free_unused_subsurface_views(sub->surface);
2097 }
2098}
2099
2100static void
2101view_list_add_subsurface_view(struct weston_compositor *compositor,
2102 struct weston_subsurface *sub,
2103 struct weston_view *parent)
2104{
2105 struct weston_subsurface *child;
2106 struct weston_view *view = NULL, *iv;
2107
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002108 if (!weston_surface_is_mapped(sub->surface))
2109 return;
2110
Jason Ekstranda7af7042013-10-12 22:38:11 -05002111 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2112 if (iv->geometry.parent == parent) {
2113 view = iv;
2114 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002115 }
2116 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002117
2118 if (view) {
2119 /* Put it back in the surface's list of views */
2120 wl_list_remove(&view->surface_link);
2121 wl_list_insert(&sub->surface->views, &view->surface_link);
2122 } else {
2123 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002124 weston_view_set_position(view,
2125 sub->position.x,
2126 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127 weston_view_set_transform_parent(view, parent);
2128 }
2129
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002130 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002132
Pekka Paalanenb188e912013-11-19 14:03:35 +02002133 if (wl_list_empty(&sub->surface->subsurface_list)) {
2134 wl_list_insert(compositor->view_list.prev, &view->link);
2135 return;
2136 }
2137
2138 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2139 if (child->surface == sub->surface)
2140 wl_list_insert(compositor->view_list.prev, &view->link);
2141 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002142 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002143 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144}
2145
2146static void
2147view_list_add(struct weston_compositor *compositor,
2148 struct weston_view *view)
2149{
2150 struct weston_subsurface *sub;
2151
2152 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002153
Pekka Paalanenb188e912013-11-19 14:03:35 +02002154 if (wl_list_empty(&view->surface->subsurface_list)) {
2155 wl_list_insert(compositor->view_list.prev, &view->link);
2156 return;
2157 }
2158
2159 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2160 if (sub->surface == view->surface)
2161 wl_list_insert(compositor->view_list.prev, &view->link);
2162 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002163 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002164 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002165}
2166
2167static void
2168weston_compositor_build_view_list(struct weston_compositor *compositor)
2169{
2170 struct weston_view *view;
2171 struct weston_layer *layer;
2172
2173 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002174 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002175 surface_stash_subsurface_views(view->surface);
2176
2177 wl_list_init(&compositor->view_list);
2178 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002179 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180 view_list_add(compositor, view);
2181 }
2182 }
2183
2184 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002185 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002186 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002187}
2188
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002189static void
2190weston_output_take_feedback_list(struct weston_output *output,
2191 struct weston_surface *surface)
2192{
2193 struct weston_view *view;
2194 struct weston_presentation_feedback *feedback;
2195 uint32_t flags = 0xffffffff;
2196
2197 if (wl_list_empty(&surface->feedback_list))
2198 return;
2199
2200 /* All views must have the flag for the flag to survive. */
2201 wl_list_for_each(view, &surface->views, surface_link) {
2202 /* ignore views that are not on this output at all */
2203 if (view->output_mask & (1u << output->id))
2204 flags &= view->psf_flags;
2205 }
2206
2207 wl_list_for_each(feedback, &surface->feedback_list, link)
2208 feedback->psf_flags = flags;
2209
2210 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2211 wl_list_init(&surface->feedback_list);
2212}
2213
David Herrmann1edf44c2013-10-22 17:11:26 +02002214static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002215weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002216{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002217 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002218 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002219 struct weston_animation *animation, *next;
2220 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002221 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002222 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002223 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002224
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002225 if (output->destroying)
2226 return 0;
2227
Pekka Paalanenb5026542014-11-12 15:09:24 +02002228 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2229
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002230 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002231 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002232
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002233 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002234 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002235 } else {
2236 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002237 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002238 ev->psf_flags = 0;
2239 }
2240 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002241
Pekka Paalanene67858b2013-04-25 13:57:42 +03002242 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002243 wl_list_for_each(ev, &ec->view_list, link) {
2244 /* Note: This operation is safe to do multiple times on the
2245 * same surface.
2246 */
2247 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002248 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002249 &ev->surface->frame_callback_list);
2250 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002251
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002252 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002253 }
2254 }
2255
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002256 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002257
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002258 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002259 pixman_region32_intersect(&output_damage,
2260 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002261 pixman_region32_subtract(&output_damage,
2262 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002263
Scott Moreauccbf29d2012-02-22 14:21:41 -07002264 if (output->dirty)
2265 weston_output_update_matrix(output);
2266
David Herrmann1edf44c2013-10-22 17:11:26 +02002267 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002268
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002269 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002270
Kristian Høgsbergef044142011-06-21 15:02:12 -04002271 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002272
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002273 weston_compositor_repick(ec);
2274 wl_event_loop_dispatch(ec->input_loop, 0);
2275
Jonas Ådahldb773762012-06-13 00:01:21 +02002276 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002277 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002278 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002279 }
2280
Scott Moreaud64cf212012-06-08 19:40:54 -06002281 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002282 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002283 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002284 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002285
Pekka Paalanenb5026542014-11-12 15:09:24 +02002286 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2287
David Herrmann1edf44c2013-10-22 17:11:26 +02002288 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002289}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002290
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002291static int
2292weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002293{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002294 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002295
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002296 wl_event_loop_dispatch(compositor->input_loop, 0);
2297
2298 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002299}
2300
Pekka Paalanen82919792014-05-21 13:51:49 +03002301static void
2302weston_output_schedule_repaint_reset(struct weston_output *output)
2303{
2304 struct weston_compositor *compositor = output->compositor;
2305 struct wl_event_loop *loop;
2306 int fd;
2307
2308 output->repaint_scheduled = 0;
2309 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2310
2311 if (compositor->input_loop_source)
2312 return;
2313
2314 loop = wl_display_get_event_loop(compositor->wl_display);
2315 fd = wl_event_loop_get_fd(compositor->input_loop);
2316 compositor->input_loop_source =
2317 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2318 weston_compositor_read_input, compositor);
2319}
2320
Pekka Paalanen0513a952014-05-21 16:17:27 +03002321static int
2322output_repaint_timer_handler(void *data)
2323{
2324 struct weston_output *output = data;
2325 struct weston_compositor *compositor = output->compositor;
2326
2327 if (output->repaint_needed &&
2328 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2329 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2330 weston_output_repaint(output) == 0)
2331 return 0;
2332
2333 weston_output_schedule_repaint_reset(output);
2334
2335 return 0;
2336}
2337
Kristian Høgsbergef044142011-06-21 15:02:12 -04002338WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002339weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002340 const struct timespec *stamp,
2341 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002342{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002343 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002344 int32_t refresh_nsec;
2345 struct timespec now;
2346 struct timespec gone;
2347 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002348
Pekka Paalanenb5026542014-11-12 15:09:24 +02002349 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2350 TLP_VBLANK(stamp), TLP_END);
2351
Pekka Paalanen0513a952014-05-21 16:17:27 +03002352 refresh_nsec = 1000000000000LL / output->current_mode->refresh;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002353 weston_presentation_feedback_present_list(&output->feedback_list,
2354 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002355 output->msc,
2356 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002357
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002358 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002359
Pekka Paalanen0513a952014-05-21 16:17:27 +03002360 weston_compositor_read_presentation_clock(compositor, &now);
2361 timespec_sub(&gone, &now, stamp);
2362 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2363 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002364
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002365 if (msec < -1000 || msec > 1000) {
2366 static bool warned;
2367
2368 if (!warned)
2369 weston_log("Warning: computed repaint delay is "
2370 "insane: %d msec\n", msec);
2371 warned = true;
2372
2373 msec = 0;
2374 }
2375
2376 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002377 output_repaint_timer_handler(output);
2378 else
2379 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002380}
2381
2382static void
2383idle_repaint(void *data)
2384{
2385 struct weston_output *output = data;
2386
Jonas Ådahle5a12252013-04-05 23:07:11 +02002387 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002388}
2389
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002390WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002391weston_layer_entry_insert(struct weston_layer_entry *list,
2392 struct weston_layer_entry *entry)
2393{
2394 wl_list_insert(&list->link, &entry->link);
2395 entry->layer = list->layer;
2396}
2397
2398WL_EXPORT void
2399weston_layer_entry_remove(struct weston_layer_entry *entry)
2400{
2401 wl_list_remove(&entry->link);
2402 wl_list_init(&entry->link);
2403 entry->layer = NULL;
2404}
2405
2406WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002407weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2408{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002409 wl_list_init(&layer->view_list.link);
2410 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002411 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002412 if (below != NULL)
2413 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002414}
2415
2416WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002417weston_layer_set_mask(struct weston_layer *layer,
2418 int x, int y, int width, int height)
2419{
2420 struct weston_view *view;
2421
2422 layer->mask.x1 = x;
2423 layer->mask.x2 = x + width;
2424 layer->mask.y1 = y;
2425 layer->mask.y2 = y + height;
2426
2427 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2428 weston_view_geometry_dirty(view);
2429 }
2430}
2431
2432WL_EXPORT void
2433weston_layer_set_mask_infinite(struct weston_layer *layer)
2434{
2435 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2436 UINT32_MAX, UINT32_MAX);
2437}
2438
2439WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002440weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002441{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002442 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002443 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002444
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002445 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2446 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002447 return;
2448
Pekka Paalanenb5026542014-11-12 15:09:24 +02002449 if (!output->repaint_needed)
2450 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2451
Kristian Høgsbergef044142011-06-21 15:02:12 -04002452 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002453 output->repaint_needed = 1;
2454 if (output->repaint_scheduled)
2455 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002456
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002457 wl_event_loop_add_idle(loop, idle_repaint, output);
2458 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002459 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2460
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002461
2462 if (compositor->input_loop_source) {
2463 wl_event_source_remove(compositor->input_loop_source);
2464 compositor->input_loop_source = NULL;
2465 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002466}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002467
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002468WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002469weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2470{
2471 struct weston_output *output;
2472
2473 wl_list_for_each(output, &compositor->output_list, link)
2474 weston_output_schedule_repaint(output);
2475}
2476
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002477static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002478surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002479{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002480 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002481}
2482
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002483static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002484surface_attach(struct wl_client *client,
2485 struct wl_resource *resource,
2486 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2487{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002488 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002489 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002490
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002491 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002492 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002493 if (buffer == NULL) {
2494 wl_client_post_no_memory(client);
2495 return;
2496 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002497 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002498
Pekka Paalanende685b82012-12-04 15:58:12 +02002499 /* Attach, attach, without commit in between does not send
2500 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002501 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002502
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002503 surface->pending.sx = sx;
2504 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002505 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002506}
2507
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002508static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002509surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002510 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002511 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002512{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002513 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002514
Pekka Paalanen8e159182012-10-10 12:49:25 +03002515 pixman_region32_union_rect(&surface->pending.damage,
2516 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002517 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002518}
2519
Kristian Høgsberg33418202011-08-16 23:01:28 -04002520static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002521destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002522{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002523 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002524
2525 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002526 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002527}
2528
2529static void
2530surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002531 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002532{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002533 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002534 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002535
2536 cb = malloc(sizeof *cb);
2537 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002538 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002539 return;
2540 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002541
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002542 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2543 callback);
2544 if (cb->resource == NULL) {
2545 free(cb);
2546 wl_resource_post_no_memory(resource);
2547 return;
2548 }
2549
Jason Ekstranda85118c2013-06-27 20:17:02 -05002550 wl_resource_set_implementation(cb->resource, NULL, cb,
2551 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002552
Pekka Paalanenbc106382012-10-10 12:49:31 +03002553 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002554}
2555
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002556static void
2557surface_set_opaque_region(struct wl_client *client,
2558 struct wl_resource *resource,
2559 struct wl_resource *region_resource)
2560{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002561 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002562 struct weston_region *region;
2563
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002564 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002565 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002566 pixman_region32_copy(&surface->pending.opaque,
2567 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002568 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002569 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002570 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002571}
2572
2573static void
2574surface_set_input_region(struct wl_client *client,
2575 struct wl_resource *resource,
2576 struct wl_resource *region_resource)
2577{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002578 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002579 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002580
2581 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002582 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002583 pixman_region32_copy(&surface->pending.input,
2584 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002585 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002586 pixman_region32_fini(&surface->pending.input);
2587 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002588 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002589}
2590
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002591static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002592weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002593{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002594 struct weston_subsurface *sub;
2595
2596 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2597 parent_link_pending) {
2598 wl_list_remove(&sub->parent_link);
2599 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2600 }
2601}
2602
2603static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002604weston_surface_build_buffer_matrix(struct weston_surface *surface,
2605 struct weston_matrix *matrix)
2606{
2607 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2608 double src_width, src_height, dest_width, dest_height;
2609
2610 weston_matrix_init(matrix);
2611
2612 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2613 src_width = surface->width_from_buffer;
2614 src_height = surface->height_from_buffer;
2615 } else {
2616 src_width = wl_fixed_to_double(vp->buffer.src_width);
2617 src_height = wl_fixed_to_double(vp->buffer.src_height);
2618 }
2619
2620 if (vp->surface.width == -1) {
2621 dest_width = src_width;
2622 dest_height = src_height;
2623 } else {
2624 dest_width = vp->surface.width;
2625 dest_height = vp->surface.height;
2626 }
2627
2628 if (src_width != dest_width || src_height != dest_height)
2629 weston_matrix_scale(matrix,
2630 src_width / dest_width,
2631 src_height / dest_height, 1);
2632
2633 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2634 weston_matrix_translate(matrix,
2635 wl_fixed_to_double(vp->buffer.src_x),
2636 wl_fixed_to_double(vp->buffer.src_y),
2637 0);
2638
2639 switch (vp->buffer.transform) {
2640 case WL_OUTPUT_TRANSFORM_FLIPPED:
2641 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2642 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2643 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2644 weston_matrix_scale(matrix, -1, 1, 1);
2645 weston_matrix_translate(matrix,
2646 surface->width_from_buffer, 0, 0);
2647 break;
2648 }
2649
2650 switch (vp->buffer.transform) {
2651 default:
2652 case WL_OUTPUT_TRANSFORM_NORMAL:
2653 case WL_OUTPUT_TRANSFORM_FLIPPED:
2654 break;
2655 case WL_OUTPUT_TRANSFORM_90:
2656 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2657 weston_matrix_rotate_xy(matrix, 0, 1);
2658 weston_matrix_translate(matrix,
2659 surface->height_from_buffer, 0, 0);
2660 break;
2661 case WL_OUTPUT_TRANSFORM_180:
2662 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2663 weston_matrix_rotate_xy(matrix, -1, 0);
2664 weston_matrix_translate(matrix,
2665 surface->width_from_buffer,
2666 surface->height_from_buffer, 0);
2667 break;
2668 case WL_OUTPUT_TRANSFORM_270:
2669 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2670 weston_matrix_rotate_xy(matrix, 0, -1);
2671 weston_matrix_translate(matrix,
2672 0, surface->width_from_buffer, 0);
2673 break;
2674 }
2675
2676 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2677}
2678
2679static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002680weston_surface_commit_state(struct weston_surface *surface,
2681 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002682{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002683 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002684 pixman_region32_t opaque;
2685
Alexander Larsson4ea95522013-05-22 14:41:37 +02002686 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002687 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002688 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002689 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002690
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002691 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002692 if (state->newly_attached)
2693 weston_surface_attach(surface, state->buffer);
2694 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002695
Jason Ekstrand1e059042014-10-16 10:55:19 -05002696 weston_surface_build_buffer_matrix(surface,
2697 &surface->surface_to_buffer_matrix);
2698 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2699 &surface->surface_to_buffer_matrix);
2700
Jason Ekstrand7b982072014-05-20 14:33:03 -05002701 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002702 weston_surface_update_size(surface);
2703 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002704 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002705 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002706
Jason Ekstrand7b982072014-05-20 14:33:03 -05002707 state->sx = 0;
2708 state->sy = 0;
2709 state->newly_attached = 0;
2710 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002711
2712 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002713 if (weston_timeline_enabled_ &&
2714 pixman_region32_not_empty(&state->damage))
2715 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002716 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002717 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002718 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002719 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002720 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002721
2722 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002723 pixman_region32_init(&opaque);
2724 pixman_region32_intersect_rect(&opaque, &state->opaque,
2725 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002726
2727 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2728 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002729 wl_list_for_each(view, &surface->views, surface_link)
2730 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002731 }
2732
2733 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002734
2735 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002736 pixman_region32_intersect_rect(&surface->input, &state->input,
2737 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002738
Pekka Paalanenbc106382012-10-10 12:49:31 +03002739 /* wl_surface.frame */
2740 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002741 &state->frame_callback_list);
2742 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002743
2744 /* XXX:
2745 * What should happen with a feedback request, if there
2746 * is no wl_buffer attached for this commit?
2747 */
2748
2749 /* presentation.feedback */
2750 wl_list_insert_list(&surface->feedback_list,
2751 &state->feedback_list);
2752 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002753}
2754
2755static void
2756weston_surface_commit(struct weston_surface *surface)
2757{
2758 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002759
Pekka Paalanene67858b2013-04-25 13:57:42 +03002760 weston_surface_commit_subsurface_order(surface);
2761
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002762 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002763}
2764
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002765static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002766weston_subsurface_commit(struct weston_subsurface *sub);
2767
2768static void
2769weston_subsurface_parent_commit(struct weston_subsurface *sub,
2770 int parent_is_synchronized);
2771
2772static void
2773surface_commit(struct wl_client *client, struct wl_resource *resource)
2774{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002775 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002776 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2777
2778 if (sub) {
2779 weston_subsurface_commit(sub);
2780 return;
2781 }
2782
2783 weston_surface_commit(surface);
2784
2785 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2786 if (sub->surface != surface)
2787 weston_subsurface_parent_commit(sub, 0);
2788 }
2789}
2790
2791static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002792surface_set_buffer_transform(struct wl_client *client,
2793 struct wl_resource *resource, int transform)
2794{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002795 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002796
Jonny Lamba55f1392014-05-30 12:07:15 +02002797 /* if wl_output.transform grows more members this will need to be updated. */
2798 if (transform < 0 ||
2799 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2800 wl_resource_post_error(resource,
2801 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2802 "buffer transform must be a valid transform "
2803 "('%d' specified)", transform);
2804 return;
2805 }
2806
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002807 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002808 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002809}
2810
Alexander Larsson4ea95522013-05-22 14:41:37 +02002811static void
2812surface_set_buffer_scale(struct wl_client *client,
2813 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002814 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002815{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002816 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002817
Jonny Lamba55f1392014-05-30 12:07:15 +02002818 if (scale < 1) {
2819 wl_resource_post_error(resource,
2820 WL_SURFACE_ERROR_INVALID_SCALE,
2821 "buffer scale must be at least one "
2822 "('%d' specified)", scale);
2823 return;
2824 }
2825
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002826 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002827 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002828}
2829
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002830static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002831 surface_destroy,
2832 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002833 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002834 surface_frame,
2835 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002836 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002837 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002838 surface_set_buffer_transform,
2839 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002840};
2841
2842static void
2843compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002844 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002845{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002846 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002847 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002848
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002849 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002850 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002851 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002852 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002853 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002854
Jason Ekstranda85118c2013-06-27 20:17:02 -05002855 surface->resource =
2856 wl_resource_create(client, &wl_surface_interface,
2857 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002858 if (surface->resource == NULL) {
2859 weston_surface_destroy(surface);
2860 wl_resource_post_no_memory(resource);
2861 return;
2862 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002863 wl_resource_set_implementation(surface->resource, &surface_interface,
2864 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002865
2866 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002867}
2868
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002869static void
2870destroy_region(struct wl_resource *resource)
2871{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002872 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002873
2874 pixman_region32_fini(&region->region);
2875 free(region);
2876}
2877
2878static void
2879region_destroy(struct wl_client *client, struct wl_resource *resource)
2880{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002881 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002882}
2883
2884static void
2885region_add(struct wl_client *client, struct wl_resource *resource,
2886 int32_t x, int32_t y, int32_t width, int32_t height)
2887{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002888 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002889
2890 pixman_region32_union_rect(&region->region, &region->region,
2891 x, y, width, height);
2892}
2893
2894static void
2895region_subtract(struct wl_client *client, struct wl_resource *resource,
2896 int32_t x, int32_t y, int32_t width, int32_t height)
2897{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002898 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002899 pixman_region32_t rect;
2900
2901 pixman_region32_init_rect(&rect, x, y, width, height);
2902 pixman_region32_subtract(&region->region, &region->region, &rect);
2903 pixman_region32_fini(&rect);
2904}
2905
2906static const struct wl_region_interface region_interface = {
2907 region_destroy,
2908 region_add,
2909 region_subtract
2910};
2911
2912static void
2913compositor_create_region(struct wl_client *client,
2914 struct wl_resource *resource, uint32_t id)
2915{
2916 struct weston_region *region;
2917
2918 region = malloc(sizeof *region);
2919 if (region == NULL) {
2920 wl_resource_post_no_memory(resource);
2921 return;
2922 }
2923
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002924 pixman_region32_init(&region->region);
2925
Jason Ekstranda85118c2013-06-27 20:17:02 -05002926 region->resource =
2927 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002928 if (region->resource == NULL) {
2929 free(region);
2930 wl_resource_post_no_memory(resource);
2931 return;
2932 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002933 wl_resource_set_implementation(region->resource, &region_interface,
2934 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002935}
2936
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002937static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002938 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002939 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002940};
2941
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002942static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002943weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2944{
2945 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002946
Jason Ekstrand7b982072014-05-20 14:33:03 -05002947 weston_surface_commit_state(surface, &sub->cached);
2948 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002949
2950 weston_surface_commit_subsurface_order(surface);
2951
2952 weston_surface_schedule_repaint(surface);
2953
Jason Ekstrand7b982072014-05-20 14:33:03 -05002954 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002955}
2956
2957static void
2958weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2959{
2960 struct weston_surface *surface = sub->surface;
2961
2962 /*
2963 * If this commit would cause the surface to move by the
2964 * attach(dx, dy) parameters, the old damage region must be
2965 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002966 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002967 */
2968 pixman_region32_translate(&sub->cached.damage,
2969 -surface->pending.sx, -surface->pending.sy);
2970 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2971 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002972 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002973
2974 if (surface->pending.newly_attached) {
2975 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002976 weston_surface_state_set_buffer(&sub->cached,
2977 surface->pending.buffer);
2978 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002979 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002980 weston_presentation_feedback_discard_list(
2981 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002982 }
2983 sub->cached.sx += surface->pending.sx;
2984 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002985
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002986 sub->cached.buffer_viewport.changed |=
2987 surface->pending.buffer_viewport.changed;
2988 sub->cached.buffer_viewport.buffer =
2989 surface->pending.buffer_viewport.buffer;
2990 sub->cached.buffer_viewport.surface =
2991 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002992
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002993 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002994
2995 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2996
2997 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2998
2999 wl_list_insert_list(&sub->cached.frame_callback_list,
3000 &surface->pending.frame_callback_list);
3001 wl_list_init(&surface->pending.frame_callback_list);
3002
Pekka Paalanen133e4392014-09-23 22:08:46 -04003003 wl_list_insert_list(&sub->cached.feedback_list,
3004 &surface->pending.feedback_list);
3005 wl_list_init(&surface->pending.feedback_list);
3006
Jason Ekstrand7b982072014-05-20 14:33:03 -05003007 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003008}
3009
Derek Foreman280e7dd2014-10-03 13:13:42 -05003010static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003011weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3012{
3013 while (sub) {
3014 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003015 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003016
3017 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003018 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003019
3020 sub = weston_surface_to_subsurface(sub->parent);
3021 }
3022
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003023 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003024}
3025
3026static void
3027weston_subsurface_commit(struct weston_subsurface *sub)
3028{
3029 struct weston_surface *surface = sub->surface;
3030 struct weston_subsurface *tmp;
3031
3032 /* Recursive check for effectively synchronized. */
3033 if (weston_subsurface_is_synchronized(sub)) {
3034 weston_subsurface_commit_to_cache(sub);
3035 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003036 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003037 /* flush accumulated state from cache */
3038 weston_subsurface_commit_to_cache(sub);
3039 weston_subsurface_commit_from_cache(sub);
3040 } else {
3041 weston_surface_commit(surface);
3042 }
3043
3044 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3045 if (tmp->surface != surface)
3046 weston_subsurface_parent_commit(tmp, 0);
3047 }
3048 }
3049}
3050
3051static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003052weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003053{
3054 struct weston_surface *surface = sub->surface;
3055 struct weston_subsurface *tmp;
3056
Pekka Paalanene67858b2013-04-25 13:57:42 +03003057 /* From now on, commit_from_cache the whole sub-tree, regardless of
3058 * the synchronized mode of each child. This sub-surface or some
3059 * of its ancestors were synchronized, so we are synchronized
3060 * all the way down.
3061 */
3062
Jason Ekstrand7b982072014-05-20 14:33:03 -05003063 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003064 weston_subsurface_commit_from_cache(sub);
3065
3066 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3067 if (tmp->surface != surface)
3068 weston_subsurface_parent_commit(tmp, 1);
3069 }
3070}
3071
3072static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003073weston_subsurface_parent_commit(struct weston_subsurface *sub,
3074 int parent_is_synchronized)
3075{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003076 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003077 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003078 wl_list_for_each(view, &sub->surface->views, surface_link)
3079 weston_view_set_position(view,
3080 sub->position.x,
3081 sub->position.y);
3082
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003083 sub->position.set = 0;
3084 }
3085
3086 if (parent_is_synchronized || sub->synchronized)
3087 weston_subsurface_synchronized_commit(sub);
3088}
3089
Pekka Paalanen8274d902014-08-06 19:36:51 +03003090static int
3091subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3092{
3093 return snprintf(buf, len, "sub-surface");
3094}
3095
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003096static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003097subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003098{
3099 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003100 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003101
Jason Ekstranda7af7042013-10-12 22:38:11 -05003102 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003103 weston_view_set_position(view,
3104 view->geometry.x + dx,
3105 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003106
3107 /* No need to check parent mappedness, because if parent is not
3108 * mapped, parent is not in a visible layer, so this sub-surface
3109 * will not be drawn either.
3110 */
3111 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003112 struct weston_output *output;
3113
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003114 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003115 * because that would call it also for the parent surface,
3116 * which might not be mapped yet. That would lead to
3117 * inconsistent state, where the window could never be
3118 * mapped.
3119 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003120 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003121 * weston_surface_is_mapped() return true, so that when the
3122 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003123 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003124 */
3125 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003126 output = container_of(compositor->output_list.next,
3127 struct weston_output, link);
3128
3129 surface->output = output;
3130 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003131 }
3132}
3133
3134static struct weston_subsurface *
3135weston_surface_to_subsurface(struct weston_surface *surface)
3136{
3137 if (surface->configure == subsurface_configure)
3138 return surface->configure_private;
3139
3140 return NULL;
3141}
3142
Pekka Paalanen01388e22013-04-25 13:57:44 +03003143WL_EXPORT struct weston_surface *
3144weston_surface_get_main_surface(struct weston_surface *surface)
3145{
3146 struct weston_subsurface *sub;
3147
3148 while (surface && (sub = weston_surface_to_subsurface(surface)))
3149 surface = sub->parent;
3150
3151 return surface;
3152}
3153
Pekka Paalanen50b67472014-10-01 15:02:41 +03003154WL_EXPORT int
3155weston_surface_set_role(struct weston_surface *surface,
3156 const char *role_name,
3157 struct wl_resource *error_resource,
3158 uint32_t error_code)
3159{
3160 assert(role_name);
3161
3162 if (surface->role_name == NULL ||
3163 surface->role_name == role_name ||
3164 strcmp(surface->role_name, role_name) == 0) {
3165 surface->role_name = role_name;
3166
3167 return 0;
3168 }
3169
3170 wl_resource_post_error(error_resource, error_code,
3171 "Cannot assign role %s to wl_surface@%d,"
3172 " already has role %s\n",
3173 role_name,
3174 wl_resource_get_id(surface->resource),
3175 surface->role_name);
3176 return -1;
3177}
3178
Pekka Paalanen8274d902014-08-06 19:36:51 +03003179WL_EXPORT void
3180weston_surface_set_label_func(struct weston_surface *surface,
3181 int (*desc)(struct weston_surface *,
3182 char *, size_t))
3183{
3184 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003185 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003186}
3187
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003188/** Get the size of surface contents
3189 *
3190 * \param surface The surface to query.
3191 * \param width Returns the width of raw contents.
3192 * \param height Returns the height of raw contents.
3193 *
3194 * Retrieves the raw surface content size in pixels for the given surface.
3195 * This is the whole content size in buffer pixels. If the surface
3196 * has no content or the renderer does not implement this feature,
3197 * zeroes are returned.
3198 *
3199 * This function is used to determine the buffer size needed for
3200 * a weston_surface_copy_content() call.
3201 */
3202WL_EXPORT void
3203weston_surface_get_content_size(struct weston_surface *surface,
3204 int *width, int *height)
3205{
3206 struct weston_renderer *rer = surface->compositor->renderer;
3207
3208 if (!rer->surface_get_content_size) {
3209 *width = 0;
3210 *height = 0;
3211 return;
3212 }
3213
3214 rer->surface_get_content_size(surface, width, height);
3215}
3216
3217/** Copy surface contents to system memory.
3218 *
3219 * \param surface The surface to copy from.
3220 * \param target Pointer to the target memory buffer.
3221 * \param size Size of the target buffer in bytes.
3222 * \param src_x X location on contents to copy from.
3223 * \param src_y Y location on contents to copy from.
3224 * \param width Width in pixels of the area to copy.
3225 * \param height Height in pixels of the area to copy.
3226 * \return 0 for success, -1 for failure.
3227 *
3228 * Surface contents are maintained by the renderer. They can be in a
3229 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3230 * else.
3231 *
3232 * Surface contents are copied into memory pointed to by target,
3233 * which has size bytes of space available. The target memory
3234 * may be larger than needed, but being smaller returns an error.
3235 * The extra bytes in target may or may not be written; their content is
3236 * unspecified. Size must be large enough to hold the image.
3237 *
3238 * The image in the target memory will be arranged in rows from
3239 * top to bottom, and pixels on a row from left to right. The pixel
3240 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3241 * width * 4.
3242 *
3243 * Parameters src_x and src_y define the upper-left corner in buffer
3244 * coordinates (pixels) to copy from. Parameters width and height
3245 * define the size of the area to copy in pixels.
3246 *
3247 * The rectangle defined by src_x, src_y, width, height must fit in
3248 * the surface contents. Otherwise an error is returned.
3249 *
3250 * Use surface_get_data_size to determine the content size; the
3251 * needed target buffer size and rectangle limits.
3252 *
3253 * CURRENT IMPLEMENTATION RESTRICTIONS:
3254 * - the machine must be little-endian due to Pixman formats.
3255 *
3256 * NOTE: Pixman formats are premultiplied.
3257 */
3258WL_EXPORT int
3259weston_surface_copy_content(struct weston_surface *surface,
3260 void *target, size_t size,
3261 int src_x, int src_y,
3262 int width, int height)
3263{
3264 struct weston_renderer *rer = surface->compositor->renderer;
3265 int cw, ch;
3266 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3267
3268 if (!rer->surface_copy_content)
3269 return -1;
3270
3271 weston_surface_get_content_size(surface, &cw, &ch);
3272
3273 if (src_x < 0 || src_y < 0)
3274 return -1;
3275
3276 if (width <= 0 || height <= 0)
3277 return -1;
3278
3279 if (src_x + width > cw || src_y + height > ch)
3280 return -1;
3281
3282 if (width * bytespp * height > size)
3283 return -1;
3284
3285 return rer->surface_copy_content(surface, target, size,
3286 src_x, src_y, width, height);
3287}
3288
Pekka Paalanene67858b2013-04-25 13:57:42 +03003289static void
3290subsurface_set_position(struct wl_client *client,
3291 struct wl_resource *resource, int32_t x, int32_t y)
3292{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003293 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003294
3295 if (!sub)
3296 return;
3297
3298 sub->position.x = x;
3299 sub->position.y = y;
3300 sub->position.set = 1;
3301}
3302
3303static struct weston_subsurface *
3304subsurface_from_surface(struct weston_surface *surface)
3305{
3306 struct weston_subsurface *sub;
3307
3308 sub = weston_surface_to_subsurface(surface);
3309 if (sub)
3310 return sub;
3311
3312 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3313 if (sub->surface == surface)
3314 return sub;
3315
3316 return NULL;
3317}
3318
3319static struct weston_subsurface *
3320subsurface_sibling_check(struct weston_subsurface *sub,
3321 struct weston_surface *surface,
3322 const char *request)
3323{
3324 struct weston_subsurface *sibling;
3325
3326 sibling = subsurface_from_surface(surface);
3327
3328 if (!sibling) {
3329 wl_resource_post_error(sub->resource,
3330 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3331 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003332 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003333 return NULL;
3334 }
3335
3336 if (sibling->parent != sub->parent) {
3337 wl_resource_post_error(sub->resource,
3338 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3339 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003340 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003341 return NULL;
3342 }
3343
3344 return sibling;
3345}
3346
3347static void
3348subsurface_place_above(struct wl_client *client,
3349 struct wl_resource *resource,
3350 struct wl_resource *sibling_resource)
3351{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003352 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003353 struct weston_surface *surface =
3354 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003355 struct weston_subsurface *sibling;
3356
3357 if (!sub)
3358 return;
3359
3360 sibling = subsurface_sibling_check(sub, surface, "place_above");
3361 if (!sibling)
3362 return;
3363
3364 wl_list_remove(&sub->parent_link_pending);
3365 wl_list_insert(sibling->parent_link_pending.prev,
3366 &sub->parent_link_pending);
3367}
3368
3369static void
3370subsurface_place_below(struct wl_client *client,
3371 struct wl_resource *resource,
3372 struct wl_resource *sibling_resource)
3373{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003374 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003375 struct weston_surface *surface =
3376 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003377 struct weston_subsurface *sibling;
3378
3379 if (!sub)
3380 return;
3381
3382 sibling = subsurface_sibling_check(sub, surface, "place_below");
3383 if (!sibling)
3384 return;
3385
3386 wl_list_remove(&sub->parent_link_pending);
3387 wl_list_insert(&sibling->parent_link_pending,
3388 &sub->parent_link_pending);
3389}
3390
3391static void
3392subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3393{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003394 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003395
3396 if (sub)
3397 sub->synchronized = 1;
3398}
3399
3400static void
3401subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3402{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003403 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003404
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003405 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003406 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003407
3408 /* If sub became effectively desynchronized, flush. */
3409 if (!weston_subsurface_is_synchronized(sub))
3410 weston_subsurface_synchronized_commit(sub);
3411 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003412}
3413
3414static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003415weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3416{
3417 wl_list_remove(&sub->parent_link);
3418 wl_list_remove(&sub->parent_link_pending);
3419 wl_list_remove(&sub->parent_destroy_listener.link);
3420 sub->parent = NULL;
3421}
3422
3423static void
3424weston_subsurface_destroy(struct weston_subsurface *sub);
3425
3426static void
3427subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3428{
3429 struct weston_subsurface *sub =
3430 container_of(listener, struct weston_subsurface,
3431 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003432 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003433
3434 /* The protocol object (wl_resource) is left inert. */
3435 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003436 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003437
3438 weston_subsurface_destroy(sub);
3439}
3440
3441static void
3442subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3443{
3444 struct weston_subsurface *sub =
3445 container_of(listener, struct weston_subsurface,
3446 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003447 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003448 assert(sub->surface != sub->parent);
3449
3450 if (weston_surface_is_mapped(sub->surface))
3451 weston_surface_unmap(sub->surface);
3452
3453 weston_subsurface_unlink_parent(sub);
3454}
3455
3456static void
3457subsurface_resource_destroy(struct wl_resource *resource)
3458{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003459 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003460
3461 if (sub)
3462 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003463}
3464
3465static void
3466subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3467{
3468 wl_resource_destroy(resource);
3469}
3470
3471static void
3472weston_subsurface_link_parent(struct weston_subsurface *sub,
3473 struct weston_surface *parent)
3474{
3475 sub->parent = parent;
3476 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003477 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003478 &sub->parent_destroy_listener);
3479
3480 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3481 wl_list_insert(&parent->subsurface_list_pending,
3482 &sub->parent_link_pending);
3483}
3484
3485static void
3486weston_subsurface_link_surface(struct weston_subsurface *sub,
3487 struct weston_surface *surface)
3488{
3489 sub->surface = surface;
3490 sub->surface_destroy_listener.notify =
3491 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003492 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003493 &sub->surface_destroy_listener);
3494}
3495
3496static void
3497weston_subsurface_destroy(struct weston_subsurface *sub)
3498{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003499 struct weston_view *view, *next;
3500
Pekka Paalanene67858b2013-04-25 13:57:42 +03003501 assert(sub->surface);
3502
3503 if (sub->resource) {
3504 assert(weston_surface_to_subsurface(sub->surface) == sub);
3505 assert(sub->parent_destroy_listener.notify ==
3506 subsurface_handle_parent_destroy);
3507
George Kiagiadakised04d382014-06-13 18:10:26 +02003508 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3509 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003510 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003511 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003512
Pekka Paalanene67858b2013-04-25 13:57:42 +03003513 if (sub->parent)
3514 weston_subsurface_unlink_parent(sub);
3515
Jason Ekstrand7b982072014-05-20 14:33:03 -05003516 weston_surface_state_fini(&sub->cached);
3517 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003518
3519 sub->surface->configure = NULL;
3520 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003521 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003522 } else {
3523 /* the dummy weston_subsurface for the parent itself */
3524 assert(sub->parent_destroy_listener.notify == NULL);
3525 wl_list_remove(&sub->parent_link);
3526 wl_list_remove(&sub->parent_link_pending);
3527 }
3528
3529 wl_list_remove(&sub->surface_destroy_listener.link);
3530 free(sub);
3531}
3532
3533static const struct wl_subsurface_interface subsurface_implementation = {
3534 subsurface_destroy,
3535 subsurface_set_position,
3536 subsurface_place_above,
3537 subsurface_place_below,
3538 subsurface_set_sync,
3539 subsurface_set_desync
3540};
3541
3542static struct weston_subsurface *
3543weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3544 struct weston_surface *parent)
3545{
3546 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003547 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003548
Bryce Harringtonde16d892014-11-20 22:21:57 -08003549 sub = zalloc(sizeof *sub);
3550 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003551 return NULL;
3552
Jason Ekstranda7af7042013-10-12 22:38:11 -05003553 wl_list_init(&sub->unused_views);
3554
Jason Ekstranda85118c2013-06-27 20:17:02 -05003555 sub->resource =
3556 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003557 if (!sub->resource) {
3558 free(sub);
3559 return NULL;
3560 }
3561
Jason Ekstranda85118c2013-06-27 20:17:02 -05003562 wl_resource_set_implementation(sub->resource,
3563 &subsurface_implementation,
3564 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003565 weston_subsurface_link_surface(sub, surface);
3566 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003567 weston_surface_state_init(&sub->cached);
3568 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003569 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003570
3571 return sub;
3572}
3573
3574/* Create a dummy subsurface for having the parent itself in its
3575 * sub-surface lists. Makes stacking order manipulation easy.
3576 */
3577static struct weston_subsurface *
3578weston_subsurface_create_for_parent(struct weston_surface *parent)
3579{
3580 struct weston_subsurface *sub;
3581
Bryce Harringtonde16d892014-11-20 22:21:57 -08003582 sub = zalloc(sizeof *sub);
3583 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003584 return NULL;
3585
3586 weston_subsurface_link_surface(sub, parent);
3587 sub->parent = parent;
3588 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3589 wl_list_insert(&parent->subsurface_list_pending,
3590 &sub->parent_link_pending);
3591
3592 return sub;
3593}
3594
3595static void
3596subcompositor_get_subsurface(struct wl_client *client,
3597 struct wl_resource *resource,
3598 uint32_t id,
3599 struct wl_resource *surface_resource,
3600 struct wl_resource *parent_resource)
3601{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003602 struct weston_surface *surface =
3603 wl_resource_get_user_data(surface_resource);
3604 struct weston_surface *parent =
3605 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003606 struct weston_subsurface *sub;
3607 static const char where[] = "get_subsurface: wl_subsurface@";
3608
3609 if (surface == parent) {
3610 wl_resource_post_error(resource,
3611 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3612 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003613 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003614 return;
3615 }
3616
3617 if (weston_surface_to_subsurface(surface)) {
3618 wl_resource_post_error(resource,
3619 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3620 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003621 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003622 return;
3623 }
3624
Pekka Paalanen50b67472014-10-01 15:02:41 +03003625 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3626 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003627 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003628
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003629 if (weston_surface_get_main_surface(parent) == surface) {
3630 wl_resource_post_error(resource,
3631 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3632 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003633 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003634 return;
3635 }
3636
Pekka Paalanene67858b2013-04-25 13:57:42 +03003637 /* make sure the parent is in its own list */
3638 if (wl_list_empty(&parent->subsurface_list)) {
3639 if (!weston_subsurface_create_for_parent(parent)) {
3640 wl_resource_post_no_memory(resource);
3641 return;
3642 }
3643 }
3644
3645 sub = weston_subsurface_create(id, surface, parent);
3646 if (!sub) {
3647 wl_resource_post_no_memory(resource);
3648 return;
3649 }
3650
3651 surface->configure = subsurface_configure;
3652 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003653 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003654}
3655
3656static void
3657subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3658{
3659 wl_resource_destroy(resource);
3660}
3661
3662static const struct wl_subcompositor_interface subcompositor_interface = {
3663 subcompositor_destroy,
3664 subcompositor_get_subsurface
3665};
3666
3667static void
3668bind_subcompositor(struct wl_client *client,
3669 void *data, uint32_t version, uint32_t id)
3670{
3671 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003672 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003673
Jason Ekstranda85118c2013-06-27 20:17:02 -05003674 resource =
3675 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003676 if (resource == NULL) {
3677 wl_client_post_no_memory(client);
3678 return;
3679 }
3680 wl_resource_set_implementation(resource, &subcompositor_interface,
3681 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003682}
3683
3684static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003685weston_compositor_dpms(struct weston_compositor *compositor,
3686 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003687{
3688 struct weston_output *output;
3689
3690 wl_list_for_each(output, &compositor->output_list, link)
3691 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003692 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003693}
3694
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003695WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003696weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003697{
Neil Roberts8b62e202013-09-30 13:14:47 +01003698 uint32_t old_state = compositor->state;
3699
3700 /* The state needs to be changed before emitting the wake
3701 * signal because that may try to schedule a repaint which
3702 * will not work if the compositor is still sleeping */
3703 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3704
3705 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003706 case WESTON_COMPOSITOR_SLEEPING:
3707 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3708 /* fall through */
3709 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003710 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003711 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003712 /* fall through */
3713 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003714 wl_event_source_timer_update(compositor->idle_source,
3715 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003716 }
3717}
3718
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003719WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003720weston_compositor_offscreen(struct weston_compositor *compositor)
3721{
3722 switch (compositor->state) {
3723 case WESTON_COMPOSITOR_OFFSCREEN:
3724 return;
3725 case WESTON_COMPOSITOR_SLEEPING:
3726 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3727 /* fall through */
3728 default:
3729 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3730 wl_event_source_timer_update(compositor->idle_source, 0);
3731 }
3732}
3733
3734WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003735weston_compositor_sleep(struct weston_compositor *compositor)
3736{
3737 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3738 return;
3739
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003740 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003741 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3742 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3743}
3744
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003745static int
3746idle_handler(void *data)
3747{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003748 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003749
3750 if (compositor->idle_inhibit)
3751 return 1;
3752
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003753 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003754 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003755
3756 return 1;
3757}
3758
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003759WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003760weston_plane_init(struct weston_plane *plane,
3761 struct weston_compositor *ec,
3762 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003763{
3764 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003765 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003766 plane->x = x;
3767 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003768 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003769
3770 /* Init the link so that the call to wl_list_remove() when releasing
3771 * the plane without ever stacking doesn't lead to a crash */
3772 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003773}
3774
3775WL_EXPORT void
3776weston_plane_release(struct weston_plane *plane)
3777{
Xiong Zhang97116532013-10-23 13:58:31 +08003778 struct weston_view *view;
3779
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003780 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003781 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003782
Xiong Zhang97116532013-10-23 13:58:31 +08003783 wl_list_for_each(view, &plane->compositor->view_list, link) {
3784 if (view->plane == plane)
3785 view->plane = NULL;
3786 }
3787
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003788 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003789}
3790
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003791WL_EXPORT void
3792weston_compositor_stack_plane(struct weston_compositor *ec,
3793 struct weston_plane *plane,
3794 struct weston_plane *above)
3795{
3796 if (above)
3797 wl_list_insert(above->link.prev, &plane->link);
3798 else
3799 wl_list_insert(&ec->plane_list, &plane->link);
3800}
3801
Casey Dahlin9074db52012-04-19 22:50:09 -04003802static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003803{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003804 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003805}
3806
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003807static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003808bind_output(struct wl_client *client,
3809 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003810{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003811 struct weston_output *output = data;
3812 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003813 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003814
Jason Ekstranda85118c2013-06-27 20:17:02 -05003815 resource = wl_resource_create(client, &wl_output_interface,
3816 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003817 if (resource == NULL) {
3818 wl_client_post_no_memory(client);
3819 return;
3820 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003821
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003822 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003823 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003824
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003825 wl_output_send_geometry(resource,
3826 output->x,
3827 output->y,
3828 output->mm_width,
3829 output->mm_height,
3830 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003831 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003832 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003833 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003834 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003835 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003836
3837 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003838 wl_output_send_mode(resource,
3839 mode->flags,
3840 mode->width,
3841 mode->height,
3842 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003843 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003844
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003845 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003846 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003847}
3848
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003849/* Move other outputs when one is removed so the space remains contiguos. */
3850static void
3851weston_compositor_remove_output(struct weston_compositor *compositor,
3852 struct weston_output *remove_output)
3853{
3854 struct weston_output *output;
3855 int offset = 0;
3856
3857 wl_list_for_each(output, &compositor->output_list, link) {
3858 if (output == remove_output) {
3859 offset = output->width;
3860 continue;
3861 }
3862
3863 if (offset > 0) {
3864 weston_output_move(output,
3865 output->x - offset, output->y);
3866 output->dirty = 1;
3867 }
3868 }
3869}
3870
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003871WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003872weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003873{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003874 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003875 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003876
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003877 output->destroying = 1;
3878
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003879 wl_list_for_each(view, &output->compositor->view_list, link) {
3880 if (view->output_mask & (1 << output->id))
3881 weston_view_assign_output(view);
3882 }
3883
Pekka Paalanen0513a952014-05-21 16:17:27 +03003884 wl_event_source_remove(output->repaint_timer);
3885
Pekka Paalanen133e4392014-09-23 22:08:46 -04003886 weston_presentation_feedback_discard_list(&output->feedback_list);
3887
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003888 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003889 wl_list_remove(&output->link);
3890
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003891 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003892 wl_signal_emit(&output->destroy_signal, output);
3893
Richard Hughesafe690c2013-05-02 10:10:04 +01003894 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003895 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003896 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003897 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003898
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003899 wl_resource_for_each(resource, &output->resource_list) {
3900 wl_resource_set_destructor(resource, NULL);
3901 }
3902
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003903 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003904}
3905
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003906WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003907weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003908{
Scott Moreau850ca422012-05-21 15:21:25 -06003909 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003910
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003911 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003912 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003913
Scott Moreauccbf29d2012-02-22 14:21:41 -07003914 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003915 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003916 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003917 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003918 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003919 weston_matrix_scale(&output->matrix, magnification,
3920 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003921 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003922
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003923 switch (output->transform) {
3924 case WL_OUTPUT_TRANSFORM_FLIPPED:
3925 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3926 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3927 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3928 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3929 weston_matrix_scale(&output->matrix, -1, 1, 1);
3930 break;
3931 }
3932
3933 switch (output->transform) {
3934 default:
3935 case WL_OUTPUT_TRANSFORM_NORMAL:
3936 case WL_OUTPUT_TRANSFORM_FLIPPED:
3937 break;
3938 case WL_OUTPUT_TRANSFORM_90:
3939 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3940 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
3941 weston_matrix_rotate_xy(&output->matrix, 0, 1);
3942 break;
3943 case WL_OUTPUT_TRANSFORM_180:
3944 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3945 weston_matrix_translate(&output->matrix,
3946 -output->width, -output->height, 0);
3947 weston_matrix_rotate_xy(&output->matrix, -1, 0);
3948 break;
3949 case WL_OUTPUT_TRANSFORM_270:
3950 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3951 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3952 weston_matrix_rotate_xy(&output->matrix, 0, -1);
3953 break;
3954 }
3955
3956 if (output->current_scale != 1)
3957 weston_matrix_scale(&output->matrix,
3958 output->current_scale,
3959 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003960
Scott Moreauccbf29d2012-02-22 14:21:41 -07003961 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05003962
3963 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003964}
3965
Scott Moreau1bad5db2012-08-18 01:04:05 -06003966static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003967weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003968{
3969 output->transform = transform;
3970
3971 switch (transform) {
3972 case WL_OUTPUT_TRANSFORM_90:
3973 case WL_OUTPUT_TRANSFORM_270:
3974 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3975 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3976 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003977 output->width = output->current_mode->height;
3978 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003979 break;
3980 case WL_OUTPUT_TRANSFORM_NORMAL:
3981 case WL_OUTPUT_TRANSFORM_180:
3982 case WL_OUTPUT_TRANSFORM_FLIPPED:
3983 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003984 output->width = output->current_mode->width;
3985 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003986 break;
3987 default:
3988 break;
3989 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003990
Hardening57388e42013-09-18 23:56:36 +02003991 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003992 output->width /= scale;
3993 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003994}
3995
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003996static void
3997weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003998{
3999 output->x = x;
4000 output->y = y;
4001
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004002 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004003 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004004 output->width,
4005 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004006}
4007
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004008WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004009weston_output_move(struct weston_output *output, int x, int y)
4010{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004011 struct wl_resource *resource;
4012
4013 output->move_x = x - output->x;
4014 output->move_y = y - output->y;
4015
4016 if (output->move_x == 0 && output->move_y == 0)
4017 return;
4018
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004019 weston_output_init_geometry(output, x, y);
4020
4021 output->dirty = 1;
4022
4023 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004024 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004025
4026 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004027 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004028 wl_output_send_geometry(resource,
4029 output->x,
4030 output->y,
4031 output->mm_width,
4032 output->mm_height,
4033 output->subpixel,
4034 output->make,
4035 output->model,
4036 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004037
4038 if (wl_resource_get_version(resource) >= 2)
4039 wl_output_send_done(resource);
4040 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004041}
4042
4043WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004044weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004045 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004046 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004047{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004048 struct wl_event_loop *loop;
4049
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004050 output->compositor = c;
4051 output->x = x;
4052 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004053 output->mm_width = mm_width;
4054 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004055 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004056 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004057
Alexander Larsson0b135062013-05-28 16:23:36 +02004058 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004059 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004060
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004061 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004062 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004063
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004064 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004065 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004066 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004067 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004068 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02004069
Pekka Paalanen0513a952014-05-21 16:17:27 +03004070 loop = wl_display_get_event_loop(c->wl_display);
4071 output->repaint_timer = wl_event_loop_add_timer(loop,
4072 output_repaint_timer_handler, output);
4073
Casey Dahlin58ba1372012-04-19 22:50:08 -04004074 output->id = ffs(~output->compositor->output_id_pool) - 1;
4075 output->compositor->output_id_pool |= 1 << output->id;
4076
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004077 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004078 wl_global_create(c->wl_display, &wl_output_interface, 2,
4079 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004080}
4081
4082/** Adds an output to the compositor's output list and
4083 * send the compositor's output_created signal.
4084 *
4085 * \param compositor The compositor instance.
4086 * \param output The output to be added.
4087 */
4088WL_EXPORT void
4089weston_compositor_add_output(struct weston_compositor *compositor,
4090 struct weston_output *output)
4091{
4092 wl_list_insert(compositor->output_list.prev, &output->link);
4093 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004094}
4095
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004096WL_EXPORT void
4097weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004098 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004099 wl_fixed_t *x, wl_fixed_t *y)
4100{
Derek Foreman0f679412014-10-02 13:41:17 -05004101 struct weston_vector p = { {
4102 wl_fixed_to_double(device_x),
4103 wl_fixed_to_double(device_y),
4104 0.0,
4105 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004106
Derek Foreman67a18b92015-03-24 11:36:14 -05004107 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004108
Derek Foreman0f679412014-10-02 13:41:17 -05004109 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4110 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004111}
4112
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004113static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004114destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004115{
Jonny Lamb74130762013-11-26 18:19:46 +01004116 struct weston_surface *surface =
4117 wl_resource_get_user_data(resource);
4118
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004119 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004120 surface->pending.buffer_viewport.buffer.src_width =
4121 wl_fixed_from_int(-1);
4122 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004123 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004124}
4125
4126static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004127viewport_destroy(struct wl_client *client,
4128 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004129{
4130 wl_resource_destroy(resource);
4131}
4132
4133static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004134viewport_set(struct wl_client *client,
4135 struct wl_resource *resource,
4136 wl_fixed_t src_x,
4137 wl_fixed_t src_y,
4138 wl_fixed_t src_width,
4139 wl_fixed_t src_height,
4140 int32_t dst_width,
4141 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004142{
Jonny Lamb74130762013-11-26 18:19:46 +01004143 struct weston_surface *surface =
4144 wl_resource_get_user_data(resource);
4145
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004146 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004147
Jonny Lamb8ae35902013-11-26 18:19:45 +01004148 if (wl_fixed_to_double(src_width) < 0 ||
4149 wl_fixed_to_double(src_height) < 0) {
4150 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004151 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004152 "source dimensions must be non-negative (%fx%f)",
4153 wl_fixed_to_double(src_width),
4154 wl_fixed_to_double(src_height));
4155 return;
4156 }
4157
4158 if (dst_width <= 0 || dst_height <= 0) {
4159 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004160 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004161 "destination dimensions must be positive (%dx%d)",
4162 dst_width, dst_height);
4163 return;
4164 }
4165
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004166 surface->pending.buffer_viewport.buffer.src_x = src_x;
4167 surface->pending.buffer_viewport.buffer.src_y = src_y;
4168 surface->pending.buffer_viewport.buffer.src_width = src_width;
4169 surface->pending.buffer_viewport.buffer.src_height = src_height;
4170 surface->pending.buffer_viewport.surface.width = dst_width;
4171 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004172 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004173}
4174
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004175static void
4176viewport_set_source(struct wl_client *client,
4177 struct wl_resource *resource,
4178 wl_fixed_t src_x,
4179 wl_fixed_t src_y,
4180 wl_fixed_t src_width,
4181 wl_fixed_t src_height)
4182{
4183 struct weston_surface *surface =
4184 wl_resource_get_user_data(resource);
4185
4186 assert(surface->viewport_resource != NULL);
4187
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004188 if (src_width == wl_fixed_from_int(-1) &&
4189 src_height == wl_fixed_from_int(-1)) {
4190 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004191 surface->pending.buffer_viewport.buffer.src_width =
4192 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004193 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004194 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004195 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004196
4197 if (src_width <= 0 || src_height <= 0) {
4198 wl_resource_post_error(resource,
4199 WL_VIEWPORT_ERROR_BAD_VALUE,
4200 "source size must be positive (%fx%f)",
4201 wl_fixed_to_double(src_width),
4202 wl_fixed_to_double(src_height));
4203 return;
4204 }
4205
4206 surface->pending.buffer_viewport.buffer.src_x = src_x;
4207 surface->pending.buffer_viewport.buffer.src_y = src_y;
4208 surface->pending.buffer_viewport.buffer.src_width = src_width;
4209 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004210 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004211}
4212
4213static void
4214viewport_set_destination(struct wl_client *client,
4215 struct wl_resource *resource,
4216 int32_t dst_width,
4217 int32_t dst_height)
4218{
4219 struct weston_surface *surface =
4220 wl_resource_get_user_data(resource);
4221
4222 assert(surface->viewport_resource != NULL);
4223
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004224 if (dst_width == -1 && dst_height == -1) {
4225 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004226 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004227 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004228 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004229 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004230
4231 if (dst_width <= 0 || dst_height <= 0) {
4232 wl_resource_post_error(resource,
4233 WL_VIEWPORT_ERROR_BAD_VALUE,
4234 "destination size must be positive (%dx%d)",
4235 dst_width, dst_height);
4236 return;
4237 }
4238
4239 surface->pending.buffer_viewport.surface.width = dst_width;
4240 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004241 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004242}
4243
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004244static const struct wl_viewport_interface viewport_interface = {
4245 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004246 viewport_set,
4247 viewport_set_source,
4248 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004249};
4250
4251static void
4252scaler_destroy(struct wl_client *client,
4253 struct wl_resource *resource)
4254{
4255 wl_resource_destroy(resource);
4256}
4257
4258static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004259scaler_get_viewport(struct wl_client *client,
4260 struct wl_resource *scaler,
4261 uint32_t id,
4262 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004263{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004264 int version = wl_resource_get_version(scaler);
4265 struct weston_surface *surface =
4266 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004267 struct wl_resource *resource;
4268
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004269 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004270 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004271 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4272 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004273 return;
4274 }
4275
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004276 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004277 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004278 if (resource == NULL) {
4279 wl_client_post_no_memory(client);
4280 return;
4281 }
4282
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004283 wl_resource_set_implementation(resource, &viewport_interface,
4284 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004285
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004286 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004287}
4288
4289static const struct wl_scaler_interface scaler_interface = {
4290 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004291 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004292};
4293
4294static void
4295bind_scaler(struct wl_client *client,
4296 void *data, uint32_t version, uint32_t id)
4297{
4298 struct wl_resource *resource;
4299
4300 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004301 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004302 if (resource == NULL) {
4303 wl_client_post_no_memory(client);
4304 return;
4305 }
4306
4307 wl_resource_set_implementation(resource, &scaler_interface,
4308 NULL, NULL);
4309}
4310
4311static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004312destroy_presentation_feedback(struct wl_resource *feedback_resource)
4313{
4314 struct weston_presentation_feedback *feedback;
4315
4316 feedback = wl_resource_get_user_data(feedback_resource);
4317
4318 wl_list_remove(&feedback->link);
4319 free(feedback);
4320}
4321
4322static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004323presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4324{
4325 wl_resource_destroy(resource);
4326}
4327
4328static void
4329presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004330 struct wl_resource *presentation_resource,
4331 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004332 uint32_t callback)
4333{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004334 struct weston_surface *surface;
4335 struct weston_presentation_feedback *feedback;
4336
4337 surface = wl_resource_get_user_data(surface_resource);
4338
Bryce Harringtonde16d892014-11-20 22:21:57 -08004339 feedback = zalloc(sizeof *feedback);
4340 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004341 goto err_calloc;
4342
4343 feedback->resource = wl_resource_create(client,
4344 &presentation_feedback_interface,
4345 1, callback);
4346 if (!feedback->resource)
4347 goto err_create;
4348
4349 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4350 destroy_presentation_feedback);
4351
4352 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4353
4354 return;
4355
4356err_create:
4357 free(feedback);
4358
4359err_calloc:
4360 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004361}
4362
4363static const struct presentation_interface presentation_implementation = {
4364 presentation_destroy,
4365 presentation_feedback
4366};
4367
4368static void
4369bind_presentation(struct wl_client *client,
4370 void *data, uint32_t version, uint32_t id)
4371{
4372 struct weston_compositor *compositor = data;
4373 struct wl_resource *resource;
4374
4375 resource = wl_resource_create(client, &presentation_interface,
4376 MIN(version, 1), id);
4377 if (resource == NULL) {
4378 wl_client_post_no_memory(client);
4379 return;
4380 }
4381
4382 wl_resource_set_implementation(resource, &presentation_implementation,
4383 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004384 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004385}
4386
4387static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004388compositor_bind(struct wl_client *client,
4389 void *data, uint32_t version, uint32_t id)
4390{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004391 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004392 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004393
Jason Ekstranda85118c2013-06-27 20:17:02 -05004394 resource = wl_resource_create(client, &wl_compositor_interface,
4395 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004396 if (resource == NULL) {
4397 wl_client_post_no_memory(client);
4398 return;
4399 }
4400
4401 wl_resource_set_implementation(resource, &compositor_interface,
4402 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004403}
4404
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004405WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004406weston_environment_get_fd(const char *env)
4407{
4408 char *e, *end;
4409 int fd, flags;
4410
4411 e = getenv(env);
4412 if (!e)
4413 return -1;
4414 fd = strtol(e, &end, 0);
4415 if (*end != '\0')
4416 return -1;
4417
4418 flags = fcntl(fd, F_GETFD);
4419 if (flags == -1)
4420 return -1;
4421
4422 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4423 unsetenv(env);
4424
4425 return fd;
4426}
4427
Pekka Paalanenb5026542014-11-12 15:09:24 +02004428static void
4429timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4430 uint32_t key, void *data)
4431{
4432 struct weston_compositor *compositor = data;
4433
4434 if (weston_timeline_enabled_)
4435 weston_timeline_close();
4436 else
4437 weston_timeline_open(compositor);
4438}
4439
Giulio Camuffo459137b2014-10-11 23:56:24 +03004440/** Create the compositor.
4441 *
4442 * This functions creates and initializes a compositor instance.
4443 *
4444 * \param display The Wayland display to be used.
4445 * \param user_data A pointer to an object that can later be retrieved
4446 * using the \ref weston_compositor_get_user_data function.
4447 * \return The compositor instance on success or NULL on failure.
4448 */
4449WL_EXPORT struct weston_compositor *
4450weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004451{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004452 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004453 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004454
Giulio Camuffo459137b2014-10-11 23:56:24 +03004455 ec = zalloc(sizeof *ec);
4456 if (!ec)
4457 return NULL;
4458
4459 ec->wl_display = display;
4460 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004461 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004462 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004463 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004464 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004465 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004466 wl_signal_init(&ec->idle_signal);
4467 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004468 wl_signal_init(&ec->show_input_panel_signal);
4469 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004470 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004471 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004472 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004473 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004474 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004475 wl_signal_init(&ec->session_signal);
4476 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004477
Casey Dahlin58ba1372012-04-19 22:50:08 -04004478 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004479 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004480
Giulio Camuffo954f1832014-10-11 18:27:30 +03004481 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004482 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004483 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004484
Giulio Camuffo954f1832014-10-11 18:27:30 +03004485 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004486 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004487 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004488
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004489 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004490 ec, bind_scaler))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004491 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004492
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004493 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4494 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004495 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004496
Jason Ekstranda7af7042013-10-12 22:38:11 -05004497 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004498 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004499 wl_list_init(&ec->layer_list);
4500 wl_list_init(&ec->seat_list);
4501 wl_list_init(&ec->output_list);
4502 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004503 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004504 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004505 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004506 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004507 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004508
Xiong Zhang97116532013-10-23 13:58:31 +08004509 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004510 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004511
Giulio Camuffo459137b2014-10-11 23:56:24 +03004512 wl_data_device_manager_init(ec->wl_display);
4513
4514 wl_display_init_shm(ec->wl_display);
4515
4516 loop = wl_display_get_event_loop(ec->wl_display);
4517 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4518 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4519
4520 ec->input_loop = wl_event_loop_create();
4521
4522 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4523 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4524
4525 weston_compositor_add_debug_binding(ec, KEY_T,
4526 timeline_key_binding_handler, ec);
4527
4528 weston_compositor_schedule_repaint(ec);
4529
4530 return ec;
4531
4532fail:
4533 free(ec);
4534 return NULL;
4535}
4536
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004537WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004538weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004539{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004540 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004541
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004542 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004543 if (ec->input_loop_source)
4544 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004545
Matt Roper361d2ad2011-08-29 13:52:23 -07004546 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004547 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004548 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004549
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004550 if (ec->renderer)
4551 ec->renderer->destroy(ec);
4552
Daniel Stone325fc2d2012-05-30 16:31:58 +01004553 weston_binding_list_destroy_all(&ec->key_binding_list);
4554 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004555 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004556 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004557 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004558
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004559 weston_plane_release(&ec->primary_plane);
4560
Jonas Ådahlc97af922012-03-28 22:36:09 +02004561 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07004562}
4563
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004564WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004565weston_compositor_exit_with_code(struct weston_compositor *compositor,
4566 int exit_code)
4567{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004568 if (compositor->exit_code == EXIT_SUCCESS)
4569 compositor->exit_code = exit_code;
4570
Giulio Camuffo459137b2014-10-11 23:56:24 +03004571 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004572}
4573
4574WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004575weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4576 const struct weston_pointer_grab_interface *interface)
4577{
4578 struct weston_seat *seat;
4579
4580 ec->default_pointer_grab = interface;
4581 wl_list_for_each(seat, &ec->seat_list, link) {
4582 if (seat->pointer) {
4583 weston_pointer_set_default_grab(seat->pointer,
4584 interface);
4585 }
4586 }
4587}
4588
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004589WL_EXPORT int
4590weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4591 clockid_t clk_id)
4592{
4593 struct timespec ts;
4594
4595 if (clock_gettime(clk_id, &ts) < 0)
4596 return -1;
4597
4598 compositor->presentation_clock = clk_id;
4599
4600 return 0;
4601}
4602
4603/*
4604 * For choosing the software clock, when the display hardware or API
4605 * does not expose a compatible presentation timestamp.
4606 */
4607WL_EXPORT int
4608weston_compositor_set_presentation_clock_software(
4609 struct weston_compositor *compositor)
4610{
4611 /* In order of preference */
4612 static const clockid_t clocks[] = {
4613 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4614 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4615 CLOCK_MONOTONIC, /* no jumps, may crawl */
4616 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4617 CLOCK_REALTIME /* may jump and crawl */
4618 };
4619 unsigned i;
4620
4621 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4622 if (weston_compositor_set_presentation_clock(compositor,
4623 clocks[i]) == 0)
4624 return 0;
4625
4626 weston_log("Error: no suitable presentation clock available.\n");
4627
4628 return -1;
4629}
4630
Pekka Paalanen662f3842015-03-18 12:17:26 +02004631/** Read the current time from the Presentation clock
4632 *
4633 * \param compositor
4634 * \param ts[out] The current time.
4635 *
4636 * \note Reading the current time in user space is always imprecise to some
4637 * degree.
4638 *
4639 * This function is never meant to fail. If reading the clock does fail,
4640 * an error message is logged and a zero time is returned. Callers are not
4641 * supposed to detect or react to failures.
4642 */
4643WL_EXPORT void
4644weston_compositor_read_presentation_clock(
4645 const struct weston_compositor *compositor,
4646 struct timespec *ts)
4647{
4648 static bool warned;
4649 int ret;
4650
4651 ret = clock_gettime(compositor->presentation_clock, ts);
4652 if (ret < 0) {
4653 ts->tv_sec = 0;
4654 ts->tv_nsec = 0;
4655
4656 if (!warned)
4657 weston_log("Error: failure to read "
4658 "the presentation clock %#x: '%m' (%d)\n",
4659 compositor->presentation_clock, errno);
4660 warned = true;
4661 }
4662}
4663
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004664WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004665weston_version(int *major, int *minor, int *micro)
4666{
4667 *major = WESTON_VERSION_MAJOR;
4668 *minor = WESTON_VERSION_MINOR;
4669 *micro = WESTON_VERSION_MICRO;
4670}
4671
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004672WL_EXPORT void *
4673weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004674{
4675 char path[PATH_MAX];
4676 void *module, *init;
4677
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004678 if (name == NULL)
4679 return NULL;
4680
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004681 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004682 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004683 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004684 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004685
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004686 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4687 if (module) {
4688 weston_log("Module '%s' already loaded\n", path);
4689 dlclose(module);
4690 return NULL;
4691 }
4692
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004693 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004694 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004695 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004696 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004697 return NULL;
4698 }
4699
4700 init = dlsym(module, entrypoint);
4701 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004702 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004703 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004704 return NULL;
4705 }
4706
4707 return init;
4708}
4709
Giulio Camuffo459137b2014-10-11 23:56:24 +03004710
4711/** Destroys the compositor.
4712 *
4713 * This function cleans up the compositor state and destroys it.
4714 *
4715 * \param compositor The compositor to be destroyed.
4716 */
4717WL_EXPORT void
4718weston_compositor_destroy(struct weston_compositor *compositor)
4719{
4720 /* prevent further rendering while shutting down */
4721 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4722
4723 wl_signal_emit(&compositor->destroy_signal, compositor);
4724
4725 weston_compositor_xkb_destroy(compositor);
4726
4727 compositor->backend->destroy(compositor);
4728 free(compositor);
4729}
4730
4731/** Instruct the compositor to exit.
4732 *
4733 * This functions does not directly destroy the compositor object, it merely
4734 * command it to start the tear down process. It is not guaranteed that the
4735 * tear down will happen immediately.
4736 *
4737 * \param compositor The compositor to tear down.
4738 */
4739WL_EXPORT void
4740weston_compositor_exit(struct weston_compositor *compositor)
4741{
4742 compositor->exit(compositor);
4743}
4744
4745/** Return the user data stored in the compositor.
4746 *
4747 * This function returns the user data pointer set with user_data parameter
4748 * to the \ref weston_compositor_create function.
4749 */
4750WL_EXPORT void *
4751weston_compositor_get_user_data(struct weston_compositor *compositor)
4752{
4753 return compositor->user_data;
4754}