blob: bbac110e5bb64cf519e78e8724466903592a6598 [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
771
772 if (vec1.f[1] < vec2.f[1]) {
773 dest_rects[i].y1 = floor(vec1.f[1]);
774 dest_rects[i].y2 = ceil(vec2.f[1]);
775 } else {
776 dest_rects[i].y1 = floor(vec2.f[1]);
777 dest_rects[i].y2 = ceil(vec1.f[1]);
778 }
779 }
780
781 pixman_region32_clear(dest);
782 pixman_region32_init_rects(dest, dest_rects, nrects);
783 free(dest_rects);
784}
785
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200786WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500787weston_transformed_region(int width, int height,
788 enum wl_output_transform transform,
789 int32_t scale,
790 pixman_region32_t *src, pixman_region32_t *dest)
791{
792 pixman_box32_t *src_rects, *dest_rects;
793 int nrects, i;
794
795 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
796 if (src != dest)
797 pixman_region32_copy(dest, src);
798 return;
799 }
800
801 src_rects = pixman_region32_rectangles(src, &nrects);
802 dest_rects = malloc(nrects * sizeof(*dest_rects));
803 if (!dest_rects)
804 return;
805
806 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
807 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
808 } else {
809 for (i = 0; i < nrects; i++) {
810 switch (transform) {
811 default:
812 case WL_OUTPUT_TRANSFORM_NORMAL:
813 dest_rects[i].x1 = src_rects[i].x1;
814 dest_rects[i].y1 = src_rects[i].y1;
815 dest_rects[i].x2 = src_rects[i].x2;
816 dest_rects[i].y2 = src_rects[i].y2;
817 break;
818 case WL_OUTPUT_TRANSFORM_90:
819 dest_rects[i].x1 = height - src_rects[i].y2;
820 dest_rects[i].y1 = src_rects[i].x1;
821 dest_rects[i].x2 = height - src_rects[i].y1;
822 dest_rects[i].y2 = src_rects[i].x2;
823 break;
824 case WL_OUTPUT_TRANSFORM_180:
825 dest_rects[i].x1 = width - src_rects[i].x2;
826 dest_rects[i].y1 = height - src_rects[i].y2;
827 dest_rects[i].x2 = width - src_rects[i].x1;
828 dest_rects[i].y2 = height - src_rects[i].y1;
829 break;
830 case WL_OUTPUT_TRANSFORM_270:
831 dest_rects[i].x1 = src_rects[i].y1;
832 dest_rects[i].y1 = width - src_rects[i].x2;
833 dest_rects[i].x2 = src_rects[i].y2;
834 dest_rects[i].y2 = width - src_rects[i].x1;
835 break;
836 case WL_OUTPUT_TRANSFORM_FLIPPED:
837 dest_rects[i].x1 = width - src_rects[i].x2;
838 dest_rects[i].y1 = src_rects[i].y1;
839 dest_rects[i].x2 = width - src_rects[i].x1;
840 dest_rects[i].y2 = src_rects[i].y2;
841 break;
842 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
843 dest_rects[i].x1 = height - src_rects[i].y2;
844 dest_rects[i].y1 = width - src_rects[i].x2;
845 dest_rects[i].x2 = height - src_rects[i].y1;
846 dest_rects[i].y2 = width - src_rects[i].x1;
847 break;
848 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
849 dest_rects[i].x1 = src_rects[i].x1;
850 dest_rects[i].y1 = height - src_rects[i].y2;
851 dest_rects[i].x2 = src_rects[i].x2;
852 dest_rects[i].y2 = height - src_rects[i].y1;
853 break;
854 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
855 dest_rects[i].x1 = src_rects[i].y1;
856 dest_rects[i].y1 = src_rects[i].x1;
857 dest_rects[i].x2 = src_rects[i].y2;
858 dest_rects[i].y2 = src_rects[i].x2;
859 break;
860 }
861 }
862 }
863
864 if (scale != 1) {
865 for (i = 0; i < nrects; i++) {
866 dest_rects[i].x1 *= scale;
867 dest_rects[i].x2 *= scale;
868 dest_rects[i].y1 *= scale;
869 dest_rects[i].y2 *= scale;
870 }
871 }
872
873 pixman_region32_clear(dest);
874 pixman_region32_init_rects(dest, dest_rects, nrects);
875 free(dest_rects);
876}
877
Jonny Lamb74130762013-11-26 18:19:46 +0100878static void
879scaler_surface_to_buffer(struct weston_surface *surface,
880 float sx, float sy, float *bx, float *by)
881{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200882 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200883 double src_width, src_height;
884 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200885
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200886 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
887 if (vp->surface.width == -1) {
888 *bx = sx;
889 *by = sy;
890 return;
891 }
Jonny Lamb74130762013-11-26 18:19:46 +0100892
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200893 src_x = 0.0;
894 src_y = 0.0;
895 src_width = surface->width_from_buffer;
896 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100897 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200898 src_x = wl_fixed_to_double(vp->buffer.src_x);
899 src_y = wl_fixed_to_double(vp->buffer.src_y);
900 src_width = wl_fixed_to_double(vp->buffer.src_width);
901 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100902 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200903
904 *bx = sx * src_width / surface->width + src_x;
905 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100906}
907
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500908WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200909weston_surface_to_buffer_float(struct weston_surface *surface,
910 float sx, float sy, float *bx, float *by)
911{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200912 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
913
Jonny Lamb74130762013-11-26 18:19:46 +0100914 /* first transform coordinates if the scaler is set */
915 scaler_surface_to_buffer(surface, sx, sy, bx, by);
916
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500917 weston_transformed_coord(surface->width_from_buffer,
918 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200919 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100920 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200921}
922
Alexander Larsson4ea95522013-05-22 14:41:37 +0200923WL_EXPORT void
924weston_surface_to_buffer(struct weston_surface *surface,
925 int sx, int sy, int *bx, int *by)
926{
927 float bxf, byf;
928
Jonny Lamb74130762013-11-26 18:19:46 +0100929 weston_surface_to_buffer_float(surface,
930 sx, sy, &bxf, &byf);
931
Alexander Larsson4ea95522013-05-22 14:41:37 +0200932 *bx = floorf(bxf);
933 *by = floorf(byf);
934}
935
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200936WL_EXPORT pixman_box32_t
937weston_surface_to_buffer_rect(struct weston_surface *surface,
938 pixman_box32_t rect)
939{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200940 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100941 float xf, yf;
942
943 /* first transform box coordinates if the scaler is set */
944 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
945 rect.x1 = floorf(xf);
946 rect.y1 = floorf(yf);
947
948 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
949 rect.x2 = floorf(xf);
950 rect.y2 = floorf(yf);
951
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500952 return weston_transformed_rect(surface->width_from_buffer,
953 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200954 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200955 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200956}
957
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200958/** Transform a region from surface coordinates to buffer coordinates
959 *
960 * \param surface The surface to fetch wl_viewport and buffer transformation
961 * from.
962 * \param surface_region[in] The region in surface coordinates.
963 * \param buffer_region[out] The region converted to buffer coordinates.
964 *
965 * Buffer_region must be init'd, but will be completely overwritten.
966 *
967 * Viewport and buffer transformations can only do translation, scaling,
968 * and rotations in 90-degree steps. Therefore the only loss in the
969 * conversion is coordinate flooring (rounding).
970 */
971WL_EXPORT void
972weston_surface_to_buffer_region(struct weston_surface *surface,
973 pixman_region32_t *surface_region,
974 pixman_region32_t *buffer_region)
975{
976 pixman_box32_t *src_rects, *dest_rects;
977 int nrects, i;
978
979 src_rects = pixman_region32_rectangles(surface_region, &nrects);
980 dest_rects = malloc(nrects * sizeof(*dest_rects));
981 if (!dest_rects)
982 return;
983
984 for (i = 0; i < nrects; i++) {
985 dest_rects[i] = weston_surface_to_buffer_rect(surface,
986 src_rects[i]);
987 }
988
989 pixman_region32_fini(buffer_region);
990 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
991 free(dest_rects);
992}
993
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200994WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400996 struct weston_plane *plane)
997{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400999 return;
1000
Jason Ekstranda7af7042013-10-12 22:38:11 -05001001 weston_view_damage_below(view);
1002 view->plane = plane;
1003 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001004}
1005
Pekka Paalanen51723d52015-02-17 13:10:01 +02001006/** Inflict damage on the plane where the view is visible.
1007 *
1008 * \param view The view that causes the damage.
1009 *
1010 * If the view is currently on a plane (including the primary plane),
1011 * take the view's boundingbox, subtract all the opaque views that cover it,
1012 * and add the remaining region as damage to the plane. This corresponds
1013 * to the damage inflicted to the plane if this view disappeared.
1014 *
1015 * A repaint is scheduled for this view.
1016 *
1017 * The region of all opaque views covering this view is stored in
1018 * weston_view::clip and updated by view_accumulate_damage() during
1019 * weston_output_repaint(). Specifically, that region matches the
1020 * scenegraph as it was last painted.
1021 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001022WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001024{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001025 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001026
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001027 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001028 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001029 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001030 if (view->plane)
1031 pixman_region32_union(&view->plane->damage,
1032 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001033 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001034 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001035}
1036
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001037static void
1038weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1039{
1040 uint32_t different = es->output_mask ^ mask;
1041 uint32_t entered = mask & different;
1042 uint32_t left = es->output_mask & different;
1043 struct weston_output *output;
1044 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001045 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001046
1047 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001048 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001049 return;
1050 if (different == 0)
1051 return;
1052
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001053 client = wl_resource_get_client(es->resource);
1054
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001055 wl_list_for_each(output, &es->compositor->output_list, link) {
1056 if (1 << output->id & different)
1057 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001058 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001059 client);
1060 if (resource == NULL)
1061 continue;
1062 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001063 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001064 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001065 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001066 }
1067}
1068
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001069
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001070static void
1071weston_surface_assign_output(struct weston_surface *es)
1072{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073 struct weston_output *new_output;
1074 struct weston_view *view;
1075 pixman_region32_t region;
1076 uint32_t max, area, mask;
1077 pixman_box32_t *e;
1078
1079 new_output = NULL;
1080 max = 0;
1081 mask = 0;
1082 pixman_region32_init(&region);
1083 wl_list_for_each(view, &es->views, surface_link) {
1084 if (!view->output)
1085 continue;
1086
1087 pixman_region32_intersect(&region, &view->transform.boundingbox,
1088 &view->output->region);
1089
1090 e = pixman_region32_extents(&region);
1091 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1092
1093 mask |= view->output_mask;
1094
1095 if (area >= max) {
1096 new_output = view->output;
1097 max = area;
1098 }
1099 }
1100 pixman_region32_fini(&region);
1101
1102 es->output = new_output;
1103 weston_surface_update_output_mask(es, mask);
1104}
1105
1106static void
1107weston_view_assign_output(struct weston_view *ev)
1108{
1109 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001110 struct weston_output *output, *new_output;
1111 pixman_region32_t region;
1112 uint32_t max, area, mask;
1113 pixman_box32_t *e;
1114
1115 new_output = NULL;
1116 max = 0;
1117 mask = 0;
1118 pixman_region32_init(&region);
1119 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001120 if (output->destroying)
1121 continue;
1122
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001124 &output->region);
1125
1126 e = pixman_region32_extents(&region);
1127 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1128
1129 if (area > 0)
1130 mask |= 1 << output->id;
1131
1132 if (area >= max) {
1133 new_output = output;
1134 max = area;
1135 }
1136 }
1137 pixman_region32_fini(&region);
1138
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 ev->output = new_output;
1140 ev->output_mask = mask;
1141
1142 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001143}
1144
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001145static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001146weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1147 int from_x, int from_y, int *to_x, int *to_y)
1148{
1149 float x, y;
1150
1151 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1152 weston_view_from_global_float(to, x, y, &x, &y);
1153
1154 *to_x = round(x);
1155 *to_y = round(y);
1156}
1157
1158static void
1159weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1160{
1161 pixman_box32_t *a;
1162 pixman_box32_t b;
1163
1164 a = pixman_region32_extents(&from->geometry.scissor);
1165
1166 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1167 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1168
1169 pixman_region32_fini(&to->geometry.scissor);
1170 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1171}
1172
1173static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001174view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001176{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001177 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1178 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001179 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001180 { inbox->x1, inbox->y1 },
1181 { inbox->x1, inbox->y2 },
1182 { inbox->x2, inbox->y1 },
1183 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001184 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001185 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001186 int i;
1187
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001188 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001189 /* avoid rounding empty bbox to 1x1 */
1190 pixman_region32_init(bbox);
1191 return;
1192 }
1193
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001194 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001195 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001197 if (x < min_x)
1198 min_x = x;
1199 if (x > max_x)
1200 max_x = x;
1201 if (y < min_y)
1202 min_y = y;
1203 if (y > max_y)
1204 max_y = y;
1205 }
1206
Pekka Paalanen219b9822012-02-08 15:38:37 +02001207 int_x = floorf(min_x);
1208 int_y = floorf(min_y);
1209 pixman_region32_init_rect(bbox, int_x, int_y,
1210 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001211}
1212
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001213static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001215{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001217
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001218 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 view->geometry.x = roundf(view->geometry.x);
1220 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001221
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001222 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1224 view->transform.position.matrix.d[12] = view->geometry.x;
1225 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001226
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001228
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 view->transform.inverse = view->transform.position.matrix;
1230 view->transform.inverse.d[12] = -view->geometry.x;
1231 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001232
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001234 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001235 view->surface->width,
1236 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001237 if (view->geometry.scissor_enabled)
1238 pixman_region32_intersect(&view->transform.boundingbox,
1239 &view->transform.boundingbox,
1240 &view->geometry.scissor);
1241
1242 pixman_region32_translate(&view->transform.boundingbox,
1243 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001244
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 if (view->alpha == 1.0) {
1246 pixman_region32_copy(&view->transform.opaque,
1247 &view->surface->opaque);
1248 pixman_region32_translate(&view->transform.opaque,
1249 view->geometry.x,
1250 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001251 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001252}
1253
1254static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001256{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257 struct weston_view *parent = view->geometry.parent;
1258 struct weston_matrix *matrix = &view->transform.matrix;
1259 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001260 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001261 pixman_region32_t surfregion;
1262 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001263
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001265
1266 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1268 view->transform.position.matrix.d[12] = view->geometry.x;
1269 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001270
1271 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001273 weston_matrix_multiply(matrix, &tform->matrix);
1274
Pekka Paalanen483243f2013-03-08 14:56:50 +02001275 if (parent)
1276 weston_matrix_multiply(matrix, &parent->transform.matrix);
1277
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001278 if (weston_matrix_invert(inverse, matrix) < 0) {
1279 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 weston_log("error: weston_view %p"
1281 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001282 return -1;
1283 }
1284
Pekka Paalanen380adf52015-02-16 14:39:11 +02001285 pixman_region32_init_rect(&surfregion, 0, 0,
1286 view->surface->width, view->surface->height);
1287 if (view->geometry.scissor_enabled)
1288 pixman_region32_intersect(&surfregion, &surfregion,
1289 &view->geometry.scissor);
1290 surfbox = pixman_region32_extents(&surfregion);
1291
1292 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1293 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001294
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001295 return 0;
1296}
1297
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001298static struct weston_layer *
1299get_view_layer(struct weston_view *view)
1300{
1301 if (view->parent_view)
1302 return get_view_layer(view->parent_view);
1303 return view->layer_link.layer;
1304}
1305
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001306WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001308{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001310 struct weston_layer *layer;
1311 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001312
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001314 return;
1315
Pekka Paalanen483243f2013-03-08 14:56:50 +02001316 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001318
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001320
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001322
Jason Ekstranda7af7042013-10-12 22:38:11 -05001323 pixman_region32_fini(&view->transform.boundingbox);
1324 pixman_region32_fini(&view->transform.opaque);
1325 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001326
Pekka Paalanencd403622012-01-25 13:37:39 +02001327 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 if (view->geometry.transformation_list.next ==
1329 &view->transform.position.link &&
1330 view->geometry.transformation_list.prev ==
1331 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001332 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001334 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 if (weston_view_update_transform_enable(view) < 0)
1336 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001337 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001338
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001339 layer = get_view_layer(view);
1340 if (layer) {
1341 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001342 pixman_region32_intersect(&view->transform.boundingbox,
1343 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001344 pixman_region32_intersect(&view->transform.opaque,
1345 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001346 pixman_region32_fini(&mask);
1347 }
1348
Pekka Paalanen380adf52015-02-16 14:39:11 +02001349 if (parent) {
1350 if (parent->geometry.scissor_enabled) {
1351 view->geometry.scissor_enabled = true;
1352 weston_view_transfer_scissor(parent, view);
1353 } else {
1354 view->geometry.scissor_enabled = false;
1355 }
1356 }
1357
Jason Ekstranda7af7042013-10-12 22:38:11 -05001358 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001359
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001361
Jason Ekstranda7af7042013-10-12 22:38:11 -05001362 wl_signal_emit(&view->surface->compositor->transform_signal,
1363 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001364}
1365
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001366WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001368{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001370
1371 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 * The invariant: if view->geometry.dirty, then all views
1373 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001374 * Corollary: if not parent->geometry.dirty, then all ancestors
1375 * are not dirty.
1376 */
1377
Jason Ekstranda7af7042013-10-12 22:38:11 -05001378 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001379 return;
1380
Jason Ekstranda7af7042013-10-12 22:38:11 -05001381 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001382
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001384 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001386}
1387
1388WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389weston_view_to_global_fixed(struct weston_view *view,
1390 wl_fixed_t vx, wl_fixed_t vy,
1391 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001392{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001393 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001394
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395 weston_view_to_global_float(view,
1396 wl_fixed_to_double(vx),
1397 wl_fixed_to_double(vy),
1398 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001399 *x = wl_fixed_from_double(xf);
1400 *y = wl_fixed_from_double(yf);
1401}
1402
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001403WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404weston_view_from_global_float(struct weston_view *view,
1405 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001406{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001408 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1409
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001411
1412 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001413 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001415 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 *vx = 0;
1417 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001418 return;
1419 }
1420
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 *vx = v.f[0] / v.f[3];
1422 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001423 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 *vx = x - view->geometry.x;
1425 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001426 }
1427}
1428
1429WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001430weston_view_from_global_fixed(struct weston_view *view,
1431 wl_fixed_t x, wl_fixed_t y,
1432 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001433{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001435
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 weston_view_from_global_float(view,
1437 wl_fixed_to_double(x),
1438 wl_fixed_to_double(y),
1439 &vxf, &vyf);
1440 *vx = wl_fixed_from_double(vxf);
1441 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001442}
1443
1444WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445weston_view_from_global(struct weston_view *view,
1446 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001447{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001449
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1451 *vx = floorf(vxf);
1452 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001453}
1454
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001455WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001456weston_surface_schedule_repaint(struct weston_surface *surface)
1457{
1458 struct weston_output *output;
1459
1460 wl_list_for_each(output, &surface->compositor->output_list, link)
1461 if (surface->output_mask & (1 << output->id))
1462 weston_output_schedule_repaint(output);
1463}
1464
1465WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466weston_view_schedule_repaint(struct weston_view *view)
1467{
1468 struct weston_output *output;
1469
1470 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1471 if (view->output_mask & (1 << output->id))
1472 weston_output_schedule_repaint(output);
1473}
1474
Pekka Paalanene508ce62015-02-19 13:59:55 +02001475/**
1476 * XXX: This function does it the wrong way.
1477 * surface->damage is the damage from the client, and causes
1478 * surface_flush_damage() to copy pixels. No window management action can
1479 * cause damage to the client-provided content, warranting re-upload!
1480 *
1481 * Instead of surface->damage, this function should record the damage
1482 * with all the views for this surface to avoid extraneous texture
1483 * uploads.
1484 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001485WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001486weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001487{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001488 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 0, 0, surface->width,
1490 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001491
Kristian Høgsberg98238702012-08-03 16:29:12 -04001492 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001493}
1494
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001495WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001497{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001498 if (view->geometry.x == x && view->geometry.y == y)
1499 return;
1500
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501 view->geometry.x = x;
1502 view->geometry.y = y;
1503 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001504}
1505
Pekka Paalanen483243f2013-03-08 14:56:50 +02001506static void
1507transform_parent_handle_parent_destroy(struct wl_listener *listener,
1508 void *data)
1509{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001510 struct weston_view *view =
1511 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001512 geometry.parent_destroy_listener);
1513
Jason Ekstranda7af7042013-10-12 22:38:11 -05001514 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001515}
1516
1517WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001519 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001520{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521 if (view->geometry.parent) {
1522 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1523 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001524
1525 if (!parent)
1526 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001527 }
1528
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001530
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001532 transform_parent_handle_parent_destroy;
1533 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001534 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001536 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001538 }
1539
Jason Ekstranda7af7042013-10-12 22:38:11 -05001540 weston_view_geometry_dirty(view);
1541}
1542
Pekka Paalanen380adf52015-02-16 14:39:11 +02001543/** Set a clip mask rectangle on a view
1544 *
1545 * \param view The view to set the clip mask on.
1546 * \param x Top-left corner X coordinate of the clip rectangle.
1547 * \param y Top-left corner Y coordinate of the clip rectangle.
1548 * \param width Width of the clip rectangle, non-negative.
1549 * \param height Height of the clip rectangle, non-negative.
1550 *
1551 * A shell may set a clip mask rectangle on a view. Everything outside
1552 * the rectangle is cut away for input and output purposes: it is
1553 * not drawn and cannot be hit by hit-test based input like pointer
1554 * motion or touch-downs. Everything inside the rectangle will behave
1555 * normally. Clients are unaware of clipping.
1556 *
1557 * The rectangle is set in the surface local coordinates. Setting a clip
1558 * mask rectangle does not affect the view position, the view is positioned
1559 * as it would be without a clip. The clip also does not change
1560 * weston_surface::width,height.
1561 *
1562 * The clip mask rectangle is part of transformation inheritance
1563 * (weston_view_set_transform_parent()). A clip set in the root of the
1564 * transformation inheritance tree will affect all views in the tree.
1565 * A clip can be set only on the root view. Attempting to set a clip
1566 * on view that has a transformation parent will fail. Assigning a parent
1567 * to a view that has a clip set will cause the clip to be forgotten.
1568 *
1569 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1570 * on the additional transformations in the child views. These transformations
1571 * may not rotate the coordinate axes, i.e., only translation and scaling
1572 * are allowed. Violating this restriction causes the clipping to malfunction.
1573 * Furthermore, using scaling may cause rounding errors in child clipping.
1574 *
1575 * The clip mask rectangle is not automatically adjusted based on
1576 * wl_surface.attach dx and dy arguments.
1577 *
1578 * A clip mask rectangle can be set only if the compositor capability
1579 * WESTON_CAP_VIEW_CLIP_MASK is present.
1580 *
1581 * This function sets the clip mask rectangle and schedules a repaint for
1582 * the view.
1583 */
1584WL_EXPORT void
1585weston_view_set_mask(struct weston_view *view,
1586 int x, int y, int width, int height)
1587{
1588 struct weston_compositor *compositor = view->surface->compositor;
1589
1590 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1591 weston_log("%s not allowed without capability!\n", __func__);
1592 return;
1593 }
1594
1595 if (view->geometry.parent) {
1596 weston_log("view %p has a parent, clip forbidden!\n", view);
1597 return;
1598 }
1599
1600 if (width < 0 || height < 0) {
1601 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1602 x, y, width, height);
1603 return;
1604 }
1605
1606 pixman_region32_fini(&view->geometry.scissor);
1607 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1608 view->geometry.scissor_enabled = true;
1609 weston_view_geometry_dirty(view);
1610 weston_view_schedule_repaint(view);
1611}
1612
1613/** Remove the clip mask from a view
1614 *
1615 * \param view The view to remove the clip mask from.
1616 *
1617 * Removed the clip mask rectangle and schedules a repaint.
1618 *
1619 * \sa weston_view_set_mask
1620 */
1621WL_EXPORT void
1622weston_view_set_mask_infinite(struct weston_view *view)
1623{
1624 view->geometry.scissor_enabled = false;
1625 weston_view_geometry_dirty(view);
1626 weston_view_schedule_repaint(view);
1627}
1628
Derek Foreman280e7dd2014-10-03 13:13:42 -05001629WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001630weston_view_is_mapped(struct weston_view *view)
1631{
1632 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001633 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001634 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001635 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001636}
1637
Derek Foreman280e7dd2014-10-03 13:13:42 -05001638WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001639weston_surface_is_mapped(struct weston_surface *surface)
1640{
1641 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001642 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001643 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001644 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001645}
1646
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001647static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001648surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001649{
1650 struct weston_view *view;
1651
1652 if (surface->width == width && surface->height == height)
1653 return;
1654
1655 surface->width = width;
1656 surface->height = height;
1657
1658 wl_list_for_each(view, &surface->views, surface_link)
1659 weston_view_geometry_dirty(view);
1660}
1661
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001662WL_EXPORT void
1663weston_surface_set_size(struct weston_surface *surface,
1664 int32_t width, int32_t height)
1665{
1666 assert(!surface->resource);
1667 surface_set_size(surface, width, height);
1668}
1669
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001670static int
1671fixed_round_up_to_int(wl_fixed_t f)
1672{
1673 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1674}
1675
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001676static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001677weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001678{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001679 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001680 int32_t width, height;
1681
1682 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001683 surface->width_from_buffer = 0;
1684 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001685 return;
1686 }
1687
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001688 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001689 case WL_OUTPUT_TRANSFORM_90:
1690 case WL_OUTPUT_TRANSFORM_270:
1691 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1692 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001693 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1694 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001695 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001696 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001697 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1698 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001699 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001700 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001701
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001702 surface->width_from_buffer = width;
1703 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001704}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001705
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001706static void
1707weston_surface_update_size(struct weston_surface *surface)
1708{
1709 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1710 int32_t width, height;
1711
1712 width = surface->width_from_buffer;
1713 height = surface->height_from_buffer;
1714
1715 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001716 surface_set_size(surface,
1717 vp->surface.width, vp->surface.height);
1718 return;
1719 }
1720
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001721 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001722 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1723 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1724
1725 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001726 return;
1727 }
1728
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001729 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001730}
1731
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001732WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001733weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001734{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001735 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001736
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001737 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001738
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001739 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001740}
1741
Jason Ekstranda7af7042013-10-12 22:38:11 -05001742WL_EXPORT struct weston_view *
1743weston_compositor_pick_view(struct weston_compositor *compositor,
1744 wl_fixed_t x, wl_fixed_t y,
1745 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001746{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001747 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001748 wl_fixed_t view_x, view_y;
1749 int view_ix, view_iy;
1750 int ix = wl_fixed_to_int(x);
1751 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001752
Jason Ekstranda7af7042013-10-12 22:38:11 -05001753 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001754 if (!pixman_region32_contains_point(
1755 &view->transform.boundingbox, ix, iy, NULL))
1756 continue;
1757
1758 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1759 view_ix = wl_fixed_to_int(view_x);
1760 view_iy = wl_fixed_to_int(view_y);
1761
1762 if (!pixman_region32_contains_point(&view->surface->input,
1763 view_ix, view_iy, NULL))
1764 continue;
1765
Pekka Paalanen380adf52015-02-16 14:39:11 +02001766 if (view->geometry.scissor_enabled &&
1767 !pixman_region32_contains_point(&view->geometry.scissor,
1768 view_ix, view_iy, NULL))
1769 continue;
1770
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001771 *vx = view_x;
1772 *vy = view_y;
1773 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001774 }
1775
Derek Foremanf9318d12015-05-11 15:40:11 -05001776 *vx = wl_fixed_from_int(-1000000);
1777 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001778 return NULL;
1779}
1780
1781static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001782weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001783{
Daniel Stone37816df2012-05-16 18:45:18 +01001784 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001785
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001786 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001787 return;
1788
Daniel Stone37816df2012-05-16 18:45:18 +01001789 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001790 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001791}
1792
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001793WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001794weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001795{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001796 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001797
Jason Ekstranda7af7042013-10-12 22:38:11 -05001798 if (!weston_view_is_mapped(view))
1799 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001800
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801 weston_view_damage_below(view);
1802 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001803 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001804 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001805 wl_list_remove(&view->link);
1806 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001807 view->output_mask = 0;
1808 weston_surface_assign_output(view->surface);
1809
1810 if (weston_surface_is_mapped(view->surface))
1811 return;
1812
1813 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001814 struct weston_touch *touch = weston_seat_get_touch(seat);
1815 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1816 struct weston_keyboard *keyboard =
1817 weston_seat_get_keyboard(seat);
1818
1819 if (keyboard && keyboard->focus == view->surface)
1820 weston_keyboard_set_focus(keyboard, NULL);
1821 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001822 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001823 if (touch && touch->focus == view)
1824 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001825 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001827
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828WL_EXPORT void
1829weston_surface_unmap(struct weston_surface *surface)
1830{
1831 struct weston_view *view;
1832
1833 wl_list_for_each(view, &surface->views, surface_link)
1834 weston_view_unmap(view);
1835 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001836}
1837
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001838static void
1839weston_surface_reset_pending_buffer(struct weston_surface *surface)
1840{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001841 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001842 surface->pending.sx = 0;
1843 surface->pending.sy = 0;
1844 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001845 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001846}
1847
Jason Ekstranda7af7042013-10-12 22:38:11 -05001848WL_EXPORT void
1849weston_view_destroy(struct weston_view *view)
1850{
1851 wl_signal_emit(&view->destroy_signal, view);
1852
1853 assert(wl_list_empty(&view->geometry.child_list));
1854
1855 if (weston_view_is_mapped(view)) {
1856 weston_view_unmap(view);
1857 weston_compositor_build_view_list(view->surface->compositor);
1858 }
1859
1860 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001861 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001862
1863 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001864 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001865 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001866 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001867
1868 weston_view_set_transform_parent(view, NULL);
1869
Jason Ekstranda7af7042013-10-12 22:38:11 -05001870 wl_list_remove(&view->surface_link);
1871
1872 free(view);
1873}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001874
1875WL_EXPORT void
1876weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001877{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001878 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001879 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001880
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001881 if (--surface->ref_count > 0)
1882 return;
1883
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001884 assert(surface->resource == NULL);
1885
Pekka Paalanenca790762015-04-17 14:23:38 +03001886 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001887
Pekka Paalanene67858b2013-04-25 13:57:42 +03001888 assert(wl_list_empty(&surface->subsurface_list_pending));
1889 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001890
Jason Ekstranda7af7042013-10-12 22:38:11 -05001891 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1892 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001893
Jason Ekstrand7b982072014-05-20 14:33:03 -05001894 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001895
Pekka Paalanende685b82012-12-04 15:58:12 +02001896 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001897
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001898 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001899 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001900 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001901
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001902 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001903 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001904
Pekka Paalanen133e4392014-09-23 22:08:46 -04001905 weston_presentation_feedback_discard_list(&surface->feedback_list);
1906
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001907 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001908}
1909
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001910static void
1911destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001912{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001913 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001914
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001915 assert(surface);
1916
Giulio Camuffo0d379742013-11-15 22:06:15 +01001917 /* Set the resource to NULL, since we don't want to leave a
1918 * dangling pointer if the surface was refcounted and survives
1919 * the weston_surface_destroy() call. */
1920 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001921 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001922}
1923
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001924static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001925weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1926{
1927 struct weston_buffer *buffer =
1928 container_of(listener, struct weston_buffer, destroy_listener);
1929
1930 wl_signal_emit(&buffer->destroy_signal, buffer);
1931 free(buffer);
1932}
1933
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001934WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001935weston_buffer_from_resource(struct wl_resource *resource)
1936{
1937 struct weston_buffer *buffer;
1938 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001939
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001940 listener = wl_resource_get_destroy_listener(resource,
1941 weston_buffer_destroy_handler);
1942
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001943 if (listener)
1944 return container_of(listener, struct weston_buffer,
1945 destroy_listener);
1946
1947 buffer = zalloc(sizeof *buffer);
1948 if (buffer == NULL)
1949 return NULL;
1950
1951 buffer->resource = resource;
1952 wl_signal_init(&buffer->destroy_signal);
1953 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001954 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001955 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001956
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001957 return buffer;
1958}
1959
1960static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001961weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1962 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001963{
Pekka Paalanende685b82012-12-04 15:58:12 +02001964 struct weston_buffer_reference *ref =
1965 container_of(listener, struct weston_buffer_reference,
1966 destroy_listener);
1967
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001968 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001969 ref->buffer = NULL;
1970}
1971
1972WL_EXPORT void
1973weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001974 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001975{
1976 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001977 ref->buffer->busy_count--;
1978 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001979 assert(wl_resource_get_client(ref->buffer->resource));
1980 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001981 WL_BUFFER_RELEASE);
1982 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001983 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001984 }
1985
Pekka Paalanende685b82012-12-04 15:58:12 +02001986 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001987 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001988 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001989 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001990 }
1991
Pekka Paalanende685b82012-12-04 15:58:12 +02001992 ref->buffer = buffer;
1993 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1994}
1995
1996static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001997weston_surface_attach(struct weston_surface *surface,
1998 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001999{
2000 weston_buffer_reference(&surface->buffer_ref, buffer);
2001
Pekka Paalanena6421c42012-12-04 15:58:10 +02002002 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002003 if (weston_surface_is_mapped(surface))
2004 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002005 }
2006
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002007 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002008
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002009 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002010 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002011}
2012
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002013WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002014weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002015{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002016 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002017
2018 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002019 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002020}
2021
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002022WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002023weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002024{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002025 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002026
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002027 pixman_region32_union(&compositor->primary_plane.damage,
2028 &compositor->primary_plane.damage,
2029 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002030 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002031}
2032
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002033static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002034surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002035{
Pekka Paalanende685b82012-12-04 15:58:12 +02002036 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002037 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002038 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002039
Pekka Paalanenb5026542014-11-12 15:09:24 +02002040 if (weston_timeline_enabled_ &&
2041 pixman_region32_not_empty(&surface->damage))
2042 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2043 TLP_OUTPUT(surface->output), TLP_END);
2044
Jason Ekstrandef540082014-06-26 10:37:36 -07002045 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002046}
2047
2048static void
2049view_accumulate_damage(struct weston_view *view,
2050 pixman_region32_t *opaque)
2051{
2052 pixman_region32_t damage;
2053
2054 pixman_region32_init(&damage);
2055 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002056 pixman_box32_t *extents;
2057
Jason Ekstranda7af7042013-10-12 22:38:11 -05002058 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002059 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002060 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002061 pixman_region32_copy(&damage, &view->surface->damage);
2062 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002063 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002064 }
2065
Pekka Paalanen380adf52015-02-16 14:39:11 +02002066 pixman_region32_intersect(&damage, &damage,
2067 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002068 pixman_region32_subtract(&damage, &damage, opaque);
2069 pixman_region32_union(&view->plane->damage,
2070 &view->plane->damage, &damage);
2071 pixman_region32_fini(&damage);
2072 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002073 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002074}
2075
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002076static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002077compositor_accumulate_damage(struct weston_compositor *ec)
2078{
2079 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002080 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002081 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002082
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002083 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002084
2085 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002086 pixman_region32_copy(&plane->clip, &clip);
2087
2088 pixman_region32_init(&opaque);
2089
Jason Ekstranda7af7042013-10-12 22:38:11 -05002090 wl_list_for_each(ev, &ec->view_list, link) {
2091 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002092 continue;
2093
Jason Ekstranda7af7042013-10-12 22:38:11 -05002094 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002095 }
2096
2097 pixman_region32_union(&clip, &clip, &opaque);
2098 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002099 }
2100
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002101 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002102
Jason Ekstranda7af7042013-10-12 22:38:11 -05002103 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002104 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002105
2106 wl_list_for_each(ev, &ec->view_list, link) {
2107 if (ev->surface->touched)
2108 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002109 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002110
2111 surface_flush_damage(ev->surface);
2112
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002113 /* Both the renderer and the backend have seen the buffer
2114 * by now. If renderer needs the buffer, it has its own
2115 * reference set. If the backend wants to keep the buffer
2116 * around for migrating the surface into a non-primary plane
2117 * later, keep_buffer is true. Otherwise, drop the core
2118 * reference now, and allow early buffer release. This enables
2119 * clients to use single-buffering.
2120 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 if (!ev->surface->keep_buffer)
2122 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002123 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002124}
2125
2126static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002128{
2129 struct weston_subsurface *sub;
2130
Pekka Paalanene67858b2013-04-25 13:57:42 +03002131 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002132 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002133 continue;
2134
Jason Ekstranda7af7042013-10-12 22:38:11 -05002135 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2136 wl_list_init(&sub->surface->views);
2137
2138 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002139 }
2140}
2141
2142static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002143surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002144{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002145 struct weston_subsurface *sub;
2146 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002147
Jason Ekstranda7af7042013-10-12 22:38:11 -05002148 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2149 if (sub->surface == surface)
2150 continue;
2151
George Kiagiadakised04d382014-06-13 18:10:26 +02002152 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2153 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002154 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002155 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002156
2157 surface_free_unused_subsurface_views(sub->surface);
2158 }
2159}
2160
2161static void
2162view_list_add_subsurface_view(struct weston_compositor *compositor,
2163 struct weston_subsurface *sub,
2164 struct weston_view *parent)
2165{
2166 struct weston_subsurface *child;
2167 struct weston_view *view = NULL, *iv;
2168
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002169 if (!weston_surface_is_mapped(sub->surface))
2170 return;
2171
Jason Ekstranda7af7042013-10-12 22:38:11 -05002172 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2173 if (iv->geometry.parent == parent) {
2174 view = iv;
2175 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002176 }
2177 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178
2179 if (view) {
2180 /* Put it back in the surface's list of views */
2181 wl_list_remove(&view->surface_link);
2182 wl_list_insert(&sub->surface->views, &view->surface_link);
2183 } else {
2184 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002185 weston_view_set_position(view,
2186 sub->position.x,
2187 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002188 weston_view_set_transform_parent(view, parent);
2189 }
2190
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002191 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002192 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002193
Pekka Paalanenb188e912013-11-19 14:03:35 +02002194 if (wl_list_empty(&sub->surface->subsurface_list)) {
2195 wl_list_insert(compositor->view_list.prev, &view->link);
2196 return;
2197 }
2198
2199 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2200 if (child->surface == sub->surface)
2201 wl_list_insert(compositor->view_list.prev, &view->link);
2202 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002204 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002205}
2206
2207static void
2208view_list_add(struct weston_compositor *compositor,
2209 struct weston_view *view)
2210{
2211 struct weston_subsurface *sub;
2212
2213 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002214
Pekka Paalanenb188e912013-11-19 14:03:35 +02002215 if (wl_list_empty(&view->surface->subsurface_list)) {
2216 wl_list_insert(compositor->view_list.prev, &view->link);
2217 return;
2218 }
2219
2220 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2221 if (sub->surface == view->surface)
2222 wl_list_insert(compositor->view_list.prev, &view->link);
2223 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002224 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002225 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226}
2227
2228static void
2229weston_compositor_build_view_list(struct weston_compositor *compositor)
2230{
2231 struct weston_view *view;
2232 struct weston_layer *layer;
2233
2234 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002235 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002236 surface_stash_subsurface_views(view->surface);
2237
2238 wl_list_init(&compositor->view_list);
2239 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002240 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002241 view_list_add(compositor, view);
2242 }
2243 }
2244
2245 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002246 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002247 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002248}
2249
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002250static void
2251weston_output_take_feedback_list(struct weston_output *output,
2252 struct weston_surface *surface)
2253{
2254 struct weston_view *view;
2255 struct weston_presentation_feedback *feedback;
2256 uint32_t flags = 0xffffffff;
2257
2258 if (wl_list_empty(&surface->feedback_list))
2259 return;
2260
2261 /* All views must have the flag for the flag to survive. */
2262 wl_list_for_each(view, &surface->views, surface_link) {
2263 /* ignore views that are not on this output at all */
2264 if (view->output_mask & (1u << output->id))
2265 flags &= view->psf_flags;
2266 }
2267
2268 wl_list_for_each(feedback, &surface->feedback_list, link)
2269 feedback->psf_flags = flags;
2270
2271 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2272 wl_list_init(&surface->feedback_list);
2273}
2274
David Herrmann1edf44c2013-10-22 17:11:26 +02002275static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002276weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002277{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002278 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002279 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002280 struct weston_animation *animation, *next;
2281 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002282 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002283 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002284 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002285
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002286 if (output->destroying)
2287 return 0;
2288
Pekka Paalanenb5026542014-11-12 15:09:24 +02002289 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2290
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002291 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002292 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002293
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002294 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002295 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002296 } else {
2297 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002298 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002299 ev->psf_flags = 0;
2300 }
2301 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002302
Pekka Paalanene67858b2013-04-25 13:57:42 +03002303 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002304 wl_list_for_each(ev, &ec->view_list, link) {
2305 /* Note: This operation is safe to do multiple times on the
2306 * same surface.
2307 */
2308 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002309 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002310 &ev->surface->frame_callback_list);
2311 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002312
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002313 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002314 }
2315 }
2316
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002317 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002318
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002319 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002320 pixman_region32_intersect(&output_damage,
2321 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002322 pixman_region32_subtract(&output_damage,
2323 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002324
Scott Moreauccbf29d2012-02-22 14:21:41 -07002325 if (output->dirty)
2326 weston_output_update_matrix(output);
2327
David Herrmann1edf44c2013-10-22 17:11:26 +02002328 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002329
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002330 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002331
Kristian Høgsbergef044142011-06-21 15:02:12 -04002332 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002333
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002334 weston_compositor_repick(ec);
2335 wl_event_loop_dispatch(ec->input_loop, 0);
2336
Jonas Ådahldb773762012-06-13 00:01:21 +02002337 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002338 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002339 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002340 }
2341
Scott Moreaud64cf212012-06-08 19:40:54 -06002342 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002343 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002344 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002345 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002346
Pekka Paalanenb5026542014-11-12 15:09:24 +02002347 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2348
David Herrmann1edf44c2013-10-22 17:11:26 +02002349 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002350}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002351
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002352static int
2353weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002354{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002355 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002356
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002357 wl_event_loop_dispatch(compositor->input_loop, 0);
2358
2359 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002360}
2361
Pekka Paalanen82919792014-05-21 13:51:49 +03002362static void
2363weston_output_schedule_repaint_reset(struct weston_output *output)
2364{
2365 struct weston_compositor *compositor = output->compositor;
2366 struct wl_event_loop *loop;
2367 int fd;
2368
2369 output->repaint_scheduled = 0;
2370 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2371
2372 if (compositor->input_loop_source)
2373 return;
2374
2375 loop = wl_display_get_event_loop(compositor->wl_display);
2376 fd = wl_event_loop_get_fd(compositor->input_loop);
2377 compositor->input_loop_source =
2378 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2379 weston_compositor_read_input, compositor);
2380}
2381
Pekka Paalanen0513a952014-05-21 16:17:27 +03002382static int
2383output_repaint_timer_handler(void *data)
2384{
2385 struct weston_output *output = data;
2386 struct weston_compositor *compositor = output->compositor;
2387
2388 if (output->repaint_needed &&
2389 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2390 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2391 weston_output_repaint(output) == 0)
2392 return 0;
2393
2394 weston_output_schedule_repaint_reset(output);
2395
2396 return 0;
2397}
2398
Kristian Høgsbergef044142011-06-21 15:02:12 -04002399WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002400weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002401 const struct timespec *stamp,
2402 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002403{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002404 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002405 int32_t refresh_nsec;
2406 struct timespec now;
2407 struct timespec gone;
2408 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002409
Pekka Paalanenb5026542014-11-12 15:09:24 +02002410 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2411 TLP_VBLANK(stamp), TLP_END);
2412
Pekka Paalanend7894d02015-07-03 15:08:53 +03002413 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002414 weston_presentation_feedback_present_list(&output->feedback_list,
2415 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002416 output->msc,
2417 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002418
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002419 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002420
Pekka Paalanen0513a952014-05-21 16:17:27 +03002421 weston_compositor_read_presentation_clock(compositor, &now);
2422 timespec_sub(&gone, &now, stamp);
2423 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2424 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002425
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002426 if (msec < -1000 || msec > 1000) {
2427 static bool warned;
2428
2429 if (!warned)
2430 weston_log("Warning: computed repaint delay is "
2431 "insane: %d msec\n", msec);
2432 warned = true;
2433
2434 msec = 0;
2435 }
2436
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002437 /* Called from restart_repaint_loop and restart happens already after
2438 * the deadline given by repaint_msec? In that case we delay until
2439 * the deadline of the next frame, to give clients a more predictable
2440 * timing of the repaint cycle to lock on. */
2441 if (presented_flags == PRESENTATION_FEEDBACK_INVALID && msec < 0)
2442 msec += refresh_nsec / 1000000;
2443
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002444 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002445 output_repaint_timer_handler(output);
2446 else
2447 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002448}
2449
2450static void
2451idle_repaint(void *data)
2452{
2453 struct weston_output *output = data;
2454
Jonas Ådahle5a12252013-04-05 23:07:11 +02002455 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002456}
2457
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002458WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002459weston_layer_entry_insert(struct weston_layer_entry *list,
2460 struct weston_layer_entry *entry)
2461{
2462 wl_list_insert(&list->link, &entry->link);
2463 entry->layer = list->layer;
2464}
2465
2466WL_EXPORT void
2467weston_layer_entry_remove(struct weston_layer_entry *entry)
2468{
2469 wl_list_remove(&entry->link);
2470 wl_list_init(&entry->link);
2471 entry->layer = NULL;
2472}
2473
2474WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002475weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2476{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002477 wl_list_init(&layer->view_list.link);
2478 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002479 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002480 if (below != NULL)
2481 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002482}
2483
2484WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002485weston_layer_set_mask(struct weston_layer *layer,
2486 int x, int y, int width, int height)
2487{
2488 struct weston_view *view;
2489
2490 layer->mask.x1 = x;
2491 layer->mask.x2 = x + width;
2492 layer->mask.y1 = y;
2493 layer->mask.y2 = y + height;
2494
2495 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2496 weston_view_geometry_dirty(view);
2497 }
2498}
2499
2500WL_EXPORT void
2501weston_layer_set_mask_infinite(struct weston_layer *layer)
2502{
2503 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2504 UINT32_MAX, UINT32_MAX);
2505}
2506
2507WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002508weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002509{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002510 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002511 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002512
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002513 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2514 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002515 return;
2516
Pekka Paalanenb5026542014-11-12 15:09:24 +02002517 if (!output->repaint_needed)
2518 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2519
Kristian Høgsbergef044142011-06-21 15:02:12 -04002520 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002521 output->repaint_needed = 1;
2522 if (output->repaint_scheduled)
2523 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002524
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002525 wl_event_loop_add_idle(loop, idle_repaint, output);
2526 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002527 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2528
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002529
2530 if (compositor->input_loop_source) {
2531 wl_event_source_remove(compositor->input_loop_source);
2532 compositor->input_loop_source = NULL;
2533 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002534}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002535
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002536WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002537weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2538{
2539 struct weston_output *output;
2540
2541 wl_list_for_each(output, &compositor->output_list, link)
2542 weston_output_schedule_repaint(output);
2543}
2544
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002545static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002546surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002547{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002548 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002549}
2550
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002551static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002552surface_attach(struct wl_client *client,
2553 struct wl_resource *resource,
2554 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2555{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002556 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002557 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002558
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002559 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002560 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002561 if (buffer == NULL) {
2562 wl_client_post_no_memory(client);
2563 return;
2564 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002565 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002566
Pekka Paalanende685b82012-12-04 15:58:12 +02002567 /* Attach, attach, without commit in between does not send
2568 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002569 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002570
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002571 surface->pending.sx = sx;
2572 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002573 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002574}
2575
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002576static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002577surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002578 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002579 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002580{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002581 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002582
Pekka Paalanen8e159182012-10-10 12:49:25 +03002583 pixman_region32_union_rect(&surface->pending.damage,
2584 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002585 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002586}
2587
Kristian Høgsberg33418202011-08-16 23:01:28 -04002588static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002589destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002590{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002591 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002592
2593 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002594 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002595}
2596
2597static void
2598surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002599 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002600{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002601 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002602 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002603
2604 cb = malloc(sizeof *cb);
2605 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002606 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002607 return;
2608 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002609
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002610 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2611 callback);
2612 if (cb->resource == NULL) {
2613 free(cb);
2614 wl_resource_post_no_memory(resource);
2615 return;
2616 }
2617
Jason Ekstranda85118c2013-06-27 20:17:02 -05002618 wl_resource_set_implementation(cb->resource, NULL, cb,
2619 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002620
Pekka Paalanenbc106382012-10-10 12:49:31 +03002621 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002622}
2623
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002624static void
2625surface_set_opaque_region(struct wl_client *client,
2626 struct wl_resource *resource,
2627 struct wl_resource *region_resource)
2628{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002629 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002630 struct weston_region *region;
2631
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002632 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002633 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002634 pixman_region32_copy(&surface->pending.opaque,
2635 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002636 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002637 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002638 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002639}
2640
2641static void
2642surface_set_input_region(struct wl_client *client,
2643 struct wl_resource *resource,
2644 struct wl_resource *region_resource)
2645{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002646 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002647 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002648
2649 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002650 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002651 pixman_region32_copy(&surface->pending.input,
2652 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002653 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002654 pixman_region32_fini(&surface->pending.input);
2655 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002656 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002657}
2658
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002659static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002660weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002661{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002662 struct weston_subsurface *sub;
2663
2664 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2665 parent_link_pending) {
2666 wl_list_remove(&sub->parent_link);
2667 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2668 }
2669}
2670
2671static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002672weston_surface_build_buffer_matrix(struct weston_surface *surface,
2673 struct weston_matrix *matrix)
2674{
2675 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2676 double src_width, src_height, dest_width, dest_height;
2677
2678 weston_matrix_init(matrix);
2679
2680 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2681 src_width = surface->width_from_buffer;
2682 src_height = surface->height_from_buffer;
2683 } else {
2684 src_width = wl_fixed_to_double(vp->buffer.src_width);
2685 src_height = wl_fixed_to_double(vp->buffer.src_height);
2686 }
2687
2688 if (vp->surface.width == -1) {
2689 dest_width = src_width;
2690 dest_height = src_height;
2691 } else {
2692 dest_width = vp->surface.width;
2693 dest_height = vp->surface.height;
2694 }
2695
2696 if (src_width != dest_width || src_height != dest_height)
2697 weston_matrix_scale(matrix,
2698 src_width / dest_width,
2699 src_height / dest_height, 1);
2700
2701 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2702 weston_matrix_translate(matrix,
2703 wl_fixed_to_double(vp->buffer.src_x),
2704 wl_fixed_to_double(vp->buffer.src_y),
2705 0);
2706
2707 switch (vp->buffer.transform) {
2708 case WL_OUTPUT_TRANSFORM_FLIPPED:
2709 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2710 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2711 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2712 weston_matrix_scale(matrix, -1, 1, 1);
2713 weston_matrix_translate(matrix,
2714 surface->width_from_buffer, 0, 0);
2715 break;
2716 }
2717
2718 switch (vp->buffer.transform) {
2719 default:
2720 case WL_OUTPUT_TRANSFORM_NORMAL:
2721 case WL_OUTPUT_TRANSFORM_FLIPPED:
2722 break;
2723 case WL_OUTPUT_TRANSFORM_90:
2724 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2725 weston_matrix_rotate_xy(matrix, 0, 1);
2726 weston_matrix_translate(matrix,
2727 surface->height_from_buffer, 0, 0);
2728 break;
2729 case WL_OUTPUT_TRANSFORM_180:
2730 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2731 weston_matrix_rotate_xy(matrix, -1, 0);
2732 weston_matrix_translate(matrix,
2733 surface->width_from_buffer,
2734 surface->height_from_buffer, 0);
2735 break;
2736 case WL_OUTPUT_TRANSFORM_270:
2737 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2738 weston_matrix_rotate_xy(matrix, 0, -1);
2739 weston_matrix_translate(matrix,
2740 0, surface->width_from_buffer, 0);
2741 break;
2742 }
2743
2744 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2745}
2746
2747static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002748weston_surface_commit_state(struct weston_surface *surface,
2749 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002750{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002751 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002752 pixman_region32_t opaque;
2753
Alexander Larsson4ea95522013-05-22 14:41:37 +02002754 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002755 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002756 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002757 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002758
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002759 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002760 if (state->newly_attached)
2761 weston_surface_attach(surface, state->buffer);
2762 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002763
Jason Ekstrand1e059042014-10-16 10:55:19 -05002764 weston_surface_build_buffer_matrix(surface,
2765 &surface->surface_to_buffer_matrix);
2766 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2767 &surface->surface_to_buffer_matrix);
2768
Jason Ekstrand7b982072014-05-20 14:33:03 -05002769 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002770 weston_surface_update_size(surface);
2771 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002772 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002773 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002774
Jason Ekstrand7b982072014-05-20 14:33:03 -05002775 state->sx = 0;
2776 state->sy = 0;
2777 state->newly_attached = 0;
2778 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002779
2780 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002781 if (weston_timeline_enabled_ &&
2782 pixman_region32_not_empty(&state->damage))
2783 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002784 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002785 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002786 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002787 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002788 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002789
2790 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002791 pixman_region32_init(&opaque);
2792 pixman_region32_intersect_rect(&opaque, &state->opaque,
2793 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002794
2795 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2796 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002797 wl_list_for_each(view, &surface->views, surface_link)
2798 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002799 }
2800
2801 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002802
2803 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002804 pixman_region32_intersect_rect(&surface->input, &state->input,
2805 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002806
Pekka Paalanenbc106382012-10-10 12:49:31 +03002807 /* wl_surface.frame */
2808 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002809 &state->frame_callback_list);
2810 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002811
2812 /* XXX:
2813 * What should happen with a feedback request, if there
2814 * is no wl_buffer attached for this commit?
2815 */
2816
2817 /* presentation.feedback */
2818 wl_list_insert_list(&surface->feedback_list,
2819 &state->feedback_list);
2820 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002821}
2822
2823static void
2824weston_surface_commit(struct weston_surface *surface)
2825{
2826 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002827
Pekka Paalanene67858b2013-04-25 13:57:42 +03002828 weston_surface_commit_subsurface_order(surface);
2829
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002830 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002831}
2832
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002833static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002834weston_subsurface_commit(struct weston_subsurface *sub);
2835
2836static void
2837weston_subsurface_parent_commit(struct weston_subsurface *sub,
2838 int parent_is_synchronized);
2839
2840static void
2841surface_commit(struct wl_client *client, struct wl_resource *resource)
2842{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002843 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002844 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2845
2846 if (sub) {
2847 weston_subsurface_commit(sub);
2848 return;
2849 }
2850
2851 weston_surface_commit(surface);
2852
2853 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2854 if (sub->surface != surface)
2855 weston_subsurface_parent_commit(sub, 0);
2856 }
2857}
2858
2859static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002860surface_set_buffer_transform(struct wl_client *client,
2861 struct wl_resource *resource, int transform)
2862{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002863 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002864
Jonny Lamba55f1392014-05-30 12:07:15 +02002865 /* if wl_output.transform grows more members this will need to be updated. */
2866 if (transform < 0 ||
2867 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2868 wl_resource_post_error(resource,
2869 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2870 "buffer transform must be a valid transform "
2871 "('%d' specified)", transform);
2872 return;
2873 }
2874
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002875 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002876 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002877}
2878
Alexander Larsson4ea95522013-05-22 14:41:37 +02002879static void
2880surface_set_buffer_scale(struct wl_client *client,
2881 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002882 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002883{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002884 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002885
Jonny Lamba55f1392014-05-30 12:07:15 +02002886 if (scale < 1) {
2887 wl_resource_post_error(resource,
2888 WL_SURFACE_ERROR_INVALID_SCALE,
2889 "buffer scale must be at least one "
2890 "('%d' specified)", scale);
2891 return;
2892 }
2893
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002894 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002895 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002896}
2897
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002898static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002899 surface_destroy,
2900 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002901 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002902 surface_frame,
2903 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002904 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002905 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002906 surface_set_buffer_transform,
2907 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002908};
2909
2910static void
2911compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002912 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002913{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002914 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002915 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002916
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002917 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002918 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002919 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002920 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002921 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002922
Jason Ekstranda85118c2013-06-27 20:17:02 -05002923 surface->resource =
2924 wl_resource_create(client, &wl_surface_interface,
2925 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002926 if (surface->resource == NULL) {
2927 weston_surface_destroy(surface);
2928 wl_resource_post_no_memory(resource);
2929 return;
2930 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002931 wl_resource_set_implementation(surface->resource, &surface_interface,
2932 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002933
2934 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002935}
2936
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002937static void
2938destroy_region(struct wl_resource *resource)
2939{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002940 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002941
2942 pixman_region32_fini(&region->region);
2943 free(region);
2944}
2945
2946static void
2947region_destroy(struct wl_client *client, struct wl_resource *resource)
2948{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002949 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002950}
2951
2952static void
2953region_add(struct wl_client *client, struct wl_resource *resource,
2954 int32_t x, int32_t y, int32_t width, int32_t height)
2955{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002956 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002957
2958 pixman_region32_union_rect(&region->region, &region->region,
2959 x, y, width, height);
2960}
2961
2962static void
2963region_subtract(struct wl_client *client, struct wl_resource *resource,
2964 int32_t x, int32_t y, int32_t width, int32_t height)
2965{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002966 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002967 pixman_region32_t rect;
2968
2969 pixman_region32_init_rect(&rect, x, y, width, height);
2970 pixman_region32_subtract(&region->region, &region->region, &rect);
2971 pixman_region32_fini(&rect);
2972}
2973
2974static const struct wl_region_interface region_interface = {
2975 region_destroy,
2976 region_add,
2977 region_subtract
2978};
2979
2980static void
2981compositor_create_region(struct wl_client *client,
2982 struct wl_resource *resource, uint32_t id)
2983{
2984 struct weston_region *region;
2985
2986 region = malloc(sizeof *region);
2987 if (region == NULL) {
2988 wl_resource_post_no_memory(resource);
2989 return;
2990 }
2991
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002992 pixman_region32_init(&region->region);
2993
Jason Ekstranda85118c2013-06-27 20:17:02 -05002994 region->resource =
2995 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002996 if (region->resource == NULL) {
2997 free(region);
2998 wl_resource_post_no_memory(resource);
2999 return;
3000 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003001 wl_resource_set_implementation(region->resource, &region_interface,
3002 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003003}
3004
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003005static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003006 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003007 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003008};
3009
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003010static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003011weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3012{
3013 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003014
Jason Ekstrand7b982072014-05-20 14:33:03 -05003015 weston_surface_commit_state(surface, &sub->cached);
3016 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003017
3018 weston_surface_commit_subsurface_order(surface);
3019
3020 weston_surface_schedule_repaint(surface);
3021
Jason Ekstrand7b982072014-05-20 14:33:03 -05003022 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003023}
3024
3025static void
3026weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3027{
3028 struct weston_surface *surface = sub->surface;
3029
3030 /*
3031 * If this commit would cause the surface to move by the
3032 * attach(dx, dy) parameters, the old damage region must be
3033 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02003034 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003035 */
3036 pixman_region32_translate(&sub->cached.damage,
3037 -surface->pending.sx, -surface->pending.sy);
3038 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
3039 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07003040 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003041
3042 if (surface->pending.newly_attached) {
3043 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003044 weston_surface_state_set_buffer(&sub->cached,
3045 surface->pending.buffer);
3046 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003047 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003048 weston_presentation_feedback_discard_list(
3049 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003050 }
3051 sub->cached.sx += surface->pending.sx;
3052 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003053
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003054 sub->cached.buffer_viewport.changed |=
3055 surface->pending.buffer_viewport.changed;
3056 sub->cached.buffer_viewport.buffer =
3057 surface->pending.buffer_viewport.buffer;
3058 sub->cached.buffer_viewport.surface =
3059 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003060
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003061 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003062
3063 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3064
3065 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3066
3067 wl_list_insert_list(&sub->cached.frame_callback_list,
3068 &surface->pending.frame_callback_list);
3069 wl_list_init(&surface->pending.frame_callback_list);
3070
Pekka Paalanen133e4392014-09-23 22:08:46 -04003071 wl_list_insert_list(&sub->cached.feedback_list,
3072 &surface->pending.feedback_list);
3073 wl_list_init(&surface->pending.feedback_list);
3074
Jason Ekstrand7b982072014-05-20 14:33:03 -05003075 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003076}
3077
Derek Foreman280e7dd2014-10-03 13:13:42 -05003078static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003079weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3080{
3081 while (sub) {
3082 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003083 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003084
3085 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003086 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003087
3088 sub = weston_surface_to_subsurface(sub->parent);
3089 }
3090
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003091 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003092}
3093
3094static void
3095weston_subsurface_commit(struct weston_subsurface *sub)
3096{
3097 struct weston_surface *surface = sub->surface;
3098 struct weston_subsurface *tmp;
3099
3100 /* Recursive check for effectively synchronized. */
3101 if (weston_subsurface_is_synchronized(sub)) {
3102 weston_subsurface_commit_to_cache(sub);
3103 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003104 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003105 /* flush accumulated state from cache */
3106 weston_subsurface_commit_to_cache(sub);
3107 weston_subsurface_commit_from_cache(sub);
3108 } else {
3109 weston_surface_commit(surface);
3110 }
3111
3112 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3113 if (tmp->surface != surface)
3114 weston_subsurface_parent_commit(tmp, 0);
3115 }
3116 }
3117}
3118
3119static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003120weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003121{
3122 struct weston_surface *surface = sub->surface;
3123 struct weston_subsurface *tmp;
3124
Pekka Paalanene67858b2013-04-25 13:57:42 +03003125 /* From now on, commit_from_cache the whole sub-tree, regardless of
3126 * the synchronized mode of each child. This sub-surface or some
3127 * of its ancestors were synchronized, so we are synchronized
3128 * all the way down.
3129 */
3130
Jason Ekstrand7b982072014-05-20 14:33:03 -05003131 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003132 weston_subsurface_commit_from_cache(sub);
3133
3134 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3135 if (tmp->surface != surface)
3136 weston_subsurface_parent_commit(tmp, 1);
3137 }
3138}
3139
3140static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003141weston_subsurface_parent_commit(struct weston_subsurface *sub,
3142 int parent_is_synchronized)
3143{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003144 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003145 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003146 wl_list_for_each(view, &sub->surface->views, surface_link)
3147 weston_view_set_position(view,
3148 sub->position.x,
3149 sub->position.y);
3150
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003151 sub->position.set = 0;
3152 }
3153
3154 if (parent_is_synchronized || sub->synchronized)
3155 weston_subsurface_synchronized_commit(sub);
3156}
3157
Pekka Paalanen8274d902014-08-06 19:36:51 +03003158static int
3159subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3160{
3161 return snprintf(buf, len, "sub-surface");
3162}
3163
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003164static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003165subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003166{
3167 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003168 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003169
Jason Ekstranda7af7042013-10-12 22:38:11 -05003170 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003171 weston_view_set_position(view,
3172 view->geometry.x + dx,
3173 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003174
3175 /* No need to check parent mappedness, because if parent is not
3176 * mapped, parent is not in a visible layer, so this sub-surface
3177 * will not be drawn either.
3178 */
3179 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003180 struct weston_output *output;
3181
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003182 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003183 * because that would call it also for the parent surface,
3184 * which might not be mapped yet. That would lead to
3185 * inconsistent state, where the window could never be
3186 * mapped.
3187 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003188 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189 * weston_surface_is_mapped() return true, so that when the
3190 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003191 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003192 */
3193 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003194 output = container_of(compositor->output_list.next,
3195 struct weston_output, link);
3196
3197 surface->output = output;
3198 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003199 }
3200}
3201
3202static struct weston_subsurface *
3203weston_surface_to_subsurface(struct weston_surface *surface)
3204{
3205 if (surface->configure == subsurface_configure)
3206 return surface->configure_private;
3207
3208 return NULL;
3209}
3210
Pekka Paalanen01388e22013-04-25 13:57:44 +03003211WL_EXPORT struct weston_surface *
3212weston_surface_get_main_surface(struct weston_surface *surface)
3213{
3214 struct weston_subsurface *sub;
3215
3216 while (surface && (sub = weston_surface_to_subsurface(surface)))
3217 surface = sub->parent;
3218
3219 return surface;
3220}
3221
Pekka Paalanen50b67472014-10-01 15:02:41 +03003222WL_EXPORT int
3223weston_surface_set_role(struct weston_surface *surface,
3224 const char *role_name,
3225 struct wl_resource *error_resource,
3226 uint32_t error_code)
3227{
3228 assert(role_name);
3229
3230 if (surface->role_name == NULL ||
3231 surface->role_name == role_name ||
3232 strcmp(surface->role_name, role_name) == 0) {
3233 surface->role_name = role_name;
3234
3235 return 0;
3236 }
3237
3238 wl_resource_post_error(error_resource, error_code,
3239 "Cannot assign role %s to wl_surface@%d,"
3240 " already has role %s\n",
3241 role_name,
3242 wl_resource_get_id(surface->resource),
3243 surface->role_name);
3244 return -1;
3245}
3246
Pekka Paalanen8274d902014-08-06 19:36:51 +03003247WL_EXPORT void
3248weston_surface_set_label_func(struct weston_surface *surface,
3249 int (*desc)(struct weston_surface *,
3250 char *, size_t))
3251{
3252 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003253 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003254}
3255
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003256/** Get the size of surface contents
3257 *
3258 * \param surface The surface to query.
3259 * \param width Returns the width of raw contents.
3260 * \param height Returns the height of raw contents.
3261 *
3262 * Retrieves the raw surface content size in pixels for the given surface.
3263 * This is the whole content size in buffer pixels. If the surface
3264 * has no content or the renderer does not implement this feature,
3265 * zeroes are returned.
3266 *
3267 * This function is used to determine the buffer size needed for
3268 * a weston_surface_copy_content() call.
3269 */
3270WL_EXPORT void
3271weston_surface_get_content_size(struct weston_surface *surface,
3272 int *width, int *height)
3273{
3274 struct weston_renderer *rer = surface->compositor->renderer;
3275
3276 if (!rer->surface_get_content_size) {
3277 *width = 0;
3278 *height = 0;
3279 return;
3280 }
3281
3282 rer->surface_get_content_size(surface, width, height);
3283}
3284
3285/** Copy surface contents to system memory.
3286 *
3287 * \param surface The surface to copy from.
3288 * \param target Pointer to the target memory buffer.
3289 * \param size Size of the target buffer in bytes.
3290 * \param src_x X location on contents to copy from.
3291 * \param src_y Y location on contents to copy from.
3292 * \param width Width in pixels of the area to copy.
3293 * \param height Height in pixels of the area to copy.
3294 * \return 0 for success, -1 for failure.
3295 *
3296 * Surface contents are maintained by the renderer. They can be in a
3297 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3298 * else.
3299 *
3300 * Surface contents are copied into memory pointed to by target,
3301 * which has size bytes of space available. The target memory
3302 * may be larger than needed, but being smaller returns an error.
3303 * The extra bytes in target may or may not be written; their content is
3304 * unspecified. Size must be large enough to hold the image.
3305 *
3306 * The image in the target memory will be arranged in rows from
3307 * top to bottom, and pixels on a row from left to right. The pixel
3308 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3309 * width * 4.
3310 *
3311 * Parameters src_x and src_y define the upper-left corner in buffer
3312 * coordinates (pixels) to copy from. Parameters width and height
3313 * define the size of the area to copy in pixels.
3314 *
3315 * The rectangle defined by src_x, src_y, width, height must fit in
3316 * the surface contents. Otherwise an error is returned.
3317 *
3318 * Use surface_get_data_size to determine the content size; the
3319 * needed target buffer size and rectangle limits.
3320 *
3321 * CURRENT IMPLEMENTATION RESTRICTIONS:
3322 * - the machine must be little-endian due to Pixman formats.
3323 *
3324 * NOTE: Pixman formats are premultiplied.
3325 */
3326WL_EXPORT int
3327weston_surface_copy_content(struct weston_surface *surface,
3328 void *target, size_t size,
3329 int src_x, int src_y,
3330 int width, int height)
3331{
3332 struct weston_renderer *rer = surface->compositor->renderer;
3333 int cw, ch;
3334 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3335
3336 if (!rer->surface_copy_content)
3337 return -1;
3338
3339 weston_surface_get_content_size(surface, &cw, &ch);
3340
3341 if (src_x < 0 || src_y < 0)
3342 return -1;
3343
3344 if (width <= 0 || height <= 0)
3345 return -1;
3346
3347 if (src_x + width > cw || src_y + height > ch)
3348 return -1;
3349
3350 if (width * bytespp * height > size)
3351 return -1;
3352
3353 return rer->surface_copy_content(surface, target, size,
3354 src_x, src_y, width, height);
3355}
3356
Pekka Paalanene67858b2013-04-25 13:57:42 +03003357static void
3358subsurface_set_position(struct wl_client *client,
3359 struct wl_resource *resource, int32_t x, int32_t y)
3360{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003361 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003362
3363 if (!sub)
3364 return;
3365
3366 sub->position.x = x;
3367 sub->position.y = y;
3368 sub->position.set = 1;
3369}
3370
3371static struct weston_subsurface *
3372subsurface_from_surface(struct weston_surface *surface)
3373{
3374 struct weston_subsurface *sub;
3375
3376 sub = weston_surface_to_subsurface(surface);
3377 if (sub)
3378 return sub;
3379
3380 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3381 if (sub->surface == surface)
3382 return sub;
3383
3384 return NULL;
3385}
3386
3387static struct weston_subsurface *
3388subsurface_sibling_check(struct weston_subsurface *sub,
3389 struct weston_surface *surface,
3390 const char *request)
3391{
3392 struct weston_subsurface *sibling;
3393
3394 sibling = subsurface_from_surface(surface);
3395
3396 if (!sibling) {
3397 wl_resource_post_error(sub->resource,
3398 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3399 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003400 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003401 return NULL;
3402 }
3403
3404 if (sibling->parent != sub->parent) {
3405 wl_resource_post_error(sub->resource,
3406 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3407 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003408 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003409 return NULL;
3410 }
3411
3412 return sibling;
3413}
3414
3415static void
3416subsurface_place_above(struct wl_client *client,
3417 struct wl_resource *resource,
3418 struct wl_resource *sibling_resource)
3419{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003420 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003421 struct weston_surface *surface =
3422 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003423 struct weston_subsurface *sibling;
3424
3425 if (!sub)
3426 return;
3427
3428 sibling = subsurface_sibling_check(sub, surface, "place_above");
3429 if (!sibling)
3430 return;
3431
3432 wl_list_remove(&sub->parent_link_pending);
3433 wl_list_insert(sibling->parent_link_pending.prev,
3434 &sub->parent_link_pending);
3435}
3436
3437static void
3438subsurface_place_below(struct wl_client *client,
3439 struct wl_resource *resource,
3440 struct wl_resource *sibling_resource)
3441{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003442 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003443 struct weston_surface *surface =
3444 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003445 struct weston_subsurface *sibling;
3446
3447 if (!sub)
3448 return;
3449
3450 sibling = subsurface_sibling_check(sub, surface, "place_below");
3451 if (!sibling)
3452 return;
3453
3454 wl_list_remove(&sub->parent_link_pending);
3455 wl_list_insert(&sibling->parent_link_pending,
3456 &sub->parent_link_pending);
3457}
3458
3459static void
3460subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3461{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003462 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003463
3464 if (sub)
3465 sub->synchronized = 1;
3466}
3467
3468static void
3469subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3470{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003471 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003472
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003473 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003474 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003475
3476 /* If sub became effectively desynchronized, flush. */
3477 if (!weston_subsurface_is_synchronized(sub))
3478 weston_subsurface_synchronized_commit(sub);
3479 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003480}
3481
3482static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003483weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3484{
3485 wl_list_remove(&sub->parent_link);
3486 wl_list_remove(&sub->parent_link_pending);
3487 wl_list_remove(&sub->parent_destroy_listener.link);
3488 sub->parent = NULL;
3489}
3490
3491static void
3492weston_subsurface_destroy(struct weston_subsurface *sub);
3493
3494static void
3495subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3496{
3497 struct weston_subsurface *sub =
3498 container_of(listener, struct weston_subsurface,
3499 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003500 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003501
3502 /* The protocol object (wl_resource) is left inert. */
3503 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003504 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003505
3506 weston_subsurface_destroy(sub);
3507}
3508
3509static void
3510subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3511{
3512 struct weston_subsurface *sub =
3513 container_of(listener, struct weston_subsurface,
3514 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003515 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003516 assert(sub->surface != sub->parent);
3517
3518 if (weston_surface_is_mapped(sub->surface))
3519 weston_surface_unmap(sub->surface);
3520
3521 weston_subsurface_unlink_parent(sub);
3522}
3523
3524static void
3525subsurface_resource_destroy(struct wl_resource *resource)
3526{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003527 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003528
3529 if (sub)
3530 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003531}
3532
3533static void
3534subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3535{
3536 wl_resource_destroy(resource);
3537}
3538
3539static void
3540weston_subsurface_link_parent(struct weston_subsurface *sub,
3541 struct weston_surface *parent)
3542{
3543 sub->parent = parent;
3544 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003545 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003546 &sub->parent_destroy_listener);
3547
3548 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3549 wl_list_insert(&parent->subsurface_list_pending,
3550 &sub->parent_link_pending);
3551}
3552
3553static void
3554weston_subsurface_link_surface(struct weston_subsurface *sub,
3555 struct weston_surface *surface)
3556{
3557 sub->surface = surface;
3558 sub->surface_destroy_listener.notify =
3559 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003560 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003561 &sub->surface_destroy_listener);
3562}
3563
3564static void
3565weston_subsurface_destroy(struct weston_subsurface *sub)
3566{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003567 struct weston_view *view, *next;
3568
Pekka Paalanene67858b2013-04-25 13:57:42 +03003569 assert(sub->surface);
3570
3571 if (sub->resource) {
3572 assert(weston_surface_to_subsurface(sub->surface) == sub);
3573 assert(sub->parent_destroy_listener.notify ==
3574 subsurface_handle_parent_destroy);
3575
George Kiagiadakised04d382014-06-13 18:10:26 +02003576 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3577 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003578 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003579 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003580
Pekka Paalanene67858b2013-04-25 13:57:42 +03003581 if (sub->parent)
3582 weston_subsurface_unlink_parent(sub);
3583
Jason Ekstrand7b982072014-05-20 14:33:03 -05003584 weston_surface_state_fini(&sub->cached);
3585 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003586
3587 sub->surface->configure = NULL;
3588 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003589 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003590 } else {
3591 /* the dummy weston_subsurface for the parent itself */
3592 assert(sub->parent_destroy_listener.notify == NULL);
3593 wl_list_remove(&sub->parent_link);
3594 wl_list_remove(&sub->parent_link_pending);
3595 }
3596
3597 wl_list_remove(&sub->surface_destroy_listener.link);
3598 free(sub);
3599}
3600
3601static const struct wl_subsurface_interface subsurface_implementation = {
3602 subsurface_destroy,
3603 subsurface_set_position,
3604 subsurface_place_above,
3605 subsurface_place_below,
3606 subsurface_set_sync,
3607 subsurface_set_desync
3608};
3609
3610static struct weston_subsurface *
3611weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3612 struct weston_surface *parent)
3613{
3614 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003615 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003616
Bryce Harringtonde16d892014-11-20 22:21:57 -08003617 sub = zalloc(sizeof *sub);
3618 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003619 return NULL;
3620
Jason Ekstranda7af7042013-10-12 22:38:11 -05003621 wl_list_init(&sub->unused_views);
3622
Jason Ekstranda85118c2013-06-27 20:17:02 -05003623 sub->resource =
3624 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003625 if (!sub->resource) {
3626 free(sub);
3627 return NULL;
3628 }
3629
Jason Ekstranda85118c2013-06-27 20:17:02 -05003630 wl_resource_set_implementation(sub->resource,
3631 &subsurface_implementation,
3632 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003633 weston_subsurface_link_surface(sub, surface);
3634 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003635 weston_surface_state_init(&sub->cached);
3636 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003637 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003638
3639 return sub;
3640}
3641
3642/* Create a dummy subsurface for having the parent itself in its
3643 * sub-surface lists. Makes stacking order manipulation easy.
3644 */
3645static struct weston_subsurface *
3646weston_subsurface_create_for_parent(struct weston_surface *parent)
3647{
3648 struct weston_subsurface *sub;
3649
Bryce Harringtonde16d892014-11-20 22:21:57 -08003650 sub = zalloc(sizeof *sub);
3651 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003652 return NULL;
3653
3654 weston_subsurface_link_surface(sub, parent);
3655 sub->parent = parent;
3656 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3657 wl_list_insert(&parent->subsurface_list_pending,
3658 &sub->parent_link_pending);
3659
3660 return sub;
3661}
3662
3663static void
3664subcompositor_get_subsurface(struct wl_client *client,
3665 struct wl_resource *resource,
3666 uint32_t id,
3667 struct wl_resource *surface_resource,
3668 struct wl_resource *parent_resource)
3669{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003670 struct weston_surface *surface =
3671 wl_resource_get_user_data(surface_resource);
3672 struct weston_surface *parent =
3673 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003674 struct weston_subsurface *sub;
3675 static const char where[] = "get_subsurface: wl_subsurface@";
3676
3677 if (surface == parent) {
3678 wl_resource_post_error(resource,
3679 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3680 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003681 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003682 return;
3683 }
3684
3685 if (weston_surface_to_subsurface(surface)) {
3686 wl_resource_post_error(resource,
3687 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3688 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003689 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003690 return;
3691 }
3692
Pekka Paalanen50b67472014-10-01 15:02:41 +03003693 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3694 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003695 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003696
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003697 if (weston_surface_get_main_surface(parent) == surface) {
3698 wl_resource_post_error(resource,
3699 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3700 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003701 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003702 return;
3703 }
3704
Pekka Paalanene67858b2013-04-25 13:57:42 +03003705 /* make sure the parent is in its own list */
3706 if (wl_list_empty(&parent->subsurface_list)) {
3707 if (!weston_subsurface_create_for_parent(parent)) {
3708 wl_resource_post_no_memory(resource);
3709 return;
3710 }
3711 }
3712
3713 sub = weston_subsurface_create(id, surface, parent);
3714 if (!sub) {
3715 wl_resource_post_no_memory(resource);
3716 return;
3717 }
3718
3719 surface->configure = subsurface_configure;
3720 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003721 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003722}
3723
3724static void
3725subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3726{
3727 wl_resource_destroy(resource);
3728}
3729
3730static const struct wl_subcompositor_interface subcompositor_interface = {
3731 subcompositor_destroy,
3732 subcompositor_get_subsurface
3733};
3734
3735static void
3736bind_subcompositor(struct wl_client *client,
3737 void *data, uint32_t version, uint32_t id)
3738{
3739 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003740 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003741
Jason Ekstranda85118c2013-06-27 20:17:02 -05003742 resource =
3743 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003744 if (resource == NULL) {
3745 wl_client_post_no_memory(client);
3746 return;
3747 }
3748 wl_resource_set_implementation(resource, &subcompositor_interface,
3749 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003750}
3751
3752static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003753weston_compositor_dpms(struct weston_compositor *compositor,
3754 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003755{
3756 struct weston_output *output;
3757
3758 wl_list_for_each(output, &compositor->output_list, link)
3759 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003760 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003761}
3762
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003763WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003764weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003765{
Neil Roberts8b62e202013-09-30 13:14:47 +01003766 uint32_t old_state = compositor->state;
3767
3768 /* The state needs to be changed before emitting the wake
3769 * signal because that may try to schedule a repaint which
3770 * will not work if the compositor is still sleeping */
3771 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3772
3773 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003774 case WESTON_COMPOSITOR_SLEEPING:
3775 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3776 /* fall through */
3777 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003778 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003779 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003780 /* fall through */
3781 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003782 wl_event_source_timer_update(compositor->idle_source,
3783 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003784 }
3785}
3786
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003787WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003788weston_compositor_offscreen(struct weston_compositor *compositor)
3789{
3790 switch (compositor->state) {
3791 case WESTON_COMPOSITOR_OFFSCREEN:
3792 return;
3793 case WESTON_COMPOSITOR_SLEEPING:
3794 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3795 /* fall through */
3796 default:
3797 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3798 wl_event_source_timer_update(compositor->idle_source, 0);
3799 }
3800}
3801
3802WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003803weston_compositor_sleep(struct weston_compositor *compositor)
3804{
3805 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3806 return;
3807
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003808 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003809 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3810 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3811}
3812
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003813static int
3814idle_handler(void *data)
3815{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003816 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003817
3818 if (compositor->idle_inhibit)
3819 return 1;
3820
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003821 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003822 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003823
3824 return 1;
3825}
3826
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003827WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003828weston_plane_init(struct weston_plane *plane,
3829 struct weston_compositor *ec,
3830 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003831{
3832 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003833 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003834 plane->x = x;
3835 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003836 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003837
3838 /* Init the link so that the call to wl_list_remove() when releasing
3839 * the plane without ever stacking doesn't lead to a crash */
3840 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003841}
3842
3843WL_EXPORT void
3844weston_plane_release(struct weston_plane *plane)
3845{
Xiong Zhang97116532013-10-23 13:58:31 +08003846 struct weston_view *view;
3847
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003848 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003849 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003850
Xiong Zhang97116532013-10-23 13:58:31 +08003851 wl_list_for_each(view, &plane->compositor->view_list, link) {
3852 if (view->plane == plane)
3853 view->plane = NULL;
3854 }
3855
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003856 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003857}
3858
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003859WL_EXPORT void
3860weston_compositor_stack_plane(struct weston_compositor *ec,
3861 struct weston_plane *plane,
3862 struct weston_plane *above)
3863{
3864 if (above)
3865 wl_list_insert(above->link.prev, &plane->link);
3866 else
3867 wl_list_insert(&ec->plane_list, &plane->link);
3868}
3869
Casey Dahlin9074db52012-04-19 22:50:09 -04003870static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003871{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003872 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003873}
3874
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003875static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003876bind_output(struct wl_client *client,
3877 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003878{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003879 struct weston_output *output = data;
3880 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003881 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003882
Jason Ekstranda85118c2013-06-27 20:17:02 -05003883 resource = wl_resource_create(client, &wl_output_interface,
3884 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003885 if (resource == NULL) {
3886 wl_client_post_no_memory(client);
3887 return;
3888 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003889
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003890 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003891 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003892
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003893 wl_output_send_geometry(resource,
3894 output->x,
3895 output->y,
3896 output->mm_width,
3897 output->mm_height,
3898 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003899 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003900 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003901 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003902 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003903 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003904
3905 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003906 wl_output_send_mode(resource,
3907 mode->flags,
3908 mode->width,
3909 mode->height,
3910 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003911 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003912
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003913 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003914 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003915}
3916
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003917/* Move other outputs when one is removed so the space remains contiguos. */
3918static void
3919weston_compositor_remove_output(struct weston_compositor *compositor,
3920 struct weston_output *remove_output)
3921{
3922 struct weston_output *output;
3923 int offset = 0;
3924
3925 wl_list_for_each(output, &compositor->output_list, link) {
3926 if (output == remove_output) {
3927 offset = output->width;
3928 continue;
3929 }
3930
3931 if (offset > 0) {
3932 weston_output_move(output,
3933 output->x - offset, output->y);
3934 output->dirty = 1;
3935 }
3936 }
3937}
3938
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003939WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003940weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003941{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003942 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003943 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003944
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003945 output->destroying = 1;
3946
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003947 wl_list_for_each(view, &output->compositor->view_list, link) {
3948 if (view->output_mask & (1 << output->id))
3949 weston_view_assign_output(view);
3950 }
3951
Pekka Paalanen0513a952014-05-21 16:17:27 +03003952 wl_event_source_remove(output->repaint_timer);
3953
Pekka Paalanen133e4392014-09-23 22:08:46 -04003954 weston_presentation_feedback_discard_list(&output->feedback_list);
3955
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003956 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003957 wl_list_remove(&output->link);
3958
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003959 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003960 wl_signal_emit(&output->destroy_signal, output);
3961
Richard Hughesafe690c2013-05-02 10:10:04 +01003962 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003963 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003964 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003965 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003966
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003967 wl_resource_for_each(resource, &output->resource_list) {
3968 wl_resource_set_destructor(resource, NULL);
3969 }
3970
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003971 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003972}
3973
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003974WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003975weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003976{
Scott Moreau850ca422012-05-21 15:21:25 -06003977 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003978
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003979 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003980 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003981
Scott Moreauccbf29d2012-02-22 14:21:41 -07003982 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003983 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003984 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003985 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003986 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003987 weston_matrix_scale(&output->matrix, magnification,
3988 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003989 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003990
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003991 switch (output->transform) {
3992 case WL_OUTPUT_TRANSFORM_FLIPPED:
3993 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3994 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3995 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3996 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3997 weston_matrix_scale(&output->matrix, -1, 1, 1);
3998 break;
3999 }
4000
4001 switch (output->transform) {
4002 default:
4003 case WL_OUTPUT_TRANSFORM_NORMAL:
4004 case WL_OUTPUT_TRANSFORM_FLIPPED:
4005 break;
4006 case WL_OUTPUT_TRANSFORM_90:
4007 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4008 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4009 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4010 break;
4011 case WL_OUTPUT_TRANSFORM_180:
4012 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4013 weston_matrix_translate(&output->matrix,
4014 -output->width, -output->height, 0);
4015 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4016 break;
4017 case WL_OUTPUT_TRANSFORM_270:
4018 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4019 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4020 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4021 break;
4022 }
4023
4024 if (output->current_scale != 1)
4025 weston_matrix_scale(&output->matrix,
4026 output->current_scale,
4027 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004028
Scott Moreauccbf29d2012-02-22 14:21:41 -07004029 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004030
4031 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004032}
4033
Scott Moreau1bad5db2012-08-18 01:04:05 -06004034static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004035weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004036{
4037 output->transform = transform;
4038
4039 switch (transform) {
4040 case WL_OUTPUT_TRANSFORM_90:
4041 case WL_OUTPUT_TRANSFORM_270:
4042 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4043 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4044 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02004045 output->width = output->current_mode->height;
4046 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004047 break;
4048 case WL_OUTPUT_TRANSFORM_NORMAL:
4049 case WL_OUTPUT_TRANSFORM_180:
4050 case WL_OUTPUT_TRANSFORM_FLIPPED:
4051 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02004052 output->width = output->current_mode->width;
4053 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004054 break;
4055 default:
4056 break;
4057 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06004058
Hardening57388e42013-09-18 23:56:36 +02004059 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02004060 output->width /= scale;
4061 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004062}
4063
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004064static void
4065weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004066{
4067 output->x = x;
4068 output->y = y;
4069
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004070 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004071 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004072 output->width,
4073 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004074}
4075
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004076WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004077weston_output_move(struct weston_output *output, int x, int y)
4078{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004079 struct wl_resource *resource;
4080
4081 output->move_x = x - output->x;
4082 output->move_y = y - output->y;
4083
4084 if (output->move_x == 0 && output->move_y == 0)
4085 return;
4086
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004087 weston_output_init_geometry(output, x, y);
4088
4089 output->dirty = 1;
4090
4091 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004092 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004093
4094 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004095 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004096 wl_output_send_geometry(resource,
4097 output->x,
4098 output->y,
4099 output->mm_width,
4100 output->mm_height,
4101 output->subpixel,
4102 output->make,
4103 output->model,
4104 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004105
4106 if (wl_resource_get_version(resource) >= 2)
4107 wl_output_send_done(resource);
4108 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004109}
4110
4111WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004112weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004113 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004114 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004115{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004116 struct wl_event_loop *loop;
4117
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004118 output->compositor = c;
4119 output->x = x;
4120 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004121 output->mm_width = mm_width;
4122 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004123 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004124 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004125
Alexander Larsson0b135062013-05-28 16:23:36 +02004126 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004127 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004128
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004129 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004130 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004131
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004132 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004133 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004134 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004135 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004136 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004137 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004138
Pekka Paalanen0513a952014-05-21 16:17:27 +03004139 loop = wl_display_get_event_loop(c->wl_display);
4140 output->repaint_timer = wl_event_loop_add_timer(loop,
4141 output_repaint_timer_handler, output);
4142
Casey Dahlin58ba1372012-04-19 22:50:08 -04004143 output->id = ffs(~output->compositor->output_id_pool) - 1;
4144 output->compositor->output_id_pool |= 1 << output->id;
4145
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004146 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004147 wl_global_create(c->wl_display, &wl_output_interface, 2,
4148 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004149}
4150
4151/** Adds an output to the compositor's output list and
4152 * send the compositor's output_created signal.
4153 *
4154 * \param compositor The compositor instance.
4155 * \param output The output to be added.
4156 */
4157WL_EXPORT void
4158weston_compositor_add_output(struct weston_compositor *compositor,
4159 struct weston_output *output)
4160{
4161 wl_list_insert(compositor->output_list.prev, &output->link);
4162 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004163}
4164
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004165WL_EXPORT void
4166weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004167 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004168 wl_fixed_t *x, wl_fixed_t *y)
4169{
Derek Foreman0f679412014-10-02 13:41:17 -05004170 struct weston_vector p = { {
4171 wl_fixed_to_double(device_x),
4172 wl_fixed_to_double(device_y),
4173 0.0,
4174 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004175
Derek Foreman67a18b92015-03-24 11:36:14 -05004176 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004177
Derek Foreman0f679412014-10-02 13:41:17 -05004178 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4179 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004180}
4181
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004182static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004183destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004184{
Jonny Lamb74130762013-11-26 18:19:46 +01004185 struct weston_surface *surface =
4186 wl_resource_get_user_data(resource);
4187
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004188 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004189 surface->pending.buffer_viewport.buffer.src_width =
4190 wl_fixed_from_int(-1);
4191 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004192 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004193}
4194
4195static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004196viewport_destroy(struct wl_client *client,
4197 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004198{
4199 wl_resource_destroy(resource);
4200}
4201
4202static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004203viewport_set(struct wl_client *client,
4204 struct wl_resource *resource,
4205 wl_fixed_t src_x,
4206 wl_fixed_t src_y,
4207 wl_fixed_t src_width,
4208 wl_fixed_t src_height,
4209 int32_t dst_width,
4210 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004211{
Jonny Lamb74130762013-11-26 18:19:46 +01004212 struct weston_surface *surface =
4213 wl_resource_get_user_data(resource);
4214
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004215 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004216
Jonny Lamb8ae35902013-11-26 18:19:45 +01004217 if (wl_fixed_to_double(src_width) < 0 ||
4218 wl_fixed_to_double(src_height) < 0) {
4219 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004220 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004221 "source dimensions must be non-negative (%fx%f)",
4222 wl_fixed_to_double(src_width),
4223 wl_fixed_to_double(src_height));
4224 return;
4225 }
4226
4227 if (dst_width <= 0 || dst_height <= 0) {
4228 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004229 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004230 "destination dimensions must be positive (%dx%d)",
4231 dst_width, dst_height);
4232 return;
4233 }
4234
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004235 surface->pending.buffer_viewport.buffer.src_x = src_x;
4236 surface->pending.buffer_viewport.buffer.src_y = src_y;
4237 surface->pending.buffer_viewport.buffer.src_width = src_width;
4238 surface->pending.buffer_viewport.buffer.src_height = src_height;
4239 surface->pending.buffer_viewport.surface.width = dst_width;
4240 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004241 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004242}
4243
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004244static void
4245viewport_set_source(struct wl_client *client,
4246 struct wl_resource *resource,
4247 wl_fixed_t src_x,
4248 wl_fixed_t src_y,
4249 wl_fixed_t src_width,
4250 wl_fixed_t src_height)
4251{
4252 struct weston_surface *surface =
4253 wl_resource_get_user_data(resource);
4254
4255 assert(surface->viewport_resource != NULL);
4256
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004257 if (src_width == wl_fixed_from_int(-1) &&
4258 src_height == wl_fixed_from_int(-1)) {
4259 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004260 surface->pending.buffer_viewport.buffer.src_width =
4261 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004262 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004263 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004264 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004265
4266 if (src_width <= 0 || src_height <= 0) {
4267 wl_resource_post_error(resource,
4268 WL_VIEWPORT_ERROR_BAD_VALUE,
4269 "source size must be positive (%fx%f)",
4270 wl_fixed_to_double(src_width),
4271 wl_fixed_to_double(src_height));
4272 return;
4273 }
4274
4275 surface->pending.buffer_viewport.buffer.src_x = src_x;
4276 surface->pending.buffer_viewport.buffer.src_y = src_y;
4277 surface->pending.buffer_viewport.buffer.src_width = src_width;
4278 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004279 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004280}
4281
4282static void
4283viewport_set_destination(struct wl_client *client,
4284 struct wl_resource *resource,
4285 int32_t dst_width,
4286 int32_t dst_height)
4287{
4288 struct weston_surface *surface =
4289 wl_resource_get_user_data(resource);
4290
4291 assert(surface->viewport_resource != NULL);
4292
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004293 if (dst_width == -1 && dst_height == -1) {
4294 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004295 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004296 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004297 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004298 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004299
4300 if (dst_width <= 0 || dst_height <= 0) {
4301 wl_resource_post_error(resource,
4302 WL_VIEWPORT_ERROR_BAD_VALUE,
4303 "destination size must be positive (%dx%d)",
4304 dst_width, dst_height);
4305 return;
4306 }
4307
4308 surface->pending.buffer_viewport.surface.width = dst_width;
4309 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004310 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004311}
4312
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004313static const struct wl_viewport_interface viewport_interface = {
4314 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004315 viewport_set,
4316 viewport_set_source,
4317 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004318};
4319
4320static void
4321scaler_destroy(struct wl_client *client,
4322 struct wl_resource *resource)
4323{
4324 wl_resource_destroy(resource);
4325}
4326
4327static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004328scaler_get_viewport(struct wl_client *client,
4329 struct wl_resource *scaler,
4330 uint32_t id,
4331 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004332{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004333 int version = wl_resource_get_version(scaler);
4334 struct weston_surface *surface =
4335 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004336 struct wl_resource *resource;
4337
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004338 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004339 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004340 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4341 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004342 return;
4343 }
4344
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004345 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004346 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004347 if (resource == NULL) {
4348 wl_client_post_no_memory(client);
4349 return;
4350 }
4351
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004352 wl_resource_set_implementation(resource, &viewport_interface,
4353 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004354
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004355 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004356}
4357
4358static const struct wl_scaler_interface scaler_interface = {
4359 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004360 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004361};
4362
4363static void
4364bind_scaler(struct wl_client *client,
4365 void *data, uint32_t version, uint32_t id)
4366{
4367 struct wl_resource *resource;
4368
4369 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004370 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004371 if (resource == NULL) {
4372 wl_client_post_no_memory(client);
4373 return;
4374 }
4375
4376 wl_resource_set_implementation(resource, &scaler_interface,
4377 NULL, NULL);
4378}
4379
4380static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004381destroy_presentation_feedback(struct wl_resource *feedback_resource)
4382{
4383 struct weston_presentation_feedback *feedback;
4384
4385 feedback = wl_resource_get_user_data(feedback_resource);
4386
4387 wl_list_remove(&feedback->link);
4388 free(feedback);
4389}
4390
4391static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004392presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4393{
4394 wl_resource_destroy(resource);
4395}
4396
4397static void
4398presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004399 struct wl_resource *presentation_resource,
4400 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004401 uint32_t callback)
4402{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004403 struct weston_surface *surface;
4404 struct weston_presentation_feedback *feedback;
4405
4406 surface = wl_resource_get_user_data(surface_resource);
4407
Bryce Harringtonde16d892014-11-20 22:21:57 -08004408 feedback = zalloc(sizeof *feedback);
4409 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004410 goto err_calloc;
4411
4412 feedback->resource = wl_resource_create(client,
4413 &presentation_feedback_interface,
4414 1, callback);
4415 if (!feedback->resource)
4416 goto err_create;
4417
4418 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4419 destroy_presentation_feedback);
4420
4421 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4422
4423 return;
4424
4425err_create:
4426 free(feedback);
4427
4428err_calloc:
4429 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004430}
4431
4432static const struct presentation_interface presentation_implementation = {
4433 presentation_destroy,
4434 presentation_feedback
4435};
4436
4437static void
4438bind_presentation(struct wl_client *client,
4439 void *data, uint32_t version, uint32_t id)
4440{
4441 struct weston_compositor *compositor = data;
4442 struct wl_resource *resource;
4443
4444 resource = wl_resource_create(client, &presentation_interface,
4445 MIN(version, 1), id);
4446 if (resource == NULL) {
4447 wl_client_post_no_memory(client);
4448 return;
4449 }
4450
4451 wl_resource_set_implementation(resource, &presentation_implementation,
4452 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004453 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004454}
4455
4456static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004457compositor_bind(struct wl_client *client,
4458 void *data, uint32_t version, uint32_t id)
4459{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004460 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004461 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004462
Jason Ekstranda85118c2013-06-27 20:17:02 -05004463 resource = wl_resource_create(client, &wl_compositor_interface,
4464 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004465 if (resource == NULL) {
4466 wl_client_post_no_memory(client);
4467 return;
4468 }
4469
4470 wl_resource_set_implementation(resource, &compositor_interface,
4471 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004472}
4473
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004474WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004475weston_environment_get_fd(const char *env)
4476{
4477 char *e, *end;
4478 int fd, flags;
4479
4480 e = getenv(env);
4481 if (!e)
4482 return -1;
4483 fd = strtol(e, &end, 0);
4484 if (*end != '\0')
4485 return -1;
4486
4487 flags = fcntl(fd, F_GETFD);
4488 if (flags == -1)
4489 return -1;
4490
4491 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4492 unsetenv(env);
4493
4494 return fd;
4495}
4496
Pekka Paalanenb5026542014-11-12 15:09:24 +02004497static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004498timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004499 uint32_t key, void *data)
4500{
4501 struct weston_compositor *compositor = data;
4502
4503 if (weston_timeline_enabled_)
4504 weston_timeline_close();
4505 else
4506 weston_timeline_open(compositor);
4507}
4508
Giulio Camuffo459137b2014-10-11 23:56:24 +03004509/** Create the compositor.
4510 *
4511 * This functions creates and initializes a compositor instance.
4512 *
4513 * \param display The Wayland display to be used.
4514 * \param user_data A pointer to an object that can later be retrieved
4515 * using the \ref weston_compositor_get_user_data function.
4516 * \return The compositor instance on success or NULL on failure.
4517 */
4518WL_EXPORT struct weston_compositor *
4519weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004520{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004521 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004522 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004523
Giulio Camuffo459137b2014-10-11 23:56:24 +03004524 ec = zalloc(sizeof *ec);
4525 if (!ec)
4526 return NULL;
4527
4528 ec->wl_display = display;
4529 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004530 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004531 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004532 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004533 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004534 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004535 wl_signal_init(&ec->idle_signal);
4536 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004537 wl_signal_init(&ec->show_input_panel_signal);
4538 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004539 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004540 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004541 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004542 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004543 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004544 wl_signal_init(&ec->session_signal);
4545 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004546
Casey Dahlin58ba1372012-04-19 22:50:08 -04004547 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004548 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004549
Giulio Camuffo954f1832014-10-11 18:27:30 +03004550 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004551 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004552 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004553
Giulio Camuffo954f1832014-10-11 18:27:30 +03004554 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004555 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004556 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004557
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004558 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004559 ec, bind_scaler))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004560 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004561
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004562 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4563 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004564 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004565
Jason Ekstranda7af7042013-10-12 22:38:11 -05004566 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004567 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004568 wl_list_init(&ec->layer_list);
4569 wl_list_init(&ec->seat_list);
4570 wl_list_init(&ec->output_list);
4571 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004572 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004573 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004574 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004575 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004576 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004577
Xiong Zhang97116532013-10-23 13:58:31 +08004578 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004579 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004580
Giulio Camuffo459137b2014-10-11 23:56:24 +03004581 wl_data_device_manager_init(ec->wl_display);
4582
4583 wl_display_init_shm(ec->wl_display);
4584
4585 loop = wl_display_get_event_loop(ec->wl_display);
4586 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004587
4588 ec->input_loop = wl_event_loop_create();
4589
4590 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4591 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4592
4593 weston_compositor_add_debug_binding(ec, KEY_T,
4594 timeline_key_binding_handler, ec);
4595
Giulio Camuffo459137b2014-10-11 23:56:24 +03004596 return ec;
4597
4598fail:
4599 free(ec);
4600 return NULL;
4601}
4602
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004603WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004604weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004605{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004606 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004607
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004608 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004609 if (ec->input_loop_source)
4610 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004611
Matt Roper361d2ad2011-08-29 13:52:23 -07004612 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004613 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004614 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004615
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004616 if (ec->renderer)
4617 ec->renderer->destroy(ec);
4618
Daniel Stone325fc2d2012-05-30 16:31:58 +01004619 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004620 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004621 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004622 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004623 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004624 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004625
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004626 weston_plane_release(&ec->primary_plane);
4627
Jonas Ådahlc97af922012-03-28 22:36:09 +02004628 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07004629}
4630
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004631WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004632weston_compositor_exit_with_code(struct weston_compositor *compositor,
4633 int exit_code)
4634{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004635 if (compositor->exit_code == EXIT_SUCCESS)
4636 compositor->exit_code = exit_code;
4637
Giulio Camuffo459137b2014-10-11 23:56:24 +03004638 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004639}
4640
4641WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004642weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4643 const struct weston_pointer_grab_interface *interface)
4644{
4645 struct weston_seat *seat;
4646
4647 ec->default_pointer_grab = interface;
4648 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004649 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4650
4651 if (pointer)
4652 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004653 }
4654}
4655
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004656WL_EXPORT int
4657weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4658 clockid_t clk_id)
4659{
4660 struct timespec ts;
4661
4662 if (clock_gettime(clk_id, &ts) < 0)
4663 return -1;
4664
4665 compositor->presentation_clock = clk_id;
4666
4667 return 0;
4668}
4669
4670/*
4671 * For choosing the software clock, when the display hardware or API
4672 * does not expose a compatible presentation timestamp.
4673 */
4674WL_EXPORT int
4675weston_compositor_set_presentation_clock_software(
4676 struct weston_compositor *compositor)
4677{
4678 /* In order of preference */
4679 static const clockid_t clocks[] = {
4680 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4681 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4682 CLOCK_MONOTONIC, /* no jumps, may crawl */
4683 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4684 CLOCK_REALTIME /* may jump and crawl */
4685 };
4686 unsigned i;
4687
4688 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4689 if (weston_compositor_set_presentation_clock(compositor,
4690 clocks[i]) == 0)
4691 return 0;
4692
4693 weston_log("Error: no suitable presentation clock available.\n");
4694
4695 return -1;
4696}
4697
Pekka Paalanen662f3842015-03-18 12:17:26 +02004698/** Read the current time from the Presentation clock
4699 *
4700 * \param compositor
4701 * \param ts[out] The current time.
4702 *
4703 * \note Reading the current time in user space is always imprecise to some
4704 * degree.
4705 *
4706 * This function is never meant to fail. If reading the clock does fail,
4707 * an error message is logged and a zero time is returned. Callers are not
4708 * supposed to detect or react to failures.
4709 */
4710WL_EXPORT void
4711weston_compositor_read_presentation_clock(
4712 const struct weston_compositor *compositor,
4713 struct timespec *ts)
4714{
4715 static bool warned;
4716 int ret;
4717
4718 ret = clock_gettime(compositor->presentation_clock, ts);
4719 if (ret < 0) {
4720 ts->tv_sec = 0;
4721 ts->tv_nsec = 0;
4722
4723 if (!warned)
4724 weston_log("Error: failure to read "
4725 "the presentation clock %#x: '%m' (%d)\n",
4726 compositor->presentation_clock, errno);
4727 warned = true;
4728 }
4729}
4730
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004731/** Import dmabuf buffer into current renderer
4732 *
4733 * \param compositor
4734 * \param buffer the dmabuf buffer to import
4735 * \return true on usable buffers, false otherwise
4736 *
4737 * This function tests that the linux_dmabuf_buffer is usable
4738 * for the current renderer. Returns false on unusable buffers. Usually
4739 * usability is tested by importing the dmabufs for composition.
4740 *
4741 * This hook is also used for detecting if the renderer supports
4742 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4743 * supported.
4744 * */
4745WL_EXPORT bool
4746weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4747 struct linux_dmabuf_buffer *buffer)
4748{
4749 struct weston_renderer *renderer;
4750
4751 renderer = compositor->renderer;
4752
4753 if (renderer->import_dmabuf == NULL)
4754 return false;
4755
4756 return renderer->import_dmabuf(compositor, buffer);
4757}
4758
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004759WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004760weston_version(int *major, int *minor, int *micro)
4761{
4762 *major = WESTON_VERSION_MAJOR;
4763 *minor = WESTON_VERSION_MINOR;
4764 *micro = WESTON_VERSION_MICRO;
4765}
4766
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004767WL_EXPORT void *
4768weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004769{
Derek Foreman3f86e502015-06-08 11:46:54 -05004770 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004771 char path[PATH_MAX];
4772 void *module, *init;
4773
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004774 if (name == NULL)
4775 return NULL;
4776
Derek Foreman3f86e502015-06-08 11:46:54 -05004777 if (name[0] != '/') {
4778 if (builddir)
4779 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4780 else
4781 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
4782 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004783 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004784 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004785
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004786 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4787 if (module) {
4788 weston_log("Module '%s' already loaded\n", path);
4789 dlclose(module);
4790 return NULL;
4791 }
4792
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004793 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004794 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004795 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004796 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004797 return NULL;
4798 }
4799
4800 init = dlsym(module, entrypoint);
4801 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004802 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004803 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004804 return NULL;
4805 }
4806
4807 return init;
4808}
4809
Giulio Camuffo459137b2014-10-11 23:56:24 +03004810
4811/** Destroys the compositor.
4812 *
4813 * This function cleans up the compositor state and destroys it.
4814 *
4815 * \param compositor The compositor to be destroyed.
4816 */
4817WL_EXPORT void
4818weston_compositor_destroy(struct weston_compositor *compositor)
4819{
4820 /* prevent further rendering while shutting down */
4821 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4822
4823 wl_signal_emit(&compositor->destroy_signal, compositor);
4824
4825 weston_compositor_xkb_destroy(compositor);
4826
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004827 if (compositor->backend)
4828 compositor->backend->destroy(compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004829 free(compositor);
4830}
4831
4832/** Instruct the compositor to exit.
4833 *
4834 * This functions does not directly destroy the compositor object, it merely
4835 * command it to start the tear down process. It is not guaranteed that the
4836 * tear down will happen immediately.
4837 *
4838 * \param compositor The compositor to tear down.
4839 */
4840WL_EXPORT void
4841weston_compositor_exit(struct weston_compositor *compositor)
4842{
4843 compositor->exit(compositor);
4844}
4845
4846/** Return the user data stored in the compositor.
4847 *
4848 * This function returns the user data pointer set with user_data parameter
4849 * to the \ref weston_compositor_create function.
4850 */
4851WL_EXPORT void *
4852weston_compositor_get_user_data(struct weston_compositor *compositor)
4853{
4854 return compositor->user_data;
4855}