blob: d723b64d8171ff4c71302e860690c8ace1117b4c [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
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600729/** Transform a region by a matrix, restricted to axis-aligned transformations
730 *
731 * Warning: This function does not work for projective, affine, or matrices
732 * that encode arbitrary rotations. Only 90-degree step rotations are
733 * supported.
734 */
735WL_EXPORT void
736weston_matrix_transform_region(pixman_region32_t *dest,
737 struct weston_matrix *matrix,
738 pixman_region32_t *src)
739{
740 pixman_box32_t *src_rects, *dest_rects;
741 int nrects, i;
742
743 src_rects = pixman_region32_rectangles(src, &nrects);
744 dest_rects = malloc(nrects * sizeof(*dest_rects));
745 if (!dest_rects)
746 return;
747
748 for (i = 0; i < nrects; i++) {
749 struct weston_vector vec1 = {{
750 src_rects[i].x1, src_rects[i].y1, 0, 1
751 }};
752 weston_matrix_transform(matrix, &vec1);
753 vec1.f[0] /= vec1.f[3];
754 vec1.f[1] /= vec1.f[3];
755
756 struct weston_vector vec2 = {{
757 src_rects[i].x2, src_rects[i].y2, 0, 1
758 }};
759 weston_matrix_transform(matrix, &vec2);
760 vec2.f[0] /= vec2.f[3];
761 vec2.f[1] /= vec2.f[3];
762
763 if (vec1.f[0] < vec2.f[0]) {
764 dest_rects[i].x1 = floor(vec1.f[0]);
765 dest_rects[i].x2 = ceil(vec2.f[0]);
766 } else {
767 dest_rects[i].x1 = floor(vec2.f[0]);
768 dest_rects[i].x2 = ceil(vec1.f[0]);
769 }
770
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600771 if (vec1.f[1] < vec2.f[1]) {
772 dest_rects[i].y1 = floor(vec1.f[1]);
773 dest_rects[i].y2 = ceil(vec2.f[1]);
774 } else {
775 dest_rects[i].y1 = floor(vec2.f[1]);
776 dest_rects[i].y2 = ceil(vec1.f[1]);
777 }
778 }
779
780 pixman_region32_clear(dest);
781 pixman_region32_init_rects(dest, dest_rects, nrects);
782 free(dest_rects);
783}
784
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200785WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500786weston_transformed_region(int width, int height,
787 enum wl_output_transform transform,
788 int32_t scale,
789 pixman_region32_t *src, pixman_region32_t *dest)
790{
791 pixman_box32_t *src_rects, *dest_rects;
792 int nrects, i;
793
794 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
795 if (src != dest)
796 pixman_region32_copy(dest, src);
797 return;
798 }
799
800 src_rects = pixman_region32_rectangles(src, &nrects);
801 dest_rects = malloc(nrects * sizeof(*dest_rects));
802 if (!dest_rects)
803 return;
804
805 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
806 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
807 } else {
808 for (i = 0; i < nrects; i++) {
809 switch (transform) {
810 default:
811 case WL_OUTPUT_TRANSFORM_NORMAL:
812 dest_rects[i].x1 = src_rects[i].x1;
813 dest_rects[i].y1 = src_rects[i].y1;
814 dest_rects[i].x2 = src_rects[i].x2;
815 dest_rects[i].y2 = src_rects[i].y2;
816 break;
817 case WL_OUTPUT_TRANSFORM_90:
818 dest_rects[i].x1 = height - src_rects[i].y2;
819 dest_rects[i].y1 = src_rects[i].x1;
820 dest_rects[i].x2 = height - src_rects[i].y1;
821 dest_rects[i].y2 = src_rects[i].x2;
822 break;
823 case WL_OUTPUT_TRANSFORM_180:
824 dest_rects[i].x1 = width - src_rects[i].x2;
825 dest_rects[i].y1 = height - src_rects[i].y2;
826 dest_rects[i].x2 = width - src_rects[i].x1;
827 dest_rects[i].y2 = height - src_rects[i].y1;
828 break;
829 case WL_OUTPUT_TRANSFORM_270:
830 dest_rects[i].x1 = src_rects[i].y1;
831 dest_rects[i].y1 = width - src_rects[i].x2;
832 dest_rects[i].x2 = src_rects[i].y2;
833 dest_rects[i].y2 = width - src_rects[i].x1;
834 break;
835 case WL_OUTPUT_TRANSFORM_FLIPPED:
836 dest_rects[i].x1 = width - src_rects[i].x2;
837 dest_rects[i].y1 = src_rects[i].y1;
838 dest_rects[i].x2 = width - src_rects[i].x1;
839 dest_rects[i].y2 = src_rects[i].y2;
840 break;
841 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
842 dest_rects[i].x1 = height - src_rects[i].y2;
843 dest_rects[i].y1 = width - src_rects[i].x2;
844 dest_rects[i].x2 = height - src_rects[i].y1;
845 dest_rects[i].y2 = width - src_rects[i].x1;
846 break;
847 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
848 dest_rects[i].x1 = src_rects[i].x1;
849 dest_rects[i].y1 = height - src_rects[i].y2;
850 dest_rects[i].x2 = src_rects[i].x2;
851 dest_rects[i].y2 = height - src_rects[i].y1;
852 break;
853 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
854 dest_rects[i].x1 = src_rects[i].y1;
855 dest_rects[i].y1 = src_rects[i].x1;
856 dest_rects[i].x2 = src_rects[i].y2;
857 dest_rects[i].y2 = src_rects[i].x2;
858 break;
859 }
860 }
861 }
862
863 if (scale != 1) {
864 for (i = 0; i < nrects; i++) {
865 dest_rects[i].x1 *= scale;
866 dest_rects[i].x2 *= scale;
867 dest_rects[i].y1 *= scale;
868 dest_rects[i].y2 *= scale;
869 }
870 }
871
872 pixman_region32_clear(dest);
873 pixman_region32_init_rects(dest, dest_rects, nrects);
874 free(dest_rects);
875}
876
Jonny Lamb74130762013-11-26 18:19:46 +0100877static void
878scaler_surface_to_buffer(struct weston_surface *surface,
879 float sx, float sy, float *bx, float *by)
880{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200881 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200882 double src_width, src_height;
883 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200884
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200885 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
886 if (vp->surface.width == -1) {
887 *bx = sx;
888 *by = sy;
889 return;
890 }
Jonny Lamb74130762013-11-26 18:19:46 +0100891
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200892 src_x = 0.0;
893 src_y = 0.0;
894 src_width = surface->width_from_buffer;
895 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100896 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200897 src_x = wl_fixed_to_double(vp->buffer.src_x);
898 src_y = wl_fixed_to_double(vp->buffer.src_y);
899 src_width = wl_fixed_to_double(vp->buffer.src_width);
900 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100901 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200902
903 *bx = sx * src_width / surface->width + src_x;
904 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100905}
906
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500907WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200908weston_surface_to_buffer_float(struct weston_surface *surface,
909 float sx, float sy, float *bx, float *by)
910{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200911 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
912
Jonny Lamb74130762013-11-26 18:19:46 +0100913 /* first transform coordinates if the scaler is set */
914 scaler_surface_to_buffer(surface, sx, sy, bx, by);
915
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500916 weston_transformed_coord(surface->width_from_buffer,
917 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200918 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100919 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200920}
921
Alexander Larsson4ea95522013-05-22 14:41:37 +0200922WL_EXPORT void
923weston_surface_to_buffer(struct weston_surface *surface,
924 int sx, int sy, int *bx, int *by)
925{
926 float bxf, byf;
927
Jonny Lamb74130762013-11-26 18:19:46 +0100928 weston_surface_to_buffer_float(surface,
929 sx, sy, &bxf, &byf);
930
Alexander Larsson4ea95522013-05-22 14:41:37 +0200931 *bx = floorf(bxf);
932 *by = floorf(byf);
933}
934
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200935WL_EXPORT pixman_box32_t
936weston_surface_to_buffer_rect(struct weston_surface *surface,
937 pixman_box32_t rect)
938{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200939 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100940 float xf, yf;
941
942 /* first transform box coordinates if the scaler is set */
943 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
944 rect.x1 = floorf(xf);
945 rect.y1 = floorf(yf);
946
947 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
948 rect.x2 = floorf(xf);
949 rect.y2 = floorf(yf);
950
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500951 return weston_transformed_rect(surface->width_from_buffer,
952 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200953 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200954 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200955}
956
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200957/** Transform a region from surface coordinates to buffer coordinates
958 *
959 * \param surface The surface to fetch wl_viewport and buffer transformation
960 * from.
961 * \param surface_region[in] The region in surface coordinates.
962 * \param buffer_region[out] The region converted to buffer coordinates.
963 *
964 * Buffer_region must be init'd, but will be completely overwritten.
965 *
966 * Viewport and buffer transformations can only do translation, scaling,
967 * and rotations in 90-degree steps. Therefore the only loss in the
968 * conversion is coordinate flooring (rounding).
969 */
970WL_EXPORT void
971weston_surface_to_buffer_region(struct weston_surface *surface,
972 pixman_region32_t *surface_region,
973 pixman_region32_t *buffer_region)
974{
975 pixman_box32_t *src_rects, *dest_rects;
976 int nrects, i;
977
978 src_rects = pixman_region32_rectangles(surface_region, &nrects);
979 dest_rects = malloc(nrects * sizeof(*dest_rects));
980 if (!dest_rects)
981 return;
982
983 for (i = 0; i < nrects; i++) {
984 dest_rects[i] = weston_surface_to_buffer_rect(surface,
985 src_rects[i]);
986 }
987
988 pixman_region32_fini(buffer_region);
989 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
990 free(dest_rects);
991}
992
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200993WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400995 struct weston_plane *plane)
996{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400998 return;
999
Jason Ekstranda7af7042013-10-12 22:38:11 -05001000 weston_view_damage_below(view);
1001 view->plane = plane;
1002 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001003}
1004
Pekka Paalanen51723d52015-02-17 13:10:01 +02001005/** Inflict damage on the plane where the view is visible.
1006 *
1007 * \param view The view that causes the damage.
1008 *
1009 * If the view is currently on a plane (including the primary plane),
1010 * take the view's boundingbox, subtract all the opaque views that cover it,
1011 * and add the remaining region as damage to the plane. This corresponds
1012 * to the damage inflicted to the plane if this view disappeared.
1013 *
1014 * A repaint is scheduled for this view.
1015 *
1016 * The region of all opaque views covering this view is stored in
1017 * weston_view::clip and updated by view_accumulate_damage() during
1018 * weston_output_repaint(). Specifically, that region matches the
1019 * scenegraph as it was last painted.
1020 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001021WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001023{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001024 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001025
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001026 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001027 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001028 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001029 if (view->plane)
1030 pixman_region32_union(&view->plane->damage,
1031 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001032 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001033 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001034}
1035
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001036static void
1037weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1038{
1039 uint32_t different = es->output_mask ^ mask;
1040 uint32_t entered = mask & different;
1041 uint32_t left = es->output_mask & different;
1042 struct weston_output *output;
1043 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001044 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001045
1046 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001047 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001048 return;
1049 if (different == 0)
1050 return;
1051
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001052 client = wl_resource_get_client(es->resource);
1053
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001054 wl_list_for_each(output, &es->compositor->output_list, link) {
1055 if (1 << output->id & different)
1056 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001057 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001058 client);
1059 if (resource == NULL)
1060 continue;
1061 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001062 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001063 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001064 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001065 }
1066}
1067
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001068
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001069static void
1070weston_surface_assign_output(struct weston_surface *es)
1071{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001072 struct weston_output *new_output;
1073 struct weston_view *view;
1074 pixman_region32_t region;
1075 uint32_t max, area, mask;
1076 pixman_box32_t *e;
1077
1078 new_output = NULL;
1079 max = 0;
1080 mask = 0;
1081 pixman_region32_init(&region);
1082 wl_list_for_each(view, &es->views, surface_link) {
1083 if (!view->output)
1084 continue;
1085
1086 pixman_region32_intersect(&region, &view->transform.boundingbox,
1087 &view->output->region);
1088
1089 e = pixman_region32_extents(&region);
1090 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1091
1092 mask |= view->output_mask;
1093
1094 if (area >= max) {
1095 new_output = view->output;
1096 max = area;
1097 }
1098 }
1099 pixman_region32_fini(&region);
1100
1101 es->output = new_output;
1102 weston_surface_update_output_mask(es, mask);
1103}
1104
1105static void
1106weston_view_assign_output(struct weston_view *ev)
1107{
1108 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001109 struct weston_output *output, *new_output;
1110 pixman_region32_t region;
1111 uint32_t max, area, mask;
1112 pixman_box32_t *e;
1113
1114 new_output = NULL;
1115 max = 0;
1116 mask = 0;
1117 pixman_region32_init(&region);
1118 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001119 if (output->destroying)
1120 continue;
1121
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001123 &output->region);
1124
1125 e = pixman_region32_extents(&region);
1126 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1127
1128 if (area > 0)
1129 mask |= 1 << output->id;
1130
1131 if (area >= max) {
1132 new_output = output;
1133 max = area;
1134 }
1135 }
1136 pixman_region32_fini(&region);
1137
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 ev->output = new_output;
1139 ev->output_mask = mask;
1140
1141 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001142}
1143
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001144static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001145weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1146 int from_x, int from_y, int *to_x, int *to_y)
1147{
1148 float x, y;
1149
1150 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1151 weston_view_from_global_float(to, x, y, &x, &y);
1152
1153 *to_x = round(x);
1154 *to_y = round(y);
1155}
1156
1157static void
1158weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1159{
1160 pixman_box32_t *a;
1161 pixman_box32_t b;
1162
1163 a = pixman_region32_extents(&from->geometry.scissor);
1164
1165 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1166 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1167
1168 pixman_region32_fini(&to->geometry.scissor);
1169 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1170}
1171
1172static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001173view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001175{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001176 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1177 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001178 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001179 { inbox->x1, inbox->y1 },
1180 { inbox->x1, inbox->y2 },
1181 { inbox->x2, inbox->y1 },
1182 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001183 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001184 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001185 int i;
1186
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001187 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001188 /* avoid rounding empty bbox to 1x1 */
1189 pixman_region32_init(bbox);
1190 return;
1191 }
1192
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001193 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001194 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001195 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001196 if (x < min_x)
1197 min_x = x;
1198 if (x > max_x)
1199 max_x = x;
1200 if (y < min_y)
1201 min_y = y;
1202 if (y > max_y)
1203 max_y = y;
1204 }
1205
Pekka Paalanen219b9822012-02-08 15:38:37 +02001206 int_x = floorf(min_x);
1207 int_y = floorf(min_y);
1208 pixman_region32_init_rect(bbox, int_x, int_y,
1209 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001210}
1211
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001212static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001214{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001216
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001217 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001218 view->geometry.x = roundf(view->geometry.x);
1219 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001220
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001221 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1223 view->transform.position.matrix.d[12] = view->geometry.x;
1224 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001225
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001227
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 view->transform.inverse = view->transform.position.matrix;
1229 view->transform.inverse.d[12] = -view->geometry.x;
1230 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001231
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001233 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001234 view->surface->width,
1235 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001236 if (view->geometry.scissor_enabled)
1237 pixman_region32_intersect(&view->transform.boundingbox,
1238 &view->transform.boundingbox,
1239 &view->geometry.scissor);
1240
1241 pixman_region32_translate(&view->transform.boundingbox,
1242 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001243
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 if (view->alpha == 1.0) {
1245 pixman_region32_copy(&view->transform.opaque,
1246 &view->surface->opaque);
1247 pixman_region32_translate(&view->transform.opaque,
1248 view->geometry.x,
1249 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001250 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001251}
1252
1253static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001254weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001255{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 struct weston_view *parent = view->geometry.parent;
1257 struct weston_matrix *matrix = &view->transform.matrix;
1258 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001259 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001260 pixman_region32_t surfregion;
1261 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001262
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001264
1265 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1267 view->transform.position.matrix.d[12] = view->geometry.x;
1268 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001269
1270 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001272 weston_matrix_multiply(matrix, &tform->matrix);
1273
Pekka Paalanen483243f2013-03-08 14:56:50 +02001274 if (parent)
1275 weston_matrix_multiply(matrix, &parent->transform.matrix);
1276
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001277 if (weston_matrix_invert(inverse, matrix) < 0) {
1278 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 weston_log("error: weston_view %p"
1280 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001281 return -1;
1282 }
1283
Pekka Paalanen380adf52015-02-16 14:39:11 +02001284 pixman_region32_init_rect(&surfregion, 0, 0,
1285 view->surface->width, view->surface->height);
1286 if (view->geometry.scissor_enabled)
1287 pixman_region32_intersect(&surfregion, &surfregion,
1288 &view->geometry.scissor);
1289 surfbox = pixman_region32_extents(&surfregion);
1290
1291 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1292 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001293
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001294 return 0;
1295}
1296
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001297static struct weston_layer *
1298get_view_layer(struct weston_view *view)
1299{
1300 if (view->parent_view)
1301 return get_view_layer(view->parent_view);
1302 return view->layer_link.layer;
1303}
1304
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001305WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001307{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001309 struct weston_layer *layer;
1310 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001311
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001313 return;
1314
Pekka Paalanen483243f2013-03-08 14:56:50 +02001315 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001317
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001319
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001321
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 pixman_region32_fini(&view->transform.boundingbox);
1323 pixman_region32_fini(&view->transform.opaque);
1324 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001325
Pekka Paalanencd403622012-01-25 13:37:39 +02001326 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327 if (view->geometry.transformation_list.next ==
1328 &view->transform.position.link &&
1329 view->geometry.transformation_list.prev ==
1330 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001331 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001333 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334 if (weston_view_update_transform_enable(view) < 0)
1335 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001336 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001337
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001338 layer = get_view_layer(view);
1339 if (layer) {
1340 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001341 pixman_region32_intersect(&view->transform.boundingbox,
1342 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001343 pixman_region32_intersect(&view->transform.opaque,
1344 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001345 pixman_region32_fini(&mask);
1346 }
1347
Pekka Paalanen380adf52015-02-16 14:39:11 +02001348 if (parent) {
1349 if (parent->geometry.scissor_enabled) {
1350 view->geometry.scissor_enabled = true;
1351 weston_view_transfer_scissor(parent, view);
1352 } else {
1353 view->geometry.scissor_enabled = false;
1354 }
1355 }
1356
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001358
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001360
Jason Ekstranda7af7042013-10-12 22:38:11 -05001361 wl_signal_emit(&view->surface->compositor->transform_signal,
1362 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001363}
1364
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001365WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001367{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001368 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001369
1370 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371 * The invariant: if view->geometry.dirty, then all views
1372 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001373 * Corollary: if not parent->geometry.dirty, then all ancestors
1374 * are not dirty.
1375 */
1376
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001378 return;
1379
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001381
Jason Ekstranda7af7042013-10-12 22:38:11 -05001382 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001383 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001385}
1386
1387WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001388weston_view_to_global_fixed(struct weston_view *view,
1389 wl_fixed_t vx, wl_fixed_t vy,
1390 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001391{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001392 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001393
Jason Ekstranda7af7042013-10-12 22:38:11 -05001394 weston_view_to_global_float(view,
1395 wl_fixed_to_double(vx),
1396 wl_fixed_to_double(vy),
1397 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001398 *x = wl_fixed_from_double(xf);
1399 *y = wl_fixed_from_double(yf);
1400}
1401
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001402WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403weston_view_from_global_float(struct weston_view *view,
1404 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001405{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001407 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1408
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001410
1411 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001412 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001414 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415 *vx = 0;
1416 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001417 return;
1418 }
1419
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 *vx = v.f[0] / v.f[3];
1421 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001422 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423 *vx = x - view->geometry.x;
1424 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001425 }
1426}
1427
1428WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429weston_view_from_global_fixed(struct weston_view *view,
1430 wl_fixed_t x, wl_fixed_t y,
1431 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001432{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001434
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435 weston_view_from_global_float(view,
1436 wl_fixed_to_double(x),
1437 wl_fixed_to_double(y),
1438 &vxf, &vyf);
1439 *vx = wl_fixed_from_double(vxf);
1440 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001441}
1442
1443WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001444weston_view_from_global(struct weston_view *view,
1445 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001446{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001448
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1450 *vx = floorf(vxf);
1451 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001452}
1453
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001454WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001455weston_surface_schedule_repaint(struct weston_surface *surface)
1456{
1457 struct weston_output *output;
1458
1459 wl_list_for_each(output, &surface->compositor->output_list, link)
1460 if (surface->output_mask & (1 << output->id))
1461 weston_output_schedule_repaint(output);
1462}
1463
1464WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001465weston_view_schedule_repaint(struct weston_view *view)
1466{
1467 struct weston_output *output;
1468
1469 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1470 if (view->output_mask & (1 << output->id))
1471 weston_output_schedule_repaint(output);
1472}
1473
Pekka Paalanene508ce62015-02-19 13:59:55 +02001474/**
1475 * XXX: This function does it the wrong way.
1476 * surface->damage is the damage from the client, and causes
1477 * surface_flush_damage() to copy pixels. No window management action can
1478 * cause damage to the client-provided content, warranting re-upload!
1479 *
1480 * Instead of surface->damage, this function should record the damage
1481 * with all the views for this surface to avoid extraneous texture
1482 * uploads.
1483 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001484WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001485weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001486{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001487 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001488 0, 0, surface->width,
1489 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001490
Kristian Høgsberg98238702012-08-03 16:29:12 -04001491 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001492}
1493
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001494WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001495weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001496{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001497 if (view->geometry.x == x && view->geometry.y == y)
1498 return;
1499
Jason Ekstranda7af7042013-10-12 22:38:11 -05001500 view->geometry.x = x;
1501 view->geometry.y = y;
1502 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001503}
1504
Pekka Paalanen483243f2013-03-08 14:56:50 +02001505static void
1506transform_parent_handle_parent_destroy(struct wl_listener *listener,
1507 void *data)
1508{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001509 struct weston_view *view =
1510 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001511 geometry.parent_destroy_listener);
1512
Jason Ekstranda7af7042013-10-12 22:38:11 -05001513 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001514}
1515
1516WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001517weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001518 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001519{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001520 if (view->geometry.parent) {
1521 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1522 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001523
1524 if (!parent)
1525 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001526 }
1527
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001529
Jason Ekstranda7af7042013-10-12 22:38:11 -05001530 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001531 transform_parent_handle_parent_destroy;
1532 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001533 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001535 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001536 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001537 }
1538
Jason Ekstranda7af7042013-10-12 22:38:11 -05001539 weston_view_geometry_dirty(view);
1540}
1541
Pekka Paalanen380adf52015-02-16 14:39:11 +02001542/** Set a clip mask rectangle on a view
1543 *
1544 * \param view The view to set the clip mask on.
1545 * \param x Top-left corner X coordinate of the clip rectangle.
1546 * \param y Top-left corner Y coordinate of the clip rectangle.
1547 * \param width Width of the clip rectangle, non-negative.
1548 * \param height Height of the clip rectangle, non-negative.
1549 *
1550 * A shell may set a clip mask rectangle on a view. Everything outside
1551 * the rectangle is cut away for input and output purposes: it is
1552 * not drawn and cannot be hit by hit-test based input like pointer
1553 * motion or touch-downs. Everything inside the rectangle will behave
1554 * normally. Clients are unaware of clipping.
1555 *
1556 * The rectangle is set in the surface local coordinates. Setting a clip
1557 * mask rectangle does not affect the view position, the view is positioned
1558 * as it would be without a clip. The clip also does not change
1559 * weston_surface::width,height.
1560 *
1561 * The clip mask rectangle is part of transformation inheritance
1562 * (weston_view_set_transform_parent()). A clip set in the root of the
1563 * transformation inheritance tree will affect all views in the tree.
1564 * A clip can be set only on the root view. Attempting to set a clip
1565 * on view that has a transformation parent will fail. Assigning a parent
1566 * to a view that has a clip set will cause the clip to be forgotten.
1567 *
1568 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1569 * on the additional transformations in the child views. These transformations
1570 * may not rotate the coordinate axes, i.e., only translation and scaling
1571 * are allowed. Violating this restriction causes the clipping to malfunction.
1572 * Furthermore, using scaling may cause rounding errors in child clipping.
1573 *
1574 * The clip mask rectangle is not automatically adjusted based on
1575 * wl_surface.attach dx and dy arguments.
1576 *
1577 * A clip mask rectangle can be set only if the compositor capability
1578 * WESTON_CAP_VIEW_CLIP_MASK is present.
1579 *
1580 * This function sets the clip mask rectangle and schedules a repaint for
1581 * the view.
1582 */
1583WL_EXPORT void
1584weston_view_set_mask(struct weston_view *view,
1585 int x, int y, int width, int height)
1586{
1587 struct weston_compositor *compositor = view->surface->compositor;
1588
1589 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1590 weston_log("%s not allowed without capability!\n", __func__);
1591 return;
1592 }
1593
1594 if (view->geometry.parent) {
1595 weston_log("view %p has a parent, clip forbidden!\n", view);
1596 return;
1597 }
1598
1599 if (width < 0 || height < 0) {
1600 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1601 x, y, width, height);
1602 return;
1603 }
1604
1605 pixman_region32_fini(&view->geometry.scissor);
1606 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1607 view->geometry.scissor_enabled = true;
1608 weston_view_geometry_dirty(view);
1609 weston_view_schedule_repaint(view);
1610}
1611
1612/** Remove the clip mask from a view
1613 *
1614 * \param view The view to remove the clip mask from.
1615 *
1616 * Removed the clip mask rectangle and schedules a repaint.
1617 *
1618 * \sa weston_view_set_mask
1619 */
1620WL_EXPORT void
1621weston_view_set_mask_infinite(struct weston_view *view)
1622{
1623 view->geometry.scissor_enabled = false;
1624 weston_view_geometry_dirty(view);
1625 weston_view_schedule_repaint(view);
1626}
1627
Derek Foreman280e7dd2014-10-03 13:13:42 -05001628WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001629weston_view_is_mapped(struct weston_view *view)
1630{
1631 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001632 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001633 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001634 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001635}
1636
Derek Foreman280e7dd2014-10-03 13:13:42 -05001637WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001638weston_surface_is_mapped(struct weston_surface *surface)
1639{
1640 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001641 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001642 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001643 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001644}
1645
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001646static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001647surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001648{
1649 struct weston_view *view;
1650
1651 if (surface->width == width && surface->height == height)
1652 return;
1653
1654 surface->width = width;
1655 surface->height = height;
1656
1657 wl_list_for_each(view, &surface->views, surface_link)
1658 weston_view_geometry_dirty(view);
1659}
1660
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001661WL_EXPORT void
1662weston_surface_set_size(struct weston_surface *surface,
1663 int32_t width, int32_t height)
1664{
1665 assert(!surface->resource);
1666 surface_set_size(surface, width, height);
1667}
1668
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001669static int
1670fixed_round_up_to_int(wl_fixed_t f)
1671{
1672 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1673}
1674
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001675static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001676weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001677{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001678 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001679 int32_t width, height;
1680
1681 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001682 surface->width_from_buffer = 0;
1683 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001684 return;
1685 }
1686
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001687 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001688 case WL_OUTPUT_TRANSFORM_90:
1689 case WL_OUTPUT_TRANSFORM_270:
1690 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1691 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001692 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1693 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001694 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001695 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001696 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1697 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001698 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001699 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001700
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001701 surface->width_from_buffer = width;
1702 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001703}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001704
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001705static void
1706weston_surface_update_size(struct weston_surface *surface)
1707{
1708 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1709 int32_t width, height;
1710
1711 width = surface->width_from_buffer;
1712 height = surface->height_from_buffer;
1713
1714 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001715 surface_set_size(surface,
1716 vp->surface.width, vp->surface.height);
1717 return;
1718 }
1719
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001720 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001721 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1722 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1723
1724 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001725 return;
1726 }
1727
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001728 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001729}
1730
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001731WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001732weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001733{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001734 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001735
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001736 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001737
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001738 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001739}
1740
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741WL_EXPORT struct weston_view *
1742weston_compositor_pick_view(struct weston_compositor *compositor,
1743 wl_fixed_t x, wl_fixed_t y,
1744 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001745{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001747 wl_fixed_t view_x, view_y;
1748 int view_ix, view_iy;
1749 int ix = wl_fixed_to_int(x);
1750 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001751
Jason Ekstranda7af7042013-10-12 22:38:11 -05001752 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001753 if (!pixman_region32_contains_point(
1754 &view->transform.boundingbox, ix, iy, NULL))
1755 continue;
1756
1757 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1758 view_ix = wl_fixed_to_int(view_x);
1759 view_iy = wl_fixed_to_int(view_y);
1760
1761 if (!pixman_region32_contains_point(&view->surface->input,
1762 view_ix, view_iy, NULL))
1763 continue;
1764
Pekka Paalanen380adf52015-02-16 14:39:11 +02001765 if (view->geometry.scissor_enabled &&
1766 !pixman_region32_contains_point(&view->geometry.scissor,
1767 view_ix, view_iy, NULL))
1768 continue;
1769
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001770 *vx = view_x;
1771 *vy = view_y;
1772 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001773 }
1774
Derek Foremanf9318d12015-05-11 15:40:11 -05001775 *vx = wl_fixed_from_int(-1000000);
1776 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001777 return NULL;
1778}
1779
1780static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001781weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001782{
Daniel Stone37816df2012-05-16 18:45:18 +01001783 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001784
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001785 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001786 return;
1787
Daniel Stone37816df2012-05-16 18:45:18 +01001788 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001789 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001790}
1791
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001792WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001794{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001795 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001796
Jason Ekstranda7af7042013-10-12 22:38:11 -05001797 if (!weston_view_is_mapped(view))
1798 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001799
Jason Ekstranda7af7042013-10-12 22:38:11 -05001800 weston_view_damage_below(view);
1801 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001802 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001803 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001804 wl_list_remove(&view->link);
1805 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001806 view->output_mask = 0;
1807 weston_surface_assign_output(view->surface);
1808
1809 if (weston_surface_is_mapped(view->surface))
1810 return;
1811
1812 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001813 struct weston_touch *touch = weston_seat_get_touch(seat);
1814 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1815 struct weston_keyboard *keyboard =
1816 weston_seat_get_keyboard(seat);
1817
1818 if (keyboard && keyboard->focus == view->surface)
1819 weston_keyboard_set_focus(keyboard, NULL);
1820 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001821 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001822 if (touch && touch->focus == view)
1823 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001824 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001826
Jason Ekstranda7af7042013-10-12 22:38:11 -05001827WL_EXPORT void
1828weston_surface_unmap(struct weston_surface *surface)
1829{
1830 struct weston_view *view;
1831
1832 wl_list_for_each(view, &surface->views, surface_link)
1833 weston_view_unmap(view);
1834 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001835}
1836
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001837static void
1838weston_surface_reset_pending_buffer(struct weston_surface *surface)
1839{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001840 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001841 surface->pending.sx = 0;
1842 surface->pending.sy = 0;
1843 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001844 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001845}
1846
Jason Ekstranda7af7042013-10-12 22:38:11 -05001847WL_EXPORT void
1848weston_view_destroy(struct weston_view *view)
1849{
1850 wl_signal_emit(&view->destroy_signal, view);
1851
1852 assert(wl_list_empty(&view->geometry.child_list));
1853
1854 if (weston_view_is_mapped(view)) {
1855 weston_view_unmap(view);
1856 weston_compositor_build_view_list(view->surface->compositor);
1857 }
1858
1859 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001860 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001861
1862 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001863 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001864 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001865 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001866
1867 weston_view_set_transform_parent(view, NULL);
1868
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 wl_list_remove(&view->surface_link);
1870
1871 free(view);
1872}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001873
1874WL_EXPORT void
1875weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001876{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001877 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001878 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001879
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001880 if (--surface->ref_count > 0)
1881 return;
1882
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001883 assert(surface->resource == NULL);
1884
Pekka Paalanenca790762015-04-17 14:23:38 +03001885 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001886
Pekka Paalanene67858b2013-04-25 13:57:42 +03001887 assert(wl_list_empty(&surface->subsurface_list_pending));
1888 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001889
Jason Ekstranda7af7042013-10-12 22:38:11 -05001890 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1891 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001892
Jason Ekstrand7b982072014-05-20 14:33:03 -05001893 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001894
Pekka Paalanende685b82012-12-04 15:58:12 +02001895 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001896
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001897 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001898 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001899 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001900
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001901 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001902 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001903
Pekka Paalanen133e4392014-09-23 22:08:46 -04001904 weston_presentation_feedback_discard_list(&surface->feedback_list);
1905
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001906 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001907}
1908
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001909static void
1910destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001911{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001912 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001913
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001914 assert(surface);
1915
Giulio Camuffo0d379742013-11-15 22:06:15 +01001916 /* Set the resource to NULL, since we don't want to leave a
1917 * dangling pointer if the surface was refcounted and survives
1918 * the weston_surface_destroy() call. */
1919 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001920 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001921}
1922
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001923static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001924weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1925{
1926 struct weston_buffer *buffer =
1927 container_of(listener, struct weston_buffer, destroy_listener);
1928
1929 wl_signal_emit(&buffer->destroy_signal, buffer);
1930 free(buffer);
1931}
1932
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001933WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001934weston_buffer_from_resource(struct wl_resource *resource)
1935{
1936 struct weston_buffer *buffer;
1937 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001938
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001939 listener = wl_resource_get_destroy_listener(resource,
1940 weston_buffer_destroy_handler);
1941
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001942 if (listener)
1943 return container_of(listener, struct weston_buffer,
1944 destroy_listener);
1945
1946 buffer = zalloc(sizeof *buffer);
1947 if (buffer == NULL)
1948 return NULL;
1949
1950 buffer->resource = resource;
1951 wl_signal_init(&buffer->destroy_signal);
1952 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001953 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001954 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001955
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001956 return buffer;
1957}
1958
1959static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001960weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1961 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001962{
Pekka Paalanende685b82012-12-04 15:58:12 +02001963 struct weston_buffer_reference *ref =
1964 container_of(listener, struct weston_buffer_reference,
1965 destroy_listener);
1966
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001967 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001968 ref->buffer = NULL;
1969}
1970
1971WL_EXPORT void
1972weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001973 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001974{
1975 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001976 ref->buffer->busy_count--;
1977 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001978 assert(wl_resource_get_client(ref->buffer->resource));
1979 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001980 WL_BUFFER_RELEASE);
1981 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001982 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001983 }
1984
Pekka Paalanende685b82012-12-04 15:58:12 +02001985 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001986 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001987 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001988 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001989 }
1990
Pekka Paalanende685b82012-12-04 15:58:12 +02001991 ref->buffer = buffer;
1992 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1993}
1994
1995static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001996weston_surface_attach(struct weston_surface *surface,
1997 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001998{
1999 weston_buffer_reference(&surface->buffer_ref, buffer);
2000
Pekka Paalanena6421c42012-12-04 15:58:10 +02002001 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002002 if (weston_surface_is_mapped(surface))
2003 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002004 }
2005
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002006 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002007
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002008 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002009 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002010}
2011
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002012WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002013weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002014{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002015 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002016
2017 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002018 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002019}
2020
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002021WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002022weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002023{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002024 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002025
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002026 pixman_region32_union(&compositor->primary_plane.damage,
2027 &compositor->primary_plane.damage,
2028 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002029 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002030}
2031
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002032static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002033surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002034{
Pekka Paalanende685b82012-12-04 15:58:12 +02002035 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002036 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002037 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002038
Pekka Paalanenb5026542014-11-12 15:09:24 +02002039 if (weston_timeline_enabled_ &&
2040 pixman_region32_not_empty(&surface->damage))
2041 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2042 TLP_OUTPUT(surface->output), TLP_END);
2043
Jason Ekstrandef540082014-06-26 10:37:36 -07002044 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002045}
2046
2047static void
2048view_accumulate_damage(struct weston_view *view,
2049 pixman_region32_t *opaque)
2050{
2051 pixman_region32_t damage;
2052
2053 pixman_region32_init(&damage);
2054 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002055 pixman_box32_t *extents;
2056
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002058 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002059 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002060 pixman_region32_copy(&damage, &view->surface->damage);
2061 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002062 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002063 }
2064
Pekka Paalanen380adf52015-02-16 14:39:11 +02002065 pixman_region32_intersect(&damage, &damage,
2066 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002067 pixman_region32_subtract(&damage, &damage, opaque);
2068 pixman_region32_union(&view->plane->damage,
2069 &view->plane->damage, &damage);
2070 pixman_region32_fini(&damage);
2071 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002072 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002073}
2074
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002075static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002076compositor_accumulate_damage(struct weston_compositor *ec)
2077{
2078 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002080 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002081
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002082 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002083
2084 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002085 pixman_region32_copy(&plane->clip, &clip);
2086
2087 pixman_region32_init(&opaque);
2088
Jason Ekstranda7af7042013-10-12 22:38:11 -05002089 wl_list_for_each(ev, &ec->view_list, link) {
2090 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002091 continue;
2092
Jason Ekstranda7af7042013-10-12 22:38:11 -05002093 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002094 }
2095
2096 pixman_region32_union(&clip, &clip, &opaque);
2097 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002098 }
2099
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002100 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002101
Jason Ekstranda7af7042013-10-12 22:38:11 -05002102 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002103 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002104
2105 wl_list_for_each(ev, &ec->view_list, link) {
2106 if (ev->surface->touched)
2107 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002108 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109
2110 surface_flush_damage(ev->surface);
2111
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002112 /* Both the renderer and the backend have seen the buffer
2113 * by now. If renderer needs the buffer, it has its own
2114 * reference set. If the backend wants to keep the buffer
2115 * around for migrating the surface into a non-primary plane
2116 * later, keep_buffer is true. Otherwise, drop the core
2117 * reference now, and allow early buffer release. This enables
2118 * clients to use single-buffering.
2119 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002120 if (!ev->surface->keep_buffer)
2121 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002122 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002123}
2124
2125static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002126surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002127{
2128 struct weston_subsurface *sub;
2129
Pekka Paalanene67858b2013-04-25 13:57:42 +03002130 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002132 continue;
2133
Jason Ekstranda7af7042013-10-12 22:38:11 -05002134 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2135 wl_list_init(&sub->surface->views);
2136
2137 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002138 }
2139}
2140
2141static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002142surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002143{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144 struct weston_subsurface *sub;
2145 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002146
Jason Ekstranda7af7042013-10-12 22:38:11 -05002147 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2148 if (sub->surface == surface)
2149 continue;
2150
George Kiagiadakised04d382014-06-13 18:10:26 +02002151 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2152 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002153 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002154 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002155
2156 surface_free_unused_subsurface_views(sub->surface);
2157 }
2158}
2159
2160static void
2161view_list_add_subsurface_view(struct weston_compositor *compositor,
2162 struct weston_subsurface *sub,
2163 struct weston_view *parent)
2164{
2165 struct weston_subsurface *child;
2166 struct weston_view *view = NULL, *iv;
2167
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002168 if (!weston_surface_is_mapped(sub->surface))
2169 return;
2170
Jason Ekstranda7af7042013-10-12 22:38:11 -05002171 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2172 if (iv->geometry.parent == parent) {
2173 view = iv;
2174 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002175 }
2176 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177
2178 if (view) {
2179 /* Put it back in the surface's list of views */
2180 wl_list_remove(&view->surface_link);
2181 wl_list_insert(&sub->surface->views, &view->surface_link);
2182 } else {
2183 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002184 weston_view_set_position(view,
2185 sub->position.x,
2186 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002187 weston_view_set_transform_parent(view, parent);
2188 }
2189
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002190 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002192
Pekka Paalanenb188e912013-11-19 14:03:35 +02002193 if (wl_list_empty(&sub->surface->subsurface_list)) {
2194 wl_list_insert(compositor->view_list.prev, &view->link);
2195 return;
2196 }
2197
2198 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2199 if (child->surface == sub->surface)
2200 wl_list_insert(compositor->view_list.prev, &view->link);
2201 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002202 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002203 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002204}
2205
2206static void
2207view_list_add(struct weston_compositor *compositor,
2208 struct weston_view *view)
2209{
2210 struct weston_subsurface *sub;
2211
2212 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002213
Pekka Paalanenb188e912013-11-19 14:03:35 +02002214 if (wl_list_empty(&view->surface->subsurface_list)) {
2215 wl_list_insert(compositor->view_list.prev, &view->link);
2216 return;
2217 }
2218
2219 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2220 if (sub->surface == view->surface)
2221 wl_list_insert(compositor->view_list.prev, &view->link);
2222 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002223 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002224 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225}
2226
2227static void
2228weston_compositor_build_view_list(struct weston_compositor *compositor)
2229{
2230 struct weston_view *view;
2231 struct weston_layer *layer;
2232
2233 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002234 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002235 surface_stash_subsurface_views(view->surface);
2236
2237 wl_list_init(&compositor->view_list);
2238 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002239 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002240 view_list_add(compositor, view);
2241 }
2242 }
2243
2244 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002245 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002246 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002247}
2248
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002249static void
2250weston_output_take_feedback_list(struct weston_output *output,
2251 struct weston_surface *surface)
2252{
2253 struct weston_view *view;
2254 struct weston_presentation_feedback *feedback;
2255 uint32_t flags = 0xffffffff;
2256
2257 if (wl_list_empty(&surface->feedback_list))
2258 return;
2259
2260 /* All views must have the flag for the flag to survive. */
2261 wl_list_for_each(view, &surface->views, surface_link) {
2262 /* ignore views that are not on this output at all */
2263 if (view->output_mask & (1u << output->id))
2264 flags &= view->psf_flags;
2265 }
2266
2267 wl_list_for_each(feedback, &surface->feedback_list, link)
2268 feedback->psf_flags = flags;
2269
2270 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2271 wl_list_init(&surface->feedback_list);
2272}
2273
David Herrmann1edf44c2013-10-22 17:11:26 +02002274static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002275weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002276{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002277 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002278 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002279 struct weston_animation *animation, *next;
2280 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002281 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002282 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002283 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002284
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002285 if (output->destroying)
2286 return 0;
2287
Pekka Paalanenb5026542014-11-12 15:09:24 +02002288 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2289
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002290 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002291 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002292
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002293 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002294 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002295 } else {
2296 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002297 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002298 ev->psf_flags = 0;
2299 }
2300 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002301
Pekka Paalanene67858b2013-04-25 13:57:42 +03002302 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002303 wl_list_for_each(ev, &ec->view_list, link) {
2304 /* Note: This operation is safe to do multiple times on the
2305 * same surface.
2306 */
2307 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002308 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002309 &ev->surface->frame_callback_list);
2310 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002311
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002312 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002313 }
2314 }
2315
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002316 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002317
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002318 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002319 pixman_region32_intersect(&output_damage,
2320 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002321 pixman_region32_subtract(&output_damage,
2322 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002323
Scott Moreauccbf29d2012-02-22 14:21:41 -07002324 if (output->dirty)
2325 weston_output_update_matrix(output);
2326
David Herrmann1edf44c2013-10-22 17:11:26 +02002327 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002328
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002329 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002330
Kristian Høgsbergef044142011-06-21 15:02:12 -04002331 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002332
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002333 weston_compositor_repick(ec);
2334 wl_event_loop_dispatch(ec->input_loop, 0);
2335
Jonas Ådahldb773762012-06-13 00:01:21 +02002336 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002337 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002338 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002339 }
2340
Scott Moreaud64cf212012-06-08 19:40:54 -06002341 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002342 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002343 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002344 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002345
Pekka Paalanenb5026542014-11-12 15:09:24 +02002346 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2347
David Herrmann1edf44c2013-10-22 17:11:26 +02002348 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002349}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002350
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002351static int
2352weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002353{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002354 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002355
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002356 wl_event_loop_dispatch(compositor->input_loop, 0);
2357
2358 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002359}
2360
Pekka Paalanen82919792014-05-21 13:51:49 +03002361static void
2362weston_output_schedule_repaint_reset(struct weston_output *output)
2363{
2364 struct weston_compositor *compositor = output->compositor;
2365 struct wl_event_loop *loop;
2366 int fd;
2367
2368 output->repaint_scheduled = 0;
2369 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2370
2371 if (compositor->input_loop_source)
2372 return;
2373
2374 loop = wl_display_get_event_loop(compositor->wl_display);
2375 fd = wl_event_loop_get_fd(compositor->input_loop);
2376 compositor->input_loop_source =
2377 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2378 weston_compositor_read_input, compositor);
2379}
2380
Pekka Paalanen0513a952014-05-21 16:17:27 +03002381static int
2382output_repaint_timer_handler(void *data)
2383{
2384 struct weston_output *output = data;
2385 struct weston_compositor *compositor = output->compositor;
2386
2387 if (output->repaint_needed &&
2388 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2389 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2390 weston_output_repaint(output) == 0)
2391 return 0;
2392
2393 weston_output_schedule_repaint_reset(output);
2394
2395 return 0;
2396}
2397
Kristian Høgsbergef044142011-06-21 15:02:12 -04002398WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002399weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002400 const struct timespec *stamp,
2401 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002402{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002403 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002404 int32_t refresh_nsec;
2405 struct timespec now;
2406 struct timespec gone;
2407 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002408
Pekka Paalanenb5026542014-11-12 15:09:24 +02002409 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2410 TLP_VBLANK(stamp), TLP_END);
2411
Pekka Paalanend7894d02015-07-03 15:08:53 +03002412 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002413 weston_presentation_feedback_present_list(&output->feedback_list,
2414 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002415 output->msc,
2416 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002417
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002418 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002419
Pekka Paalanen0513a952014-05-21 16:17:27 +03002420 weston_compositor_read_presentation_clock(compositor, &now);
2421 timespec_sub(&gone, &now, stamp);
2422 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2423 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002424
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002425 if (msec < -1000 || msec > 1000) {
2426 static bool warned;
2427
2428 if (!warned)
2429 weston_log("Warning: computed repaint delay is "
2430 "insane: %d msec\n", msec);
2431 warned = true;
2432
2433 msec = 0;
2434 }
2435
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002436 /* Called from restart_repaint_loop and restart happens already after
2437 * the deadline given by repaint_msec? In that case we delay until
2438 * the deadline of the next frame, to give clients a more predictable
2439 * timing of the repaint cycle to lock on. */
2440 if (presented_flags == PRESENTATION_FEEDBACK_INVALID && msec < 0)
2441 msec += refresh_nsec / 1000000;
2442
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002443 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002444 output_repaint_timer_handler(output);
2445 else
2446 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002447}
2448
2449static void
2450idle_repaint(void *data)
2451{
2452 struct weston_output *output = data;
2453
Jonas Ådahle5a12252013-04-05 23:07:11 +02002454 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002455}
2456
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002457WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002458weston_layer_entry_insert(struct weston_layer_entry *list,
2459 struct weston_layer_entry *entry)
2460{
2461 wl_list_insert(&list->link, &entry->link);
2462 entry->layer = list->layer;
2463}
2464
2465WL_EXPORT void
2466weston_layer_entry_remove(struct weston_layer_entry *entry)
2467{
2468 wl_list_remove(&entry->link);
2469 wl_list_init(&entry->link);
2470 entry->layer = NULL;
2471}
2472
2473WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002474weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2475{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002476 wl_list_init(&layer->view_list.link);
2477 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002478 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002479 if (below != NULL)
2480 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002481}
2482
2483WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002484weston_layer_set_mask(struct weston_layer *layer,
2485 int x, int y, int width, int height)
2486{
2487 struct weston_view *view;
2488
2489 layer->mask.x1 = x;
2490 layer->mask.x2 = x + width;
2491 layer->mask.y1 = y;
2492 layer->mask.y2 = y + height;
2493
2494 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2495 weston_view_geometry_dirty(view);
2496 }
2497}
2498
2499WL_EXPORT void
2500weston_layer_set_mask_infinite(struct weston_layer *layer)
2501{
2502 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2503 UINT32_MAX, UINT32_MAX);
2504}
2505
2506WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002507weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002508{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002509 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002510 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002511
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002512 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2513 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002514 return;
2515
Pekka Paalanenb5026542014-11-12 15:09:24 +02002516 if (!output->repaint_needed)
2517 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2518
Kristian Høgsbergef044142011-06-21 15:02:12 -04002519 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002520 output->repaint_needed = 1;
2521 if (output->repaint_scheduled)
2522 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002523
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002524 wl_event_loop_add_idle(loop, idle_repaint, output);
2525 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002526 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2527
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002528
2529 if (compositor->input_loop_source) {
2530 wl_event_source_remove(compositor->input_loop_source);
2531 compositor->input_loop_source = NULL;
2532 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002533}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002534
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002535WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002536weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2537{
2538 struct weston_output *output;
2539
2540 wl_list_for_each(output, &compositor->output_list, link)
2541 weston_output_schedule_repaint(output);
2542}
2543
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002544static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002545surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002546{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002547 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002548}
2549
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002550static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002551surface_attach(struct wl_client *client,
2552 struct wl_resource *resource,
2553 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2554{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002555 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002556 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002557
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002558 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002559 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002560 if (buffer == NULL) {
2561 wl_client_post_no_memory(client);
2562 return;
2563 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002564 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002565
Pekka Paalanende685b82012-12-04 15:58:12 +02002566 /* Attach, attach, without commit in between does not send
2567 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002568 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002569
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002570 surface->pending.sx = sx;
2571 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002572 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002573}
2574
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002575static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002576surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002577 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002578 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002579{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002580 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002581
Derek Foreman57e92ed2015-11-17 14:11:35 -06002582 if (width <= 0 || height <= 0)
2583 return;
2584
Pekka Paalanen8e159182012-10-10 12:49:25 +03002585 pixman_region32_union_rect(&surface->pending.damage,
2586 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002587 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002588}
2589
Kristian Høgsberg33418202011-08-16 23:01:28 -04002590static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002591destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002592{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002593 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002594
2595 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002596 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002597}
2598
2599static void
2600surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002601 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002602{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002603 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002604 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002605
2606 cb = malloc(sizeof *cb);
2607 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002608 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002609 return;
2610 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002611
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002612 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2613 callback);
2614 if (cb->resource == NULL) {
2615 free(cb);
2616 wl_resource_post_no_memory(resource);
2617 return;
2618 }
2619
Jason Ekstranda85118c2013-06-27 20:17:02 -05002620 wl_resource_set_implementation(cb->resource, NULL, cb,
2621 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002622
Pekka Paalanenbc106382012-10-10 12:49:31 +03002623 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002624}
2625
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002626static void
2627surface_set_opaque_region(struct wl_client *client,
2628 struct wl_resource *resource,
2629 struct wl_resource *region_resource)
2630{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002631 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002632 struct weston_region *region;
2633
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002634 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002635 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002636 pixman_region32_copy(&surface->pending.opaque,
2637 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002638 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002639 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002640 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002641}
2642
2643static void
2644surface_set_input_region(struct wl_client *client,
2645 struct wl_resource *resource,
2646 struct wl_resource *region_resource)
2647{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002648 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002649 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002650
2651 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002652 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002653 pixman_region32_copy(&surface->pending.input,
2654 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002655 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002656 pixman_region32_fini(&surface->pending.input);
2657 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002658 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002659}
2660
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002661static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002662weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002663{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002664 struct weston_subsurface *sub;
2665
2666 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2667 parent_link_pending) {
2668 wl_list_remove(&sub->parent_link);
2669 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2670 }
2671}
2672
2673static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002674weston_surface_build_buffer_matrix(struct weston_surface *surface,
2675 struct weston_matrix *matrix)
2676{
2677 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2678 double src_width, src_height, dest_width, dest_height;
2679
2680 weston_matrix_init(matrix);
2681
2682 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2683 src_width = surface->width_from_buffer;
2684 src_height = surface->height_from_buffer;
2685 } else {
2686 src_width = wl_fixed_to_double(vp->buffer.src_width);
2687 src_height = wl_fixed_to_double(vp->buffer.src_height);
2688 }
2689
2690 if (vp->surface.width == -1) {
2691 dest_width = src_width;
2692 dest_height = src_height;
2693 } else {
2694 dest_width = vp->surface.width;
2695 dest_height = vp->surface.height;
2696 }
2697
2698 if (src_width != dest_width || src_height != dest_height)
2699 weston_matrix_scale(matrix,
2700 src_width / dest_width,
2701 src_height / dest_height, 1);
2702
2703 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2704 weston_matrix_translate(matrix,
2705 wl_fixed_to_double(vp->buffer.src_x),
2706 wl_fixed_to_double(vp->buffer.src_y),
2707 0);
2708
2709 switch (vp->buffer.transform) {
2710 case WL_OUTPUT_TRANSFORM_FLIPPED:
2711 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2712 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2713 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2714 weston_matrix_scale(matrix, -1, 1, 1);
2715 weston_matrix_translate(matrix,
2716 surface->width_from_buffer, 0, 0);
2717 break;
2718 }
2719
2720 switch (vp->buffer.transform) {
2721 default:
2722 case WL_OUTPUT_TRANSFORM_NORMAL:
2723 case WL_OUTPUT_TRANSFORM_FLIPPED:
2724 break;
2725 case WL_OUTPUT_TRANSFORM_90:
2726 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2727 weston_matrix_rotate_xy(matrix, 0, 1);
2728 weston_matrix_translate(matrix,
2729 surface->height_from_buffer, 0, 0);
2730 break;
2731 case WL_OUTPUT_TRANSFORM_180:
2732 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2733 weston_matrix_rotate_xy(matrix, -1, 0);
2734 weston_matrix_translate(matrix,
2735 surface->width_from_buffer,
2736 surface->height_from_buffer, 0);
2737 break;
2738 case WL_OUTPUT_TRANSFORM_270:
2739 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2740 weston_matrix_rotate_xy(matrix, 0, -1);
2741 weston_matrix_translate(matrix,
2742 0, surface->width_from_buffer, 0);
2743 break;
2744 }
2745
2746 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2747}
2748
2749static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002750weston_surface_commit_state(struct weston_surface *surface,
2751 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002752{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002753 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002754 pixman_region32_t opaque;
2755
Alexander Larsson4ea95522013-05-22 14:41:37 +02002756 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002757 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002758 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002759 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002760
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002761 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002762 if (state->newly_attached)
2763 weston_surface_attach(surface, state->buffer);
2764 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002765
Jason Ekstrand1e059042014-10-16 10:55:19 -05002766 weston_surface_build_buffer_matrix(surface,
2767 &surface->surface_to_buffer_matrix);
2768 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2769 &surface->surface_to_buffer_matrix);
2770
Jason Ekstrand7b982072014-05-20 14:33:03 -05002771 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002772 weston_surface_update_size(surface);
2773 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002774 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002775 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002776
Jason Ekstrand7b982072014-05-20 14:33:03 -05002777 state->sx = 0;
2778 state->sy = 0;
2779 state->newly_attached = 0;
2780 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002781
2782 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002783 if (weston_timeline_enabled_ &&
2784 pixman_region32_not_empty(&state->damage))
2785 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002786 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002787 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002788 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002789 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002790 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002791
2792 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002793 pixman_region32_init(&opaque);
2794 pixman_region32_intersect_rect(&opaque, &state->opaque,
2795 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002796
2797 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2798 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002799 wl_list_for_each(view, &surface->views, surface_link)
2800 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002801 }
2802
2803 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002804
2805 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002806 pixman_region32_intersect_rect(&surface->input, &state->input,
2807 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002808
Pekka Paalanenbc106382012-10-10 12:49:31 +03002809 /* wl_surface.frame */
2810 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002811 &state->frame_callback_list);
2812 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002813
2814 /* XXX:
2815 * What should happen with a feedback request, if there
2816 * is no wl_buffer attached for this commit?
2817 */
2818
2819 /* presentation.feedback */
2820 wl_list_insert_list(&surface->feedback_list,
2821 &state->feedback_list);
2822 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002823}
2824
2825static void
2826weston_surface_commit(struct weston_surface *surface)
2827{
2828 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002829
Pekka Paalanene67858b2013-04-25 13:57:42 +03002830 weston_surface_commit_subsurface_order(surface);
2831
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002832 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002833}
2834
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002835static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002836weston_subsurface_commit(struct weston_subsurface *sub);
2837
2838static void
2839weston_subsurface_parent_commit(struct weston_subsurface *sub,
2840 int parent_is_synchronized);
2841
2842static void
2843surface_commit(struct wl_client *client, struct wl_resource *resource)
2844{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002845 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002846 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2847
2848 if (sub) {
2849 weston_subsurface_commit(sub);
2850 return;
2851 }
2852
2853 weston_surface_commit(surface);
2854
2855 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2856 if (sub->surface != surface)
2857 weston_subsurface_parent_commit(sub, 0);
2858 }
2859}
2860
2861static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002862surface_set_buffer_transform(struct wl_client *client,
2863 struct wl_resource *resource, int transform)
2864{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002865 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002866
Jonny Lamba55f1392014-05-30 12:07:15 +02002867 /* if wl_output.transform grows more members this will need to be updated. */
2868 if (transform < 0 ||
2869 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2870 wl_resource_post_error(resource,
2871 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2872 "buffer transform must be a valid transform "
2873 "('%d' specified)", transform);
2874 return;
2875 }
2876
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002877 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002878 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002879}
2880
Alexander Larsson4ea95522013-05-22 14:41:37 +02002881static void
2882surface_set_buffer_scale(struct wl_client *client,
2883 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002884 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002885{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002886 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002887
Jonny Lamba55f1392014-05-30 12:07:15 +02002888 if (scale < 1) {
2889 wl_resource_post_error(resource,
2890 WL_SURFACE_ERROR_INVALID_SCALE,
2891 "buffer scale must be at least one "
2892 "('%d' specified)", scale);
2893 return;
2894 }
2895
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002896 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002897 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002898}
2899
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002900static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002901 surface_destroy,
2902 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002903 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002904 surface_frame,
2905 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002906 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002907 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002908 surface_set_buffer_transform,
2909 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002910};
2911
2912static void
2913compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002914 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002915{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002916 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002917 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002918
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002919 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002920 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002921 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002922 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002923 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002924
Jason Ekstranda85118c2013-06-27 20:17:02 -05002925 surface->resource =
2926 wl_resource_create(client, &wl_surface_interface,
2927 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002928 if (surface->resource == NULL) {
2929 weston_surface_destroy(surface);
2930 wl_resource_post_no_memory(resource);
2931 return;
2932 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002933 wl_resource_set_implementation(surface->resource, &surface_interface,
2934 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002935
2936 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002937}
2938
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002939static void
2940destroy_region(struct wl_resource *resource)
2941{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002942 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002943
2944 pixman_region32_fini(&region->region);
2945 free(region);
2946}
2947
2948static void
2949region_destroy(struct wl_client *client, struct wl_resource *resource)
2950{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002951 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002952}
2953
2954static void
2955region_add(struct wl_client *client, struct wl_resource *resource,
2956 int32_t x, int32_t y, int32_t width, int32_t height)
2957{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002958 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002959
2960 pixman_region32_union_rect(&region->region, &region->region,
2961 x, y, width, height);
2962}
2963
2964static void
2965region_subtract(struct wl_client *client, struct wl_resource *resource,
2966 int32_t x, int32_t y, int32_t width, int32_t height)
2967{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002968 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002969 pixman_region32_t rect;
2970
2971 pixman_region32_init_rect(&rect, x, y, width, height);
2972 pixman_region32_subtract(&region->region, &region->region, &rect);
2973 pixman_region32_fini(&rect);
2974}
2975
2976static const struct wl_region_interface region_interface = {
2977 region_destroy,
2978 region_add,
2979 region_subtract
2980};
2981
2982static void
2983compositor_create_region(struct wl_client *client,
2984 struct wl_resource *resource, uint32_t id)
2985{
2986 struct weston_region *region;
2987
2988 region = malloc(sizeof *region);
2989 if (region == NULL) {
2990 wl_resource_post_no_memory(resource);
2991 return;
2992 }
2993
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002994 pixman_region32_init(&region->region);
2995
Jason Ekstranda85118c2013-06-27 20:17:02 -05002996 region->resource =
2997 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002998 if (region->resource == NULL) {
2999 free(region);
3000 wl_resource_post_no_memory(resource);
3001 return;
3002 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003003 wl_resource_set_implementation(region->resource, &region_interface,
3004 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003005}
3006
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003007static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003008 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003009 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003010};
3011
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003012static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003013weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3014{
3015 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003016
Jason Ekstrand7b982072014-05-20 14:33:03 -05003017 weston_surface_commit_state(surface, &sub->cached);
3018 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003019
3020 weston_surface_commit_subsurface_order(surface);
3021
3022 weston_surface_schedule_repaint(surface);
3023
Jason Ekstrand7b982072014-05-20 14:33:03 -05003024 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003025}
3026
3027static void
3028weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3029{
3030 struct weston_surface *surface = sub->surface;
3031
3032 /*
3033 * If this commit would cause the surface to move by the
3034 * attach(dx, dy) parameters, the old damage region must be
3035 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003036 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003037 */
3038 pixman_region32_translate(&sub->cached.damage,
3039 -surface->pending.sx, -surface->pending.sy);
3040 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
3041 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07003042 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003043
3044 if (surface->pending.newly_attached) {
3045 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003046 weston_surface_state_set_buffer(&sub->cached,
3047 surface->pending.buffer);
3048 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003049 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003050 weston_presentation_feedback_discard_list(
3051 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003052 }
3053 sub->cached.sx += surface->pending.sx;
3054 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003055
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003056 sub->cached.buffer_viewport.changed |=
3057 surface->pending.buffer_viewport.changed;
3058 sub->cached.buffer_viewport.buffer =
3059 surface->pending.buffer_viewport.buffer;
3060 sub->cached.buffer_viewport.surface =
3061 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003062
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003063 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003064
3065 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3066
3067 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3068
3069 wl_list_insert_list(&sub->cached.frame_callback_list,
3070 &surface->pending.frame_callback_list);
3071 wl_list_init(&surface->pending.frame_callback_list);
3072
Pekka Paalanen133e4392014-09-23 22:08:46 -04003073 wl_list_insert_list(&sub->cached.feedback_list,
3074 &surface->pending.feedback_list);
3075 wl_list_init(&surface->pending.feedback_list);
3076
Jason Ekstrand7b982072014-05-20 14:33:03 -05003077 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003078}
3079
Derek Foreman280e7dd2014-10-03 13:13:42 -05003080static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003081weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3082{
3083 while (sub) {
3084 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003085 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003086
3087 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003088 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003089
3090 sub = weston_surface_to_subsurface(sub->parent);
3091 }
3092
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003093 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003094}
3095
3096static void
3097weston_subsurface_commit(struct weston_subsurface *sub)
3098{
3099 struct weston_surface *surface = sub->surface;
3100 struct weston_subsurface *tmp;
3101
3102 /* Recursive check for effectively synchronized. */
3103 if (weston_subsurface_is_synchronized(sub)) {
3104 weston_subsurface_commit_to_cache(sub);
3105 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003106 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003107 /* flush accumulated state from cache */
3108 weston_subsurface_commit_to_cache(sub);
3109 weston_subsurface_commit_from_cache(sub);
3110 } else {
3111 weston_surface_commit(surface);
3112 }
3113
3114 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3115 if (tmp->surface != surface)
3116 weston_subsurface_parent_commit(tmp, 0);
3117 }
3118 }
3119}
3120
3121static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003122weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003123{
3124 struct weston_surface *surface = sub->surface;
3125 struct weston_subsurface *tmp;
3126
Pekka Paalanene67858b2013-04-25 13:57:42 +03003127 /* From now on, commit_from_cache the whole sub-tree, regardless of
3128 * the synchronized mode of each child. This sub-surface or some
3129 * of its ancestors were synchronized, so we are synchronized
3130 * all the way down.
3131 */
3132
Jason Ekstrand7b982072014-05-20 14:33:03 -05003133 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003134 weston_subsurface_commit_from_cache(sub);
3135
3136 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3137 if (tmp->surface != surface)
3138 weston_subsurface_parent_commit(tmp, 1);
3139 }
3140}
3141
3142static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003143weston_subsurface_parent_commit(struct weston_subsurface *sub,
3144 int parent_is_synchronized)
3145{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003146 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003147 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003148 wl_list_for_each(view, &sub->surface->views, surface_link)
3149 weston_view_set_position(view,
3150 sub->position.x,
3151 sub->position.y);
3152
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003153 sub->position.set = 0;
3154 }
3155
3156 if (parent_is_synchronized || sub->synchronized)
3157 weston_subsurface_synchronized_commit(sub);
3158}
3159
Pekka Paalanen8274d902014-08-06 19:36:51 +03003160static int
3161subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3162{
3163 return snprintf(buf, len, "sub-surface");
3164}
3165
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003166static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003167subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003168{
3169 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003170 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003171
Jason Ekstranda7af7042013-10-12 22:38:11 -05003172 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003173 weston_view_set_position(view,
3174 view->geometry.x + dx,
3175 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003176
3177 /* No need to check parent mappedness, because if parent is not
3178 * mapped, parent is not in a visible layer, so this sub-surface
3179 * will not be drawn either.
3180 */
3181 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003182 struct weston_output *output;
3183
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003184 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003185 * because that would call it also for the parent surface,
3186 * which might not be mapped yet. That would lead to
3187 * inconsistent state, where the window could never be
3188 * mapped.
3189 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003190 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003191 * weston_surface_is_mapped() return true, so that when the
3192 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003193 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003194 */
3195 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003196 output = container_of(compositor->output_list.next,
3197 struct weston_output, link);
3198
3199 surface->output = output;
3200 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003201 }
3202}
3203
3204static struct weston_subsurface *
3205weston_surface_to_subsurface(struct weston_surface *surface)
3206{
3207 if (surface->configure == subsurface_configure)
3208 return surface->configure_private;
3209
3210 return NULL;
3211}
3212
Pekka Paalanen01388e22013-04-25 13:57:44 +03003213WL_EXPORT struct weston_surface *
3214weston_surface_get_main_surface(struct weston_surface *surface)
3215{
3216 struct weston_subsurface *sub;
3217
3218 while (surface && (sub = weston_surface_to_subsurface(surface)))
3219 surface = sub->parent;
3220
3221 return surface;
3222}
3223
Pekka Paalanen50b67472014-10-01 15:02:41 +03003224WL_EXPORT int
3225weston_surface_set_role(struct weston_surface *surface,
3226 const char *role_name,
3227 struct wl_resource *error_resource,
3228 uint32_t error_code)
3229{
3230 assert(role_name);
3231
3232 if (surface->role_name == NULL ||
3233 surface->role_name == role_name ||
3234 strcmp(surface->role_name, role_name) == 0) {
3235 surface->role_name = role_name;
3236
3237 return 0;
3238 }
3239
3240 wl_resource_post_error(error_resource, error_code,
3241 "Cannot assign role %s to wl_surface@%d,"
3242 " already has role %s\n",
3243 role_name,
3244 wl_resource_get_id(surface->resource),
3245 surface->role_name);
3246 return -1;
3247}
3248
Pekka Paalanen8274d902014-08-06 19:36:51 +03003249WL_EXPORT void
3250weston_surface_set_label_func(struct weston_surface *surface,
3251 int (*desc)(struct weston_surface *,
3252 char *, size_t))
3253{
3254 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003255 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003256}
3257
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003258/** Get the size of surface contents
3259 *
3260 * \param surface The surface to query.
3261 * \param width Returns the width of raw contents.
3262 * \param height Returns the height of raw contents.
3263 *
3264 * Retrieves the raw surface content size in pixels for the given surface.
3265 * This is the whole content size in buffer pixels. If the surface
3266 * has no content or the renderer does not implement this feature,
3267 * zeroes are returned.
3268 *
3269 * This function is used to determine the buffer size needed for
3270 * a weston_surface_copy_content() call.
3271 */
3272WL_EXPORT void
3273weston_surface_get_content_size(struct weston_surface *surface,
3274 int *width, int *height)
3275{
3276 struct weston_renderer *rer = surface->compositor->renderer;
3277
3278 if (!rer->surface_get_content_size) {
3279 *width = 0;
3280 *height = 0;
3281 return;
3282 }
3283
3284 rer->surface_get_content_size(surface, width, height);
3285}
3286
3287/** Copy surface contents to system memory.
3288 *
3289 * \param surface The surface to copy from.
3290 * \param target Pointer to the target memory buffer.
3291 * \param size Size of the target buffer in bytes.
3292 * \param src_x X location on contents to copy from.
3293 * \param src_y Y location on contents to copy from.
3294 * \param width Width in pixels of the area to copy.
3295 * \param height Height in pixels of the area to copy.
3296 * \return 0 for success, -1 for failure.
3297 *
3298 * Surface contents are maintained by the renderer. They can be in a
3299 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3300 * else.
3301 *
3302 * Surface contents are copied into memory pointed to by target,
3303 * which has size bytes of space available. The target memory
3304 * may be larger than needed, but being smaller returns an error.
3305 * The extra bytes in target may or may not be written; their content is
3306 * unspecified. Size must be large enough to hold the image.
3307 *
3308 * The image in the target memory will be arranged in rows from
3309 * top to bottom, and pixels on a row from left to right. The pixel
3310 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3311 * width * 4.
3312 *
3313 * Parameters src_x and src_y define the upper-left corner in buffer
3314 * coordinates (pixels) to copy from. Parameters width and height
3315 * define the size of the area to copy in pixels.
3316 *
3317 * The rectangle defined by src_x, src_y, width, height must fit in
3318 * the surface contents. Otherwise an error is returned.
3319 *
3320 * Use surface_get_data_size to determine the content size; the
3321 * needed target buffer size and rectangle limits.
3322 *
3323 * CURRENT IMPLEMENTATION RESTRICTIONS:
3324 * - the machine must be little-endian due to Pixman formats.
3325 *
3326 * NOTE: Pixman formats are premultiplied.
3327 */
3328WL_EXPORT int
3329weston_surface_copy_content(struct weston_surface *surface,
3330 void *target, size_t size,
3331 int src_x, int src_y,
3332 int width, int height)
3333{
3334 struct weston_renderer *rer = surface->compositor->renderer;
3335 int cw, ch;
3336 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3337
3338 if (!rer->surface_copy_content)
3339 return -1;
3340
3341 weston_surface_get_content_size(surface, &cw, &ch);
3342
3343 if (src_x < 0 || src_y < 0)
3344 return -1;
3345
3346 if (width <= 0 || height <= 0)
3347 return -1;
3348
3349 if (src_x + width > cw || src_y + height > ch)
3350 return -1;
3351
3352 if (width * bytespp * height > size)
3353 return -1;
3354
3355 return rer->surface_copy_content(surface, target, size,
3356 src_x, src_y, width, height);
3357}
3358
Pekka Paalanene67858b2013-04-25 13:57:42 +03003359static void
3360subsurface_set_position(struct wl_client *client,
3361 struct wl_resource *resource, int32_t x, int32_t y)
3362{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003363 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003364
3365 if (!sub)
3366 return;
3367
3368 sub->position.x = x;
3369 sub->position.y = y;
3370 sub->position.set = 1;
3371}
3372
3373static struct weston_subsurface *
3374subsurface_from_surface(struct weston_surface *surface)
3375{
3376 struct weston_subsurface *sub;
3377
3378 sub = weston_surface_to_subsurface(surface);
3379 if (sub)
3380 return sub;
3381
3382 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3383 if (sub->surface == surface)
3384 return sub;
3385
3386 return NULL;
3387}
3388
3389static struct weston_subsurface *
3390subsurface_sibling_check(struct weston_subsurface *sub,
3391 struct weston_surface *surface,
3392 const char *request)
3393{
3394 struct weston_subsurface *sibling;
3395
3396 sibling = subsurface_from_surface(surface);
3397
3398 if (!sibling) {
3399 wl_resource_post_error(sub->resource,
3400 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3401 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003402 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003403 return NULL;
3404 }
3405
3406 if (sibling->parent != sub->parent) {
3407 wl_resource_post_error(sub->resource,
3408 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3409 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003410 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003411 return NULL;
3412 }
3413
3414 return sibling;
3415}
3416
3417static void
3418subsurface_place_above(struct wl_client *client,
3419 struct wl_resource *resource,
3420 struct wl_resource *sibling_resource)
3421{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003422 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003423 struct weston_surface *surface =
3424 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003425 struct weston_subsurface *sibling;
3426
3427 if (!sub)
3428 return;
3429
3430 sibling = subsurface_sibling_check(sub, surface, "place_above");
3431 if (!sibling)
3432 return;
3433
3434 wl_list_remove(&sub->parent_link_pending);
3435 wl_list_insert(sibling->parent_link_pending.prev,
3436 &sub->parent_link_pending);
3437}
3438
3439static void
3440subsurface_place_below(struct wl_client *client,
3441 struct wl_resource *resource,
3442 struct wl_resource *sibling_resource)
3443{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003444 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003445 struct weston_surface *surface =
3446 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003447 struct weston_subsurface *sibling;
3448
3449 if (!sub)
3450 return;
3451
3452 sibling = subsurface_sibling_check(sub, surface, "place_below");
3453 if (!sibling)
3454 return;
3455
3456 wl_list_remove(&sub->parent_link_pending);
3457 wl_list_insert(&sibling->parent_link_pending,
3458 &sub->parent_link_pending);
3459}
3460
3461static void
3462subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3463{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003464 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003465
3466 if (sub)
3467 sub->synchronized = 1;
3468}
3469
3470static void
3471subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3472{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003473 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003474
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003475 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003476 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003477
3478 /* If sub became effectively desynchronized, flush. */
3479 if (!weston_subsurface_is_synchronized(sub))
3480 weston_subsurface_synchronized_commit(sub);
3481 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003482}
3483
3484static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003485weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3486{
3487 wl_list_remove(&sub->parent_link);
3488 wl_list_remove(&sub->parent_link_pending);
3489 wl_list_remove(&sub->parent_destroy_listener.link);
3490 sub->parent = NULL;
3491}
3492
3493static void
3494weston_subsurface_destroy(struct weston_subsurface *sub);
3495
3496static void
3497subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3498{
3499 struct weston_subsurface *sub =
3500 container_of(listener, struct weston_subsurface,
3501 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003502 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003503
3504 /* The protocol object (wl_resource) is left inert. */
3505 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003506 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003507
3508 weston_subsurface_destroy(sub);
3509}
3510
3511static void
3512subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3513{
3514 struct weston_subsurface *sub =
3515 container_of(listener, struct weston_subsurface,
3516 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003517 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003518 assert(sub->surface != sub->parent);
3519
3520 if (weston_surface_is_mapped(sub->surface))
3521 weston_surface_unmap(sub->surface);
3522
3523 weston_subsurface_unlink_parent(sub);
3524}
3525
3526static void
3527subsurface_resource_destroy(struct wl_resource *resource)
3528{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003529 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003530
3531 if (sub)
3532 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003533}
3534
3535static void
3536subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3537{
3538 wl_resource_destroy(resource);
3539}
3540
3541static void
3542weston_subsurface_link_parent(struct weston_subsurface *sub,
3543 struct weston_surface *parent)
3544{
3545 sub->parent = parent;
3546 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003547 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003548 &sub->parent_destroy_listener);
3549
3550 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3551 wl_list_insert(&parent->subsurface_list_pending,
3552 &sub->parent_link_pending);
3553}
3554
3555static void
3556weston_subsurface_link_surface(struct weston_subsurface *sub,
3557 struct weston_surface *surface)
3558{
3559 sub->surface = surface;
3560 sub->surface_destroy_listener.notify =
3561 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003562 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003563 &sub->surface_destroy_listener);
3564}
3565
3566static void
3567weston_subsurface_destroy(struct weston_subsurface *sub)
3568{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003569 struct weston_view *view, *next;
3570
Pekka Paalanene67858b2013-04-25 13:57:42 +03003571 assert(sub->surface);
3572
3573 if (sub->resource) {
3574 assert(weston_surface_to_subsurface(sub->surface) == sub);
3575 assert(sub->parent_destroy_listener.notify ==
3576 subsurface_handle_parent_destroy);
3577
George Kiagiadakised04d382014-06-13 18:10:26 +02003578 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3579 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003580 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003581 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003582
Pekka Paalanene67858b2013-04-25 13:57:42 +03003583 if (sub->parent)
3584 weston_subsurface_unlink_parent(sub);
3585
Jason Ekstrand7b982072014-05-20 14:33:03 -05003586 weston_surface_state_fini(&sub->cached);
3587 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003588
3589 sub->surface->configure = NULL;
3590 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003591 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003592 } else {
3593 /* the dummy weston_subsurface for the parent itself */
3594 assert(sub->parent_destroy_listener.notify == NULL);
3595 wl_list_remove(&sub->parent_link);
3596 wl_list_remove(&sub->parent_link_pending);
3597 }
3598
3599 wl_list_remove(&sub->surface_destroy_listener.link);
3600 free(sub);
3601}
3602
3603static const struct wl_subsurface_interface subsurface_implementation = {
3604 subsurface_destroy,
3605 subsurface_set_position,
3606 subsurface_place_above,
3607 subsurface_place_below,
3608 subsurface_set_sync,
3609 subsurface_set_desync
3610};
3611
3612static struct weston_subsurface *
3613weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3614 struct weston_surface *parent)
3615{
3616 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003617 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003618
Bryce Harringtonde16d892014-11-20 22:21:57 -08003619 sub = zalloc(sizeof *sub);
3620 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003621 return NULL;
3622
Jason Ekstranda7af7042013-10-12 22:38:11 -05003623 wl_list_init(&sub->unused_views);
3624
Jason Ekstranda85118c2013-06-27 20:17:02 -05003625 sub->resource =
3626 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003627 if (!sub->resource) {
3628 free(sub);
3629 return NULL;
3630 }
3631
Jason Ekstranda85118c2013-06-27 20:17:02 -05003632 wl_resource_set_implementation(sub->resource,
3633 &subsurface_implementation,
3634 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003635 weston_subsurface_link_surface(sub, surface);
3636 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003637 weston_surface_state_init(&sub->cached);
3638 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003639 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003640
3641 return sub;
3642}
3643
3644/* Create a dummy subsurface for having the parent itself in its
3645 * sub-surface lists. Makes stacking order manipulation easy.
3646 */
3647static struct weston_subsurface *
3648weston_subsurface_create_for_parent(struct weston_surface *parent)
3649{
3650 struct weston_subsurface *sub;
3651
Bryce Harringtonde16d892014-11-20 22:21:57 -08003652 sub = zalloc(sizeof *sub);
3653 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003654 return NULL;
3655
3656 weston_subsurface_link_surface(sub, parent);
3657 sub->parent = parent;
3658 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3659 wl_list_insert(&parent->subsurface_list_pending,
3660 &sub->parent_link_pending);
3661
3662 return sub;
3663}
3664
3665static void
3666subcompositor_get_subsurface(struct wl_client *client,
3667 struct wl_resource *resource,
3668 uint32_t id,
3669 struct wl_resource *surface_resource,
3670 struct wl_resource *parent_resource)
3671{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003672 struct weston_surface *surface =
3673 wl_resource_get_user_data(surface_resource);
3674 struct weston_surface *parent =
3675 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003676 struct weston_subsurface *sub;
3677 static const char where[] = "get_subsurface: wl_subsurface@";
3678
3679 if (surface == parent) {
3680 wl_resource_post_error(resource,
3681 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3682 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003683 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003684 return;
3685 }
3686
3687 if (weston_surface_to_subsurface(surface)) {
3688 wl_resource_post_error(resource,
3689 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3690 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003691 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003692 return;
3693 }
3694
Pekka Paalanen50b67472014-10-01 15:02:41 +03003695 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3696 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003697 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003698
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003699 if (weston_surface_get_main_surface(parent) == surface) {
3700 wl_resource_post_error(resource,
3701 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3702 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003703 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003704 return;
3705 }
3706
Pekka Paalanene67858b2013-04-25 13:57:42 +03003707 /* make sure the parent is in its own list */
3708 if (wl_list_empty(&parent->subsurface_list)) {
3709 if (!weston_subsurface_create_for_parent(parent)) {
3710 wl_resource_post_no_memory(resource);
3711 return;
3712 }
3713 }
3714
3715 sub = weston_subsurface_create(id, surface, parent);
3716 if (!sub) {
3717 wl_resource_post_no_memory(resource);
3718 return;
3719 }
3720
3721 surface->configure = subsurface_configure;
3722 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003723 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003724}
3725
3726static void
3727subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3728{
3729 wl_resource_destroy(resource);
3730}
3731
3732static const struct wl_subcompositor_interface subcompositor_interface = {
3733 subcompositor_destroy,
3734 subcompositor_get_subsurface
3735};
3736
3737static void
3738bind_subcompositor(struct wl_client *client,
3739 void *data, uint32_t version, uint32_t id)
3740{
3741 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003742 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003743
Jason Ekstranda85118c2013-06-27 20:17:02 -05003744 resource =
3745 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003746 if (resource == NULL) {
3747 wl_client_post_no_memory(client);
3748 return;
3749 }
3750 wl_resource_set_implementation(resource, &subcompositor_interface,
3751 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003752}
3753
3754static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003755weston_compositor_dpms(struct weston_compositor *compositor,
3756 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003757{
3758 struct weston_output *output;
3759
3760 wl_list_for_each(output, &compositor->output_list, link)
3761 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003762 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003763}
3764
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003765WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003766weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003767{
Neil Roberts8b62e202013-09-30 13:14:47 +01003768 uint32_t old_state = compositor->state;
3769
3770 /* The state needs to be changed before emitting the wake
3771 * signal because that may try to schedule a repaint which
3772 * will not work if the compositor is still sleeping */
3773 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3774
3775 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003776 case WESTON_COMPOSITOR_SLEEPING:
3777 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3778 /* fall through */
3779 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003780 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003781 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003782 /* fall through */
3783 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003784 wl_event_source_timer_update(compositor->idle_source,
3785 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003786 }
3787}
3788
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003789WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003790weston_compositor_offscreen(struct weston_compositor *compositor)
3791{
3792 switch (compositor->state) {
3793 case WESTON_COMPOSITOR_OFFSCREEN:
3794 return;
3795 case WESTON_COMPOSITOR_SLEEPING:
3796 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3797 /* fall through */
3798 default:
3799 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3800 wl_event_source_timer_update(compositor->idle_source, 0);
3801 }
3802}
3803
3804WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003805weston_compositor_sleep(struct weston_compositor *compositor)
3806{
3807 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3808 return;
3809
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003810 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003811 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3812 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3813}
3814
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003815static int
3816idle_handler(void *data)
3817{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003818 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003819
3820 if (compositor->idle_inhibit)
3821 return 1;
3822
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003823 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003824 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003825
3826 return 1;
3827}
3828
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003829WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003830weston_plane_init(struct weston_plane *plane,
3831 struct weston_compositor *ec,
3832 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003833{
3834 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003835 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003836 plane->x = x;
3837 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003838 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003839
3840 /* Init the link so that the call to wl_list_remove() when releasing
3841 * the plane without ever stacking doesn't lead to a crash */
3842 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003843}
3844
3845WL_EXPORT void
3846weston_plane_release(struct weston_plane *plane)
3847{
Xiong Zhang97116532013-10-23 13:58:31 +08003848 struct weston_view *view;
3849
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003850 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003851 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003852
Xiong Zhang97116532013-10-23 13:58:31 +08003853 wl_list_for_each(view, &plane->compositor->view_list, link) {
3854 if (view->plane == plane)
3855 view->plane = NULL;
3856 }
3857
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003858 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003859}
3860
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003861WL_EXPORT void
3862weston_compositor_stack_plane(struct weston_compositor *ec,
3863 struct weston_plane *plane,
3864 struct weston_plane *above)
3865{
3866 if (above)
3867 wl_list_insert(above->link.prev, &plane->link);
3868 else
3869 wl_list_insert(&ec->plane_list, &plane->link);
3870}
3871
Casey Dahlin9074db52012-04-19 22:50:09 -04003872static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003873{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003874 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003875}
3876
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003877static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003878bind_output(struct wl_client *client,
3879 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003880{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003881 struct weston_output *output = data;
3882 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003883 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003884
Jason Ekstranda85118c2013-06-27 20:17:02 -05003885 resource = wl_resource_create(client, &wl_output_interface,
3886 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003887 if (resource == NULL) {
3888 wl_client_post_no_memory(client);
3889 return;
3890 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003891
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003892 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003893 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003894
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003895 wl_output_send_geometry(resource,
3896 output->x,
3897 output->y,
3898 output->mm_width,
3899 output->mm_height,
3900 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003901 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003902 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003903 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003904 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003905 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003906
3907 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003908 wl_output_send_mode(resource,
3909 mode->flags,
3910 mode->width,
3911 mode->height,
3912 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003913 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003914
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003915 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003916 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003917}
3918
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003919/* Move other outputs when one is removed so the space remains contiguos. */
3920static void
3921weston_compositor_remove_output(struct weston_compositor *compositor,
3922 struct weston_output *remove_output)
3923{
3924 struct weston_output *output;
3925 int offset = 0;
3926
3927 wl_list_for_each(output, &compositor->output_list, link) {
3928 if (output == remove_output) {
3929 offset = output->width;
3930 continue;
3931 }
3932
3933 if (offset > 0) {
3934 weston_output_move(output,
3935 output->x - offset, output->y);
3936 output->dirty = 1;
3937 }
3938 }
3939}
3940
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003941WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003942weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003943{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003944 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003945 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003946
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003947 output->destroying = 1;
3948
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003949 wl_list_for_each(view, &output->compositor->view_list, link) {
3950 if (view->output_mask & (1 << output->id))
3951 weston_view_assign_output(view);
3952 }
3953
Pekka Paalanen0513a952014-05-21 16:17:27 +03003954 wl_event_source_remove(output->repaint_timer);
3955
Pekka Paalanen133e4392014-09-23 22:08:46 -04003956 weston_presentation_feedback_discard_list(&output->feedback_list);
3957
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003958 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003959 wl_list_remove(&output->link);
3960
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003961 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003962 wl_signal_emit(&output->destroy_signal, output);
3963
Richard Hughesafe690c2013-05-02 10:10:04 +01003964 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003965 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003966 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003967 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003968
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003969 wl_resource_for_each(resource, &output->resource_list) {
3970 wl_resource_set_destructor(resource, NULL);
3971 }
3972
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003973 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003974}
3975
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003976WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003977weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003978{
Scott Moreau850ca422012-05-21 15:21:25 -06003979 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003980
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003981 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003982 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003983
Scott Moreauccbf29d2012-02-22 14:21:41 -07003984 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003985 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003986 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003987 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003988 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003989 weston_matrix_scale(&output->matrix, magnification,
3990 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003991 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003992
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003993 switch (output->transform) {
3994 case WL_OUTPUT_TRANSFORM_FLIPPED:
3995 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3996 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3997 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3998 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3999 weston_matrix_scale(&output->matrix, -1, 1, 1);
4000 break;
4001 }
4002
4003 switch (output->transform) {
4004 default:
4005 case WL_OUTPUT_TRANSFORM_NORMAL:
4006 case WL_OUTPUT_TRANSFORM_FLIPPED:
4007 break;
4008 case WL_OUTPUT_TRANSFORM_90:
4009 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4010 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4011 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4012 break;
4013 case WL_OUTPUT_TRANSFORM_180:
4014 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4015 weston_matrix_translate(&output->matrix,
4016 -output->width, -output->height, 0);
4017 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4018 break;
4019 case WL_OUTPUT_TRANSFORM_270:
4020 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4021 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4022 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4023 break;
4024 }
4025
4026 if (output->current_scale != 1)
4027 weston_matrix_scale(&output->matrix,
4028 output->current_scale,
4029 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004030
Scott Moreauccbf29d2012-02-22 14:21:41 -07004031 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004032
4033 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004034}
4035
Scott Moreau1bad5db2012-08-18 01:04:05 -06004036static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004037weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004038{
4039 output->transform = transform;
4040
4041 switch (transform) {
4042 case WL_OUTPUT_TRANSFORM_90:
4043 case WL_OUTPUT_TRANSFORM_270:
4044 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4045 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4046 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02004047 output->width = output->current_mode->height;
4048 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004049 break;
4050 case WL_OUTPUT_TRANSFORM_NORMAL:
4051 case WL_OUTPUT_TRANSFORM_180:
4052 case WL_OUTPUT_TRANSFORM_FLIPPED:
4053 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02004054 output->width = output->current_mode->width;
4055 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004056 break;
4057 default:
4058 break;
4059 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06004060
Hardening57388e42013-09-18 23:56:36 +02004061 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02004062 output->width /= scale;
4063 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004064}
4065
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004066static void
4067weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004068{
4069 output->x = x;
4070 output->y = y;
4071
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004072 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004073 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004074 output->width,
4075 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004076}
4077
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004078WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004079weston_output_move(struct weston_output *output, int x, int y)
4080{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004081 struct wl_resource *resource;
4082
4083 output->move_x = x - output->x;
4084 output->move_y = y - output->y;
4085
4086 if (output->move_x == 0 && output->move_y == 0)
4087 return;
4088
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004089 weston_output_init_geometry(output, x, y);
4090
4091 output->dirty = 1;
4092
4093 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004094 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004095
4096 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004097 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004098 wl_output_send_geometry(resource,
4099 output->x,
4100 output->y,
4101 output->mm_width,
4102 output->mm_height,
4103 output->subpixel,
4104 output->make,
4105 output->model,
4106 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004107
4108 if (wl_resource_get_version(resource) >= 2)
4109 wl_output_send_done(resource);
4110 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004111}
4112
4113WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004114weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004115 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004116 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004117{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004118 struct wl_event_loop *loop;
4119
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004120 output->compositor = c;
4121 output->x = x;
4122 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004123 output->mm_width = mm_width;
4124 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004125 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004126 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004127
Alexander Larsson0b135062013-05-28 16:23:36 +02004128 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004129 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004130
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004131 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004132 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004133
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004134 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004135 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004136 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004137 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004138 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004139 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004140
Pekka Paalanen0513a952014-05-21 16:17:27 +03004141 loop = wl_display_get_event_loop(c->wl_display);
4142 output->repaint_timer = wl_event_loop_add_timer(loop,
4143 output_repaint_timer_handler, output);
4144
Casey Dahlin58ba1372012-04-19 22:50:08 -04004145 output->id = ffs(~output->compositor->output_id_pool) - 1;
4146 output->compositor->output_id_pool |= 1 << output->id;
4147
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004148 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004149 wl_global_create(c->wl_display, &wl_output_interface, 2,
4150 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004151}
4152
4153/** Adds an output to the compositor's output list and
4154 * send the compositor's output_created signal.
4155 *
4156 * \param compositor The compositor instance.
4157 * \param output The output to be added.
4158 */
4159WL_EXPORT void
4160weston_compositor_add_output(struct weston_compositor *compositor,
4161 struct weston_output *output)
4162{
4163 wl_list_insert(compositor->output_list.prev, &output->link);
4164 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004165}
4166
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004167WL_EXPORT void
4168weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004169 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004170 wl_fixed_t *x, wl_fixed_t *y)
4171{
Derek Foreman0f679412014-10-02 13:41:17 -05004172 struct weston_vector p = { {
4173 wl_fixed_to_double(device_x),
4174 wl_fixed_to_double(device_y),
4175 0.0,
4176 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004177
Derek Foreman67a18b92015-03-24 11:36:14 -05004178 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004179
Derek Foreman0f679412014-10-02 13:41:17 -05004180 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4181 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004182}
4183
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004184static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004185destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004186{
Jonny Lamb74130762013-11-26 18:19:46 +01004187 struct weston_surface *surface =
4188 wl_resource_get_user_data(resource);
4189
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004190 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004191 surface->pending.buffer_viewport.buffer.src_width =
4192 wl_fixed_from_int(-1);
4193 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004194 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004195}
4196
4197static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004198viewport_destroy(struct wl_client *client,
4199 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004200{
4201 wl_resource_destroy(resource);
4202}
4203
4204static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004205viewport_set(struct wl_client *client,
4206 struct wl_resource *resource,
4207 wl_fixed_t src_x,
4208 wl_fixed_t src_y,
4209 wl_fixed_t src_width,
4210 wl_fixed_t src_height,
4211 int32_t dst_width,
4212 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004213{
Jonny Lamb74130762013-11-26 18:19:46 +01004214 struct weston_surface *surface =
4215 wl_resource_get_user_data(resource);
4216
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004217 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004218
Jonny Lamb8ae35902013-11-26 18:19:45 +01004219 if (wl_fixed_to_double(src_width) < 0 ||
4220 wl_fixed_to_double(src_height) < 0) {
4221 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004222 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004223 "source dimensions must be non-negative (%fx%f)",
4224 wl_fixed_to_double(src_width),
4225 wl_fixed_to_double(src_height));
4226 return;
4227 }
4228
4229 if (dst_width <= 0 || dst_height <= 0) {
4230 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004231 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004232 "destination dimensions must be positive (%dx%d)",
4233 dst_width, dst_height);
4234 return;
4235 }
4236
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004237 surface->pending.buffer_viewport.buffer.src_x = src_x;
4238 surface->pending.buffer_viewport.buffer.src_y = src_y;
4239 surface->pending.buffer_viewport.buffer.src_width = src_width;
4240 surface->pending.buffer_viewport.buffer.src_height = src_height;
4241 surface->pending.buffer_viewport.surface.width = dst_width;
4242 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004243 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004244}
4245
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004246static void
4247viewport_set_source(struct wl_client *client,
4248 struct wl_resource *resource,
4249 wl_fixed_t src_x,
4250 wl_fixed_t src_y,
4251 wl_fixed_t src_width,
4252 wl_fixed_t src_height)
4253{
4254 struct weston_surface *surface =
4255 wl_resource_get_user_data(resource);
4256
4257 assert(surface->viewport_resource != NULL);
4258
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004259 if (src_width == wl_fixed_from_int(-1) &&
4260 src_height == wl_fixed_from_int(-1)) {
4261 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004262 surface->pending.buffer_viewport.buffer.src_width =
4263 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004264 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004265 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004266 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004267
4268 if (src_width <= 0 || src_height <= 0) {
4269 wl_resource_post_error(resource,
4270 WL_VIEWPORT_ERROR_BAD_VALUE,
4271 "source size must be positive (%fx%f)",
4272 wl_fixed_to_double(src_width),
4273 wl_fixed_to_double(src_height));
4274 return;
4275 }
4276
4277 surface->pending.buffer_viewport.buffer.src_x = src_x;
4278 surface->pending.buffer_viewport.buffer.src_y = src_y;
4279 surface->pending.buffer_viewport.buffer.src_width = src_width;
4280 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004281 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004282}
4283
4284static void
4285viewport_set_destination(struct wl_client *client,
4286 struct wl_resource *resource,
4287 int32_t dst_width,
4288 int32_t dst_height)
4289{
4290 struct weston_surface *surface =
4291 wl_resource_get_user_data(resource);
4292
4293 assert(surface->viewport_resource != NULL);
4294
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004295 if (dst_width == -1 && dst_height == -1) {
4296 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004297 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004298 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004299 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004300 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004301
4302 if (dst_width <= 0 || dst_height <= 0) {
4303 wl_resource_post_error(resource,
4304 WL_VIEWPORT_ERROR_BAD_VALUE,
4305 "destination size must be positive (%dx%d)",
4306 dst_width, dst_height);
4307 return;
4308 }
4309
4310 surface->pending.buffer_viewport.surface.width = dst_width;
4311 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004312 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004313}
4314
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004315static const struct wl_viewport_interface viewport_interface = {
4316 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004317 viewport_set,
4318 viewport_set_source,
4319 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004320};
4321
4322static void
4323scaler_destroy(struct wl_client *client,
4324 struct wl_resource *resource)
4325{
4326 wl_resource_destroy(resource);
4327}
4328
4329static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004330scaler_get_viewport(struct wl_client *client,
4331 struct wl_resource *scaler,
4332 uint32_t id,
4333 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004334{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004335 int version = wl_resource_get_version(scaler);
4336 struct weston_surface *surface =
4337 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004338 struct wl_resource *resource;
4339
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004340 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004341 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004342 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4343 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004344 return;
4345 }
4346
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004347 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004348 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004349 if (resource == NULL) {
4350 wl_client_post_no_memory(client);
4351 return;
4352 }
4353
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004354 wl_resource_set_implementation(resource, &viewport_interface,
4355 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004356
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004357 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004358}
4359
4360static const struct wl_scaler_interface scaler_interface = {
4361 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004362 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004363};
4364
4365static void
4366bind_scaler(struct wl_client *client,
4367 void *data, uint32_t version, uint32_t id)
4368{
4369 struct wl_resource *resource;
4370
4371 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004372 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004373 if (resource == NULL) {
4374 wl_client_post_no_memory(client);
4375 return;
4376 }
4377
4378 wl_resource_set_implementation(resource, &scaler_interface,
4379 NULL, NULL);
4380}
4381
4382static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004383destroy_presentation_feedback(struct wl_resource *feedback_resource)
4384{
4385 struct weston_presentation_feedback *feedback;
4386
4387 feedback = wl_resource_get_user_data(feedback_resource);
4388
4389 wl_list_remove(&feedback->link);
4390 free(feedback);
4391}
4392
4393static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004394presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4395{
4396 wl_resource_destroy(resource);
4397}
4398
4399static void
4400presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004401 struct wl_resource *presentation_resource,
4402 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004403 uint32_t callback)
4404{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004405 struct weston_surface *surface;
4406 struct weston_presentation_feedback *feedback;
4407
4408 surface = wl_resource_get_user_data(surface_resource);
4409
Bryce Harringtonde16d892014-11-20 22:21:57 -08004410 feedback = zalloc(sizeof *feedback);
4411 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004412 goto err_calloc;
4413
4414 feedback->resource = wl_resource_create(client,
4415 &presentation_feedback_interface,
4416 1, callback);
4417 if (!feedback->resource)
4418 goto err_create;
4419
4420 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4421 destroy_presentation_feedback);
4422
4423 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4424
4425 return;
4426
4427err_create:
4428 free(feedback);
4429
4430err_calloc:
4431 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004432}
4433
4434static const struct presentation_interface presentation_implementation = {
4435 presentation_destroy,
4436 presentation_feedback
4437};
4438
4439static void
4440bind_presentation(struct wl_client *client,
4441 void *data, uint32_t version, uint32_t id)
4442{
4443 struct weston_compositor *compositor = data;
4444 struct wl_resource *resource;
4445
4446 resource = wl_resource_create(client, &presentation_interface,
4447 MIN(version, 1), id);
4448 if (resource == NULL) {
4449 wl_client_post_no_memory(client);
4450 return;
4451 }
4452
4453 wl_resource_set_implementation(resource, &presentation_implementation,
4454 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004455 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004456}
4457
4458static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004459compositor_bind(struct wl_client *client,
4460 void *data, uint32_t version, uint32_t id)
4461{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004462 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004463 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004464
Jason Ekstranda85118c2013-06-27 20:17:02 -05004465 resource = wl_resource_create(client, &wl_compositor_interface,
4466 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004467 if (resource == NULL) {
4468 wl_client_post_no_memory(client);
4469 return;
4470 }
4471
4472 wl_resource_set_implementation(resource, &compositor_interface,
4473 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004474}
4475
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004476WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004477weston_environment_get_fd(const char *env)
4478{
4479 char *e, *end;
4480 int fd, flags;
4481
4482 e = getenv(env);
4483 if (!e)
4484 return -1;
4485 fd = strtol(e, &end, 0);
4486 if (*end != '\0')
4487 return -1;
4488
4489 flags = fcntl(fd, F_GETFD);
4490 if (flags == -1)
4491 return -1;
4492
4493 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4494 unsetenv(env);
4495
4496 return fd;
4497}
4498
Pekka Paalanenb5026542014-11-12 15:09:24 +02004499static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004500timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004501 uint32_t key, void *data)
4502{
4503 struct weston_compositor *compositor = data;
4504
4505 if (weston_timeline_enabled_)
4506 weston_timeline_close();
4507 else
4508 weston_timeline_open(compositor);
4509}
4510
Giulio Camuffo459137b2014-10-11 23:56:24 +03004511/** Create the compositor.
4512 *
4513 * This functions creates and initializes a compositor instance.
4514 *
4515 * \param display The Wayland display to be used.
4516 * \param user_data A pointer to an object that can later be retrieved
4517 * using the \ref weston_compositor_get_user_data function.
4518 * \return The compositor instance on success or NULL on failure.
4519 */
4520WL_EXPORT struct weston_compositor *
4521weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004522{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004523 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004524 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004525
Giulio Camuffo459137b2014-10-11 23:56:24 +03004526 ec = zalloc(sizeof *ec);
4527 if (!ec)
4528 return NULL;
4529
4530 ec->wl_display = display;
4531 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004532 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004533 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004534 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004535 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004536 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004537 wl_signal_init(&ec->idle_signal);
4538 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004539 wl_signal_init(&ec->show_input_panel_signal);
4540 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004541 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004542 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004543 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004544 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004545 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004546 wl_signal_init(&ec->session_signal);
4547 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004548
Casey Dahlin58ba1372012-04-19 22:50:08 -04004549 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004550 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004551
Giulio Camuffo954f1832014-10-11 18:27:30 +03004552 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004553 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004554 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004555
Giulio Camuffo954f1832014-10-11 18:27:30 +03004556 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004557 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004558 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004559
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004560 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004561 ec, bind_scaler))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004562 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004563
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004564 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4565 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004566 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004567
Jason Ekstranda7af7042013-10-12 22:38:11 -05004568 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004569 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004570 wl_list_init(&ec->layer_list);
4571 wl_list_init(&ec->seat_list);
4572 wl_list_init(&ec->output_list);
4573 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004574 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004575 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004576 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004577 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004578 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004579
Xiong Zhang97116532013-10-23 13:58:31 +08004580 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004581 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004582
Giulio Camuffo459137b2014-10-11 23:56:24 +03004583 wl_data_device_manager_init(ec->wl_display);
4584
4585 wl_display_init_shm(ec->wl_display);
4586
4587 loop = wl_display_get_event_loop(ec->wl_display);
4588 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004589
4590 ec->input_loop = wl_event_loop_create();
4591
4592 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4593 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4594
4595 weston_compositor_add_debug_binding(ec, KEY_T,
4596 timeline_key_binding_handler, ec);
4597
Giulio Camuffo459137b2014-10-11 23:56:24 +03004598 return ec;
4599
4600fail:
4601 free(ec);
4602 return NULL;
4603}
4604
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004605WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004606weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004607{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004608 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004609
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004610 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004611 if (ec->input_loop_source)
4612 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004613
Matt Roper361d2ad2011-08-29 13:52:23 -07004614 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004615 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004616 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004617
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004618 if (ec->renderer)
4619 ec->renderer->destroy(ec);
4620
Daniel Stone325fc2d2012-05-30 16:31:58 +01004621 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004622 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004623 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004624 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004625 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004626 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004627
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004628 weston_plane_release(&ec->primary_plane);
4629
Jonas Ådahlc97af922012-03-28 22:36:09 +02004630 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07004631}
4632
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004633WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004634weston_compositor_exit_with_code(struct weston_compositor *compositor,
4635 int exit_code)
4636{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004637 if (compositor->exit_code == EXIT_SUCCESS)
4638 compositor->exit_code = exit_code;
4639
Giulio Camuffo459137b2014-10-11 23:56:24 +03004640 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004641}
4642
4643WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004644weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4645 const struct weston_pointer_grab_interface *interface)
4646{
4647 struct weston_seat *seat;
4648
4649 ec->default_pointer_grab = interface;
4650 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004651 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4652
4653 if (pointer)
4654 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004655 }
4656}
4657
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004658WL_EXPORT int
4659weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4660 clockid_t clk_id)
4661{
4662 struct timespec ts;
4663
4664 if (clock_gettime(clk_id, &ts) < 0)
4665 return -1;
4666
4667 compositor->presentation_clock = clk_id;
4668
4669 return 0;
4670}
4671
4672/*
4673 * For choosing the software clock, when the display hardware or API
4674 * does not expose a compatible presentation timestamp.
4675 */
4676WL_EXPORT int
4677weston_compositor_set_presentation_clock_software(
4678 struct weston_compositor *compositor)
4679{
4680 /* In order of preference */
4681 static const clockid_t clocks[] = {
4682 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4683 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4684 CLOCK_MONOTONIC, /* no jumps, may crawl */
4685 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4686 CLOCK_REALTIME /* may jump and crawl */
4687 };
4688 unsigned i;
4689
4690 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4691 if (weston_compositor_set_presentation_clock(compositor,
4692 clocks[i]) == 0)
4693 return 0;
4694
4695 weston_log("Error: no suitable presentation clock available.\n");
4696
4697 return -1;
4698}
4699
Pekka Paalanen662f3842015-03-18 12:17:26 +02004700/** Read the current time from the Presentation clock
4701 *
4702 * \param compositor
4703 * \param ts[out] The current time.
4704 *
4705 * \note Reading the current time in user space is always imprecise to some
4706 * degree.
4707 *
4708 * This function is never meant to fail. If reading the clock does fail,
4709 * an error message is logged and a zero time is returned. Callers are not
4710 * supposed to detect or react to failures.
4711 */
4712WL_EXPORT void
4713weston_compositor_read_presentation_clock(
4714 const struct weston_compositor *compositor,
4715 struct timespec *ts)
4716{
4717 static bool warned;
4718 int ret;
4719
4720 ret = clock_gettime(compositor->presentation_clock, ts);
4721 if (ret < 0) {
4722 ts->tv_sec = 0;
4723 ts->tv_nsec = 0;
4724
4725 if (!warned)
4726 weston_log("Error: failure to read "
4727 "the presentation clock %#x: '%m' (%d)\n",
4728 compositor->presentation_clock, errno);
4729 warned = true;
4730 }
4731}
4732
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004733/** Import dmabuf buffer into current renderer
4734 *
4735 * \param compositor
4736 * \param buffer the dmabuf buffer to import
4737 * \return true on usable buffers, false otherwise
4738 *
4739 * This function tests that the linux_dmabuf_buffer is usable
4740 * for the current renderer. Returns false on unusable buffers. Usually
4741 * usability is tested by importing the dmabufs for composition.
4742 *
4743 * This hook is also used for detecting if the renderer supports
4744 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4745 * supported.
4746 * */
4747WL_EXPORT bool
4748weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4749 struct linux_dmabuf_buffer *buffer)
4750{
4751 struct weston_renderer *renderer;
4752
4753 renderer = compositor->renderer;
4754
4755 if (renderer->import_dmabuf == NULL)
4756 return false;
4757
4758 return renderer->import_dmabuf(compositor, buffer);
4759}
4760
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004761WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004762weston_version(int *major, int *minor, int *micro)
4763{
4764 *major = WESTON_VERSION_MAJOR;
4765 *minor = WESTON_VERSION_MINOR;
4766 *micro = WESTON_VERSION_MICRO;
4767}
4768
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004769WL_EXPORT void *
4770weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004771{
Derek Foreman3f86e502015-06-08 11:46:54 -05004772 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004773 char path[PATH_MAX];
4774 void *module, *init;
4775
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004776 if (name == NULL)
4777 return NULL;
4778
Derek Foreman3f86e502015-06-08 11:46:54 -05004779 if (name[0] != '/') {
4780 if (builddir)
4781 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4782 else
4783 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
4784 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004785 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004786 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004787
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004788 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4789 if (module) {
4790 weston_log("Module '%s' already loaded\n", path);
4791 dlclose(module);
4792 return NULL;
4793 }
4794
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004795 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004796 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004797 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004798 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004799 return NULL;
4800 }
4801
4802 init = dlsym(module, entrypoint);
4803 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004804 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004805 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004806 return NULL;
4807 }
4808
4809 return init;
4810}
4811
Giulio Camuffo459137b2014-10-11 23:56:24 +03004812
4813/** Destroys the compositor.
4814 *
4815 * This function cleans up the compositor state and destroys it.
4816 *
4817 * \param compositor The compositor to be destroyed.
4818 */
4819WL_EXPORT void
4820weston_compositor_destroy(struct weston_compositor *compositor)
4821{
4822 /* prevent further rendering while shutting down */
4823 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4824
4825 wl_signal_emit(&compositor->destroy_signal, compositor);
4826
4827 weston_compositor_xkb_destroy(compositor);
4828
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004829 if (compositor->backend)
4830 compositor->backend->destroy(compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004831 free(compositor);
4832}
4833
4834/** Instruct the compositor to exit.
4835 *
4836 * This functions does not directly destroy the compositor object, it merely
4837 * command it to start the tear down process. It is not guaranteed that the
4838 * tear down will happen immediately.
4839 *
4840 * \param compositor The compositor to tear down.
4841 */
4842WL_EXPORT void
4843weston_compositor_exit(struct weston_compositor *compositor)
4844{
4845 compositor->exit(compositor);
4846}
4847
4848/** Return the user data stored in the compositor.
4849 *
4850 * This function returns the user data pointer set with user_data parameter
4851 * to the \ref weston_compositor_create function.
4852 */
4853WL_EXPORT void *
4854weston_compositor_get_user_data(struct weston_compositor *compositor)
4855{
4856 return compositor->user_data;
4857}