blob: 67be07d4b2ce37d3a5b867347816354a193d11cb [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004 * Copyright © 2012-2015 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050026 */
27
Kristian Høgsberga9410222011-01-14 17:22:35 -050028#include "config.h"
29
Daniel Stoneb7452fe2012-06-01 12:14:06 +010030#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010035#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020037#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040038#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010039#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040040#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020041#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020042#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020043#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050045#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040046#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040047#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040048#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050049#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040050#include <sys/time.h>
51#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030052#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050053
Pekka Paalanenb5026542014-11-12 15:09:24 +020054#include "timeline.h"
55
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010057#include "scaler-server-protocol.h"
Pekka Paalanen31f7d782014-09-23 22:08:43 -040058#include "presentation_timing-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070059#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070060#include "shared/os-compatibility.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030061#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040062#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050063#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050064
Pekka Paalanen0513a952014-05-21 16:17:27 +030065#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
66
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020067static void
Alexander Larsson0b135062013-05-28 16:23:36 +020068weston_output_transform_scale_init(struct weston_output *output,
69 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020070
Rob Bradford27b17932013-06-26 18:08:46 +010071static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050072weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010073
Derek Foreman6ae7bc92014-11-04 10:47:33 -060074static void weston_mode_switch_finish(struct weston_output *output,
75 int mode_changed,
76 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080077{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020078 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020079 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020080 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060081 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020082
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020083 pixman_region32_init(&old_output_region);
84 pixman_region32_copy(&old_output_region, &output->region);
85
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020086 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020087 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020088
89 pixman_region32_init(&output->previous_damage);
90 pixman_region32_init_rect(&output->region, output->x, output->y,
91 output->width, output->height);
92
93 weston_output_update_matrix(output);
94
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020095 /* If a pointer falls outside the outputs new geometry, move it to its
96 * lower-right corner */
97 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -050098 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020099 int32_t x, y;
100
101 if (!pointer)
102 continue;
103
104 x = wl_fixed_to_int(pointer->x);
105 y = wl_fixed_to_int(pointer->y);
106
107 if (!pixman_region32_contains_point(&old_output_region,
108 x, y, NULL) ||
109 pixman_region32_contains_point(&output->region,
110 x, y, NULL))
111 continue;
112
113 if (x >= output->x + output->width)
114 x = output->x + output->width - 1;
115 if (y >= output->y + output->height)
116 y = output->y + output->height - 1;
117
118 pointer->x = wl_fixed_from_int(x);
119 pointer->y = wl_fixed_from_int(y);
120 }
121
122 pixman_region32_fini(&old_output_region);
123
Derek Foremandd4cd332014-11-10 10:29:59 -0600124 if (!mode_changed && !scale_changed)
125 return;
126
Hardening57388e42013-09-18 23:56:36 +0200127 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600128 wl_resource_for_each(resource, &output->resource_list) {
129 if (mode_changed) {
130 wl_output_send_mode(resource,
131 output->current_mode->flags,
132 output->current_mode->width,
133 output->current_mode->height,
134 output->current_mode->refresh);
135 }
Hardening57388e42013-09-18 23:56:36 +0200136
Derek Foreman41bdc272014-11-05 13:26:57 -0600137 version = wl_resource_get_version(resource);
138 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600139 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200140
Derek Foreman41bdc272014-11-05 13:26:57 -0600141 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
142 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600143 }
144}
145
146WL_EXPORT int
147weston_output_mode_set_native(struct weston_output *output,
148 struct weston_mode *mode,
149 int32_t scale)
150{
151 int ret;
152 int mode_changed = 0, scale_changed = 0;
153
154 if (!output->switch_mode)
155 return -1;
156
157 if (!output->original_mode) {
158 mode_changed = 1;
159 ret = output->switch_mode(output, mode);
160 if (ret < 0)
161 return ret;
162 if (output->current_scale != scale) {
163 scale_changed = 1;
164 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200165 }
166 }
167
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600168 output->native_mode = mode;
169 output->native_scale = scale;
170
171 weston_mode_switch_finish(output, mode_changed, scale_changed);
172
173 return 0;
174}
175
176WL_EXPORT int
177weston_output_mode_switch_to_native(struct weston_output *output)
178{
179 int ret;
180 int mode_changed = 0, scale_changed = 0;
181
182 if (!output->switch_mode)
183 return -1;
184
185 if (!output->original_mode) {
186 weston_log("already in the native mode\n");
187 return -1;
188 }
189 /* the non fullscreen clients haven't seen a mode set since we
190 * switched into a temporary, so we need to notify them if the
191 * mode at that time is different from the native mode now.
192 */
193 mode_changed = (output->original_mode != output->native_mode);
194 scale_changed = (output->original_scale != output->native_scale);
195
196 ret = output->switch_mode(output, output->native_mode);
197 if (ret < 0)
198 return ret;
199
200 output->current_scale = output->native_scale;
201
202 output->original_mode = NULL;
203 output->original_scale = 0;
204
205 weston_mode_switch_finish(output, mode_changed, scale_changed);
206
207 return 0;
208}
209
210WL_EXPORT int
211weston_output_mode_switch_to_temporary(struct weston_output *output,
212 struct weston_mode *mode,
213 int32_t scale)
214{
215 int ret;
216
217 if (!output->switch_mode)
218 return -1;
219
220 /* original_mode is the last mode non full screen clients have seen,
221 * so we shouldn't change it if we already have one set.
222 */
223 if (!output->original_mode) {
224 output->original_mode = output->native_mode;
225 output->original_scale = output->native_scale;
226 }
227 ret = output->switch_mode(output, mode);
228 if (ret < 0)
229 return ret;
230
231 output->current_scale = scale;
232
233 weston_mode_switch_finish(output, 0, 0);
234
235 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800236}
237
Benjamin Franzke06286262011-05-06 19:12:33 +0200238static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200239child_client_exec(int sockfd, const char *path)
240{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500241 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200243 sigset_t allsigs;
244
245 /* do not give our signal mask to the new process */
246 sigfillset(&allsigs);
247 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200248
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100249 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
250 if (seteuid(getuid()) == -1) {
251 weston_log("compositor: failed seteuid\n");
252 return;
253 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500254
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500255 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
256 * non-CLOEXEC fd to pass through exec. */
257 clientfd = dup(sockfd);
258 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200259 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500260 return;
261 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200262
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500263 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200264 setenv("WAYLAND_SOCKET", s, 1);
265
266 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200267 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200268 path);
269}
270
271WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272weston_client_launch(struct weston_compositor *compositor,
273 struct weston_process *proc,
274 const char *path,
275 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200276{
277 int sv[2];
278 pid_t pid;
279 struct wl_client *client;
280
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300281 weston_log("launching '%s'\n", path);
282
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300283 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200284 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200285 "socketpair failed while launching '%s': %m\n",
286 path);
287 return NULL;
288 }
289
290 pid = fork();
291 if (pid == -1) {
292 close(sv[0]);
293 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200295 "fork failed while launching '%s': %m\n", path);
296 return NULL;
297 }
298
299 if (pid == 0) {
300 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700301 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200302 }
303
304 close(sv[1]);
305
306 client = wl_client_create(compositor->wl_display, sv[0]);
307 if (!client) {
308 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200310 "wl_client_create failed while launching '%s'.\n",
311 path);
312 return NULL;
313 }
314
315 proc->pid = pid;
316 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200318
319 return client;
320}
321
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300322struct process_info {
323 struct weston_process proc;
324 char *path;
325};
326
327static void
328process_handle_sigchld(struct weston_process *process, int status)
329{
330 struct process_info *pinfo =
331 container_of(process, struct process_info, proc);
332
333 /*
334 * There are no guarantees whether this runs before or after
335 * the wl_client destructor.
336 */
337
338 if (WIFEXITED(status)) {
339 weston_log("%s exited with status %d\n", pinfo->path,
340 WEXITSTATUS(status));
341 } else if (WIFSIGNALED(status)) {
342 weston_log("%s died on signal %d\n", pinfo->path,
343 WTERMSIG(status));
344 } else {
345 weston_log("%s disappeared\n", pinfo->path);
346 }
347
348 free(pinfo->path);
349 free(pinfo);
350}
351
352WL_EXPORT struct wl_client *
353weston_client_start(struct weston_compositor *compositor, const char *path)
354{
355 struct process_info *pinfo;
356 struct wl_client *client;
357
358 pinfo = zalloc(sizeof *pinfo);
359 if (!pinfo)
360 return NULL;
361
362 pinfo->path = strdup(path);
363 if (!pinfo->path)
364 goto out_free;
365
366 client = weston_client_launch(compositor, &pinfo->proc, path,
367 process_handle_sigchld);
368 if (!client)
369 goto out_str;
370
371 return client;
372
373out_str:
374 free(pinfo->path);
375
376out_free:
377 free(pinfo);
378
379 return NULL;
380}
381
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200382static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300383region_init_infinite(pixman_region32_t *region)
384{
385 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
386 UINT32_MAX, UINT32_MAX);
387}
388
Pekka Paalanene67858b2013-04-25 13:57:42 +0300389static struct weston_subsurface *
390weston_surface_to_subsurface(struct weston_surface *surface);
391
Jason Ekstranda7af7042013-10-12 22:38:11 -0500392WL_EXPORT struct weston_view *
393weston_view_create(struct weston_surface *surface)
394{
395 struct weston_view *view;
396
Bryce Harringtonde16d892014-11-20 22:21:57 -0800397 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500398 if (view == NULL)
399 return NULL;
400
401 view->surface = surface;
402
Jason Ekstranda7af7042013-10-12 22:38:11 -0500403 /* Assign to surface */
404 wl_list_insert(&surface->views, &view->surface_link);
405
406 wl_signal_init(&view->destroy_signal);
407 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300408 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500409
Jason Ekstranda7af7042013-10-12 22:38:11 -0500410 pixman_region32_init(&view->clip);
411
412 view->alpha = 1.0;
413 pixman_region32_init(&view->transform.opaque);
414
415 wl_list_init(&view->geometry.transformation_list);
416 wl_list_insert(&view->geometry.transformation_list,
417 &view->transform.position.link);
418 weston_matrix_init(&view->transform.position.matrix);
419 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200420 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500421 pixman_region32_init(&view->transform.boundingbox);
422 view->transform.dirty = 1;
423
Jason Ekstranda7af7042013-10-12 22:38:11 -0500424 return view;
425}
426
Jason Ekstrand108865d2014-06-26 10:04:49 -0700427struct weston_frame_callback {
428 struct wl_resource *resource;
429 struct wl_list link;
430};
431
Pekka Paalanen133e4392014-09-23 22:08:46 -0400432struct weston_presentation_feedback {
433 struct wl_resource *resource;
434
435 /* XXX: could use just wl_resource_get_link() instead */
436 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200437
438 /* The per-surface feedback flags */
439 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400440};
441
442static void
443weston_presentation_feedback_discard(
444 struct weston_presentation_feedback *feedback)
445{
446 presentation_feedback_send_discarded(feedback->resource);
447 wl_resource_destroy(feedback->resource);
448}
449
450static void
451weston_presentation_feedback_discard_list(struct wl_list *list)
452{
453 struct weston_presentation_feedback *feedback, *tmp;
454
455 wl_list_for_each_safe(feedback, tmp, list, link)
456 weston_presentation_feedback_discard(feedback);
457}
458
459static void
460weston_presentation_feedback_present(
461 struct weston_presentation_feedback *feedback,
462 struct weston_output *output,
463 uint32_t refresh_nsec,
464 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200465 uint64_t seq,
466 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400467{
468 struct wl_client *client = wl_resource_get_client(feedback->resource);
469 struct wl_resource *o;
470 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400471
472 wl_resource_for_each(o, &output->resource_list) {
473 if (wl_resource_get_client(o) != client)
474 continue;
475
476 presentation_feedback_send_sync_output(feedback->resource, o);
477 }
478
479 secs = ts->tv_sec;
480 presentation_feedback_send_presented(feedback->resource,
481 secs >> 32, secs & 0xffffffff,
482 ts->tv_nsec,
483 refresh_nsec,
484 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200485 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400486 wl_resource_destroy(feedback->resource);
487}
488
489static void
490weston_presentation_feedback_present_list(struct wl_list *list,
491 struct weston_output *output,
492 uint32_t refresh_nsec,
493 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200494 uint64_t seq,
495 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400496{
497 struct weston_presentation_feedback *feedback, *tmp;
498
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200499 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
500 wl_list_empty(list));
501
Pekka Paalanen133e4392014-09-23 22:08:46 -0400502 wl_list_for_each_safe(feedback, tmp, list, link)
503 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200504 refresh_nsec, ts, seq,
505 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400506}
507
Jason Ekstrand7b982072014-05-20 14:33:03 -0500508static void
509surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
510{
511 struct weston_surface_state *state =
512 container_of(listener, struct weston_surface_state,
513 buffer_destroy_listener);
514
515 state->buffer = NULL;
516}
517
518static void
519weston_surface_state_init(struct weston_surface_state *state)
520{
521 state->newly_attached = 0;
522 state->buffer = NULL;
523 state->buffer_destroy_listener.notify =
524 surface_state_handle_buffer_destroy;
525 state->sx = 0;
526 state->sy = 0;
527
528 pixman_region32_init(&state->damage);
529 pixman_region32_init(&state->opaque);
530 region_init_infinite(&state->input);
531
532 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400533 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500534
535 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
536 state->buffer_viewport.buffer.scale = 1;
537 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
538 state->buffer_viewport.surface.width = -1;
539 state->buffer_viewport.changed = 0;
540}
541
542static void
543weston_surface_state_fini(struct weston_surface_state *state)
544{
545 struct weston_frame_callback *cb, *next;
546
547 wl_list_for_each_safe(cb, next,
548 &state->frame_callback_list, link)
549 wl_resource_destroy(cb->resource);
550
Pekka Paalanen133e4392014-09-23 22:08:46 -0400551 weston_presentation_feedback_discard_list(&state->feedback_list);
552
Jason Ekstrand7b982072014-05-20 14:33:03 -0500553 pixman_region32_fini(&state->input);
554 pixman_region32_fini(&state->opaque);
555 pixman_region32_fini(&state->damage);
556
557 if (state->buffer)
558 wl_list_remove(&state->buffer_destroy_listener.link);
559 state->buffer = NULL;
560}
561
562static void
563weston_surface_state_set_buffer(struct weston_surface_state *state,
564 struct weston_buffer *buffer)
565{
566 if (state->buffer == buffer)
567 return;
568
569 if (state->buffer)
570 wl_list_remove(&state->buffer_destroy_listener.link);
571 state->buffer = buffer;
572 if (state->buffer)
573 wl_signal_add(&state->buffer->destroy_signal,
574 &state->buffer_destroy_listener);
575}
576
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500577WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500578weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500579{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500580 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400581
Bryce Harringtonde16d892014-11-20 22:21:57 -0800582 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400583 if (surface == NULL)
584 return NULL;
585
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500586 wl_signal_init(&surface->destroy_signal);
587
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500588 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200589 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400590
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200591 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
592 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200593 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
594 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500595
596 weston_surface_state_init(&surface->pending);
597
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400598 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500599 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300600 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400601
Jason Ekstranda7af7042013-10-12 22:38:11 -0500602 wl_list_init(&surface->views);
603
604 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400605 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500606
Pekka Paalanene67858b2013-04-25 13:57:42 +0300607 wl_list_init(&surface->subsurface_list);
608 wl_list_init(&surface->subsurface_list_pending);
609
Jason Ekstrand1e059042014-10-16 10:55:19 -0500610 weston_matrix_init(&surface->buffer_to_surface_matrix);
611 weston_matrix_init(&surface->surface_to_buffer_matrix);
612
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400613 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500614}
615
Alex Wu8811bf92012-02-28 18:07:54 +0800616WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500617weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200618 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500619{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100620 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500621}
622
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400623WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500624weston_view_to_global_float(struct weston_view *view,
625 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200626{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500627 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200628 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
629
Jason Ekstranda7af7042013-10-12 22:38:11 -0500630 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200631
632 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200633 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700634 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200635 v.f[3]);
636 *x = 0;
637 *y = 0;
638 return;
639 }
640
641 *x = v.f[0] / v.f[3];
642 *y = v.f[1] / v.f[3];
643 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500644 *x = sx + view->geometry.x;
645 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200646 }
647}
648
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500649WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200650weston_transformed_coord(int width, int height,
651 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200652 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200653 float sx, float sy, float *bx, float *by)
654{
655 switch (transform) {
656 case WL_OUTPUT_TRANSFORM_NORMAL:
657 default:
658 *bx = sx;
659 *by = sy;
660 break;
661 case WL_OUTPUT_TRANSFORM_FLIPPED:
662 *bx = width - sx;
663 *by = sy;
664 break;
665 case WL_OUTPUT_TRANSFORM_90:
666 *bx = height - sy;
667 *by = sx;
668 break;
669 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
670 *bx = height - sy;
671 *by = width - sx;
672 break;
673 case WL_OUTPUT_TRANSFORM_180:
674 *bx = width - sx;
675 *by = height - sy;
676 break;
677 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
678 *bx = sx;
679 *by = height - sy;
680 break;
681 case WL_OUTPUT_TRANSFORM_270:
682 *bx = sy;
683 *by = width - sx;
684 break;
685 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
686 *bx = sy;
687 *by = sx;
688 break;
689 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200690
691 *bx *= scale;
692 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200693}
694
695WL_EXPORT pixman_box32_t
696weston_transformed_rect(int width, int height,
697 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200698 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200699 pixman_box32_t rect)
700{
701 float x1, x2, y1, y2;
702
703 pixman_box32_t ret;
704
Alexander Larsson4ea95522013-05-22 14:41:37 +0200705 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200706 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200707 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200708 rect.x2, rect.y2, &x2, &y2);
709
710 if (x1 <= x2) {
711 ret.x1 = x1;
712 ret.x2 = x2;
713 } else {
714 ret.x1 = x2;
715 ret.x2 = x1;
716 }
717
718 if (y1 <= y2) {
719 ret.y1 = y1;
720 ret.y2 = y2;
721 } else {
722 ret.y1 = y2;
723 ret.y2 = y1;
724 }
725
726 return ret;
727}
728
729WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500730weston_transformed_region(int width, int height,
731 enum wl_output_transform transform,
732 int32_t scale,
733 pixman_region32_t *src, pixman_region32_t *dest)
734{
735 pixman_box32_t *src_rects, *dest_rects;
736 int nrects, i;
737
738 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
739 if (src != dest)
740 pixman_region32_copy(dest, src);
741 return;
742 }
743
744 src_rects = pixman_region32_rectangles(src, &nrects);
745 dest_rects = malloc(nrects * sizeof(*dest_rects));
746 if (!dest_rects)
747 return;
748
749 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
750 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
751 } else {
752 for (i = 0; i < nrects; i++) {
753 switch (transform) {
754 default:
755 case WL_OUTPUT_TRANSFORM_NORMAL:
756 dest_rects[i].x1 = src_rects[i].x1;
757 dest_rects[i].y1 = src_rects[i].y1;
758 dest_rects[i].x2 = src_rects[i].x2;
759 dest_rects[i].y2 = src_rects[i].y2;
760 break;
761 case WL_OUTPUT_TRANSFORM_90:
762 dest_rects[i].x1 = height - src_rects[i].y2;
763 dest_rects[i].y1 = src_rects[i].x1;
764 dest_rects[i].x2 = height - src_rects[i].y1;
765 dest_rects[i].y2 = src_rects[i].x2;
766 break;
767 case WL_OUTPUT_TRANSFORM_180:
768 dest_rects[i].x1 = width - src_rects[i].x2;
769 dest_rects[i].y1 = height - src_rects[i].y2;
770 dest_rects[i].x2 = width - src_rects[i].x1;
771 dest_rects[i].y2 = height - src_rects[i].y1;
772 break;
773 case WL_OUTPUT_TRANSFORM_270:
774 dest_rects[i].x1 = src_rects[i].y1;
775 dest_rects[i].y1 = width - src_rects[i].x2;
776 dest_rects[i].x2 = src_rects[i].y2;
777 dest_rects[i].y2 = width - src_rects[i].x1;
778 break;
779 case WL_OUTPUT_TRANSFORM_FLIPPED:
780 dest_rects[i].x1 = width - src_rects[i].x2;
781 dest_rects[i].y1 = src_rects[i].y1;
782 dest_rects[i].x2 = width - src_rects[i].x1;
783 dest_rects[i].y2 = src_rects[i].y2;
784 break;
785 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
786 dest_rects[i].x1 = height - src_rects[i].y2;
787 dest_rects[i].y1 = width - src_rects[i].x2;
788 dest_rects[i].x2 = height - src_rects[i].y1;
789 dest_rects[i].y2 = width - src_rects[i].x1;
790 break;
791 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
792 dest_rects[i].x1 = src_rects[i].x1;
793 dest_rects[i].y1 = height - src_rects[i].y2;
794 dest_rects[i].x2 = src_rects[i].x2;
795 dest_rects[i].y2 = height - src_rects[i].y1;
796 break;
797 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
798 dest_rects[i].x1 = src_rects[i].y1;
799 dest_rects[i].y1 = src_rects[i].x1;
800 dest_rects[i].x2 = src_rects[i].y2;
801 dest_rects[i].y2 = src_rects[i].x2;
802 break;
803 }
804 }
805 }
806
807 if (scale != 1) {
808 for (i = 0; i < nrects; i++) {
809 dest_rects[i].x1 *= scale;
810 dest_rects[i].x2 *= scale;
811 dest_rects[i].y1 *= scale;
812 dest_rects[i].y2 *= scale;
813 }
814 }
815
816 pixman_region32_clear(dest);
817 pixman_region32_init_rects(dest, dest_rects, nrects);
818 free(dest_rects);
819}
820
Jonny Lamb74130762013-11-26 18:19:46 +0100821static void
822scaler_surface_to_buffer(struct weston_surface *surface,
823 float sx, float sy, float *bx, float *by)
824{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200825 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200826 double src_width, src_height;
827 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200828
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200829 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
830 if (vp->surface.width == -1) {
831 *bx = sx;
832 *by = sy;
833 return;
834 }
Jonny Lamb74130762013-11-26 18:19:46 +0100835
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200836 src_x = 0.0;
837 src_y = 0.0;
838 src_width = surface->width_from_buffer;
839 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100840 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200841 src_x = wl_fixed_to_double(vp->buffer.src_x);
842 src_y = wl_fixed_to_double(vp->buffer.src_y);
843 src_width = wl_fixed_to_double(vp->buffer.src_width);
844 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100845 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200846
847 *bx = sx * src_width / surface->width + src_x;
848 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100849}
850
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500851WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200852weston_surface_to_buffer_float(struct weston_surface *surface,
853 float sx, float sy, float *bx, float *by)
854{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200855 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
856
Jonny Lamb74130762013-11-26 18:19:46 +0100857 /* first transform coordinates if the scaler is set */
858 scaler_surface_to_buffer(surface, sx, sy, bx, by);
859
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500860 weston_transformed_coord(surface->width_from_buffer,
861 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200862 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100863 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200864}
865
Alexander Larsson4ea95522013-05-22 14:41:37 +0200866WL_EXPORT void
867weston_surface_to_buffer(struct weston_surface *surface,
868 int sx, int sy, int *bx, int *by)
869{
870 float bxf, byf;
871
Jonny Lamb74130762013-11-26 18:19:46 +0100872 weston_surface_to_buffer_float(surface,
873 sx, sy, &bxf, &byf);
874
Alexander Larsson4ea95522013-05-22 14:41:37 +0200875 *bx = floorf(bxf);
876 *by = floorf(byf);
877}
878
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200879WL_EXPORT pixman_box32_t
880weston_surface_to_buffer_rect(struct weston_surface *surface,
881 pixman_box32_t rect)
882{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200883 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100884 float xf, yf;
885
886 /* first transform box coordinates if the scaler is set */
887 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
888 rect.x1 = floorf(xf);
889 rect.y1 = floorf(yf);
890
891 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
892 rect.x2 = floorf(xf);
893 rect.y2 = floorf(yf);
894
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500895 return weston_transformed_rect(surface->width_from_buffer,
896 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200897 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200898 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200899}
900
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200901/** Transform a region from surface coordinates to buffer coordinates
902 *
903 * \param surface The surface to fetch wl_viewport and buffer transformation
904 * from.
905 * \param surface_region[in] The region in surface coordinates.
906 * \param buffer_region[out] The region converted to buffer coordinates.
907 *
908 * Buffer_region must be init'd, but will be completely overwritten.
909 *
910 * Viewport and buffer transformations can only do translation, scaling,
911 * and rotations in 90-degree steps. Therefore the only loss in the
912 * conversion is coordinate flooring (rounding).
913 */
914WL_EXPORT void
915weston_surface_to_buffer_region(struct weston_surface *surface,
916 pixman_region32_t *surface_region,
917 pixman_region32_t *buffer_region)
918{
919 pixman_box32_t *src_rects, *dest_rects;
920 int nrects, i;
921
922 src_rects = pixman_region32_rectangles(surface_region, &nrects);
923 dest_rects = malloc(nrects * sizeof(*dest_rects));
924 if (!dest_rects)
925 return;
926
927 for (i = 0; i < nrects; i++) {
928 dest_rects[i] = weston_surface_to_buffer_rect(surface,
929 src_rects[i]);
930 }
931
932 pixman_region32_fini(buffer_region);
933 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
934 free(dest_rects);
935}
936
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200937WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400939 struct weston_plane *plane)
940{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500941 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400942 return;
943
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944 weston_view_damage_below(view);
945 view->plane = plane;
946 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400947}
948
Pekka Paalanen51723d52015-02-17 13:10:01 +0200949/** Inflict damage on the plane where the view is visible.
950 *
951 * \param view The view that causes the damage.
952 *
953 * If the view is currently on a plane (including the primary plane),
954 * take the view's boundingbox, subtract all the opaque views that cover it,
955 * and add the remaining region as damage to the plane. This corresponds
956 * to the damage inflicted to the plane if this view disappeared.
957 *
958 * A repaint is scheduled for this view.
959 *
960 * The region of all opaque views covering this view is stored in
961 * weston_view::clip and updated by view_accumulate_damage() during
962 * weston_output_repaint(). Specifically, that region matches the
963 * scenegraph as it was last painted.
964 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400965WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500966weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200967{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500968 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200969
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500970 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200971 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800973 if (view->plane)
974 pixman_region32_union(&view->plane->damage,
975 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500976 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800977 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200978}
979
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400980static void
981weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
982{
983 uint32_t different = es->output_mask ^ mask;
984 uint32_t entered = mask & different;
985 uint32_t left = es->output_mask & different;
986 struct weston_output *output;
987 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500988 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400989
990 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500991 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400992 return;
993 if (different == 0)
994 return;
995
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500996 client = wl_resource_get_client(es->resource);
997
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400998 wl_list_for_each(output, &es->compositor->output_list, link) {
999 if (1 << output->id & different)
1000 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001001 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001002 client);
1003 if (resource == NULL)
1004 continue;
1005 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001006 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001007 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001008 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001009 }
1010}
1011
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001012
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001013static void
1014weston_surface_assign_output(struct weston_surface *es)
1015{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016 struct weston_output *new_output;
1017 struct weston_view *view;
1018 pixman_region32_t region;
1019 uint32_t max, area, mask;
1020 pixman_box32_t *e;
1021
1022 new_output = NULL;
1023 max = 0;
1024 mask = 0;
1025 pixman_region32_init(&region);
1026 wl_list_for_each(view, &es->views, surface_link) {
1027 if (!view->output)
1028 continue;
1029
1030 pixman_region32_intersect(&region, &view->transform.boundingbox,
1031 &view->output->region);
1032
1033 e = pixman_region32_extents(&region);
1034 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1035
1036 mask |= view->output_mask;
1037
1038 if (area >= max) {
1039 new_output = view->output;
1040 max = area;
1041 }
1042 }
1043 pixman_region32_fini(&region);
1044
1045 es->output = new_output;
1046 weston_surface_update_output_mask(es, mask);
1047}
1048
1049static void
1050weston_view_assign_output(struct weston_view *ev)
1051{
1052 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001053 struct weston_output *output, *new_output;
1054 pixman_region32_t region;
1055 uint32_t max, area, mask;
1056 pixman_box32_t *e;
1057
1058 new_output = NULL;
1059 max = 0;
1060 mask = 0;
1061 pixman_region32_init(&region);
1062 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001063 if (output->destroying)
1064 continue;
1065
Jason Ekstranda7af7042013-10-12 22:38:11 -05001066 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001067 &output->region);
1068
1069 e = pixman_region32_extents(&region);
1070 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1071
1072 if (area > 0)
1073 mask |= 1 << output->id;
1074
1075 if (area >= max) {
1076 new_output = output;
1077 max = area;
1078 }
1079 }
1080 pixman_region32_fini(&region);
1081
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 ev->output = new_output;
1083 ev->output_mask = mask;
1084
1085 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001086}
1087
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001088static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001089weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1090 int from_x, int from_y, int *to_x, int *to_y)
1091{
1092 float x, y;
1093
1094 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1095 weston_view_from_global_float(to, x, y, &x, &y);
1096
1097 *to_x = round(x);
1098 *to_y = round(y);
1099}
1100
1101static void
1102weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1103{
1104 pixman_box32_t *a;
1105 pixman_box32_t b;
1106
1107 a = pixman_region32_extents(&from->geometry.scissor);
1108
1109 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1110 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1111
1112 pixman_region32_fini(&to->geometry.scissor);
1113 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1114}
1115
1116static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001117view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001118 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001119{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001120 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1121 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001122 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001123 { inbox->x1, inbox->y1 },
1124 { inbox->x1, inbox->y2 },
1125 { inbox->x2, inbox->y1 },
1126 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001127 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001128 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001129 int i;
1130
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001131 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001132 /* avoid rounding empty bbox to 1x1 */
1133 pixman_region32_init(bbox);
1134 return;
1135 }
1136
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001137 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001138 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001140 if (x < min_x)
1141 min_x = x;
1142 if (x > max_x)
1143 max_x = x;
1144 if (y < min_y)
1145 min_y = y;
1146 if (y > max_y)
1147 max_y = y;
1148 }
1149
Pekka Paalanen219b9822012-02-08 15:38:37 +02001150 int_x = floorf(min_x);
1151 int_y = floorf(min_y);
1152 pixman_region32_init_rect(bbox, int_x, int_y,
1153 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001154}
1155
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001156static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001158{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001160
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001161 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162 view->geometry.x = roundf(view->geometry.x);
1163 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001164
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001165 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1167 view->transform.position.matrix.d[12] = view->geometry.x;
1168 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001169
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001171
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 view->transform.inverse = view->transform.position.matrix;
1173 view->transform.inverse.d[12] = -view->geometry.x;
1174 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001175
Jason Ekstranda7af7042013-10-12 22:38:11 -05001176 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001177 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001178 view->surface->width,
1179 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001180 if (view->geometry.scissor_enabled)
1181 pixman_region32_intersect(&view->transform.boundingbox,
1182 &view->transform.boundingbox,
1183 &view->geometry.scissor);
1184
1185 pixman_region32_translate(&view->transform.boundingbox,
1186 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001187
Jason Ekstranda7af7042013-10-12 22:38:11 -05001188 if (view->alpha == 1.0) {
1189 pixman_region32_copy(&view->transform.opaque,
1190 &view->surface->opaque);
1191 pixman_region32_translate(&view->transform.opaque,
1192 view->geometry.x,
1193 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001194 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001195}
1196
1197static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001199{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 struct weston_view *parent = view->geometry.parent;
1201 struct weston_matrix *matrix = &view->transform.matrix;
1202 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001203 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001204 pixman_region32_t surfregion;
1205 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001206
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001208
1209 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1211 view->transform.position.matrix.d[12] = view->geometry.x;
1212 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001213
1214 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001216 weston_matrix_multiply(matrix, &tform->matrix);
1217
Pekka Paalanen483243f2013-03-08 14:56:50 +02001218 if (parent)
1219 weston_matrix_multiply(matrix, &parent->transform.matrix);
1220
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001221 if (weston_matrix_invert(inverse, matrix) < 0) {
1222 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 weston_log("error: weston_view %p"
1224 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001225 return -1;
1226 }
1227
Pekka Paalanen380adf52015-02-16 14:39:11 +02001228 pixman_region32_init_rect(&surfregion, 0, 0,
1229 view->surface->width, view->surface->height);
1230 if (view->geometry.scissor_enabled)
1231 pixman_region32_intersect(&surfregion, &surfregion,
1232 &view->geometry.scissor);
1233 surfbox = pixman_region32_extents(&surfregion);
1234
1235 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1236 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001237
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001238 return 0;
1239}
1240
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001241static struct weston_layer *
1242get_view_layer(struct weston_view *view)
1243{
1244 if (view->parent_view)
1245 return get_view_layer(view->parent_view);
1246 return view->layer_link.layer;
1247}
1248
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001249WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001251{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001253 struct weston_layer *layer;
1254 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001255
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001257 return;
1258
Pekka Paalanen483243f2013-03-08 14:56:50 +02001259 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001261
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001263
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001265
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 pixman_region32_fini(&view->transform.boundingbox);
1267 pixman_region32_fini(&view->transform.opaque);
1268 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001269
Pekka Paalanencd403622012-01-25 13:37:39 +02001270 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 if (view->geometry.transformation_list.next ==
1272 &view->transform.position.link &&
1273 view->geometry.transformation_list.prev ==
1274 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001275 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001277 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 if (weston_view_update_transform_enable(view) < 0)
1279 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001280 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001281
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001282 layer = get_view_layer(view);
1283 if (layer) {
1284 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001285 pixman_region32_intersect(&view->transform.boundingbox,
1286 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001287 pixman_region32_intersect(&view->transform.opaque,
1288 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001289 pixman_region32_fini(&mask);
1290 }
1291
Pekka Paalanen380adf52015-02-16 14:39:11 +02001292 if (parent) {
1293 if (parent->geometry.scissor_enabled) {
1294 view->geometry.scissor_enabled = true;
1295 weston_view_transfer_scissor(parent, view);
1296 } else {
1297 view->geometry.scissor_enabled = false;
1298 }
1299 }
1300
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001302
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001304
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 wl_signal_emit(&view->surface->compositor->transform_signal,
1306 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001307}
1308
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001309WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001311{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001313
1314 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 * The invariant: if view->geometry.dirty, then all views
1316 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001317 * Corollary: if not parent->geometry.dirty, then all ancestors
1318 * are not dirty.
1319 */
1320
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001322 return;
1323
Jason Ekstranda7af7042013-10-12 22:38:11 -05001324 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001325
Jason Ekstranda7af7042013-10-12 22:38:11 -05001326 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001327 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001329}
1330
1331WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332weston_view_to_global_fixed(struct weston_view *view,
1333 wl_fixed_t vx, wl_fixed_t vy,
1334 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001335{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001336 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001337
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338 weston_view_to_global_float(view,
1339 wl_fixed_to_double(vx),
1340 wl_fixed_to_double(vy),
1341 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001342 *x = wl_fixed_from_double(xf);
1343 *y = wl_fixed_from_double(yf);
1344}
1345
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001346WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347weston_view_from_global_float(struct weston_view *view,
1348 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001349{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001351 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1352
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001354
1355 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001356 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001358 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 *vx = 0;
1360 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001361 return;
1362 }
1363
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364 *vx = v.f[0] / v.f[3];
1365 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001366 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 *vx = x - view->geometry.x;
1368 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001369 }
1370}
1371
1372WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373weston_view_from_global_fixed(struct weston_view *view,
1374 wl_fixed_t x, wl_fixed_t y,
1375 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001376{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001378
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 weston_view_from_global_float(view,
1380 wl_fixed_to_double(x),
1381 wl_fixed_to_double(y),
1382 &vxf, &vyf);
1383 *vx = wl_fixed_from_double(vxf);
1384 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001385}
1386
1387WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001388weston_view_from_global(struct weston_view *view,
1389 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001390{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001392
Jason Ekstranda7af7042013-10-12 22:38:11 -05001393 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1394 *vx = floorf(vxf);
1395 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001396}
1397
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001398WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001399weston_surface_schedule_repaint(struct weston_surface *surface)
1400{
1401 struct weston_output *output;
1402
1403 wl_list_for_each(output, &surface->compositor->output_list, link)
1404 if (surface->output_mask & (1 << output->id))
1405 weston_output_schedule_repaint(output);
1406}
1407
1408WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409weston_view_schedule_repaint(struct weston_view *view)
1410{
1411 struct weston_output *output;
1412
1413 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1414 if (view->output_mask & (1 << output->id))
1415 weston_output_schedule_repaint(output);
1416}
1417
Pekka Paalanene508ce62015-02-19 13:59:55 +02001418/**
1419 * XXX: This function does it the wrong way.
1420 * surface->damage is the damage from the client, and causes
1421 * surface_flush_damage() to copy pixels. No window management action can
1422 * cause damage to the client-provided content, warranting re-upload!
1423 *
1424 * Instead of surface->damage, this function should record the damage
1425 * with all the views for this surface to avoid extraneous texture
1426 * uploads.
1427 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001429weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001430{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001431 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 0, 0, surface->width,
1433 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001434
Kristian Høgsberg98238702012-08-03 16:29:12 -04001435 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001436}
1437
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001438WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001440{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001441 if (view->geometry.x == x && view->geometry.y == y)
1442 return;
1443
Jason Ekstranda7af7042013-10-12 22:38:11 -05001444 view->geometry.x = x;
1445 view->geometry.y = y;
1446 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001447}
1448
Pekka Paalanen483243f2013-03-08 14:56:50 +02001449static void
1450transform_parent_handle_parent_destroy(struct wl_listener *listener,
1451 void *data)
1452{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 struct weston_view *view =
1454 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001455 geometry.parent_destroy_listener);
1456
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001458}
1459
1460WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001462 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001463{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 if (view->geometry.parent) {
1465 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1466 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001467
1468 if (!parent)
1469 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001470 }
1471
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001473
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001475 transform_parent_handle_parent_destroy;
1476 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001477 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001478 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001479 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001481 }
1482
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 weston_view_geometry_dirty(view);
1484}
1485
Pekka Paalanen380adf52015-02-16 14:39:11 +02001486/** Set a clip mask rectangle on a view
1487 *
1488 * \param view The view to set the clip mask on.
1489 * \param x Top-left corner X coordinate of the clip rectangle.
1490 * \param y Top-left corner Y coordinate of the clip rectangle.
1491 * \param width Width of the clip rectangle, non-negative.
1492 * \param height Height of the clip rectangle, non-negative.
1493 *
1494 * A shell may set a clip mask rectangle on a view. Everything outside
1495 * the rectangle is cut away for input and output purposes: it is
1496 * not drawn and cannot be hit by hit-test based input like pointer
1497 * motion or touch-downs. Everything inside the rectangle will behave
1498 * normally. Clients are unaware of clipping.
1499 *
1500 * The rectangle is set in the surface local coordinates. Setting a clip
1501 * mask rectangle does not affect the view position, the view is positioned
1502 * as it would be without a clip. The clip also does not change
1503 * weston_surface::width,height.
1504 *
1505 * The clip mask rectangle is part of transformation inheritance
1506 * (weston_view_set_transform_parent()). A clip set in the root of the
1507 * transformation inheritance tree will affect all views in the tree.
1508 * A clip can be set only on the root view. Attempting to set a clip
1509 * on view that has a transformation parent will fail. Assigning a parent
1510 * to a view that has a clip set will cause the clip to be forgotten.
1511 *
1512 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1513 * on the additional transformations in the child views. These transformations
1514 * may not rotate the coordinate axes, i.e., only translation and scaling
1515 * are allowed. Violating this restriction causes the clipping to malfunction.
1516 * Furthermore, using scaling may cause rounding errors in child clipping.
1517 *
1518 * The clip mask rectangle is not automatically adjusted based on
1519 * wl_surface.attach dx and dy arguments.
1520 *
1521 * A clip mask rectangle can be set only if the compositor capability
1522 * WESTON_CAP_VIEW_CLIP_MASK is present.
1523 *
1524 * This function sets the clip mask rectangle and schedules a repaint for
1525 * the view.
1526 */
1527WL_EXPORT void
1528weston_view_set_mask(struct weston_view *view,
1529 int x, int y, int width, int height)
1530{
1531 struct weston_compositor *compositor = view->surface->compositor;
1532
1533 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1534 weston_log("%s not allowed without capability!\n", __func__);
1535 return;
1536 }
1537
1538 if (view->geometry.parent) {
1539 weston_log("view %p has a parent, clip forbidden!\n", view);
1540 return;
1541 }
1542
1543 if (width < 0 || height < 0) {
1544 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1545 x, y, width, height);
1546 return;
1547 }
1548
1549 pixman_region32_fini(&view->geometry.scissor);
1550 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1551 view->geometry.scissor_enabled = true;
1552 weston_view_geometry_dirty(view);
1553 weston_view_schedule_repaint(view);
1554}
1555
1556/** Remove the clip mask from a view
1557 *
1558 * \param view The view to remove the clip mask from.
1559 *
1560 * Removed the clip mask rectangle and schedules a repaint.
1561 *
1562 * \sa weston_view_set_mask
1563 */
1564WL_EXPORT void
1565weston_view_set_mask_infinite(struct weston_view *view)
1566{
1567 view->geometry.scissor_enabled = false;
1568 weston_view_geometry_dirty(view);
1569 weston_view_schedule_repaint(view);
1570}
1571
Derek Foreman280e7dd2014-10-03 13:13:42 -05001572WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573weston_view_is_mapped(struct weston_view *view)
1574{
1575 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001576 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001578 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001579}
1580
Derek Foreman280e7dd2014-10-03 13:13:42 -05001581WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001582weston_surface_is_mapped(struct weston_surface *surface)
1583{
1584 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001585 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001586 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001587 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001588}
1589
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001590static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001591surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001592{
1593 struct weston_view *view;
1594
1595 if (surface->width == width && surface->height == height)
1596 return;
1597
1598 surface->width = width;
1599 surface->height = height;
1600
1601 wl_list_for_each(view, &surface->views, surface_link)
1602 weston_view_geometry_dirty(view);
1603}
1604
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001605WL_EXPORT void
1606weston_surface_set_size(struct weston_surface *surface,
1607 int32_t width, int32_t height)
1608{
1609 assert(!surface->resource);
1610 surface_set_size(surface, width, height);
1611}
1612
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001613static int
1614fixed_round_up_to_int(wl_fixed_t f)
1615{
1616 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1617}
1618
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001619static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001620weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001621{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001622 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001623 int32_t width, height;
1624
1625 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001626 surface->width_from_buffer = 0;
1627 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001628 return;
1629 }
1630
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001631 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001632 case WL_OUTPUT_TRANSFORM_90:
1633 case WL_OUTPUT_TRANSFORM_270:
1634 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1635 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001636 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1637 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001638 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001639 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001640 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1641 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001642 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001643 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001644
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001645 surface->width_from_buffer = width;
1646 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001647}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001648
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001649static void
1650weston_surface_update_size(struct weston_surface *surface)
1651{
1652 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1653 int32_t width, height;
1654
1655 width = surface->width_from_buffer;
1656 height = surface->height_from_buffer;
1657
1658 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001659 surface_set_size(surface,
1660 vp->surface.width, vp->surface.height);
1661 return;
1662 }
1663
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001664 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001665 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1666 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1667
1668 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001669 return;
1670 }
1671
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001672 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001673}
1674
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001675WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001676weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001677{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001678 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001679
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001680 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001681
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001682 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001683}
1684
Jason Ekstranda7af7042013-10-12 22:38:11 -05001685WL_EXPORT struct weston_view *
1686weston_compositor_pick_view(struct weston_compositor *compositor,
1687 wl_fixed_t x, wl_fixed_t y,
1688 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001689{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001691 wl_fixed_t view_x, view_y;
1692 int view_ix, view_iy;
1693 int ix = wl_fixed_to_int(x);
1694 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001695
Jason Ekstranda7af7042013-10-12 22:38:11 -05001696 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001697 if (!pixman_region32_contains_point(
1698 &view->transform.boundingbox, ix, iy, NULL))
1699 continue;
1700
1701 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1702 view_ix = wl_fixed_to_int(view_x);
1703 view_iy = wl_fixed_to_int(view_y);
1704
1705 if (!pixman_region32_contains_point(&view->surface->input,
1706 view_ix, view_iy, NULL))
1707 continue;
1708
Pekka Paalanen380adf52015-02-16 14:39:11 +02001709 if (view->geometry.scissor_enabled &&
1710 !pixman_region32_contains_point(&view->geometry.scissor,
1711 view_ix, view_iy, NULL))
1712 continue;
1713
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001714 *vx = view_x;
1715 *vy = view_y;
1716 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001717 }
1718
Derek Foremanf9318d12015-05-11 15:40:11 -05001719 *vx = wl_fixed_from_int(-1000000);
1720 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001721 return NULL;
1722}
1723
1724static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001725weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001726{
Daniel Stone37816df2012-05-16 18:45:18 +01001727 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001728
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001729 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001730 return;
1731
Daniel Stone37816df2012-05-16 18:45:18 +01001732 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001733 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001734}
1735
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001736WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001737weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001738{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001739 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001740
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 if (!weston_view_is_mapped(view))
1742 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001743
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744 weston_view_damage_below(view);
1745 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001746 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001747 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001748 wl_list_remove(&view->link);
1749 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750 view->output_mask = 0;
1751 weston_surface_assign_output(view->surface);
1752
1753 if (weston_surface_is_mapped(view->surface))
1754 return;
1755
1756 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001757 struct weston_touch *touch = weston_seat_get_touch(seat);
1758 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1759 struct weston_keyboard *keyboard =
1760 weston_seat_get_keyboard(seat);
1761
1762 if (keyboard && keyboard->focus == view->surface)
1763 weston_keyboard_set_focus(keyboard, NULL);
1764 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001765 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001766 if (touch && touch->focus == view)
1767 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001768 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001769}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001770
Jason Ekstranda7af7042013-10-12 22:38:11 -05001771WL_EXPORT void
1772weston_surface_unmap(struct weston_surface *surface)
1773{
1774 struct weston_view *view;
1775
1776 wl_list_for_each(view, &surface->views, surface_link)
1777 weston_view_unmap(view);
1778 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001779}
1780
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001781static void
1782weston_surface_reset_pending_buffer(struct weston_surface *surface)
1783{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001784 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001785 surface->pending.sx = 0;
1786 surface->pending.sy = 0;
1787 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001788 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001789}
1790
Jason Ekstranda7af7042013-10-12 22:38:11 -05001791WL_EXPORT void
1792weston_view_destroy(struct weston_view *view)
1793{
1794 wl_signal_emit(&view->destroy_signal, view);
1795
1796 assert(wl_list_empty(&view->geometry.child_list));
1797
1798 if (weston_view_is_mapped(view)) {
1799 weston_view_unmap(view);
1800 weston_compositor_build_view_list(view->surface->compositor);
1801 }
1802
1803 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001804 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001805
1806 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001807 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001808 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001809 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001810
1811 weston_view_set_transform_parent(view, NULL);
1812
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813 wl_list_remove(&view->surface_link);
1814
1815 free(view);
1816}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001817
1818WL_EXPORT void
1819weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001820{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001821 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001822 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001823
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001824 if (--surface->ref_count > 0)
1825 return;
1826
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001827 assert(surface->resource == NULL);
1828
Pekka Paalanenca790762015-04-17 14:23:38 +03001829 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001830
Pekka Paalanene67858b2013-04-25 13:57:42 +03001831 assert(wl_list_empty(&surface->subsurface_list_pending));
1832 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001833
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1835 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001836
Jason Ekstrand7b982072014-05-20 14:33:03 -05001837 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001838
Pekka Paalanende685b82012-12-04 15:58:12 +02001839 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001840
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001841 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001842 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001843 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001844
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001845 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001846 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001847
Pekka Paalanen133e4392014-09-23 22:08:46 -04001848 weston_presentation_feedback_discard_list(&surface->feedback_list);
1849
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001850 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001851}
1852
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001853static void
1854destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001855{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001856 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001857
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001858 assert(surface);
1859
Giulio Camuffo0d379742013-11-15 22:06:15 +01001860 /* Set the resource to NULL, since we don't want to leave a
1861 * dangling pointer if the surface was refcounted and survives
1862 * the weston_surface_destroy() call. */
1863 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001864 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001865}
1866
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001867static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001868weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1869{
1870 struct weston_buffer *buffer =
1871 container_of(listener, struct weston_buffer, destroy_listener);
1872
1873 wl_signal_emit(&buffer->destroy_signal, buffer);
1874 free(buffer);
1875}
1876
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001877WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001878weston_buffer_from_resource(struct wl_resource *resource)
1879{
1880 struct weston_buffer *buffer;
1881 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001882
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001883 listener = wl_resource_get_destroy_listener(resource,
1884 weston_buffer_destroy_handler);
1885
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001886 if (listener)
1887 return container_of(listener, struct weston_buffer,
1888 destroy_listener);
1889
1890 buffer = zalloc(sizeof *buffer);
1891 if (buffer == NULL)
1892 return NULL;
1893
1894 buffer->resource = resource;
1895 wl_signal_init(&buffer->destroy_signal);
1896 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001897 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001898 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001899
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001900 return buffer;
1901}
1902
1903static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001904weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1905 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001906{
Pekka Paalanende685b82012-12-04 15:58:12 +02001907 struct weston_buffer_reference *ref =
1908 container_of(listener, struct weston_buffer_reference,
1909 destroy_listener);
1910
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001911 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001912 ref->buffer = NULL;
1913}
1914
1915WL_EXPORT void
1916weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001917 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001918{
1919 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001920 ref->buffer->busy_count--;
1921 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001922 assert(wl_resource_get_client(ref->buffer->resource));
1923 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001924 WL_BUFFER_RELEASE);
1925 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001926 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001927 }
1928
Pekka Paalanende685b82012-12-04 15:58:12 +02001929 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001930 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001931 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001932 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001933 }
1934
Pekka Paalanende685b82012-12-04 15:58:12 +02001935 ref->buffer = buffer;
1936 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1937}
1938
1939static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001940weston_surface_attach(struct weston_surface *surface,
1941 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001942{
1943 weston_buffer_reference(&surface->buffer_ref, buffer);
1944
Pekka Paalanena6421c42012-12-04 15:58:10 +02001945 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001946 if (weston_surface_is_mapped(surface))
1947 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001948 }
1949
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001950 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001951
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001952 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001953 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001954}
1955
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001956WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001957weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001958{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001959 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001960
1961 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001962 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001963}
1964
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001965WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001966weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001967{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001968 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001969
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001970 pixman_region32_union(&compositor->primary_plane.damage,
1971 &compositor->primary_plane.damage,
1972 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001973 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001974}
1975
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001976static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001977surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001978{
Pekka Paalanende685b82012-12-04 15:58:12 +02001979 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001980 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001981 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001982
Pekka Paalanenb5026542014-11-12 15:09:24 +02001983 if (weston_timeline_enabled_ &&
1984 pixman_region32_not_empty(&surface->damage))
1985 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1986 TLP_OUTPUT(surface->output), TLP_END);
1987
Jason Ekstrandef540082014-06-26 10:37:36 -07001988 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001989}
1990
1991static void
1992view_accumulate_damage(struct weston_view *view,
1993 pixman_region32_t *opaque)
1994{
1995 pixman_region32_t damage;
1996
1997 pixman_region32_init(&damage);
1998 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001999 pixman_box32_t *extents;
2000
Jason Ekstranda7af7042013-10-12 22:38:11 -05002001 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002002 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002003 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002004 pixman_region32_copy(&damage, &view->surface->damage);
2005 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002006 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002007 }
2008
Pekka Paalanen380adf52015-02-16 14:39:11 +02002009 pixman_region32_intersect(&damage, &damage,
2010 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002011 pixman_region32_subtract(&damage, &damage, opaque);
2012 pixman_region32_union(&view->plane->damage,
2013 &view->plane->damage, &damage);
2014 pixman_region32_fini(&damage);
2015 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002016 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002017}
2018
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002019static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002020compositor_accumulate_damage(struct weston_compositor *ec)
2021{
2022 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002024 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002025
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002026 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002027
2028 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002029 pixman_region32_copy(&plane->clip, &clip);
2030
2031 pixman_region32_init(&opaque);
2032
Jason Ekstranda7af7042013-10-12 22:38:11 -05002033 wl_list_for_each(ev, &ec->view_list, link) {
2034 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002035 continue;
2036
Jason Ekstranda7af7042013-10-12 22:38:11 -05002037 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002038 }
2039
2040 pixman_region32_union(&clip, &clip, &opaque);
2041 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002042 }
2043
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002044 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002045
Jason Ekstranda7af7042013-10-12 22:38:11 -05002046 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002047 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002048
2049 wl_list_for_each(ev, &ec->view_list, link) {
2050 if (ev->surface->touched)
2051 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002052 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002053
2054 surface_flush_damage(ev->surface);
2055
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002056 /* Both the renderer and the backend have seen the buffer
2057 * by now. If renderer needs the buffer, it has its own
2058 * reference set. If the backend wants to keep the buffer
2059 * around for migrating the surface into a non-primary plane
2060 * later, keep_buffer is true. Otherwise, drop the core
2061 * reference now, and allow early buffer release. This enables
2062 * clients to use single-buffering.
2063 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002064 if (!ev->surface->keep_buffer)
2065 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002066 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002067}
2068
2069static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002071{
2072 struct weston_subsurface *sub;
2073
Pekka Paalanene67858b2013-04-25 13:57:42 +03002074 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002075 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002076 continue;
2077
Jason Ekstranda7af7042013-10-12 22:38:11 -05002078 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2079 wl_list_init(&sub->surface->views);
2080
2081 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002082 }
2083}
2084
2085static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002086surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002087{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002088 struct weston_subsurface *sub;
2089 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002090
Jason Ekstranda7af7042013-10-12 22:38:11 -05002091 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2092 if (sub->surface == surface)
2093 continue;
2094
George Kiagiadakised04d382014-06-13 18:10:26 +02002095 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2096 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002097 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002098 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002099
2100 surface_free_unused_subsurface_views(sub->surface);
2101 }
2102}
2103
2104static void
2105view_list_add_subsurface_view(struct weston_compositor *compositor,
2106 struct weston_subsurface *sub,
2107 struct weston_view *parent)
2108{
2109 struct weston_subsurface *child;
2110 struct weston_view *view = NULL, *iv;
2111
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002112 if (!weston_surface_is_mapped(sub->surface))
2113 return;
2114
Jason Ekstranda7af7042013-10-12 22:38:11 -05002115 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2116 if (iv->geometry.parent == parent) {
2117 view = iv;
2118 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002119 }
2120 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121
2122 if (view) {
2123 /* Put it back in the surface's list of views */
2124 wl_list_remove(&view->surface_link);
2125 wl_list_insert(&sub->surface->views, &view->surface_link);
2126 } else {
2127 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002128 weston_view_set_position(view,
2129 sub->position.x,
2130 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 weston_view_set_transform_parent(view, parent);
2132 }
2133
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002134 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002135 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136
Pekka Paalanenb188e912013-11-19 14:03:35 +02002137 if (wl_list_empty(&sub->surface->subsurface_list)) {
2138 wl_list_insert(compositor->view_list.prev, &view->link);
2139 return;
2140 }
2141
2142 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2143 if (child->surface == sub->surface)
2144 wl_list_insert(compositor->view_list.prev, &view->link);
2145 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002146 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002147 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002148}
2149
2150static void
2151view_list_add(struct weston_compositor *compositor,
2152 struct weston_view *view)
2153{
2154 struct weston_subsurface *sub;
2155
2156 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002157
Pekka Paalanenb188e912013-11-19 14:03:35 +02002158 if (wl_list_empty(&view->surface->subsurface_list)) {
2159 wl_list_insert(compositor->view_list.prev, &view->link);
2160 return;
2161 }
2162
2163 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2164 if (sub->surface == view->surface)
2165 wl_list_insert(compositor->view_list.prev, &view->link);
2166 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002167 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002168 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169}
2170
2171static void
2172weston_compositor_build_view_list(struct weston_compositor *compositor)
2173{
2174 struct weston_view *view;
2175 struct weston_layer *layer;
2176
2177 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002178 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179 surface_stash_subsurface_views(view->surface);
2180
2181 wl_list_init(&compositor->view_list);
2182 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002183 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002184 view_list_add(compositor, view);
2185 }
2186 }
2187
2188 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002189 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002190 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002191}
2192
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002193static void
2194weston_output_take_feedback_list(struct weston_output *output,
2195 struct weston_surface *surface)
2196{
2197 struct weston_view *view;
2198 struct weston_presentation_feedback *feedback;
2199 uint32_t flags = 0xffffffff;
2200
2201 if (wl_list_empty(&surface->feedback_list))
2202 return;
2203
2204 /* All views must have the flag for the flag to survive. */
2205 wl_list_for_each(view, &surface->views, surface_link) {
2206 /* ignore views that are not on this output at all */
2207 if (view->output_mask & (1u << output->id))
2208 flags &= view->psf_flags;
2209 }
2210
2211 wl_list_for_each(feedback, &surface->feedback_list, link)
2212 feedback->psf_flags = flags;
2213
2214 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2215 wl_list_init(&surface->feedback_list);
2216}
2217
David Herrmann1edf44c2013-10-22 17:11:26 +02002218static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002219weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002220{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002221 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002222 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002223 struct weston_animation *animation, *next;
2224 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002225 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002226 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002227 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002228
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002229 if (output->destroying)
2230 return 0;
2231
Pekka Paalanenb5026542014-11-12 15:09:24 +02002232 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2233
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002234 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002235 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002236
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002237 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002238 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002239 } else {
2240 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002241 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002242 ev->psf_flags = 0;
2243 }
2244 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002245
Pekka Paalanene67858b2013-04-25 13:57:42 +03002246 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002247 wl_list_for_each(ev, &ec->view_list, link) {
2248 /* Note: This operation is safe to do multiple times on the
2249 * same surface.
2250 */
2251 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002252 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002253 &ev->surface->frame_callback_list);
2254 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002255
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002256 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002257 }
2258 }
2259
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002260 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002261
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002262 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002263 pixman_region32_intersect(&output_damage,
2264 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002265 pixman_region32_subtract(&output_damage,
2266 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002267
Scott Moreauccbf29d2012-02-22 14:21:41 -07002268 if (output->dirty)
2269 weston_output_update_matrix(output);
2270
David Herrmann1edf44c2013-10-22 17:11:26 +02002271 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002272
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002273 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002274
Kristian Høgsbergef044142011-06-21 15:02:12 -04002275 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002276
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002277 weston_compositor_repick(ec);
2278 wl_event_loop_dispatch(ec->input_loop, 0);
2279
Jonas Ådahldb773762012-06-13 00:01:21 +02002280 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002281 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002282 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002283 }
2284
Scott Moreaud64cf212012-06-08 19:40:54 -06002285 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002286 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002287 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002288 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002289
Pekka Paalanenb5026542014-11-12 15:09:24 +02002290 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2291
David Herrmann1edf44c2013-10-22 17:11:26 +02002292 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002293}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002294
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002295static int
2296weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002297{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002298 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002299
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002300 wl_event_loop_dispatch(compositor->input_loop, 0);
2301
2302 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002303}
2304
Pekka Paalanen82919792014-05-21 13:51:49 +03002305static void
2306weston_output_schedule_repaint_reset(struct weston_output *output)
2307{
2308 struct weston_compositor *compositor = output->compositor;
2309 struct wl_event_loop *loop;
2310 int fd;
2311
2312 output->repaint_scheduled = 0;
2313 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2314
2315 if (compositor->input_loop_source)
2316 return;
2317
2318 loop = wl_display_get_event_loop(compositor->wl_display);
2319 fd = wl_event_loop_get_fd(compositor->input_loop);
2320 compositor->input_loop_source =
2321 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2322 weston_compositor_read_input, compositor);
2323}
2324
Pekka Paalanen0513a952014-05-21 16:17:27 +03002325static int
2326output_repaint_timer_handler(void *data)
2327{
2328 struct weston_output *output = data;
2329 struct weston_compositor *compositor = output->compositor;
2330
2331 if (output->repaint_needed &&
2332 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2333 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2334 weston_output_repaint(output) == 0)
2335 return 0;
2336
2337 weston_output_schedule_repaint_reset(output);
2338
2339 return 0;
2340}
2341
Kristian Høgsbergef044142011-06-21 15:02:12 -04002342WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002343weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002344 const struct timespec *stamp,
2345 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002346{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002347 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002348 int32_t refresh_nsec;
2349 struct timespec now;
2350 struct timespec gone;
2351 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002352
Pekka Paalanenb5026542014-11-12 15:09:24 +02002353 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2354 TLP_VBLANK(stamp), TLP_END);
2355
Pekka Paalanend7894d02015-07-03 15:08:53 +03002356 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002357 weston_presentation_feedback_present_list(&output->feedback_list,
2358 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002359 output->msc,
2360 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002361
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002362 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002363
Pekka Paalanen0513a952014-05-21 16:17:27 +03002364 weston_compositor_read_presentation_clock(compositor, &now);
2365 timespec_sub(&gone, &now, stamp);
2366 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2367 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002368
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002369 if (msec < -1000 || msec > 1000) {
2370 static bool warned;
2371
2372 if (!warned)
2373 weston_log("Warning: computed repaint delay is "
2374 "insane: %d msec\n", msec);
2375 warned = true;
2376
2377 msec = 0;
2378 }
2379
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002380 /* Called from restart_repaint_loop and restart happens already after
2381 * the deadline given by repaint_msec? In that case we delay until
2382 * the deadline of the next frame, to give clients a more predictable
2383 * timing of the repaint cycle to lock on. */
2384 if (presented_flags == PRESENTATION_FEEDBACK_INVALID && msec < 0)
2385 msec += refresh_nsec / 1000000;
2386
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002387 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002388 output_repaint_timer_handler(output);
2389 else
2390 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002391}
2392
2393static void
2394idle_repaint(void *data)
2395{
2396 struct weston_output *output = data;
2397
Jonas Ådahle5a12252013-04-05 23:07:11 +02002398 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002399}
2400
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002401WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002402weston_layer_entry_insert(struct weston_layer_entry *list,
2403 struct weston_layer_entry *entry)
2404{
2405 wl_list_insert(&list->link, &entry->link);
2406 entry->layer = list->layer;
2407}
2408
2409WL_EXPORT void
2410weston_layer_entry_remove(struct weston_layer_entry *entry)
2411{
2412 wl_list_remove(&entry->link);
2413 wl_list_init(&entry->link);
2414 entry->layer = NULL;
2415}
2416
2417WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002418weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2419{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002420 wl_list_init(&layer->view_list.link);
2421 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002422 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002423 if (below != NULL)
2424 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002425}
2426
2427WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002428weston_layer_set_mask(struct weston_layer *layer,
2429 int x, int y, int width, int height)
2430{
2431 struct weston_view *view;
2432
2433 layer->mask.x1 = x;
2434 layer->mask.x2 = x + width;
2435 layer->mask.y1 = y;
2436 layer->mask.y2 = y + height;
2437
2438 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2439 weston_view_geometry_dirty(view);
2440 }
2441}
2442
2443WL_EXPORT void
2444weston_layer_set_mask_infinite(struct weston_layer *layer)
2445{
2446 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2447 UINT32_MAX, UINT32_MAX);
2448}
2449
2450WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002451weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002452{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002453 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002454 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002455
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002456 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2457 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002458 return;
2459
Pekka Paalanenb5026542014-11-12 15:09:24 +02002460 if (!output->repaint_needed)
2461 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2462
Kristian Høgsbergef044142011-06-21 15:02:12 -04002463 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002464 output->repaint_needed = 1;
2465 if (output->repaint_scheduled)
2466 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002467
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002468 wl_event_loop_add_idle(loop, idle_repaint, output);
2469 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002470 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2471
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002472
2473 if (compositor->input_loop_source) {
2474 wl_event_source_remove(compositor->input_loop_source);
2475 compositor->input_loop_source = NULL;
2476 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002477}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002478
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002479WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002480weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2481{
2482 struct weston_output *output;
2483
2484 wl_list_for_each(output, &compositor->output_list, link)
2485 weston_output_schedule_repaint(output);
2486}
2487
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002488static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002489surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002490{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002491 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002492}
2493
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002494static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002495surface_attach(struct wl_client *client,
2496 struct wl_resource *resource,
2497 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2498{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002499 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002500 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002501
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002502 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002503 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002504 if (buffer == NULL) {
2505 wl_client_post_no_memory(client);
2506 return;
2507 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002508 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002509
Pekka Paalanende685b82012-12-04 15:58:12 +02002510 /* Attach, attach, without commit in between does not send
2511 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002512 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002513
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002514 surface->pending.sx = sx;
2515 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002516 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002517}
2518
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002519static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002520surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002521 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002522 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002523{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002524 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002525
Pekka Paalanen8e159182012-10-10 12:49:25 +03002526 pixman_region32_union_rect(&surface->pending.damage,
2527 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002528 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002529}
2530
Kristian Høgsberg33418202011-08-16 23:01:28 -04002531static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002532destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002533{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002534 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002535
2536 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002537 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002538}
2539
2540static void
2541surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002542 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002543{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002544 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002545 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002546
2547 cb = malloc(sizeof *cb);
2548 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002549 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002550 return;
2551 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002552
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002553 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2554 callback);
2555 if (cb->resource == NULL) {
2556 free(cb);
2557 wl_resource_post_no_memory(resource);
2558 return;
2559 }
2560
Jason Ekstranda85118c2013-06-27 20:17:02 -05002561 wl_resource_set_implementation(cb->resource, NULL, cb,
2562 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002563
Pekka Paalanenbc106382012-10-10 12:49:31 +03002564 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002565}
2566
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002567static void
2568surface_set_opaque_region(struct wl_client *client,
2569 struct wl_resource *resource,
2570 struct wl_resource *region_resource)
2571{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002572 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002573 struct weston_region *region;
2574
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002575 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002576 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002577 pixman_region32_copy(&surface->pending.opaque,
2578 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002579 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002580 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002581 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002582}
2583
2584static void
2585surface_set_input_region(struct wl_client *client,
2586 struct wl_resource *resource,
2587 struct wl_resource *region_resource)
2588{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002589 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002590 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002591
2592 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002593 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002594 pixman_region32_copy(&surface->pending.input,
2595 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002596 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002597 pixman_region32_fini(&surface->pending.input);
2598 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002599 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002600}
2601
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002602static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002603weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002604{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002605 struct weston_subsurface *sub;
2606
2607 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2608 parent_link_pending) {
2609 wl_list_remove(&sub->parent_link);
2610 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2611 }
2612}
2613
2614static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002615weston_surface_build_buffer_matrix(struct weston_surface *surface,
2616 struct weston_matrix *matrix)
2617{
2618 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2619 double src_width, src_height, dest_width, dest_height;
2620
2621 weston_matrix_init(matrix);
2622
2623 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2624 src_width = surface->width_from_buffer;
2625 src_height = surface->height_from_buffer;
2626 } else {
2627 src_width = wl_fixed_to_double(vp->buffer.src_width);
2628 src_height = wl_fixed_to_double(vp->buffer.src_height);
2629 }
2630
2631 if (vp->surface.width == -1) {
2632 dest_width = src_width;
2633 dest_height = src_height;
2634 } else {
2635 dest_width = vp->surface.width;
2636 dest_height = vp->surface.height;
2637 }
2638
2639 if (src_width != dest_width || src_height != dest_height)
2640 weston_matrix_scale(matrix,
2641 src_width / dest_width,
2642 src_height / dest_height, 1);
2643
2644 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2645 weston_matrix_translate(matrix,
2646 wl_fixed_to_double(vp->buffer.src_x),
2647 wl_fixed_to_double(vp->buffer.src_y),
2648 0);
2649
2650 switch (vp->buffer.transform) {
2651 case WL_OUTPUT_TRANSFORM_FLIPPED:
2652 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2653 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2654 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2655 weston_matrix_scale(matrix, -1, 1, 1);
2656 weston_matrix_translate(matrix,
2657 surface->width_from_buffer, 0, 0);
2658 break;
2659 }
2660
2661 switch (vp->buffer.transform) {
2662 default:
2663 case WL_OUTPUT_TRANSFORM_NORMAL:
2664 case WL_OUTPUT_TRANSFORM_FLIPPED:
2665 break;
2666 case WL_OUTPUT_TRANSFORM_90:
2667 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2668 weston_matrix_rotate_xy(matrix, 0, 1);
2669 weston_matrix_translate(matrix,
2670 surface->height_from_buffer, 0, 0);
2671 break;
2672 case WL_OUTPUT_TRANSFORM_180:
2673 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2674 weston_matrix_rotate_xy(matrix, -1, 0);
2675 weston_matrix_translate(matrix,
2676 surface->width_from_buffer,
2677 surface->height_from_buffer, 0);
2678 break;
2679 case WL_OUTPUT_TRANSFORM_270:
2680 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2681 weston_matrix_rotate_xy(matrix, 0, -1);
2682 weston_matrix_translate(matrix,
2683 0, surface->width_from_buffer, 0);
2684 break;
2685 }
2686
2687 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2688}
2689
2690static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002691weston_surface_commit_state(struct weston_surface *surface,
2692 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002693{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002694 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002695 pixman_region32_t opaque;
2696
Alexander Larsson4ea95522013-05-22 14:41:37 +02002697 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002698 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002699 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002700 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002701
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002702 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002703 if (state->newly_attached)
2704 weston_surface_attach(surface, state->buffer);
2705 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002706
Jason Ekstrand1e059042014-10-16 10:55:19 -05002707 weston_surface_build_buffer_matrix(surface,
2708 &surface->surface_to_buffer_matrix);
2709 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2710 &surface->surface_to_buffer_matrix);
2711
Jason Ekstrand7b982072014-05-20 14:33:03 -05002712 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002713 weston_surface_update_size(surface);
2714 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002715 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002716 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002717
Jason Ekstrand7b982072014-05-20 14:33:03 -05002718 state->sx = 0;
2719 state->sy = 0;
2720 state->newly_attached = 0;
2721 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002722
2723 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002724 if (weston_timeline_enabled_ &&
2725 pixman_region32_not_empty(&state->damage))
2726 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002727 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002728 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002729 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002730 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002731 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002732
2733 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002734 pixman_region32_init(&opaque);
2735 pixman_region32_intersect_rect(&opaque, &state->opaque,
2736 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002737
2738 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2739 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002740 wl_list_for_each(view, &surface->views, surface_link)
2741 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002742 }
2743
2744 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002745
2746 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002747 pixman_region32_intersect_rect(&surface->input, &state->input,
2748 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002749
Pekka Paalanenbc106382012-10-10 12:49:31 +03002750 /* wl_surface.frame */
2751 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002752 &state->frame_callback_list);
2753 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002754
2755 /* XXX:
2756 * What should happen with a feedback request, if there
2757 * is no wl_buffer attached for this commit?
2758 */
2759
2760 /* presentation.feedback */
2761 wl_list_insert_list(&surface->feedback_list,
2762 &state->feedback_list);
2763 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002764}
2765
2766static void
2767weston_surface_commit(struct weston_surface *surface)
2768{
2769 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002770
Pekka Paalanene67858b2013-04-25 13:57:42 +03002771 weston_surface_commit_subsurface_order(surface);
2772
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002773 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002774}
2775
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002776static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002777weston_subsurface_commit(struct weston_subsurface *sub);
2778
2779static void
2780weston_subsurface_parent_commit(struct weston_subsurface *sub,
2781 int parent_is_synchronized);
2782
2783static void
2784surface_commit(struct wl_client *client, struct wl_resource *resource)
2785{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002786 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002787 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2788
2789 if (sub) {
2790 weston_subsurface_commit(sub);
2791 return;
2792 }
2793
2794 weston_surface_commit(surface);
2795
2796 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2797 if (sub->surface != surface)
2798 weston_subsurface_parent_commit(sub, 0);
2799 }
2800}
2801
2802static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002803surface_set_buffer_transform(struct wl_client *client,
2804 struct wl_resource *resource, int transform)
2805{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002806 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002807
Jonny Lamba55f1392014-05-30 12:07:15 +02002808 /* if wl_output.transform grows more members this will need to be updated. */
2809 if (transform < 0 ||
2810 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2811 wl_resource_post_error(resource,
2812 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2813 "buffer transform must be a valid transform "
2814 "('%d' specified)", transform);
2815 return;
2816 }
2817
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002818 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002819 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002820}
2821
Alexander Larsson4ea95522013-05-22 14:41:37 +02002822static void
2823surface_set_buffer_scale(struct wl_client *client,
2824 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002825 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002826{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002827 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002828
Jonny Lamba55f1392014-05-30 12:07:15 +02002829 if (scale < 1) {
2830 wl_resource_post_error(resource,
2831 WL_SURFACE_ERROR_INVALID_SCALE,
2832 "buffer scale must be at least one "
2833 "('%d' specified)", scale);
2834 return;
2835 }
2836
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002837 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002838 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002839}
2840
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002841static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002842 surface_destroy,
2843 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002844 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002845 surface_frame,
2846 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002847 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002848 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002849 surface_set_buffer_transform,
2850 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002851};
2852
2853static void
2854compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002855 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002856{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002857 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002858 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002859
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002860 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002861 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002862 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002863 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002864 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002865
Jason Ekstranda85118c2013-06-27 20:17:02 -05002866 surface->resource =
2867 wl_resource_create(client, &wl_surface_interface,
2868 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002869 if (surface->resource == NULL) {
2870 weston_surface_destroy(surface);
2871 wl_resource_post_no_memory(resource);
2872 return;
2873 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002874 wl_resource_set_implementation(surface->resource, &surface_interface,
2875 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002876
2877 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002878}
2879
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002880static void
2881destroy_region(struct wl_resource *resource)
2882{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002883 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002884
2885 pixman_region32_fini(&region->region);
2886 free(region);
2887}
2888
2889static void
2890region_destroy(struct wl_client *client, struct wl_resource *resource)
2891{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002892 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002893}
2894
2895static void
2896region_add(struct wl_client *client, struct wl_resource *resource,
2897 int32_t x, int32_t y, int32_t width, int32_t height)
2898{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002899 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002900
2901 pixman_region32_union_rect(&region->region, &region->region,
2902 x, y, width, height);
2903}
2904
2905static void
2906region_subtract(struct wl_client *client, struct wl_resource *resource,
2907 int32_t x, int32_t y, int32_t width, int32_t height)
2908{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002909 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002910 pixman_region32_t rect;
2911
2912 pixman_region32_init_rect(&rect, x, y, width, height);
2913 pixman_region32_subtract(&region->region, &region->region, &rect);
2914 pixman_region32_fini(&rect);
2915}
2916
2917static const struct wl_region_interface region_interface = {
2918 region_destroy,
2919 region_add,
2920 region_subtract
2921};
2922
2923static void
2924compositor_create_region(struct wl_client *client,
2925 struct wl_resource *resource, uint32_t id)
2926{
2927 struct weston_region *region;
2928
2929 region = malloc(sizeof *region);
2930 if (region == NULL) {
2931 wl_resource_post_no_memory(resource);
2932 return;
2933 }
2934
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002935 pixman_region32_init(&region->region);
2936
Jason Ekstranda85118c2013-06-27 20:17:02 -05002937 region->resource =
2938 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002939 if (region->resource == NULL) {
2940 free(region);
2941 wl_resource_post_no_memory(resource);
2942 return;
2943 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002944 wl_resource_set_implementation(region->resource, &region_interface,
2945 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002946}
2947
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002948static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002949 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002950 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002951};
2952
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002953static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002954weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2955{
2956 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002957
Jason Ekstrand7b982072014-05-20 14:33:03 -05002958 weston_surface_commit_state(surface, &sub->cached);
2959 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002960
2961 weston_surface_commit_subsurface_order(surface);
2962
2963 weston_surface_schedule_repaint(surface);
2964
Jason Ekstrand7b982072014-05-20 14:33:03 -05002965 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002966}
2967
2968static void
2969weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2970{
2971 struct weston_surface *surface = sub->surface;
2972
2973 /*
2974 * If this commit would cause the surface to move by the
2975 * attach(dx, dy) parameters, the old damage region must be
2976 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002977 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002978 */
2979 pixman_region32_translate(&sub->cached.damage,
2980 -surface->pending.sx, -surface->pending.sy);
2981 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2982 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002983 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002984
2985 if (surface->pending.newly_attached) {
2986 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002987 weston_surface_state_set_buffer(&sub->cached,
2988 surface->pending.buffer);
2989 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002990 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002991 weston_presentation_feedback_discard_list(
2992 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002993 }
2994 sub->cached.sx += surface->pending.sx;
2995 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002996
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002997 sub->cached.buffer_viewport.changed |=
2998 surface->pending.buffer_viewport.changed;
2999 sub->cached.buffer_viewport.buffer =
3000 surface->pending.buffer_viewport.buffer;
3001 sub->cached.buffer_viewport.surface =
3002 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003003
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003004 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003005
3006 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3007
3008 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3009
3010 wl_list_insert_list(&sub->cached.frame_callback_list,
3011 &surface->pending.frame_callback_list);
3012 wl_list_init(&surface->pending.frame_callback_list);
3013
Pekka Paalanen133e4392014-09-23 22:08:46 -04003014 wl_list_insert_list(&sub->cached.feedback_list,
3015 &surface->pending.feedback_list);
3016 wl_list_init(&surface->pending.feedback_list);
3017
Jason Ekstrand7b982072014-05-20 14:33:03 -05003018 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003019}
3020
Derek Foreman280e7dd2014-10-03 13:13:42 -05003021static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003022weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3023{
3024 while (sub) {
3025 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003026 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003027
3028 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003029 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003030
3031 sub = weston_surface_to_subsurface(sub->parent);
3032 }
3033
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003034 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003035}
3036
3037static void
3038weston_subsurface_commit(struct weston_subsurface *sub)
3039{
3040 struct weston_surface *surface = sub->surface;
3041 struct weston_subsurface *tmp;
3042
3043 /* Recursive check for effectively synchronized. */
3044 if (weston_subsurface_is_synchronized(sub)) {
3045 weston_subsurface_commit_to_cache(sub);
3046 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003047 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003048 /* flush accumulated state from cache */
3049 weston_subsurface_commit_to_cache(sub);
3050 weston_subsurface_commit_from_cache(sub);
3051 } else {
3052 weston_surface_commit(surface);
3053 }
3054
3055 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3056 if (tmp->surface != surface)
3057 weston_subsurface_parent_commit(tmp, 0);
3058 }
3059 }
3060}
3061
3062static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003063weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003064{
3065 struct weston_surface *surface = sub->surface;
3066 struct weston_subsurface *tmp;
3067
Pekka Paalanene67858b2013-04-25 13:57:42 +03003068 /* From now on, commit_from_cache the whole sub-tree, regardless of
3069 * the synchronized mode of each child. This sub-surface or some
3070 * of its ancestors were synchronized, so we are synchronized
3071 * all the way down.
3072 */
3073
Jason Ekstrand7b982072014-05-20 14:33:03 -05003074 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003075 weston_subsurface_commit_from_cache(sub);
3076
3077 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3078 if (tmp->surface != surface)
3079 weston_subsurface_parent_commit(tmp, 1);
3080 }
3081}
3082
3083static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003084weston_subsurface_parent_commit(struct weston_subsurface *sub,
3085 int parent_is_synchronized)
3086{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003087 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003088 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003089 wl_list_for_each(view, &sub->surface->views, surface_link)
3090 weston_view_set_position(view,
3091 sub->position.x,
3092 sub->position.y);
3093
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003094 sub->position.set = 0;
3095 }
3096
3097 if (parent_is_synchronized || sub->synchronized)
3098 weston_subsurface_synchronized_commit(sub);
3099}
3100
Pekka Paalanen8274d902014-08-06 19:36:51 +03003101static int
3102subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3103{
3104 return snprintf(buf, len, "sub-surface");
3105}
3106
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003107static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003108subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003109{
3110 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003111 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003112
Jason Ekstranda7af7042013-10-12 22:38:11 -05003113 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003114 weston_view_set_position(view,
3115 view->geometry.x + dx,
3116 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003117
3118 /* No need to check parent mappedness, because if parent is not
3119 * mapped, parent is not in a visible layer, so this sub-surface
3120 * will not be drawn either.
3121 */
3122 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003123 struct weston_output *output;
3124
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003125 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003126 * because that would call it also for the parent surface,
3127 * which might not be mapped yet. That would lead to
3128 * inconsistent state, where the window could never be
3129 * mapped.
3130 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003131 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003132 * weston_surface_is_mapped() return true, so that when the
3133 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003134 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003135 */
3136 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003137 output = container_of(compositor->output_list.next,
3138 struct weston_output, link);
3139
3140 surface->output = output;
3141 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003142 }
3143}
3144
3145static struct weston_subsurface *
3146weston_surface_to_subsurface(struct weston_surface *surface)
3147{
3148 if (surface->configure == subsurface_configure)
3149 return surface->configure_private;
3150
3151 return NULL;
3152}
3153
Pekka Paalanen01388e22013-04-25 13:57:44 +03003154WL_EXPORT struct weston_surface *
3155weston_surface_get_main_surface(struct weston_surface *surface)
3156{
3157 struct weston_subsurface *sub;
3158
3159 while (surface && (sub = weston_surface_to_subsurface(surface)))
3160 surface = sub->parent;
3161
3162 return surface;
3163}
3164
Pekka Paalanen50b67472014-10-01 15:02:41 +03003165WL_EXPORT int
3166weston_surface_set_role(struct weston_surface *surface,
3167 const char *role_name,
3168 struct wl_resource *error_resource,
3169 uint32_t error_code)
3170{
3171 assert(role_name);
3172
3173 if (surface->role_name == NULL ||
3174 surface->role_name == role_name ||
3175 strcmp(surface->role_name, role_name) == 0) {
3176 surface->role_name = role_name;
3177
3178 return 0;
3179 }
3180
3181 wl_resource_post_error(error_resource, error_code,
3182 "Cannot assign role %s to wl_surface@%d,"
3183 " already has role %s\n",
3184 role_name,
3185 wl_resource_get_id(surface->resource),
3186 surface->role_name);
3187 return -1;
3188}
3189
Pekka Paalanen8274d902014-08-06 19:36:51 +03003190WL_EXPORT void
3191weston_surface_set_label_func(struct weston_surface *surface,
3192 int (*desc)(struct weston_surface *,
3193 char *, size_t))
3194{
3195 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003196 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003197}
3198
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003199/** Get the size of surface contents
3200 *
3201 * \param surface The surface to query.
3202 * \param width Returns the width of raw contents.
3203 * \param height Returns the height of raw contents.
3204 *
3205 * Retrieves the raw surface content size in pixels for the given surface.
3206 * This is the whole content size in buffer pixels. If the surface
3207 * has no content or the renderer does not implement this feature,
3208 * zeroes are returned.
3209 *
3210 * This function is used to determine the buffer size needed for
3211 * a weston_surface_copy_content() call.
3212 */
3213WL_EXPORT void
3214weston_surface_get_content_size(struct weston_surface *surface,
3215 int *width, int *height)
3216{
3217 struct weston_renderer *rer = surface->compositor->renderer;
3218
3219 if (!rer->surface_get_content_size) {
3220 *width = 0;
3221 *height = 0;
3222 return;
3223 }
3224
3225 rer->surface_get_content_size(surface, width, height);
3226}
3227
3228/** Copy surface contents to system memory.
3229 *
3230 * \param surface The surface to copy from.
3231 * \param target Pointer to the target memory buffer.
3232 * \param size Size of the target buffer in bytes.
3233 * \param src_x X location on contents to copy from.
3234 * \param src_y Y location on contents to copy from.
3235 * \param width Width in pixels of the area to copy.
3236 * \param height Height in pixels of the area to copy.
3237 * \return 0 for success, -1 for failure.
3238 *
3239 * Surface contents are maintained by the renderer. They can be in a
3240 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3241 * else.
3242 *
3243 * Surface contents are copied into memory pointed to by target,
3244 * which has size bytes of space available. The target memory
3245 * may be larger than needed, but being smaller returns an error.
3246 * The extra bytes in target may or may not be written; their content is
3247 * unspecified. Size must be large enough to hold the image.
3248 *
3249 * The image in the target memory will be arranged in rows from
3250 * top to bottom, and pixels on a row from left to right. The pixel
3251 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3252 * width * 4.
3253 *
3254 * Parameters src_x and src_y define the upper-left corner in buffer
3255 * coordinates (pixels) to copy from. Parameters width and height
3256 * define the size of the area to copy in pixels.
3257 *
3258 * The rectangle defined by src_x, src_y, width, height must fit in
3259 * the surface contents. Otherwise an error is returned.
3260 *
3261 * Use surface_get_data_size to determine the content size; the
3262 * needed target buffer size and rectangle limits.
3263 *
3264 * CURRENT IMPLEMENTATION RESTRICTIONS:
3265 * - the machine must be little-endian due to Pixman formats.
3266 *
3267 * NOTE: Pixman formats are premultiplied.
3268 */
3269WL_EXPORT int
3270weston_surface_copy_content(struct weston_surface *surface,
3271 void *target, size_t size,
3272 int src_x, int src_y,
3273 int width, int height)
3274{
3275 struct weston_renderer *rer = surface->compositor->renderer;
3276 int cw, ch;
3277 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3278
3279 if (!rer->surface_copy_content)
3280 return -1;
3281
3282 weston_surface_get_content_size(surface, &cw, &ch);
3283
3284 if (src_x < 0 || src_y < 0)
3285 return -1;
3286
3287 if (width <= 0 || height <= 0)
3288 return -1;
3289
3290 if (src_x + width > cw || src_y + height > ch)
3291 return -1;
3292
3293 if (width * bytespp * height > size)
3294 return -1;
3295
3296 return rer->surface_copy_content(surface, target, size,
3297 src_x, src_y, width, height);
3298}
3299
Pekka Paalanene67858b2013-04-25 13:57:42 +03003300static void
3301subsurface_set_position(struct wl_client *client,
3302 struct wl_resource *resource, int32_t x, int32_t y)
3303{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003304 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003305
3306 if (!sub)
3307 return;
3308
3309 sub->position.x = x;
3310 sub->position.y = y;
3311 sub->position.set = 1;
3312}
3313
3314static struct weston_subsurface *
3315subsurface_from_surface(struct weston_surface *surface)
3316{
3317 struct weston_subsurface *sub;
3318
3319 sub = weston_surface_to_subsurface(surface);
3320 if (sub)
3321 return sub;
3322
3323 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3324 if (sub->surface == surface)
3325 return sub;
3326
3327 return NULL;
3328}
3329
3330static struct weston_subsurface *
3331subsurface_sibling_check(struct weston_subsurface *sub,
3332 struct weston_surface *surface,
3333 const char *request)
3334{
3335 struct weston_subsurface *sibling;
3336
3337 sibling = subsurface_from_surface(surface);
3338
3339 if (!sibling) {
3340 wl_resource_post_error(sub->resource,
3341 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3342 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003343 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003344 return NULL;
3345 }
3346
3347 if (sibling->parent != sub->parent) {
3348 wl_resource_post_error(sub->resource,
3349 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3350 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003351 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003352 return NULL;
3353 }
3354
3355 return sibling;
3356}
3357
3358static void
3359subsurface_place_above(struct wl_client *client,
3360 struct wl_resource *resource,
3361 struct wl_resource *sibling_resource)
3362{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003363 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003364 struct weston_surface *surface =
3365 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003366 struct weston_subsurface *sibling;
3367
3368 if (!sub)
3369 return;
3370
3371 sibling = subsurface_sibling_check(sub, surface, "place_above");
3372 if (!sibling)
3373 return;
3374
3375 wl_list_remove(&sub->parent_link_pending);
3376 wl_list_insert(sibling->parent_link_pending.prev,
3377 &sub->parent_link_pending);
3378}
3379
3380static void
3381subsurface_place_below(struct wl_client *client,
3382 struct wl_resource *resource,
3383 struct wl_resource *sibling_resource)
3384{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003385 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003386 struct weston_surface *surface =
3387 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003388 struct weston_subsurface *sibling;
3389
3390 if (!sub)
3391 return;
3392
3393 sibling = subsurface_sibling_check(sub, surface, "place_below");
3394 if (!sibling)
3395 return;
3396
3397 wl_list_remove(&sub->parent_link_pending);
3398 wl_list_insert(&sibling->parent_link_pending,
3399 &sub->parent_link_pending);
3400}
3401
3402static void
3403subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3404{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003405 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003406
3407 if (sub)
3408 sub->synchronized = 1;
3409}
3410
3411static void
3412subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3413{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003414 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003415
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003416 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003417 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003418
3419 /* If sub became effectively desynchronized, flush. */
3420 if (!weston_subsurface_is_synchronized(sub))
3421 weston_subsurface_synchronized_commit(sub);
3422 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003423}
3424
3425static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003426weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3427{
3428 wl_list_remove(&sub->parent_link);
3429 wl_list_remove(&sub->parent_link_pending);
3430 wl_list_remove(&sub->parent_destroy_listener.link);
3431 sub->parent = NULL;
3432}
3433
3434static void
3435weston_subsurface_destroy(struct weston_subsurface *sub);
3436
3437static void
3438subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3439{
3440 struct weston_subsurface *sub =
3441 container_of(listener, struct weston_subsurface,
3442 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003443 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003444
3445 /* The protocol object (wl_resource) is left inert. */
3446 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003447 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003448
3449 weston_subsurface_destroy(sub);
3450}
3451
3452static void
3453subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3454{
3455 struct weston_subsurface *sub =
3456 container_of(listener, struct weston_subsurface,
3457 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003458 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003459 assert(sub->surface != sub->parent);
3460
3461 if (weston_surface_is_mapped(sub->surface))
3462 weston_surface_unmap(sub->surface);
3463
3464 weston_subsurface_unlink_parent(sub);
3465}
3466
3467static void
3468subsurface_resource_destroy(struct wl_resource *resource)
3469{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003470 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003471
3472 if (sub)
3473 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003474}
3475
3476static void
3477subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3478{
3479 wl_resource_destroy(resource);
3480}
3481
3482static void
3483weston_subsurface_link_parent(struct weston_subsurface *sub,
3484 struct weston_surface *parent)
3485{
3486 sub->parent = parent;
3487 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003488 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003489 &sub->parent_destroy_listener);
3490
3491 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3492 wl_list_insert(&parent->subsurface_list_pending,
3493 &sub->parent_link_pending);
3494}
3495
3496static void
3497weston_subsurface_link_surface(struct weston_subsurface *sub,
3498 struct weston_surface *surface)
3499{
3500 sub->surface = surface;
3501 sub->surface_destroy_listener.notify =
3502 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003503 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003504 &sub->surface_destroy_listener);
3505}
3506
3507static void
3508weston_subsurface_destroy(struct weston_subsurface *sub)
3509{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003510 struct weston_view *view, *next;
3511
Pekka Paalanene67858b2013-04-25 13:57:42 +03003512 assert(sub->surface);
3513
3514 if (sub->resource) {
3515 assert(weston_surface_to_subsurface(sub->surface) == sub);
3516 assert(sub->parent_destroy_listener.notify ==
3517 subsurface_handle_parent_destroy);
3518
George Kiagiadakised04d382014-06-13 18:10:26 +02003519 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3520 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003521 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003522 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003523
Pekka Paalanene67858b2013-04-25 13:57:42 +03003524 if (sub->parent)
3525 weston_subsurface_unlink_parent(sub);
3526
Jason Ekstrand7b982072014-05-20 14:33:03 -05003527 weston_surface_state_fini(&sub->cached);
3528 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003529
3530 sub->surface->configure = NULL;
3531 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003532 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003533 } else {
3534 /* the dummy weston_subsurface for the parent itself */
3535 assert(sub->parent_destroy_listener.notify == NULL);
3536 wl_list_remove(&sub->parent_link);
3537 wl_list_remove(&sub->parent_link_pending);
3538 }
3539
3540 wl_list_remove(&sub->surface_destroy_listener.link);
3541 free(sub);
3542}
3543
3544static const struct wl_subsurface_interface subsurface_implementation = {
3545 subsurface_destroy,
3546 subsurface_set_position,
3547 subsurface_place_above,
3548 subsurface_place_below,
3549 subsurface_set_sync,
3550 subsurface_set_desync
3551};
3552
3553static struct weston_subsurface *
3554weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3555 struct weston_surface *parent)
3556{
3557 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003558 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003559
Bryce Harringtonde16d892014-11-20 22:21:57 -08003560 sub = zalloc(sizeof *sub);
3561 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003562 return NULL;
3563
Jason Ekstranda7af7042013-10-12 22:38:11 -05003564 wl_list_init(&sub->unused_views);
3565
Jason Ekstranda85118c2013-06-27 20:17:02 -05003566 sub->resource =
3567 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003568 if (!sub->resource) {
3569 free(sub);
3570 return NULL;
3571 }
3572
Jason Ekstranda85118c2013-06-27 20:17:02 -05003573 wl_resource_set_implementation(sub->resource,
3574 &subsurface_implementation,
3575 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003576 weston_subsurface_link_surface(sub, surface);
3577 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003578 weston_surface_state_init(&sub->cached);
3579 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003580 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003581
3582 return sub;
3583}
3584
3585/* Create a dummy subsurface for having the parent itself in its
3586 * sub-surface lists. Makes stacking order manipulation easy.
3587 */
3588static struct weston_subsurface *
3589weston_subsurface_create_for_parent(struct weston_surface *parent)
3590{
3591 struct weston_subsurface *sub;
3592
Bryce Harringtonde16d892014-11-20 22:21:57 -08003593 sub = zalloc(sizeof *sub);
3594 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003595 return NULL;
3596
3597 weston_subsurface_link_surface(sub, parent);
3598 sub->parent = parent;
3599 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3600 wl_list_insert(&parent->subsurface_list_pending,
3601 &sub->parent_link_pending);
3602
3603 return sub;
3604}
3605
3606static void
3607subcompositor_get_subsurface(struct wl_client *client,
3608 struct wl_resource *resource,
3609 uint32_t id,
3610 struct wl_resource *surface_resource,
3611 struct wl_resource *parent_resource)
3612{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003613 struct weston_surface *surface =
3614 wl_resource_get_user_data(surface_resource);
3615 struct weston_surface *parent =
3616 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003617 struct weston_subsurface *sub;
3618 static const char where[] = "get_subsurface: wl_subsurface@";
3619
3620 if (surface == parent) {
3621 wl_resource_post_error(resource,
3622 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3623 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003624 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003625 return;
3626 }
3627
3628 if (weston_surface_to_subsurface(surface)) {
3629 wl_resource_post_error(resource,
3630 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3631 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003632 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003633 return;
3634 }
3635
Pekka Paalanen50b67472014-10-01 15:02:41 +03003636 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3637 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003638 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003639
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003640 if (weston_surface_get_main_surface(parent) == surface) {
3641 wl_resource_post_error(resource,
3642 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3643 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003644 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003645 return;
3646 }
3647
Pekka Paalanene67858b2013-04-25 13:57:42 +03003648 /* make sure the parent is in its own list */
3649 if (wl_list_empty(&parent->subsurface_list)) {
3650 if (!weston_subsurface_create_for_parent(parent)) {
3651 wl_resource_post_no_memory(resource);
3652 return;
3653 }
3654 }
3655
3656 sub = weston_subsurface_create(id, surface, parent);
3657 if (!sub) {
3658 wl_resource_post_no_memory(resource);
3659 return;
3660 }
3661
3662 surface->configure = subsurface_configure;
3663 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003664 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003665}
3666
3667static void
3668subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3669{
3670 wl_resource_destroy(resource);
3671}
3672
3673static const struct wl_subcompositor_interface subcompositor_interface = {
3674 subcompositor_destroy,
3675 subcompositor_get_subsurface
3676};
3677
3678static void
3679bind_subcompositor(struct wl_client *client,
3680 void *data, uint32_t version, uint32_t id)
3681{
3682 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003683 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003684
Jason Ekstranda85118c2013-06-27 20:17:02 -05003685 resource =
3686 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003687 if (resource == NULL) {
3688 wl_client_post_no_memory(client);
3689 return;
3690 }
3691 wl_resource_set_implementation(resource, &subcompositor_interface,
3692 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003693}
3694
3695static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003696weston_compositor_dpms(struct weston_compositor *compositor,
3697 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003698{
3699 struct weston_output *output;
3700
3701 wl_list_for_each(output, &compositor->output_list, link)
3702 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003703 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003704}
3705
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003706WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003707weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003708{
Neil Roberts8b62e202013-09-30 13:14:47 +01003709 uint32_t old_state = compositor->state;
3710
3711 /* The state needs to be changed before emitting the wake
3712 * signal because that may try to schedule a repaint which
3713 * will not work if the compositor is still sleeping */
3714 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3715
3716 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003717 case WESTON_COMPOSITOR_SLEEPING:
3718 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3719 /* fall through */
3720 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003721 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003722 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003723 /* fall through */
3724 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003725 wl_event_source_timer_update(compositor->idle_source,
3726 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003727 }
3728}
3729
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003730WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003731weston_compositor_offscreen(struct weston_compositor *compositor)
3732{
3733 switch (compositor->state) {
3734 case WESTON_COMPOSITOR_OFFSCREEN:
3735 return;
3736 case WESTON_COMPOSITOR_SLEEPING:
3737 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3738 /* fall through */
3739 default:
3740 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3741 wl_event_source_timer_update(compositor->idle_source, 0);
3742 }
3743}
3744
3745WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003746weston_compositor_sleep(struct weston_compositor *compositor)
3747{
3748 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3749 return;
3750
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003751 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003752 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3753 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3754}
3755
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003756static int
3757idle_handler(void *data)
3758{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003759 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003760
3761 if (compositor->idle_inhibit)
3762 return 1;
3763
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003764 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003765 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003766
3767 return 1;
3768}
3769
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003770WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003771weston_plane_init(struct weston_plane *plane,
3772 struct weston_compositor *ec,
3773 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003774{
3775 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003776 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003777 plane->x = x;
3778 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003779 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003780
3781 /* Init the link so that the call to wl_list_remove() when releasing
3782 * the plane without ever stacking doesn't lead to a crash */
3783 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003784}
3785
3786WL_EXPORT void
3787weston_plane_release(struct weston_plane *plane)
3788{
Xiong Zhang97116532013-10-23 13:58:31 +08003789 struct weston_view *view;
3790
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003791 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003792 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003793
Xiong Zhang97116532013-10-23 13:58:31 +08003794 wl_list_for_each(view, &plane->compositor->view_list, link) {
3795 if (view->plane == plane)
3796 view->plane = NULL;
3797 }
3798
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003799 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003800}
3801
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003802WL_EXPORT void
3803weston_compositor_stack_plane(struct weston_compositor *ec,
3804 struct weston_plane *plane,
3805 struct weston_plane *above)
3806{
3807 if (above)
3808 wl_list_insert(above->link.prev, &plane->link);
3809 else
3810 wl_list_insert(&ec->plane_list, &plane->link);
3811}
3812
Casey Dahlin9074db52012-04-19 22:50:09 -04003813static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003814{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003815 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003816}
3817
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003818static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003819bind_output(struct wl_client *client,
3820 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003821{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003822 struct weston_output *output = data;
3823 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003824 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003825
Jason Ekstranda85118c2013-06-27 20:17:02 -05003826 resource = wl_resource_create(client, &wl_output_interface,
3827 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003828 if (resource == NULL) {
3829 wl_client_post_no_memory(client);
3830 return;
3831 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003832
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003833 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003834 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003835
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003836 wl_output_send_geometry(resource,
3837 output->x,
3838 output->y,
3839 output->mm_width,
3840 output->mm_height,
3841 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003842 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003843 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003844 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003845 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003846 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003847
3848 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003849 wl_output_send_mode(resource,
3850 mode->flags,
3851 mode->width,
3852 mode->height,
3853 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003854 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003855
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003856 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003857 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003858}
3859
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003860/* Move other outputs when one is removed so the space remains contiguos. */
3861static void
3862weston_compositor_remove_output(struct weston_compositor *compositor,
3863 struct weston_output *remove_output)
3864{
3865 struct weston_output *output;
3866 int offset = 0;
3867
3868 wl_list_for_each(output, &compositor->output_list, link) {
3869 if (output == remove_output) {
3870 offset = output->width;
3871 continue;
3872 }
3873
3874 if (offset > 0) {
3875 weston_output_move(output,
3876 output->x - offset, output->y);
3877 output->dirty = 1;
3878 }
3879 }
3880}
3881
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003882WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003883weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003884{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003885 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003886 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003887
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003888 output->destroying = 1;
3889
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003890 wl_list_for_each(view, &output->compositor->view_list, link) {
3891 if (view->output_mask & (1 << output->id))
3892 weston_view_assign_output(view);
3893 }
3894
Pekka Paalanen0513a952014-05-21 16:17:27 +03003895 wl_event_source_remove(output->repaint_timer);
3896
Pekka Paalanen133e4392014-09-23 22:08:46 -04003897 weston_presentation_feedback_discard_list(&output->feedback_list);
3898
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003899 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003900 wl_list_remove(&output->link);
3901
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003902 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003903 wl_signal_emit(&output->destroy_signal, output);
3904
Richard Hughesafe690c2013-05-02 10:10:04 +01003905 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003906 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003907 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003908 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003909
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003910 wl_resource_for_each(resource, &output->resource_list) {
3911 wl_resource_set_destructor(resource, NULL);
3912 }
3913
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003914 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003915}
3916
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003917WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003918weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003919{
Scott Moreau850ca422012-05-21 15:21:25 -06003920 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003921
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003922 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003923 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003924
Scott Moreauccbf29d2012-02-22 14:21:41 -07003925 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003926 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003927 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003928 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003929 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003930 weston_matrix_scale(&output->matrix, magnification,
3931 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003932 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003933
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003934 switch (output->transform) {
3935 case WL_OUTPUT_TRANSFORM_FLIPPED:
3936 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3937 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3938 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3939 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3940 weston_matrix_scale(&output->matrix, -1, 1, 1);
3941 break;
3942 }
3943
3944 switch (output->transform) {
3945 default:
3946 case WL_OUTPUT_TRANSFORM_NORMAL:
3947 case WL_OUTPUT_TRANSFORM_FLIPPED:
3948 break;
3949 case WL_OUTPUT_TRANSFORM_90:
3950 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3951 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
3952 weston_matrix_rotate_xy(&output->matrix, 0, 1);
3953 break;
3954 case WL_OUTPUT_TRANSFORM_180:
3955 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3956 weston_matrix_translate(&output->matrix,
3957 -output->width, -output->height, 0);
3958 weston_matrix_rotate_xy(&output->matrix, -1, 0);
3959 break;
3960 case WL_OUTPUT_TRANSFORM_270:
3961 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3962 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3963 weston_matrix_rotate_xy(&output->matrix, 0, -1);
3964 break;
3965 }
3966
3967 if (output->current_scale != 1)
3968 weston_matrix_scale(&output->matrix,
3969 output->current_scale,
3970 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003971
Scott Moreauccbf29d2012-02-22 14:21:41 -07003972 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05003973
3974 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003975}
3976
Scott Moreau1bad5db2012-08-18 01:04:05 -06003977static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003978weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003979{
3980 output->transform = transform;
3981
3982 switch (transform) {
3983 case WL_OUTPUT_TRANSFORM_90:
3984 case WL_OUTPUT_TRANSFORM_270:
3985 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3986 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3987 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003988 output->width = output->current_mode->height;
3989 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003990 break;
3991 case WL_OUTPUT_TRANSFORM_NORMAL:
3992 case WL_OUTPUT_TRANSFORM_180:
3993 case WL_OUTPUT_TRANSFORM_FLIPPED:
3994 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003995 output->width = output->current_mode->width;
3996 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003997 break;
3998 default:
3999 break;
4000 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06004001
Hardening57388e42013-09-18 23:56:36 +02004002 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02004003 output->width /= scale;
4004 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004005}
4006
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004007static void
4008weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004009{
4010 output->x = x;
4011 output->y = y;
4012
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004013 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004014 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004015 output->width,
4016 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004017}
4018
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004019WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004020weston_output_move(struct weston_output *output, int x, int y)
4021{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004022 struct wl_resource *resource;
4023
4024 output->move_x = x - output->x;
4025 output->move_y = y - output->y;
4026
4027 if (output->move_x == 0 && output->move_y == 0)
4028 return;
4029
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004030 weston_output_init_geometry(output, x, y);
4031
4032 output->dirty = 1;
4033
4034 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004035 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004036
4037 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004038 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004039 wl_output_send_geometry(resource,
4040 output->x,
4041 output->y,
4042 output->mm_width,
4043 output->mm_height,
4044 output->subpixel,
4045 output->make,
4046 output->model,
4047 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004048
4049 if (wl_resource_get_version(resource) >= 2)
4050 wl_output_send_done(resource);
4051 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004052}
4053
4054WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004055weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004056 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004057 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004058{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004059 struct wl_event_loop *loop;
4060
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004061 output->compositor = c;
4062 output->x = x;
4063 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004064 output->mm_width = mm_width;
4065 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004066 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004067 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004068
Alexander Larsson0b135062013-05-28 16:23:36 +02004069 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004070 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004071
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004072 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004073 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004074
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004075 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004076 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004077 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004078 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004079 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004080 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004081
Pekka Paalanen0513a952014-05-21 16:17:27 +03004082 loop = wl_display_get_event_loop(c->wl_display);
4083 output->repaint_timer = wl_event_loop_add_timer(loop,
4084 output_repaint_timer_handler, output);
4085
Casey Dahlin58ba1372012-04-19 22:50:08 -04004086 output->id = ffs(~output->compositor->output_id_pool) - 1;
4087 output->compositor->output_id_pool |= 1 << output->id;
4088
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004089 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004090 wl_global_create(c->wl_display, &wl_output_interface, 2,
4091 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004092}
4093
4094/** Adds an output to the compositor's output list and
4095 * send the compositor's output_created signal.
4096 *
4097 * \param compositor The compositor instance.
4098 * \param output The output to be added.
4099 */
4100WL_EXPORT void
4101weston_compositor_add_output(struct weston_compositor *compositor,
4102 struct weston_output *output)
4103{
4104 wl_list_insert(compositor->output_list.prev, &output->link);
4105 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004106}
4107
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004108WL_EXPORT void
4109weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004110 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004111 wl_fixed_t *x, wl_fixed_t *y)
4112{
Derek Foreman0f679412014-10-02 13:41:17 -05004113 struct weston_vector p = { {
4114 wl_fixed_to_double(device_x),
4115 wl_fixed_to_double(device_y),
4116 0.0,
4117 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004118
Derek Foreman67a18b92015-03-24 11:36:14 -05004119 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004120
Derek Foreman0f679412014-10-02 13:41:17 -05004121 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4122 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004123}
4124
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004125static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004126destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004127{
Jonny Lamb74130762013-11-26 18:19:46 +01004128 struct weston_surface *surface =
4129 wl_resource_get_user_data(resource);
4130
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004131 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004132 surface->pending.buffer_viewport.buffer.src_width =
4133 wl_fixed_from_int(-1);
4134 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004135 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004136}
4137
4138static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004139viewport_destroy(struct wl_client *client,
4140 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004141{
4142 wl_resource_destroy(resource);
4143}
4144
4145static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004146viewport_set(struct wl_client *client,
4147 struct wl_resource *resource,
4148 wl_fixed_t src_x,
4149 wl_fixed_t src_y,
4150 wl_fixed_t src_width,
4151 wl_fixed_t src_height,
4152 int32_t dst_width,
4153 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004154{
Jonny Lamb74130762013-11-26 18:19:46 +01004155 struct weston_surface *surface =
4156 wl_resource_get_user_data(resource);
4157
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004158 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004159
Jonny Lamb8ae35902013-11-26 18:19:45 +01004160 if (wl_fixed_to_double(src_width) < 0 ||
4161 wl_fixed_to_double(src_height) < 0) {
4162 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004163 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004164 "source dimensions must be non-negative (%fx%f)",
4165 wl_fixed_to_double(src_width),
4166 wl_fixed_to_double(src_height));
4167 return;
4168 }
4169
4170 if (dst_width <= 0 || dst_height <= 0) {
4171 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004172 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004173 "destination dimensions must be positive (%dx%d)",
4174 dst_width, dst_height);
4175 return;
4176 }
4177
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004178 surface->pending.buffer_viewport.buffer.src_x = src_x;
4179 surface->pending.buffer_viewport.buffer.src_y = src_y;
4180 surface->pending.buffer_viewport.buffer.src_width = src_width;
4181 surface->pending.buffer_viewport.buffer.src_height = src_height;
4182 surface->pending.buffer_viewport.surface.width = dst_width;
4183 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004184 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004185}
4186
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004187static void
4188viewport_set_source(struct wl_client *client,
4189 struct wl_resource *resource,
4190 wl_fixed_t src_x,
4191 wl_fixed_t src_y,
4192 wl_fixed_t src_width,
4193 wl_fixed_t src_height)
4194{
4195 struct weston_surface *surface =
4196 wl_resource_get_user_data(resource);
4197
4198 assert(surface->viewport_resource != NULL);
4199
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004200 if (src_width == wl_fixed_from_int(-1) &&
4201 src_height == wl_fixed_from_int(-1)) {
4202 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004203 surface->pending.buffer_viewport.buffer.src_width =
4204 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004205 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004206 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004207 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004208
4209 if (src_width <= 0 || src_height <= 0) {
4210 wl_resource_post_error(resource,
4211 WL_VIEWPORT_ERROR_BAD_VALUE,
4212 "source size must be positive (%fx%f)",
4213 wl_fixed_to_double(src_width),
4214 wl_fixed_to_double(src_height));
4215 return;
4216 }
4217
4218 surface->pending.buffer_viewport.buffer.src_x = src_x;
4219 surface->pending.buffer_viewport.buffer.src_y = src_y;
4220 surface->pending.buffer_viewport.buffer.src_width = src_width;
4221 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004222 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004223}
4224
4225static void
4226viewport_set_destination(struct wl_client *client,
4227 struct wl_resource *resource,
4228 int32_t dst_width,
4229 int32_t dst_height)
4230{
4231 struct weston_surface *surface =
4232 wl_resource_get_user_data(resource);
4233
4234 assert(surface->viewport_resource != NULL);
4235
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004236 if (dst_width == -1 && dst_height == -1) {
4237 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004238 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004239 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004240 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004241 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004242
4243 if (dst_width <= 0 || dst_height <= 0) {
4244 wl_resource_post_error(resource,
4245 WL_VIEWPORT_ERROR_BAD_VALUE,
4246 "destination size must be positive (%dx%d)",
4247 dst_width, dst_height);
4248 return;
4249 }
4250
4251 surface->pending.buffer_viewport.surface.width = dst_width;
4252 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004253 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004254}
4255
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004256static const struct wl_viewport_interface viewport_interface = {
4257 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004258 viewport_set,
4259 viewport_set_source,
4260 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004261};
4262
4263static void
4264scaler_destroy(struct wl_client *client,
4265 struct wl_resource *resource)
4266{
4267 wl_resource_destroy(resource);
4268}
4269
4270static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004271scaler_get_viewport(struct wl_client *client,
4272 struct wl_resource *scaler,
4273 uint32_t id,
4274 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004275{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004276 int version = wl_resource_get_version(scaler);
4277 struct weston_surface *surface =
4278 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004279 struct wl_resource *resource;
4280
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004281 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004282 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004283 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4284 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004285 return;
4286 }
4287
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004288 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004289 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004290 if (resource == NULL) {
4291 wl_client_post_no_memory(client);
4292 return;
4293 }
4294
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004295 wl_resource_set_implementation(resource, &viewport_interface,
4296 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004297
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004298 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004299}
4300
4301static const struct wl_scaler_interface scaler_interface = {
4302 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004303 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004304};
4305
4306static void
4307bind_scaler(struct wl_client *client,
4308 void *data, uint32_t version, uint32_t id)
4309{
4310 struct wl_resource *resource;
4311
4312 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004313 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004314 if (resource == NULL) {
4315 wl_client_post_no_memory(client);
4316 return;
4317 }
4318
4319 wl_resource_set_implementation(resource, &scaler_interface,
4320 NULL, NULL);
4321}
4322
4323static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004324destroy_presentation_feedback(struct wl_resource *feedback_resource)
4325{
4326 struct weston_presentation_feedback *feedback;
4327
4328 feedback = wl_resource_get_user_data(feedback_resource);
4329
4330 wl_list_remove(&feedback->link);
4331 free(feedback);
4332}
4333
4334static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004335presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4336{
4337 wl_resource_destroy(resource);
4338}
4339
4340static void
4341presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004342 struct wl_resource *presentation_resource,
4343 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004344 uint32_t callback)
4345{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004346 struct weston_surface *surface;
4347 struct weston_presentation_feedback *feedback;
4348
4349 surface = wl_resource_get_user_data(surface_resource);
4350
Bryce Harringtonde16d892014-11-20 22:21:57 -08004351 feedback = zalloc(sizeof *feedback);
4352 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004353 goto err_calloc;
4354
4355 feedback->resource = wl_resource_create(client,
4356 &presentation_feedback_interface,
4357 1, callback);
4358 if (!feedback->resource)
4359 goto err_create;
4360
4361 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4362 destroy_presentation_feedback);
4363
4364 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4365
4366 return;
4367
4368err_create:
4369 free(feedback);
4370
4371err_calloc:
4372 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004373}
4374
4375static const struct presentation_interface presentation_implementation = {
4376 presentation_destroy,
4377 presentation_feedback
4378};
4379
4380static void
4381bind_presentation(struct wl_client *client,
4382 void *data, uint32_t version, uint32_t id)
4383{
4384 struct weston_compositor *compositor = data;
4385 struct wl_resource *resource;
4386
4387 resource = wl_resource_create(client, &presentation_interface,
4388 MIN(version, 1), id);
4389 if (resource == NULL) {
4390 wl_client_post_no_memory(client);
4391 return;
4392 }
4393
4394 wl_resource_set_implementation(resource, &presentation_implementation,
4395 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004396 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004397}
4398
4399static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004400compositor_bind(struct wl_client *client,
4401 void *data, uint32_t version, uint32_t id)
4402{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004403 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004404 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004405
Jason Ekstranda85118c2013-06-27 20:17:02 -05004406 resource = wl_resource_create(client, &wl_compositor_interface,
4407 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004408 if (resource == NULL) {
4409 wl_client_post_no_memory(client);
4410 return;
4411 }
4412
4413 wl_resource_set_implementation(resource, &compositor_interface,
4414 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004415}
4416
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004417WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004418weston_environment_get_fd(const char *env)
4419{
4420 char *e, *end;
4421 int fd, flags;
4422
4423 e = getenv(env);
4424 if (!e)
4425 return -1;
4426 fd = strtol(e, &end, 0);
4427 if (*end != '\0')
4428 return -1;
4429
4430 flags = fcntl(fd, F_GETFD);
4431 if (flags == -1)
4432 return -1;
4433
4434 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4435 unsetenv(env);
4436
4437 return fd;
4438}
4439
Pekka Paalanenb5026542014-11-12 15:09:24 +02004440static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004441timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004442 uint32_t key, void *data)
4443{
4444 struct weston_compositor *compositor = data;
4445
4446 if (weston_timeline_enabled_)
4447 weston_timeline_close();
4448 else
4449 weston_timeline_open(compositor);
4450}
4451
Giulio Camuffo459137b2014-10-11 23:56:24 +03004452/** Create the compositor.
4453 *
4454 * This functions creates and initializes a compositor instance.
4455 *
4456 * \param display The Wayland display to be used.
4457 * \param user_data A pointer to an object that can later be retrieved
4458 * using the \ref weston_compositor_get_user_data function.
4459 * \return The compositor instance on success or NULL on failure.
4460 */
4461WL_EXPORT struct weston_compositor *
4462weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004463{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004464 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004465 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004466
Giulio Camuffo459137b2014-10-11 23:56:24 +03004467 ec = zalloc(sizeof *ec);
4468 if (!ec)
4469 return NULL;
4470
4471 ec->wl_display = display;
4472 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004473 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004474 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004475 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004476 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004477 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004478 wl_signal_init(&ec->idle_signal);
4479 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004480 wl_signal_init(&ec->show_input_panel_signal);
4481 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004482 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004483 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004484 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004485 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004486 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004487 wl_signal_init(&ec->session_signal);
4488 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004489
Casey Dahlin58ba1372012-04-19 22:50:08 -04004490 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004491 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004492
Giulio Camuffo954f1832014-10-11 18:27:30 +03004493 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004494 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004495 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004496
Giulio Camuffo954f1832014-10-11 18:27:30 +03004497 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004498 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004499 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004500
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004501 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004502 ec, bind_scaler))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004503 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004504
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004505 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4506 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004507 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004508
Jason Ekstranda7af7042013-10-12 22:38:11 -05004509 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004510 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004511 wl_list_init(&ec->layer_list);
4512 wl_list_init(&ec->seat_list);
4513 wl_list_init(&ec->output_list);
4514 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004515 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004516 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004517 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004518 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004519 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004520
Xiong Zhang97116532013-10-23 13:58:31 +08004521 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004522 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004523
Giulio Camuffo459137b2014-10-11 23:56:24 +03004524 wl_data_device_manager_init(ec->wl_display);
4525
4526 wl_display_init_shm(ec->wl_display);
4527
4528 loop = wl_display_get_event_loop(ec->wl_display);
4529 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004530
4531 ec->input_loop = wl_event_loop_create();
4532
4533 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4534 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4535
4536 weston_compositor_add_debug_binding(ec, KEY_T,
4537 timeline_key_binding_handler, ec);
4538
Giulio Camuffo459137b2014-10-11 23:56:24 +03004539 return ec;
4540
4541fail:
4542 free(ec);
4543 return NULL;
4544}
4545
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004546WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004547weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004548{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004549 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004550
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004551 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004552 if (ec->input_loop_source)
4553 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004554
Matt Roper361d2ad2011-08-29 13:52:23 -07004555 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004556 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004557 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004558
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004559 if (ec->renderer)
4560 ec->renderer->destroy(ec);
4561
Daniel Stone325fc2d2012-05-30 16:31:58 +01004562 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004563 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004564 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004565 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004566 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004567 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004568
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004569 weston_plane_release(&ec->primary_plane);
4570
Jonas Ådahlc97af922012-03-28 22:36:09 +02004571 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07004572}
4573
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004574WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004575weston_compositor_exit_with_code(struct weston_compositor *compositor,
4576 int exit_code)
4577{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004578 if (compositor->exit_code == EXIT_SUCCESS)
4579 compositor->exit_code = exit_code;
4580
Giulio Camuffo459137b2014-10-11 23:56:24 +03004581 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004582}
4583
4584WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004585weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4586 const struct weston_pointer_grab_interface *interface)
4587{
4588 struct weston_seat *seat;
4589
4590 ec->default_pointer_grab = interface;
4591 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004592 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4593
4594 if (pointer)
4595 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004596 }
4597}
4598
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004599WL_EXPORT int
4600weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4601 clockid_t clk_id)
4602{
4603 struct timespec ts;
4604
4605 if (clock_gettime(clk_id, &ts) < 0)
4606 return -1;
4607
4608 compositor->presentation_clock = clk_id;
4609
4610 return 0;
4611}
4612
4613/*
4614 * For choosing the software clock, when the display hardware or API
4615 * does not expose a compatible presentation timestamp.
4616 */
4617WL_EXPORT int
4618weston_compositor_set_presentation_clock_software(
4619 struct weston_compositor *compositor)
4620{
4621 /* In order of preference */
4622 static const clockid_t clocks[] = {
4623 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4624 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4625 CLOCK_MONOTONIC, /* no jumps, may crawl */
4626 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4627 CLOCK_REALTIME /* may jump and crawl */
4628 };
4629 unsigned i;
4630
4631 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4632 if (weston_compositor_set_presentation_clock(compositor,
4633 clocks[i]) == 0)
4634 return 0;
4635
4636 weston_log("Error: no suitable presentation clock available.\n");
4637
4638 return -1;
4639}
4640
Pekka Paalanen662f3842015-03-18 12:17:26 +02004641/** Read the current time from the Presentation clock
4642 *
4643 * \param compositor
4644 * \param ts[out] The current time.
4645 *
4646 * \note Reading the current time in user space is always imprecise to some
4647 * degree.
4648 *
4649 * This function is never meant to fail. If reading the clock does fail,
4650 * an error message is logged and a zero time is returned. Callers are not
4651 * supposed to detect or react to failures.
4652 */
4653WL_EXPORT void
4654weston_compositor_read_presentation_clock(
4655 const struct weston_compositor *compositor,
4656 struct timespec *ts)
4657{
4658 static bool warned;
4659 int ret;
4660
4661 ret = clock_gettime(compositor->presentation_clock, ts);
4662 if (ret < 0) {
4663 ts->tv_sec = 0;
4664 ts->tv_nsec = 0;
4665
4666 if (!warned)
4667 weston_log("Error: failure to read "
4668 "the presentation clock %#x: '%m' (%d)\n",
4669 compositor->presentation_clock, errno);
4670 warned = true;
4671 }
4672}
4673
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004674/** Import dmabuf buffer into current renderer
4675 *
4676 * \param compositor
4677 * \param buffer the dmabuf buffer to import
4678 * \return true on usable buffers, false otherwise
4679 *
4680 * This function tests that the linux_dmabuf_buffer is usable
4681 * for the current renderer. Returns false on unusable buffers. Usually
4682 * usability is tested by importing the dmabufs for composition.
4683 *
4684 * This hook is also used for detecting if the renderer supports
4685 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4686 * supported.
4687 * */
4688WL_EXPORT bool
4689weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4690 struct linux_dmabuf_buffer *buffer)
4691{
4692 struct weston_renderer *renderer;
4693
4694 renderer = compositor->renderer;
4695
4696 if (renderer->import_dmabuf == NULL)
4697 return false;
4698
4699 return renderer->import_dmabuf(compositor, buffer);
4700}
4701
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004702WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004703weston_version(int *major, int *minor, int *micro)
4704{
4705 *major = WESTON_VERSION_MAJOR;
4706 *minor = WESTON_VERSION_MINOR;
4707 *micro = WESTON_VERSION_MICRO;
4708}
4709
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004710WL_EXPORT void *
4711weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004712{
Derek Foreman3f86e502015-06-08 11:46:54 -05004713 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004714 char path[PATH_MAX];
4715 void *module, *init;
4716
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004717 if (name == NULL)
4718 return NULL;
4719
Derek Foreman3f86e502015-06-08 11:46:54 -05004720 if (name[0] != '/') {
4721 if (builddir)
4722 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4723 else
4724 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
4725 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004726 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004727 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004728
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004729 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4730 if (module) {
4731 weston_log("Module '%s' already loaded\n", path);
4732 dlclose(module);
4733 return NULL;
4734 }
4735
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004736 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004737 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004738 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004739 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004740 return NULL;
4741 }
4742
4743 init = dlsym(module, entrypoint);
4744 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004745 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004746 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004747 return NULL;
4748 }
4749
4750 return init;
4751}
4752
Giulio Camuffo459137b2014-10-11 23:56:24 +03004753
4754/** Destroys the compositor.
4755 *
4756 * This function cleans up the compositor state and destroys it.
4757 *
4758 * \param compositor The compositor to be destroyed.
4759 */
4760WL_EXPORT void
4761weston_compositor_destroy(struct weston_compositor *compositor)
4762{
4763 /* prevent further rendering while shutting down */
4764 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4765
4766 wl_signal_emit(&compositor->destroy_signal, compositor);
4767
4768 weston_compositor_xkb_destroy(compositor);
4769
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004770 if (compositor->backend)
4771 compositor->backend->destroy(compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004772 free(compositor);
4773}
4774
4775/** Instruct the compositor to exit.
4776 *
4777 * This functions does not directly destroy the compositor object, it merely
4778 * command it to start the tear down process. It is not guaranteed that the
4779 * tear down will happen immediately.
4780 *
4781 * \param compositor The compositor to tear down.
4782 */
4783WL_EXPORT void
4784weston_compositor_exit(struct weston_compositor *compositor)
4785{
4786 compositor->exit(compositor);
4787}
4788
4789/** Return the user data stored in the compositor.
4790 *
4791 * This function returns the user data pointer set with user_data parameter
4792 * to the \ref weston_compositor_create function.
4793 */
4794WL_EXPORT void *
4795weston_compositor_get_user_data(struct weston_compositor *compositor)
4796{
4797 return compositor->user_data;
4798}