blob: ee47a82706a8f9cf914669ba8bee37352b812160 [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 Paalanenb00c79b2016-02-18 16:53:27 +020058#include "presentation-time-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{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200446 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400447 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
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200476 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400477 }
478
479 secs = ts->tv_sec;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200480 wp_presentation_feedback_send_presented(feedback->resource,
481 secs >> 32, secs & 0xffffffff,
482 ts->tv_nsec,
483 refresh_nsec,
484 seq >> 32, seq & 0xffffffff,
485 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 Paalanenb00c79b2016-02-18 16:53:27 +0200499 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200500 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
Derek Foreman152254b2015-11-26 14:17:48 -0600528 pixman_region32_init(&state->damage_surface);
529 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500530 pixman_region32_init(&state->opaque);
531 region_init_infinite(&state->input);
532
533 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400534 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500535
536 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
537 state->buffer_viewport.buffer.scale = 1;
538 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
539 state->buffer_viewport.surface.width = -1;
540 state->buffer_viewport.changed = 0;
541}
542
543static void
544weston_surface_state_fini(struct weston_surface_state *state)
545{
546 struct weston_frame_callback *cb, *next;
547
548 wl_list_for_each_safe(cb, next,
549 &state->frame_callback_list, link)
550 wl_resource_destroy(cb->resource);
551
Pekka Paalanen133e4392014-09-23 22:08:46 -0400552 weston_presentation_feedback_discard_list(&state->feedback_list);
553
Jason Ekstrand7b982072014-05-20 14:33:03 -0500554 pixman_region32_fini(&state->input);
555 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600556 pixman_region32_fini(&state->damage_surface);
557 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500558
559 if (state->buffer)
560 wl_list_remove(&state->buffer_destroy_listener.link);
561 state->buffer = NULL;
562}
563
564static void
565weston_surface_state_set_buffer(struct weston_surface_state *state,
566 struct weston_buffer *buffer)
567{
568 if (state->buffer == buffer)
569 return;
570
571 if (state->buffer)
572 wl_list_remove(&state->buffer_destroy_listener.link);
573 state->buffer = buffer;
574 if (state->buffer)
575 wl_signal_add(&state->buffer->destroy_signal,
576 &state->buffer_destroy_listener);
577}
578
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500579WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500580weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500581{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500582 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400583
Bryce Harringtonde16d892014-11-20 22:21:57 -0800584 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400585 if (surface == NULL)
586 return NULL;
587
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500588 wl_signal_init(&surface->destroy_signal);
589
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500590 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200591 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400592
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200593 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
594 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200595 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
596 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500597
598 weston_surface_state_init(&surface->pending);
599
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400600 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500601 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300602 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400603
Jason Ekstranda7af7042013-10-12 22:38:11 -0500604 wl_list_init(&surface->views);
605
606 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400607 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500608
Pekka Paalanene67858b2013-04-25 13:57:42 +0300609 wl_list_init(&surface->subsurface_list);
610 wl_list_init(&surface->subsurface_list_pending);
611
Jason Ekstrand1e059042014-10-16 10:55:19 -0500612 weston_matrix_init(&surface->buffer_to_surface_matrix);
613 weston_matrix_init(&surface->surface_to_buffer_matrix);
614
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400615 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500616}
617
Alex Wu8811bf92012-02-28 18:07:54 +0800618WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500619weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200620 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500621{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100622 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500623}
624
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400625WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500626weston_view_to_global_float(struct weston_view *view,
627 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200628{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500629 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200630 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
631
Jason Ekstranda7af7042013-10-12 22:38:11 -0500632 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200633
634 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200635 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700636 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200637 v.f[3]);
638 *x = 0;
639 *y = 0;
640 return;
641 }
642
643 *x = v.f[0] / v.f[3];
644 *y = v.f[1] / v.f[3];
645 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500646 *x = sx + view->geometry.x;
647 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200648 }
649}
650
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500651WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200652weston_transformed_coord(int width, int height,
653 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200654 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200655 float sx, float sy, float *bx, float *by)
656{
657 switch (transform) {
658 case WL_OUTPUT_TRANSFORM_NORMAL:
659 default:
660 *bx = sx;
661 *by = sy;
662 break;
663 case WL_OUTPUT_TRANSFORM_FLIPPED:
664 *bx = width - sx;
665 *by = sy;
666 break;
667 case WL_OUTPUT_TRANSFORM_90:
668 *bx = height - sy;
669 *by = sx;
670 break;
671 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
672 *bx = height - sy;
673 *by = width - sx;
674 break;
675 case WL_OUTPUT_TRANSFORM_180:
676 *bx = width - sx;
677 *by = height - sy;
678 break;
679 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
680 *bx = sx;
681 *by = height - sy;
682 break;
683 case WL_OUTPUT_TRANSFORM_270:
684 *bx = sy;
685 *by = width - sx;
686 break;
687 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
688 *bx = sy;
689 *by = sx;
690 break;
691 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200692
693 *bx *= scale;
694 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200695}
696
697WL_EXPORT pixman_box32_t
698weston_transformed_rect(int width, int height,
699 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200700 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200701 pixman_box32_t rect)
702{
703 float x1, x2, y1, y2;
704
705 pixman_box32_t ret;
706
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.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200709 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200710 rect.x2, rect.y2, &x2, &y2);
711
712 if (x1 <= x2) {
713 ret.x1 = x1;
714 ret.x2 = x2;
715 } else {
716 ret.x1 = x2;
717 ret.x2 = x1;
718 }
719
720 if (y1 <= y2) {
721 ret.y1 = y1;
722 ret.y2 = y2;
723 } else {
724 ret.y1 = y2;
725 ret.y2 = y1;
726 }
727
728 return ret;
729}
730
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600731/** Transform a region by a matrix, restricted to axis-aligned transformations
732 *
733 * Warning: This function does not work for projective, affine, or matrices
734 * that encode arbitrary rotations. Only 90-degree step rotations are
735 * supported.
736 */
737WL_EXPORT void
738weston_matrix_transform_region(pixman_region32_t *dest,
739 struct weston_matrix *matrix,
740 pixman_region32_t *src)
741{
742 pixman_box32_t *src_rects, *dest_rects;
743 int nrects, i;
744
745 src_rects = pixman_region32_rectangles(src, &nrects);
746 dest_rects = malloc(nrects * sizeof(*dest_rects));
747 if (!dest_rects)
748 return;
749
750 for (i = 0; i < nrects; i++) {
751 struct weston_vector vec1 = {{
752 src_rects[i].x1, src_rects[i].y1, 0, 1
753 }};
754 weston_matrix_transform(matrix, &vec1);
755 vec1.f[0] /= vec1.f[3];
756 vec1.f[1] /= vec1.f[3];
757
758 struct weston_vector vec2 = {{
759 src_rects[i].x2, src_rects[i].y2, 0, 1
760 }};
761 weston_matrix_transform(matrix, &vec2);
762 vec2.f[0] /= vec2.f[3];
763 vec2.f[1] /= vec2.f[3];
764
765 if (vec1.f[0] < vec2.f[0]) {
766 dest_rects[i].x1 = floor(vec1.f[0]);
767 dest_rects[i].x2 = ceil(vec2.f[0]);
768 } else {
769 dest_rects[i].x1 = floor(vec2.f[0]);
770 dest_rects[i].x2 = ceil(vec1.f[0]);
771 }
772
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600773 if (vec1.f[1] < vec2.f[1]) {
774 dest_rects[i].y1 = floor(vec1.f[1]);
775 dest_rects[i].y2 = ceil(vec2.f[1]);
776 } else {
777 dest_rects[i].y1 = floor(vec2.f[1]);
778 dest_rects[i].y2 = ceil(vec1.f[1]);
779 }
780 }
781
782 pixman_region32_clear(dest);
783 pixman_region32_init_rects(dest, dest_rects, nrects);
784 free(dest_rects);
785}
786
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200787WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500788weston_transformed_region(int width, int height,
789 enum wl_output_transform transform,
790 int32_t scale,
791 pixman_region32_t *src, pixman_region32_t *dest)
792{
793 pixman_box32_t *src_rects, *dest_rects;
794 int nrects, i;
795
796 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
797 if (src != dest)
798 pixman_region32_copy(dest, src);
799 return;
800 }
801
802 src_rects = pixman_region32_rectangles(src, &nrects);
803 dest_rects = malloc(nrects * sizeof(*dest_rects));
804 if (!dest_rects)
805 return;
806
807 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
808 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
809 } else {
810 for (i = 0; i < nrects; i++) {
811 switch (transform) {
812 default:
813 case WL_OUTPUT_TRANSFORM_NORMAL:
814 dest_rects[i].x1 = src_rects[i].x1;
815 dest_rects[i].y1 = src_rects[i].y1;
816 dest_rects[i].x2 = src_rects[i].x2;
817 dest_rects[i].y2 = src_rects[i].y2;
818 break;
819 case WL_OUTPUT_TRANSFORM_90:
820 dest_rects[i].x1 = height - src_rects[i].y2;
821 dest_rects[i].y1 = src_rects[i].x1;
822 dest_rects[i].x2 = height - src_rects[i].y1;
823 dest_rects[i].y2 = src_rects[i].x2;
824 break;
825 case WL_OUTPUT_TRANSFORM_180:
826 dest_rects[i].x1 = width - src_rects[i].x2;
827 dest_rects[i].y1 = height - src_rects[i].y2;
828 dest_rects[i].x2 = width - src_rects[i].x1;
829 dest_rects[i].y2 = height - src_rects[i].y1;
830 break;
831 case WL_OUTPUT_TRANSFORM_270:
832 dest_rects[i].x1 = src_rects[i].y1;
833 dest_rects[i].y1 = width - src_rects[i].x2;
834 dest_rects[i].x2 = src_rects[i].y2;
835 dest_rects[i].y2 = width - src_rects[i].x1;
836 break;
837 case WL_OUTPUT_TRANSFORM_FLIPPED:
838 dest_rects[i].x1 = width - src_rects[i].x2;
839 dest_rects[i].y1 = src_rects[i].y1;
840 dest_rects[i].x2 = width - src_rects[i].x1;
841 dest_rects[i].y2 = src_rects[i].y2;
842 break;
843 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
844 dest_rects[i].x1 = height - src_rects[i].y2;
845 dest_rects[i].y1 = width - src_rects[i].x2;
846 dest_rects[i].x2 = height - src_rects[i].y1;
847 dest_rects[i].y2 = width - src_rects[i].x1;
848 break;
849 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
850 dest_rects[i].x1 = src_rects[i].x1;
851 dest_rects[i].y1 = height - src_rects[i].y2;
852 dest_rects[i].x2 = src_rects[i].x2;
853 dest_rects[i].y2 = height - src_rects[i].y1;
854 break;
855 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
856 dest_rects[i].x1 = src_rects[i].y1;
857 dest_rects[i].y1 = src_rects[i].x1;
858 dest_rects[i].x2 = src_rects[i].y2;
859 dest_rects[i].y2 = src_rects[i].x2;
860 break;
861 }
862 }
863 }
864
865 if (scale != 1) {
866 for (i = 0; i < nrects; i++) {
867 dest_rects[i].x1 *= scale;
868 dest_rects[i].x2 *= scale;
869 dest_rects[i].y1 *= scale;
870 dest_rects[i].y2 *= scale;
871 }
872 }
873
874 pixman_region32_clear(dest);
875 pixman_region32_init_rects(dest, dest_rects, nrects);
876 free(dest_rects);
877}
878
Jonny Lamb74130762013-11-26 18:19:46 +0100879static void
880scaler_surface_to_buffer(struct weston_surface *surface,
881 float sx, float sy, float *bx, float *by)
882{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200883 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200884 double src_width, src_height;
885 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200886
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200887 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
888 if (vp->surface.width == -1) {
889 *bx = sx;
890 *by = sy;
891 return;
892 }
Jonny Lamb74130762013-11-26 18:19:46 +0100893
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200894 src_x = 0.0;
895 src_y = 0.0;
896 src_width = surface->width_from_buffer;
897 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100898 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200899 src_x = wl_fixed_to_double(vp->buffer.src_x);
900 src_y = wl_fixed_to_double(vp->buffer.src_y);
901 src_width = wl_fixed_to_double(vp->buffer.src_width);
902 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100903 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200904
905 *bx = sx * src_width / surface->width + src_x;
906 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100907}
908
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500909WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200910weston_surface_to_buffer_float(struct weston_surface *surface,
911 float sx, float sy, float *bx, float *by)
912{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200913 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
914
Jonny Lamb74130762013-11-26 18:19:46 +0100915 /* first transform coordinates if the scaler is set */
916 scaler_surface_to_buffer(surface, sx, sy, bx, by);
917
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500918 weston_transformed_coord(surface->width_from_buffer,
919 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200920 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100921 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200922}
923
Derek Foreman99739672015-12-03 16:38:11 -0600924/** Transform a rectangle from surface coordinates to buffer coordinates
925 *
926 * \param surface The surface to fetch wl_viewport and buffer transformation
927 * from.
928 * \param rect The rectangle to transform.
929 * \return The transformed rectangle.
930 *
931 * Viewport and buffer transformations can only do translation, scaling,
932 * and rotations in 90-degree steps. Therefore the only loss in the
933 * conversion is coordinate rounding.
934 *
935 * However, some coordinate rounding takes place as an intermediate
936 * step before the buffer scale factor is applied, so the rectangle
937 * boundary may not be exactly as expected.
938 *
939 * This is OK for damage tracking since a little extra coverage is
940 * not a problem.
941 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200942WL_EXPORT pixman_box32_t
943weston_surface_to_buffer_rect(struct weston_surface *surface,
944 pixman_box32_t rect)
945{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200946 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100947 float xf, yf;
948
949 /* first transform box coordinates if the scaler is set */
950 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
951 rect.x1 = floorf(xf);
952 rect.y1 = floorf(yf);
953
954 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600955 rect.x2 = ceilf(xf);
956 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100957
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500958 return weston_transformed_rect(surface->width_from_buffer,
959 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200960 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200961 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200962}
963
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200964/** Transform a region from surface coordinates to buffer coordinates
965 *
966 * \param surface The surface to fetch wl_viewport and buffer transformation
967 * from.
968 * \param surface_region[in] The region in surface coordinates.
969 * \param buffer_region[out] The region converted to buffer coordinates.
970 *
971 * Buffer_region must be init'd, but will be completely overwritten.
972 *
973 * Viewport and buffer transformations can only do translation, scaling,
974 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600975 * conversion is from the coordinate rounding that takes place in
976 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200977 */
978WL_EXPORT void
979weston_surface_to_buffer_region(struct weston_surface *surface,
980 pixman_region32_t *surface_region,
981 pixman_region32_t *buffer_region)
982{
983 pixman_box32_t *src_rects, *dest_rects;
984 int nrects, i;
985
986 src_rects = pixman_region32_rectangles(surface_region, &nrects);
987 dest_rects = malloc(nrects * sizeof(*dest_rects));
988 if (!dest_rects)
989 return;
990
991 for (i = 0; i < nrects; i++) {
992 dest_rects[i] = weston_surface_to_buffer_rect(surface,
993 src_rects[i]);
994 }
995
996 pixman_region32_fini(buffer_region);
997 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
998 free(dest_rects);
999}
1000
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001001WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001003 struct weston_plane *plane)
1004{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001006 return;
1007
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 weston_view_damage_below(view);
1009 view->plane = plane;
1010 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001011}
1012
Pekka Paalanen51723d52015-02-17 13:10:01 +02001013/** Inflict damage on the plane where the view is visible.
1014 *
1015 * \param view The view that causes the damage.
1016 *
1017 * If the view is currently on a plane (including the primary plane),
1018 * take the view's boundingbox, subtract all the opaque views that cover it,
1019 * and add the remaining region as damage to the plane. This corresponds
1020 * to the damage inflicted to the plane if this view disappeared.
1021 *
1022 * A repaint is scheduled for this view.
1023 *
1024 * The region of all opaque views covering this view is stored in
1025 * weston_view::clip and updated by view_accumulate_damage() during
1026 * weston_output_repaint(). Specifically, that region matches the
1027 * scenegraph as it was last painted.
1028 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001029WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001030weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001031{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001032 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001033
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001034 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001035 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001037 if (view->plane)
1038 pixman_region32_union(&view->plane->damage,
1039 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001040 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001041 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001042}
1043
Bryce Harrington3f650b82015-12-23 11:01:58 -08001044/**
1045 * \param es The surface
1046 * \param mask The new set of outputs for the surface
1047 *
1048 * Sets the surface's set of outputs to the ones specified by
1049 * the new output mask provided. Identifies the outputs that
1050 * have changed, the posts enter and leave events for these
1051 * outputs as appropriate.
1052 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001053static void
1054weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1055{
1056 uint32_t different = es->output_mask ^ mask;
1057 uint32_t entered = mask & different;
1058 uint32_t left = es->output_mask & different;
1059 struct weston_output *output;
1060 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001061 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001062
1063 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001064 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001065 return;
1066 if (different == 0)
1067 return;
1068
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001069 client = wl_resource_get_client(es->resource);
1070
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001071 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08001072 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001073 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001074 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001075 client);
1076 if (resource == NULL)
1077 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -08001078 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001079 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -08001080 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001081 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001082 }
1083}
1084
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001085
Bryce Harrington3f650b82015-12-23 11:01:58 -08001086/** Recalculate which output(s) the surface has views displayed on
1087 *
1088 * \param es The surface to remap to outputs
1089 *
1090 * Finds the output that is showing the largest amount of one
1091 * of the surface's various views. This output becomes the
1092 * surface's primary output for vsync and frame event purposes.
1093 *
1094 * Also notes the primary outputs of all of the surface's views
1095 * in the output_mask for the surface.
1096 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001097static void
1098weston_surface_assign_output(struct weston_surface *es)
1099{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100 struct weston_output *new_output;
1101 struct weston_view *view;
1102 pixman_region32_t region;
1103 uint32_t max, area, mask;
1104 pixman_box32_t *e;
1105
1106 new_output = NULL;
1107 max = 0;
1108 mask = 0;
1109 pixman_region32_init(&region);
1110 wl_list_for_each(view, &es->views, surface_link) {
1111 if (!view->output)
1112 continue;
1113
1114 pixman_region32_intersect(&region, &view->transform.boundingbox,
1115 &view->output->region);
1116
1117 e = pixman_region32_extents(&region);
1118 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1119
1120 mask |= view->output_mask;
1121
1122 if (area >= max) {
1123 new_output = view->output;
1124 max = area;
1125 }
1126 }
1127 pixman_region32_fini(&region);
1128
1129 es->output = new_output;
1130 weston_surface_update_output_mask(es, mask);
1131}
1132
Bryce Harrington3f650b82015-12-23 11:01:58 -08001133/** Recalculate which output(s) the view is displayed on
1134 *
1135 * \param ev The view to remap to outputs
1136 *
1137 * Identifies the set of outputs that the view is visible on,
1138 * noting them into the output_mask. The output that the view
1139 * is most visible on is set as the view's primary output for
1140 * vsync and frame event purposes.
1141 *
1142 * Also does the same for the view's surface. See
1143 * weston_surface_assign_output().
1144 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145static void
1146weston_view_assign_output(struct weston_view *ev)
1147{
1148 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001149 struct weston_output *output, *new_output;
1150 pixman_region32_t region;
1151 uint32_t max, area, mask;
1152 pixman_box32_t *e;
1153
1154 new_output = NULL;
1155 max = 0;
1156 mask = 0;
1157 pixman_region32_init(&region);
1158 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001159 if (output->destroying)
1160 continue;
1161
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001163 &output->region);
1164
1165 e = pixman_region32_extents(&region);
1166 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1167
1168 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001169 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001170
1171 if (area >= max) {
1172 new_output = output;
1173 max = area;
1174 }
1175 }
1176 pixman_region32_fini(&region);
1177
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 ev->output = new_output;
1179 ev->output_mask = mask;
1180
1181 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001182}
1183
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001184static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001185weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1186 int from_x, int from_y, int *to_x, int *to_y)
1187{
1188 float x, y;
1189
1190 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1191 weston_view_from_global_float(to, x, y, &x, &y);
1192
1193 *to_x = round(x);
1194 *to_y = round(y);
1195}
1196
1197static void
1198weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1199{
1200 pixman_box32_t *a;
1201 pixman_box32_t b;
1202
1203 a = pixman_region32_extents(&from->geometry.scissor);
1204
1205 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1206 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1207
1208 pixman_region32_fini(&to->geometry.scissor);
1209 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1210}
1211
1212static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001213view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001215{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001216 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1217 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001218 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001219 { inbox->x1, inbox->y1 },
1220 { inbox->x1, inbox->y2 },
1221 { inbox->x2, inbox->y1 },
1222 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001223 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001224 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001225 int i;
1226
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001227 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001228 /* avoid rounding empty bbox to 1x1 */
1229 pixman_region32_init(bbox);
1230 return;
1231 }
1232
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001233 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001234 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001236 if (x < min_x)
1237 min_x = x;
1238 if (x > max_x)
1239 max_x = x;
1240 if (y < min_y)
1241 min_y = y;
1242 if (y > max_y)
1243 max_y = y;
1244 }
1245
Pekka Paalanen219b9822012-02-08 15:38:37 +02001246 int_x = floorf(min_x);
1247 int_y = floorf(min_y);
1248 pixman_region32_init_rect(bbox, int_x, int_y,
1249 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001250}
1251
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001252static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001254{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001256
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001257 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 view->geometry.x = roundf(view->geometry.x);
1259 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001260
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001261 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1263 view->transform.position.matrix.d[12] = view->geometry.x;
1264 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001265
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001267
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 view->transform.inverse = view->transform.position.matrix;
1269 view->transform.inverse.d[12] = -view->geometry.x;
1270 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001271
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001273 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001274 view->surface->width,
1275 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001276 if (view->geometry.scissor_enabled)
1277 pixman_region32_intersect(&view->transform.boundingbox,
1278 &view->transform.boundingbox,
1279 &view->geometry.scissor);
1280
1281 pixman_region32_translate(&view->transform.boundingbox,
1282 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001283
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 if (view->alpha == 1.0) {
1285 pixman_region32_copy(&view->transform.opaque,
1286 &view->surface->opaque);
1287 pixman_region32_translate(&view->transform.opaque,
1288 view->geometry.x,
1289 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001290 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001291}
1292
1293static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001295{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001296 struct weston_view *parent = view->geometry.parent;
1297 struct weston_matrix *matrix = &view->transform.matrix;
1298 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001299 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001300 pixman_region32_t surfregion;
1301 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001302
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001304
1305 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1307 view->transform.position.matrix.d[12] = view->geometry.x;
1308 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001309
1310 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001312 weston_matrix_multiply(matrix, &tform->matrix);
1313
Pekka Paalanen483243f2013-03-08 14:56:50 +02001314 if (parent)
1315 weston_matrix_multiply(matrix, &parent->transform.matrix);
1316
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001317 if (weston_matrix_invert(inverse, matrix) < 0) {
1318 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 weston_log("error: weston_view %p"
1320 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001321 return -1;
1322 }
1323
Pekka Paalanen380adf52015-02-16 14:39:11 +02001324 pixman_region32_init_rect(&surfregion, 0, 0,
1325 view->surface->width, view->surface->height);
1326 if (view->geometry.scissor_enabled)
1327 pixman_region32_intersect(&surfregion, &surfregion,
1328 &view->geometry.scissor);
1329 surfbox = pixman_region32_extents(&surfregion);
1330
1331 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1332 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001333
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001334 return 0;
1335}
1336
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001337static struct weston_layer *
1338get_view_layer(struct weston_view *view)
1339{
1340 if (view->parent_view)
1341 return get_view_layer(view->parent_view);
1342 return view->layer_link.layer;
1343}
1344
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001345WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001347{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001349 struct weston_layer *layer;
1350 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001351
Jason Ekstranda7af7042013-10-12 22:38:11 -05001352 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001353 return;
1354
Pekka Paalanen483243f2013-03-08 14:56:50 +02001355 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001356 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001357
Jason Ekstranda7af7042013-10-12 22:38:11 -05001358 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001359
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001361
Jason Ekstranda7af7042013-10-12 22:38:11 -05001362 pixman_region32_fini(&view->transform.boundingbox);
1363 pixman_region32_fini(&view->transform.opaque);
1364 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001365
Pekka Paalanencd403622012-01-25 13:37:39 +02001366 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 if (view->geometry.transformation_list.next ==
1368 &view->transform.position.link &&
1369 view->geometry.transformation_list.prev ==
1370 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001371 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001373 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374 if (weston_view_update_transform_enable(view) < 0)
1375 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001376 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001377
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001378 layer = get_view_layer(view);
1379 if (layer) {
1380 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001381 pixman_region32_intersect(&view->transform.boundingbox,
1382 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001383 pixman_region32_intersect(&view->transform.opaque,
1384 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001385 pixman_region32_fini(&mask);
1386 }
1387
Pekka Paalanen380adf52015-02-16 14:39:11 +02001388 if (parent) {
1389 if (parent->geometry.scissor_enabled) {
1390 view->geometry.scissor_enabled = true;
1391 weston_view_transfer_scissor(parent, view);
1392 } else {
1393 view->geometry.scissor_enabled = false;
1394 }
1395 }
1396
Jason Ekstranda7af7042013-10-12 22:38:11 -05001397 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001398
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001400
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 wl_signal_emit(&view->surface->compositor->transform_signal,
1402 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001403}
1404
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001405WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001407{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001409
1410 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 * The invariant: if view->geometry.dirty, then all views
1412 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001413 * Corollary: if not parent->geometry.dirty, then all ancestors
1414 * are not dirty.
1415 */
1416
Jason Ekstranda7af7042013-10-12 22:38:11 -05001417 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001418 return;
1419
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001421
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001423 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001425}
1426
1427WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428weston_view_to_global_fixed(struct weston_view *view,
1429 wl_fixed_t vx, wl_fixed_t vy,
1430 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001431{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001432 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001433
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 weston_view_to_global_float(view,
1435 wl_fixed_to_double(vx),
1436 wl_fixed_to_double(vy),
1437 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001438 *x = wl_fixed_from_double(xf);
1439 *y = wl_fixed_from_double(yf);
1440}
1441
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001442WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001443weston_view_from_global_float(struct weston_view *view,
1444 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001445{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001447 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1448
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001450
1451 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001452 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001454 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 *vx = 0;
1456 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001457 return;
1458 }
1459
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460 *vx = v.f[0] / v.f[3];
1461 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001462 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 *vx = x - view->geometry.x;
1464 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001465 }
1466}
1467
1468WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469weston_view_from_global_fixed(struct weston_view *view,
1470 wl_fixed_t x, wl_fixed_t y,
1471 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001472{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001474
Jason Ekstranda7af7042013-10-12 22:38:11 -05001475 weston_view_from_global_float(view,
1476 wl_fixed_to_double(x),
1477 wl_fixed_to_double(y),
1478 &vxf, &vyf);
1479 *vx = wl_fixed_from_double(vxf);
1480 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001481}
1482
1483WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001484weston_view_from_global(struct weston_view *view,
1485 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001486{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001487 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001488
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1490 *vx = floorf(vxf);
1491 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001492}
1493
Bryce Harrington3f650b82015-12-23 11:01:58 -08001494/**
1495 * \param surface The surface to be repainted
1496 *
1497 * Marks the output(s) that the surface is shown on as needing to be
1498 * repainted. See weston_output_schedule_repaint().
1499 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001500WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001501weston_surface_schedule_repaint(struct weston_surface *surface)
1502{
1503 struct weston_output *output;
1504
1505 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001506 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001507 weston_output_schedule_repaint(output);
1508}
1509
Bryce Harrington3f650b82015-12-23 11:01:58 -08001510/**
1511 * \param view The view to be repainted
1512 *
1513 * Marks the output(s) that the view is shown on as needing to be
1514 * repainted. See weston_output_schedule_repaint().
1515 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001516WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001517weston_view_schedule_repaint(struct weston_view *view)
1518{
1519 struct weston_output *output;
1520
1521 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001522 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 weston_output_schedule_repaint(output);
1524}
1525
Pekka Paalanene508ce62015-02-19 13:59:55 +02001526/**
1527 * XXX: This function does it the wrong way.
1528 * surface->damage is the damage from the client, and causes
1529 * surface_flush_damage() to copy pixels. No window management action can
1530 * cause damage to the client-provided content, warranting re-upload!
1531 *
1532 * Instead of surface->damage, this function should record the damage
1533 * with all the views for this surface to avoid extraneous texture
1534 * uploads.
1535 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001536WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001537weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001538{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001539 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001540 0, 0, surface->width,
1541 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001542
Kristian Høgsberg98238702012-08-03 16:29:12 -04001543 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001544}
1545
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001546WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001548{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001549 if (view->geometry.x == x && view->geometry.y == y)
1550 return;
1551
Jason Ekstranda7af7042013-10-12 22:38:11 -05001552 view->geometry.x = x;
1553 view->geometry.y = y;
1554 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001555}
1556
Pekka Paalanen483243f2013-03-08 14:56:50 +02001557static void
1558transform_parent_handle_parent_destroy(struct wl_listener *listener,
1559 void *data)
1560{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001561 struct weston_view *view =
1562 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001563 geometry.parent_destroy_listener);
1564
Jason Ekstranda7af7042013-10-12 22:38:11 -05001565 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001566}
1567
1568WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001569weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001570 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001571{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572 if (view->geometry.parent) {
1573 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1574 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001575
1576 if (!parent)
1577 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001578 }
1579
Jason Ekstranda7af7042013-10-12 22:38:11 -05001580 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001581
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001583 transform_parent_handle_parent_destroy;
1584 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001585 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001587 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001589 }
1590
Jason Ekstranda7af7042013-10-12 22:38:11 -05001591 weston_view_geometry_dirty(view);
1592}
1593
Pekka Paalanen380adf52015-02-16 14:39:11 +02001594/** Set a clip mask rectangle on a view
1595 *
1596 * \param view The view to set the clip mask on.
1597 * \param x Top-left corner X coordinate of the clip rectangle.
1598 * \param y Top-left corner Y coordinate of the clip rectangle.
1599 * \param width Width of the clip rectangle, non-negative.
1600 * \param height Height of the clip rectangle, non-negative.
1601 *
1602 * A shell may set a clip mask rectangle on a view. Everything outside
1603 * the rectangle is cut away for input and output purposes: it is
1604 * not drawn and cannot be hit by hit-test based input like pointer
1605 * motion or touch-downs. Everything inside the rectangle will behave
1606 * normally. Clients are unaware of clipping.
1607 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001608 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001609 * mask rectangle does not affect the view position, the view is positioned
1610 * as it would be without a clip. The clip also does not change
1611 * weston_surface::width,height.
1612 *
1613 * The clip mask rectangle is part of transformation inheritance
1614 * (weston_view_set_transform_parent()). A clip set in the root of the
1615 * transformation inheritance tree will affect all views in the tree.
1616 * A clip can be set only on the root view. Attempting to set a clip
1617 * on view that has a transformation parent will fail. Assigning a parent
1618 * to a view that has a clip set will cause the clip to be forgotten.
1619 *
1620 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1621 * on the additional transformations in the child views. These transformations
1622 * may not rotate the coordinate axes, i.e., only translation and scaling
1623 * are allowed. Violating this restriction causes the clipping to malfunction.
1624 * Furthermore, using scaling may cause rounding errors in child clipping.
1625 *
1626 * The clip mask rectangle is not automatically adjusted based on
1627 * wl_surface.attach dx and dy arguments.
1628 *
1629 * A clip mask rectangle can be set only if the compositor capability
1630 * WESTON_CAP_VIEW_CLIP_MASK is present.
1631 *
1632 * This function sets the clip mask rectangle and schedules a repaint for
1633 * the view.
1634 */
1635WL_EXPORT void
1636weston_view_set_mask(struct weston_view *view,
1637 int x, int y, int width, int height)
1638{
1639 struct weston_compositor *compositor = view->surface->compositor;
1640
1641 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1642 weston_log("%s not allowed without capability!\n", __func__);
1643 return;
1644 }
1645
1646 if (view->geometry.parent) {
1647 weston_log("view %p has a parent, clip forbidden!\n", view);
1648 return;
1649 }
1650
1651 if (width < 0 || height < 0) {
1652 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1653 x, y, width, height);
1654 return;
1655 }
1656
1657 pixman_region32_fini(&view->geometry.scissor);
1658 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1659 view->geometry.scissor_enabled = true;
1660 weston_view_geometry_dirty(view);
1661 weston_view_schedule_repaint(view);
1662}
1663
1664/** Remove the clip mask from a view
1665 *
1666 * \param view The view to remove the clip mask from.
1667 *
1668 * Removed the clip mask rectangle and schedules a repaint.
1669 *
1670 * \sa weston_view_set_mask
1671 */
1672WL_EXPORT void
1673weston_view_set_mask_infinite(struct weston_view *view)
1674{
1675 view->geometry.scissor_enabled = false;
1676 weston_view_geometry_dirty(view);
1677 weston_view_schedule_repaint(view);
1678}
1679
Derek Foreman280e7dd2014-10-03 13:13:42 -05001680WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001681weston_view_is_mapped(struct weston_view *view)
1682{
1683 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001684 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001685 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001686 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001687}
1688
Derek Foreman280e7dd2014-10-03 13:13:42 -05001689WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001690weston_surface_is_mapped(struct weston_surface *surface)
1691{
1692 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001693 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001694 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001695 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001696}
1697
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001698static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001699surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001700{
1701 struct weston_view *view;
1702
1703 if (surface->width == width && surface->height == height)
1704 return;
1705
1706 surface->width = width;
1707 surface->height = height;
1708
1709 wl_list_for_each(view, &surface->views, surface_link)
1710 weston_view_geometry_dirty(view);
1711}
1712
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001713WL_EXPORT void
1714weston_surface_set_size(struct weston_surface *surface,
1715 int32_t width, int32_t height)
1716{
1717 assert(!surface->resource);
1718 surface_set_size(surface, width, height);
1719}
1720
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001721static int
1722fixed_round_up_to_int(wl_fixed_t f)
1723{
1724 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1725}
1726
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001727static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001728convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1729 int32_t width, int32_t height,
1730 uint32_t transform,
1731 int32_t scale)
1732{
1733 assert(scale > 0);
1734
1735 switch (transform) {
1736 case WL_OUTPUT_TRANSFORM_NORMAL:
1737 case WL_OUTPUT_TRANSFORM_180:
1738 case WL_OUTPUT_TRANSFORM_FLIPPED:
1739 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1740 *width_out = width / scale;
1741 *height_out = height / scale;
1742 break;
1743 case WL_OUTPUT_TRANSFORM_90:
1744 case WL_OUTPUT_TRANSFORM_270:
1745 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1746 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1747 *width_out = height / scale;
1748 *height_out = width / scale;
1749 break;
1750 default:
1751 assert(0 && "invalid transform");
1752 }
1753}
1754
1755static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001756weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001757{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001758 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001759
1760 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001761 surface->width_from_buffer = 0;
1762 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001763 return;
1764 }
1765
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001766 convert_size_by_transform_scale(&surface->width_from_buffer,
1767 &surface->height_from_buffer,
1768 surface->buffer_ref.buffer->width,
1769 surface->buffer_ref.buffer->height,
1770 vp->buffer.transform,
1771 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001772}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001773
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001774static void
1775weston_surface_update_size(struct weston_surface *surface)
1776{
1777 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1778 int32_t width, height;
1779
1780 width = surface->width_from_buffer;
1781 height = surface->height_from_buffer;
1782
1783 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001784 surface_set_size(surface,
1785 vp->surface.width, vp->surface.height);
1786 return;
1787 }
1788
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001789 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001790 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1791 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1792
1793 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001794 return;
1795 }
1796
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001797 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001798}
1799
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001800WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001801weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001802{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001803 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001804
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001805 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001806
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001807 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001808}
1809
Jason Ekstranda7af7042013-10-12 22:38:11 -05001810WL_EXPORT struct weston_view *
1811weston_compositor_pick_view(struct weston_compositor *compositor,
1812 wl_fixed_t x, wl_fixed_t y,
1813 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001814{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001816 wl_fixed_t view_x, view_y;
1817 int view_ix, view_iy;
1818 int ix = wl_fixed_to_int(x);
1819 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001822 if (!pixman_region32_contains_point(
1823 &view->transform.boundingbox, ix, iy, NULL))
1824 continue;
1825
1826 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1827 view_ix = wl_fixed_to_int(view_x);
1828 view_iy = wl_fixed_to_int(view_y);
1829
1830 if (!pixman_region32_contains_point(&view->surface->input,
1831 view_ix, view_iy, NULL))
1832 continue;
1833
Pekka Paalanen380adf52015-02-16 14:39:11 +02001834 if (view->geometry.scissor_enabled &&
1835 !pixman_region32_contains_point(&view->geometry.scissor,
1836 view_ix, view_iy, NULL))
1837 continue;
1838
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001839 *vx = view_x;
1840 *vy = view_y;
1841 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001842 }
1843
Derek Foremanf9318d12015-05-11 15:40:11 -05001844 *vx = wl_fixed_from_int(-1000000);
1845 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001846 return NULL;
1847}
1848
1849static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001850weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001851{
Daniel Stone37816df2012-05-16 18:45:18 +01001852 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001853
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001854 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001855 return;
1856
Daniel Stone37816df2012-05-16 18:45:18 +01001857 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001858 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001859}
1860
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001861WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001862weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001863{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001864 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001865
Jason Ekstranda7af7042013-10-12 22:38:11 -05001866 if (!weston_view_is_mapped(view))
1867 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001868
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 weston_view_damage_below(view);
1870 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001871 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001872 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001873 wl_list_remove(&view->link);
1874 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001875 view->output_mask = 0;
1876 weston_surface_assign_output(view->surface);
1877
1878 if (weston_surface_is_mapped(view->surface))
1879 return;
1880
1881 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001882 struct weston_touch *touch = weston_seat_get_touch(seat);
1883 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1884 struct weston_keyboard *keyboard =
1885 weston_seat_get_keyboard(seat);
1886
1887 if (keyboard && keyboard->focus == view->surface)
1888 weston_keyboard_set_focus(keyboard, NULL);
1889 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001890 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001891 if (touch && touch->focus == view)
1892 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001893 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001894}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001895
Jason Ekstranda7af7042013-10-12 22:38:11 -05001896WL_EXPORT void
1897weston_surface_unmap(struct weston_surface *surface)
1898{
1899 struct weston_view *view;
1900
1901 wl_list_for_each(view, &surface->views, surface_link)
1902 weston_view_unmap(view);
1903 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001904}
1905
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001906static void
1907weston_surface_reset_pending_buffer(struct weston_surface *surface)
1908{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001909 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001910 surface->pending.sx = 0;
1911 surface->pending.sy = 0;
1912 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001913 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001914}
1915
Jason Ekstranda7af7042013-10-12 22:38:11 -05001916WL_EXPORT void
1917weston_view_destroy(struct weston_view *view)
1918{
1919 wl_signal_emit(&view->destroy_signal, view);
1920
1921 assert(wl_list_empty(&view->geometry.child_list));
1922
1923 if (weston_view_is_mapped(view)) {
1924 weston_view_unmap(view);
1925 weston_compositor_build_view_list(view->surface->compositor);
1926 }
1927
1928 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001929 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001930
1931 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001932 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001933 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001934 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001935
1936 weston_view_set_transform_parent(view, NULL);
1937
Jason Ekstranda7af7042013-10-12 22:38:11 -05001938 wl_list_remove(&view->surface_link);
1939
1940 free(view);
1941}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001942
1943WL_EXPORT void
1944weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001945{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001946 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001947 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001948
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001949 if (--surface->ref_count > 0)
1950 return;
1951
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001952 assert(surface->resource == NULL);
1953
Pekka Paalanenca790762015-04-17 14:23:38 +03001954 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001955
Pekka Paalanene67858b2013-04-25 13:57:42 +03001956 assert(wl_list_empty(&surface->subsurface_list_pending));
1957 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001958
Jason Ekstranda7af7042013-10-12 22:38:11 -05001959 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1960 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001961
Jason Ekstrand7b982072014-05-20 14:33:03 -05001962 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001963
Pekka Paalanende685b82012-12-04 15:58:12 +02001964 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001965
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001966 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001967 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001968 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001969
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001970 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001971 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001972
Pekka Paalanen133e4392014-09-23 22:08:46 -04001973 weston_presentation_feedback_discard_list(&surface->feedback_list);
1974
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001975 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001976}
1977
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001978static void
1979destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001980{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001981 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001982
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001983 assert(surface);
1984
Giulio Camuffo0d379742013-11-15 22:06:15 +01001985 /* Set the resource to NULL, since we don't want to leave a
1986 * dangling pointer if the surface was refcounted and survives
1987 * the weston_surface_destroy() call. */
1988 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001989 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001990}
1991
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001992static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001993weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1994{
1995 struct weston_buffer *buffer =
1996 container_of(listener, struct weston_buffer, destroy_listener);
1997
1998 wl_signal_emit(&buffer->destroy_signal, buffer);
1999 free(buffer);
2000}
2001
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002002WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002003weston_buffer_from_resource(struct wl_resource *resource)
2004{
2005 struct weston_buffer *buffer;
2006 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002007
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002008 listener = wl_resource_get_destroy_listener(resource,
2009 weston_buffer_destroy_handler);
2010
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002011 if (listener)
2012 return container_of(listener, struct weston_buffer,
2013 destroy_listener);
2014
2015 buffer = zalloc(sizeof *buffer);
2016 if (buffer == NULL)
2017 return NULL;
2018
2019 buffer->resource = resource;
2020 wl_signal_init(&buffer->destroy_signal);
2021 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002022 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002023 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002024
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002025 return buffer;
2026}
2027
2028static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002029weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2030 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002031{
Pekka Paalanende685b82012-12-04 15:58:12 +02002032 struct weston_buffer_reference *ref =
2033 container_of(listener, struct weston_buffer_reference,
2034 destroy_listener);
2035
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002036 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002037 ref->buffer = NULL;
2038}
2039
2040WL_EXPORT void
2041weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002042 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002043{
2044 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002045 ref->buffer->busy_count--;
2046 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002047 assert(wl_resource_get_client(ref->buffer->resource));
2048 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05002049 WL_BUFFER_RELEASE);
2050 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002051 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002052 }
2053
Pekka Paalanende685b82012-12-04 15:58:12 +02002054 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002055 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002056 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002057 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002058 }
2059
Pekka Paalanende685b82012-12-04 15:58:12 +02002060 ref->buffer = buffer;
2061 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2062}
2063
2064static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002065weston_surface_attach(struct weston_surface *surface,
2066 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002067{
2068 weston_buffer_reference(&surface->buffer_ref, buffer);
2069
Pekka Paalanena6421c42012-12-04 15:58:10 +02002070 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002071 if (weston_surface_is_mapped(surface))
2072 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002073 }
2074
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002075 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002076
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002077 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002078 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002079}
2080
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002081WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002082weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002083{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002084 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002085
2086 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002087 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002088}
2089
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002090WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002091weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002092{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002093 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002094
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002095 pixman_region32_union(&compositor->primary_plane.damage,
2096 &compositor->primary_plane.damage,
2097 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002098 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002099}
2100
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002101static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002102surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002103{
Pekka Paalanende685b82012-12-04 15:58:12 +02002104 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002105 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002106 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002107
Pekka Paalanenb5026542014-11-12 15:09:24 +02002108 if (weston_timeline_enabled_ &&
2109 pixman_region32_not_empty(&surface->damage))
2110 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2111 TLP_OUTPUT(surface->output), TLP_END);
2112
Jason Ekstrandef540082014-06-26 10:37:36 -07002113 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002114}
2115
2116static void
2117view_accumulate_damage(struct weston_view *view,
2118 pixman_region32_t *opaque)
2119{
2120 pixman_region32_t damage;
2121
2122 pixman_region32_init(&damage);
2123 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002124 pixman_box32_t *extents;
2125
Jason Ekstranda7af7042013-10-12 22:38:11 -05002126 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002127 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002128 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002129 pixman_region32_copy(&damage, &view->surface->damage);
2130 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002131 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002132 }
2133
Pekka Paalanen380adf52015-02-16 14:39:11 +02002134 pixman_region32_intersect(&damage, &damage,
2135 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136 pixman_region32_subtract(&damage, &damage, opaque);
2137 pixman_region32_union(&view->plane->damage,
2138 &view->plane->damage, &damage);
2139 pixman_region32_fini(&damage);
2140 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002141 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002142}
2143
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002144static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002145compositor_accumulate_damage(struct weston_compositor *ec)
2146{
2147 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002148 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002149 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002150
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002151 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002152
2153 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002154 pixman_region32_copy(&plane->clip, &clip);
2155
2156 pixman_region32_init(&opaque);
2157
Jason Ekstranda7af7042013-10-12 22:38:11 -05002158 wl_list_for_each(ev, &ec->view_list, link) {
2159 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002160 continue;
2161
Jason Ekstranda7af7042013-10-12 22:38:11 -05002162 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002163 }
2164
2165 pixman_region32_union(&clip, &clip, &opaque);
2166 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002167 }
2168
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002169 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002170
Jason Ekstranda7af7042013-10-12 22:38:11 -05002171 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002172 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002173
2174 wl_list_for_each(ev, &ec->view_list, link) {
2175 if (ev->surface->touched)
2176 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002177 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178
2179 surface_flush_damage(ev->surface);
2180
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002181 /* Both the renderer and the backend have seen the buffer
2182 * by now. If renderer needs the buffer, it has its own
2183 * reference set. If the backend wants to keep the buffer
2184 * around for migrating the surface into a non-primary plane
2185 * later, keep_buffer is true. Otherwise, drop the core
2186 * reference now, and allow early buffer release. This enables
2187 * clients to use single-buffering.
2188 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002189 if (!ev->surface->keep_buffer)
2190 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002191 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002192}
2193
2194static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002195surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002196{
2197 struct weston_subsurface *sub;
2198
Pekka Paalanene67858b2013-04-25 13:57:42 +03002199 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002200 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002201 continue;
2202
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2204 wl_list_init(&sub->surface->views);
2205
2206 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002207 }
2208}
2209
2210static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002211surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002212{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002213 struct weston_subsurface *sub;
2214 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002215
Jason Ekstranda7af7042013-10-12 22:38:11 -05002216 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2217 if (sub->surface == surface)
2218 continue;
2219
George Kiagiadakised04d382014-06-13 18:10:26 +02002220 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2221 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002222 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002223 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002224
2225 surface_free_unused_subsurface_views(sub->surface);
2226 }
2227}
2228
2229static void
2230view_list_add_subsurface_view(struct weston_compositor *compositor,
2231 struct weston_subsurface *sub,
2232 struct weston_view *parent)
2233{
2234 struct weston_subsurface *child;
2235 struct weston_view *view = NULL, *iv;
2236
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002237 if (!weston_surface_is_mapped(sub->surface))
2238 return;
2239
Jason Ekstranda7af7042013-10-12 22:38:11 -05002240 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2241 if (iv->geometry.parent == parent) {
2242 view = iv;
2243 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002244 }
2245 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002246
2247 if (view) {
2248 /* Put it back in the surface's list of views */
2249 wl_list_remove(&view->surface_link);
2250 wl_list_insert(&sub->surface->views, &view->surface_link);
2251 } else {
2252 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002253 weston_view_set_position(view,
2254 sub->position.x,
2255 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002256 weston_view_set_transform_parent(view, parent);
2257 }
2258
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002259 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002260 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002261
Pekka Paalanenb188e912013-11-19 14:03:35 +02002262 if (wl_list_empty(&sub->surface->subsurface_list)) {
2263 wl_list_insert(compositor->view_list.prev, &view->link);
2264 return;
2265 }
2266
2267 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2268 if (child->surface == sub->surface)
2269 wl_list_insert(compositor->view_list.prev, &view->link);
2270 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002271 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002272 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002273}
2274
2275static void
2276view_list_add(struct weston_compositor *compositor,
2277 struct weston_view *view)
2278{
2279 struct weston_subsurface *sub;
2280
2281 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002282
Pekka Paalanenb188e912013-11-19 14:03:35 +02002283 if (wl_list_empty(&view->surface->subsurface_list)) {
2284 wl_list_insert(compositor->view_list.prev, &view->link);
2285 return;
2286 }
2287
2288 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2289 if (sub->surface == view->surface)
2290 wl_list_insert(compositor->view_list.prev, &view->link);
2291 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002292 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002293 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002294}
2295
2296static void
2297weston_compositor_build_view_list(struct weston_compositor *compositor)
2298{
2299 struct weston_view *view;
2300 struct weston_layer *layer;
2301
2302 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002303 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002304 surface_stash_subsurface_views(view->surface);
2305
2306 wl_list_init(&compositor->view_list);
2307 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002308 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002309 view_list_add(compositor, view);
2310 }
2311 }
2312
2313 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002314 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002315 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002316}
2317
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002318static void
2319weston_output_take_feedback_list(struct weston_output *output,
2320 struct weston_surface *surface)
2321{
2322 struct weston_view *view;
2323 struct weston_presentation_feedback *feedback;
2324 uint32_t flags = 0xffffffff;
2325
2326 if (wl_list_empty(&surface->feedback_list))
2327 return;
2328
2329 /* All views must have the flag for the flag to survive. */
2330 wl_list_for_each(view, &surface->views, surface_link) {
2331 /* ignore views that are not on this output at all */
2332 if (view->output_mask & (1u << output->id))
2333 flags &= view->psf_flags;
2334 }
2335
2336 wl_list_for_each(feedback, &surface->feedback_list, link)
2337 feedback->psf_flags = flags;
2338
2339 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2340 wl_list_init(&surface->feedback_list);
2341}
2342
David Herrmann1edf44c2013-10-22 17:11:26 +02002343static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002344weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002345{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002346 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002347 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002348 struct weston_animation *animation, *next;
2349 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002350 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002351 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002352 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002353
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002354 if (output->destroying)
2355 return 0;
2356
Pekka Paalanenb5026542014-11-12 15:09:24 +02002357 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2358
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002359 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002360 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002361
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002362 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002363 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002364 } else {
2365 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002366 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002367 ev->psf_flags = 0;
2368 }
2369 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002370
Pekka Paalanene67858b2013-04-25 13:57:42 +03002371 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002372 wl_list_for_each(ev, &ec->view_list, link) {
2373 /* Note: This operation is safe to do multiple times on the
2374 * same surface.
2375 */
2376 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002377 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002378 &ev->surface->frame_callback_list);
2379 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002380
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002381 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002382 }
2383 }
2384
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002385 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002386
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002387 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002388 pixman_region32_intersect(&output_damage,
2389 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002390 pixman_region32_subtract(&output_damage,
2391 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002392
Scott Moreauccbf29d2012-02-22 14:21:41 -07002393 if (output->dirty)
2394 weston_output_update_matrix(output);
2395
David Herrmann1edf44c2013-10-22 17:11:26 +02002396 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002397
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002398 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002399
Kristian Høgsbergef044142011-06-21 15:02:12 -04002400 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002401
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002402 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002403
Jonas Ådahldb773762012-06-13 00:01:21 +02002404 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002405 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002406 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002407 }
2408
Scott Moreaud64cf212012-06-08 19:40:54 -06002409 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002410 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002411 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002412 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002413
Pekka Paalanenb5026542014-11-12 15:09:24 +02002414 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2415
David Herrmann1edf44c2013-10-22 17:11:26 +02002416 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002417}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002418
Pekka Paalanen82919792014-05-21 13:51:49 +03002419static void
2420weston_output_schedule_repaint_reset(struct weston_output *output)
2421{
Pekka Paalanen82919792014-05-21 13:51:49 +03002422 output->repaint_scheduled = 0;
2423 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002424}
2425
Pekka Paalanen0513a952014-05-21 16:17:27 +03002426static int
2427output_repaint_timer_handler(void *data)
2428{
2429 struct weston_output *output = data;
2430 struct weston_compositor *compositor = output->compositor;
2431
2432 if (output->repaint_needed &&
2433 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2434 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2435 weston_output_repaint(output) == 0)
2436 return 0;
2437
2438 weston_output_schedule_repaint_reset(output);
2439
2440 return 0;
2441}
2442
Kristian Høgsbergef044142011-06-21 15:02:12 -04002443WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002444weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002445 const struct timespec *stamp,
2446 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002447{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002448 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002449 int32_t refresh_nsec;
2450 struct timespec now;
2451 struct timespec gone;
2452 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002453
Pekka Paalanenb5026542014-11-12 15:09:24 +02002454 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2455 TLP_VBLANK(stamp), TLP_END);
2456
Pekka Paalanend7894d02015-07-03 15:08:53 +03002457 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002458 weston_presentation_feedback_present_list(&output->feedback_list,
2459 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002460 output->msc,
2461 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002462
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002463 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002464
Pekka Paalanen0513a952014-05-21 16:17:27 +03002465 weston_compositor_read_presentation_clock(compositor, &now);
2466 timespec_sub(&gone, &now, stamp);
2467 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2468 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002469
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002470 if (msec < -1000 || msec > 1000) {
2471 static bool warned;
2472
2473 if (!warned)
2474 weston_log("Warning: computed repaint delay is "
2475 "insane: %d msec\n", msec);
2476 warned = true;
2477
2478 msec = 0;
2479 }
2480
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002481 /* Called from restart_repaint_loop and restart happens already after
2482 * the deadline given by repaint_msec? In that case we delay until
2483 * the deadline of the next frame, to give clients a more predictable
2484 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002485 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002486 msec += refresh_nsec / 1000000;
2487
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002488 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002489 output_repaint_timer_handler(output);
2490 else
2491 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002492}
2493
2494static void
2495idle_repaint(void *data)
2496{
2497 struct weston_output *output = data;
2498
Jonas Ådahle5a12252013-04-05 23:07:11 +02002499 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002500}
2501
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002502WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002503weston_layer_entry_insert(struct weston_layer_entry *list,
2504 struct weston_layer_entry *entry)
2505{
2506 wl_list_insert(&list->link, &entry->link);
2507 entry->layer = list->layer;
2508}
2509
2510WL_EXPORT void
2511weston_layer_entry_remove(struct weston_layer_entry *entry)
2512{
2513 wl_list_remove(&entry->link);
2514 wl_list_init(&entry->link);
2515 entry->layer = NULL;
2516}
2517
2518WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002519weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2520{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002521 wl_list_init(&layer->view_list.link);
2522 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002523 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002524 if (below != NULL)
2525 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002526}
2527
2528WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002529weston_layer_set_mask(struct weston_layer *layer,
2530 int x, int y, int width, int height)
2531{
2532 struct weston_view *view;
2533
2534 layer->mask.x1 = x;
2535 layer->mask.x2 = x + width;
2536 layer->mask.y1 = y;
2537 layer->mask.y2 = y + height;
2538
2539 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2540 weston_view_geometry_dirty(view);
2541 }
2542}
2543
2544WL_EXPORT void
2545weston_layer_set_mask_infinite(struct weston_layer *layer)
2546{
2547 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2548 UINT32_MAX, UINT32_MAX);
2549}
2550
2551WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002552weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002553{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002554 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002555 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002556
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002557 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2558 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002559 return;
2560
Pekka Paalanenb5026542014-11-12 15:09:24 +02002561 if (!output->repaint_needed)
2562 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2563
Kristian Høgsbergef044142011-06-21 15:02:12 -04002564 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002565 output->repaint_needed = 1;
2566 if (output->repaint_scheduled)
2567 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002568
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002569 wl_event_loop_add_idle(loop, idle_repaint, output);
2570 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002571 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002572}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002573
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002574WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002575weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2576{
2577 struct weston_output *output;
2578
2579 wl_list_for_each(output, &compositor->output_list, link)
2580 weston_output_schedule_repaint(output);
2581}
2582
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002583static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002584surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002585{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002586 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002587}
2588
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002589static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002590surface_attach(struct wl_client *client,
2591 struct wl_resource *resource,
2592 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2593{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002594 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002595 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002596
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002597 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002598 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002599 if (buffer == NULL) {
2600 wl_client_post_no_memory(client);
2601 return;
2602 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002603 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002604
Pekka Paalanende685b82012-12-04 15:58:12 +02002605 /* Attach, attach, without commit in between does not send
2606 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002607 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002608
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002609 surface->pending.sx = sx;
2610 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002611 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002612}
2613
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002614static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002615surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002616 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002617 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002618{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002619 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002620
Derek Foreman57e92ed2015-11-17 14:11:35 -06002621 if (width <= 0 || height <= 0)
2622 return;
2623
Derek Foreman152254b2015-11-26 14:17:48 -06002624 pixman_region32_union_rect(&surface->pending.damage_surface,
2625 &surface->pending.damage_surface,
2626 x, y, width, height);
2627}
2628
2629static void
2630surface_damage_buffer(struct wl_client *client,
2631 struct wl_resource *resource,
2632 int32_t x, int32_t y, int32_t width, int32_t height)
2633{
2634 struct weston_surface *surface = wl_resource_get_user_data(resource);
2635
2636 if (width <= 0 || height <= 0)
2637 return;
2638
2639 pixman_region32_union_rect(&surface->pending.damage_buffer,
2640 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002641 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002642}
2643
Kristian Høgsberg33418202011-08-16 23:01:28 -04002644static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002645destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002646{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002647 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002648
2649 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002650 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002651}
2652
2653static void
2654surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002655 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002656{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002657 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002658 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002659
2660 cb = malloc(sizeof *cb);
2661 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002662 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002663 return;
2664 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002665
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002666 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2667 callback);
2668 if (cb->resource == NULL) {
2669 free(cb);
2670 wl_resource_post_no_memory(resource);
2671 return;
2672 }
2673
Jason Ekstranda85118c2013-06-27 20:17:02 -05002674 wl_resource_set_implementation(cb->resource, NULL, cb,
2675 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002676
Pekka Paalanenbc106382012-10-10 12:49:31 +03002677 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002678}
2679
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002680static void
2681surface_set_opaque_region(struct wl_client *client,
2682 struct wl_resource *resource,
2683 struct wl_resource *region_resource)
2684{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002685 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002686 struct weston_region *region;
2687
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002688 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002689 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002690 pixman_region32_copy(&surface->pending.opaque,
2691 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002692 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002693 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002694 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002695}
2696
2697static void
2698surface_set_input_region(struct wl_client *client,
2699 struct wl_resource *resource,
2700 struct wl_resource *region_resource)
2701{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002702 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002703 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002704
2705 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002706 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002707 pixman_region32_copy(&surface->pending.input,
2708 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002709 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002710 pixman_region32_fini(&surface->pending.input);
2711 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002712 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002713}
2714
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002715static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002716weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002717{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002718 struct weston_subsurface *sub;
2719
2720 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2721 parent_link_pending) {
2722 wl_list_remove(&sub->parent_link);
2723 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2724 }
2725}
2726
2727static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002728weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002729 struct weston_matrix *matrix)
2730{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002731 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002732 double src_width, src_height, dest_width, dest_height;
2733
2734 weston_matrix_init(matrix);
2735
2736 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2737 src_width = surface->width_from_buffer;
2738 src_height = surface->height_from_buffer;
2739 } else {
2740 src_width = wl_fixed_to_double(vp->buffer.src_width);
2741 src_height = wl_fixed_to_double(vp->buffer.src_height);
2742 }
2743
2744 if (vp->surface.width == -1) {
2745 dest_width = src_width;
2746 dest_height = src_height;
2747 } else {
2748 dest_width = vp->surface.width;
2749 dest_height = vp->surface.height;
2750 }
2751
2752 if (src_width != dest_width || src_height != dest_height)
2753 weston_matrix_scale(matrix,
2754 src_width / dest_width,
2755 src_height / dest_height, 1);
2756
2757 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2758 weston_matrix_translate(matrix,
2759 wl_fixed_to_double(vp->buffer.src_x),
2760 wl_fixed_to_double(vp->buffer.src_y),
2761 0);
2762
2763 switch (vp->buffer.transform) {
2764 case WL_OUTPUT_TRANSFORM_FLIPPED:
2765 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2766 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2767 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2768 weston_matrix_scale(matrix, -1, 1, 1);
2769 weston_matrix_translate(matrix,
2770 surface->width_from_buffer, 0, 0);
2771 break;
2772 }
2773
2774 switch (vp->buffer.transform) {
2775 default:
2776 case WL_OUTPUT_TRANSFORM_NORMAL:
2777 case WL_OUTPUT_TRANSFORM_FLIPPED:
2778 break;
2779 case WL_OUTPUT_TRANSFORM_90:
2780 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2781 weston_matrix_rotate_xy(matrix, 0, 1);
2782 weston_matrix_translate(matrix,
2783 surface->height_from_buffer, 0, 0);
2784 break;
2785 case WL_OUTPUT_TRANSFORM_180:
2786 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2787 weston_matrix_rotate_xy(matrix, -1, 0);
2788 weston_matrix_translate(matrix,
2789 surface->width_from_buffer,
2790 surface->height_from_buffer, 0);
2791 break;
2792 case WL_OUTPUT_TRANSFORM_270:
2793 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2794 weston_matrix_rotate_xy(matrix, 0, -1);
2795 weston_matrix_translate(matrix,
2796 0, surface->width_from_buffer, 0);
2797 break;
2798 }
2799
2800 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2801}
2802
Derek Foreman152254b2015-11-26 14:17:48 -06002803/* Translate pending damage in buffer co-ordinates to surface
2804 * co-ordinates and union it with a pixman_region32_t.
2805 * This should only be called after the buffer is attached.
2806 */
2807static void
2808apply_damage_buffer(pixman_region32_t *dest,
2809 struct weston_surface *surface,
2810 struct weston_surface_state *state)
2811{
2812 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2813
2814 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2815 * translated into surface co-ordinates and unioned with
2816 * any other surface damage.
2817 * None of this makes sense if there is no buffer though.
2818 */
2819 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2820 pixman_region32_t buffer_damage;
2821
2822 pixman_region32_intersect_rect(&state->damage_buffer,
2823 &state->damage_buffer,
2824 0, 0, buffer->width,
2825 buffer->height);
2826 pixman_region32_init(&buffer_damage);
2827 weston_matrix_transform_region(&buffer_damage,
2828 &surface->buffer_to_surface_matrix,
2829 &state->damage_buffer);
2830 pixman_region32_union(dest, dest, &buffer_damage);
2831 pixman_region32_fini(&buffer_damage);
2832 }
2833 /* We should clear this on commit even if there was no buffer */
2834 pixman_region32_clear(&state->damage_buffer);
2835}
2836
Jason Ekstrand1e059042014-10-16 10:55:19 -05002837static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002838weston_surface_commit_state(struct weston_surface *surface,
2839 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002840{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002841 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002842 pixman_region32_t opaque;
2843
Alexander Larsson4ea95522013-05-22 14:41:37 +02002844 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002845 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002846 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002847 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002848
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002849 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002850 if (state->newly_attached)
2851 weston_surface_attach(surface, state->buffer);
2852 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002853
Jason Ekstrand1e059042014-10-16 10:55:19 -05002854 weston_surface_build_buffer_matrix(surface,
2855 &surface->surface_to_buffer_matrix);
2856 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2857 &surface->surface_to_buffer_matrix);
2858
Jason Ekstrand7b982072014-05-20 14:33:03 -05002859 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002860 weston_surface_update_size(surface);
2861 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002862 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002863 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002864
Jason Ekstrand7b982072014-05-20 14:33:03 -05002865 state->sx = 0;
2866 state->sy = 0;
2867 state->newly_attached = 0;
2868 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002869
Derek Foreman152254b2015-11-26 14:17:48 -06002870 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002871 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002872 (pixman_region32_not_empty(&state->damage_surface) ||
2873 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002874 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002875
Pekka Paalanen8e159182012-10-10 12:49:25 +03002876 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002877 &state->damage_surface);
2878
2879 apply_damage_buffer(&surface->damage, surface, state);
2880
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002881 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002882 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002883 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002884
2885 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002886 pixman_region32_init(&opaque);
2887 pixman_region32_intersect_rect(&opaque, &state->opaque,
2888 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002889
2890 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2891 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002892 wl_list_for_each(view, &surface->views, surface_link)
2893 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002894 }
2895
2896 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002897
2898 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002899 pixman_region32_intersect_rect(&surface->input, &state->input,
2900 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002901
Pekka Paalanenbc106382012-10-10 12:49:31 +03002902 /* wl_surface.frame */
2903 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002904 &state->frame_callback_list);
2905 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002906
2907 /* XXX:
2908 * What should happen with a feedback request, if there
2909 * is no wl_buffer attached for this commit?
2910 */
2911
2912 /* presentation.feedback */
2913 wl_list_insert_list(&surface->feedback_list,
2914 &state->feedback_list);
2915 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002916}
2917
2918static void
2919weston_surface_commit(struct weston_surface *surface)
2920{
2921 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002922
Pekka Paalanene67858b2013-04-25 13:57:42 +03002923 weston_surface_commit_subsurface_order(surface);
2924
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002925 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002926}
2927
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002928static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002929weston_subsurface_commit(struct weston_subsurface *sub);
2930
2931static void
2932weston_subsurface_parent_commit(struct weston_subsurface *sub,
2933 int parent_is_synchronized);
2934
2935static void
2936surface_commit(struct wl_client *client, struct wl_resource *resource)
2937{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002938 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002939 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2940
2941 if (sub) {
2942 weston_subsurface_commit(sub);
2943 return;
2944 }
2945
2946 weston_surface_commit(surface);
2947
2948 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2949 if (sub->surface != surface)
2950 weston_subsurface_parent_commit(sub, 0);
2951 }
2952}
2953
2954static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002955surface_set_buffer_transform(struct wl_client *client,
2956 struct wl_resource *resource, int transform)
2957{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002958 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002959
Jonny Lamba55f1392014-05-30 12:07:15 +02002960 /* if wl_output.transform grows more members this will need to be updated. */
2961 if (transform < 0 ||
2962 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2963 wl_resource_post_error(resource,
2964 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2965 "buffer transform must be a valid transform "
2966 "('%d' specified)", transform);
2967 return;
2968 }
2969
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002970 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002971 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002972}
2973
Alexander Larsson4ea95522013-05-22 14:41:37 +02002974static void
2975surface_set_buffer_scale(struct wl_client *client,
2976 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002977 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002978{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002979 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002980
Jonny Lamba55f1392014-05-30 12:07:15 +02002981 if (scale < 1) {
2982 wl_resource_post_error(resource,
2983 WL_SURFACE_ERROR_INVALID_SCALE,
2984 "buffer scale must be at least one "
2985 "('%d' specified)", scale);
2986 return;
2987 }
2988
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002989 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002990 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002991}
2992
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002993static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002994 surface_destroy,
2995 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002996 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002997 surface_frame,
2998 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002999 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003000 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003001 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003002 surface_set_buffer_scale,
3003 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003004};
3005
3006static void
3007compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003008 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003009{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003010 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003011 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003012
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003013 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003014 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003015 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003016 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003017 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003018
Jason Ekstranda85118c2013-06-27 20:17:02 -05003019 surface->resource =
3020 wl_resource_create(client, &wl_surface_interface,
3021 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003022 if (surface->resource == NULL) {
3023 weston_surface_destroy(surface);
3024 wl_resource_post_no_memory(resource);
3025 return;
3026 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003027 wl_resource_set_implementation(surface->resource, &surface_interface,
3028 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003029
3030 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003031}
3032
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003033static void
3034destroy_region(struct wl_resource *resource)
3035{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003036 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003037
3038 pixman_region32_fini(&region->region);
3039 free(region);
3040}
3041
3042static void
3043region_destroy(struct wl_client *client, struct wl_resource *resource)
3044{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003045 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003046}
3047
3048static void
3049region_add(struct wl_client *client, struct wl_resource *resource,
3050 int32_t x, int32_t y, int32_t width, int32_t height)
3051{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003052 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003053
3054 pixman_region32_union_rect(&region->region, &region->region,
3055 x, y, width, height);
3056}
3057
3058static void
3059region_subtract(struct wl_client *client, struct wl_resource *resource,
3060 int32_t x, int32_t y, int32_t width, int32_t height)
3061{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003062 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003063 pixman_region32_t rect;
3064
3065 pixman_region32_init_rect(&rect, x, y, width, height);
3066 pixman_region32_subtract(&region->region, &region->region, &rect);
3067 pixman_region32_fini(&rect);
3068}
3069
3070static const struct wl_region_interface region_interface = {
3071 region_destroy,
3072 region_add,
3073 region_subtract
3074};
3075
3076static void
3077compositor_create_region(struct wl_client *client,
3078 struct wl_resource *resource, uint32_t id)
3079{
3080 struct weston_region *region;
3081
3082 region = malloc(sizeof *region);
3083 if (region == NULL) {
3084 wl_resource_post_no_memory(resource);
3085 return;
3086 }
3087
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003088 pixman_region32_init(&region->region);
3089
Jason Ekstranda85118c2013-06-27 20:17:02 -05003090 region->resource =
3091 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003092 if (region->resource == NULL) {
3093 free(region);
3094 wl_resource_post_no_memory(resource);
3095 return;
3096 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003097 wl_resource_set_implementation(region->resource, &region_interface,
3098 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003099}
3100
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003101static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003102 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003103 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003104};
3105
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003106static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003107weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3108{
3109 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003110
Jason Ekstrand7b982072014-05-20 14:33:03 -05003111 weston_surface_commit_state(surface, &sub->cached);
3112 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003113
3114 weston_surface_commit_subsurface_order(surface);
3115
3116 weston_surface_schedule_repaint(surface);
3117
Jason Ekstrand7b982072014-05-20 14:33:03 -05003118 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003119}
3120
3121static void
3122weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3123{
3124 struct weston_surface *surface = sub->surface;
3125
3126 /*
3127 * If this commit would cause the surface to move by the
3128 * attach(dx, dy) parameters, the old damage region must be
3129 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003130 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003131 */
Derek Foreman152254b2015-11-26 14:17:48 -06003132 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003133 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003134 pixman_region32_union(&sub->cached.damage_surface,
3135 &sub->cached.damage_surface,
3136 &surface->pending.damage_surface);
3137 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003138
3139 if (surface->pending.newly_attached) {
3140 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003141 weston_surface_state_set_buffer(&sub->cached,
3142 surface->pending.buffer);
3143 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003144 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003145 weston_presentation_feedback_discard_list(
3146 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003147 }
3148 sub->cached.sx += surface->pending.sx;
3149 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003150
Derek Foreman152254b2015-11-26 14:17:48 -06003151 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3152
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003153 sub->cached.buffer_viewport.changed |=
3154 surface->pending.buffer_viewport.changed;
3155 sub->cached.buffer_viewport.buffer =
3156 surface->pending.buffer_viewport.buffer;
3157 sub->cached.buffer_viewport.surface =
3158 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003159
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003160 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003161
3162 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3163
3164 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3165
3166 wl_list_insert_list(&sub->cached.frame_callback_list,
3167 &surface->pending.frame_callback_list);
3168 wl_list_init(&surface->pending.frame_callback_list);
3169
Pekka Paalanen133e4392014-09-23 22:08:46 -04003170 wl_list_insert_list(&sub->cached.feedback_list,
3171 &surface->pending.feedback_list);
3172 wl_list_init(&surface->pending.feedback_list);
3173
Jason Ekstrand7b982072014-05-20 14:33:03 -05003174 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003175}
3176
Derek Foreman280e7dd2014-10-03 13:13:42 -05003177static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003178weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3179{
3180 while (sub) {
3181 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003182 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003183
3184 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003185 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003186
3187 sub = weston_surface_to_subsurface(sub->parent);
3188 }
3189
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003190 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003191}
3192
3193static void
3194weston_subsurface_commit(struct weston_subsurface *sub)
3195{
3196 struct weston_surface *surface = sub->surface;
3197 struct weston_subsurface *tmp;
3198
3199 /* Recursive check for effectively synchronized. */
3200 if (weston_subsurface_is_synchronized(sub)) {
3201 weston_subsurface_commit_to_cache(sub);
3202 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003203 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003204 /* flush accumulated state from cache */
3205 weston_subsurface_commit_to_cache(sub);
3206 weston_subsurface_commit_from_cache(sub);
3207 } else {
3208 weston_surface_commit(surface);
3209 }
3210
3211 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3212 if (tmp->surface != surface)
3213 weston_subsurface_parent_commit(tmp, 0);
3214 }
3215 }
3216}
3217
3218static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003219weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003220{
3221 struct weston_surface *surface = sub->surface;
3222 struct weston_subsurface *tmp;
3223
Pekka Paalanene67858b2013-04-25 13:57:42 +03003224 /* From now on, commit_from_cache the whole sub-tree, regardless of
3225 * the synchronized mode of each child. This sub-surface or some
3226 * of its ancestors were synchronized, so we are synchronized
3227 * all the way down.
3228 */
3229
Jason Ekstrand7b982072014-05-20 14:33:03 -05003230 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003231 weston_subsurface_commit_from_cache(sub);
3232
3233 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3234 if (tmp->surface != surface)
3235 weston_subsurface_parent_commit(tmp, 1);
3236 }
3237}
3238
3239static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003240weston_subsurface_parent_commit(struct weston_subsurface *sub,
3241 int parent_is_synchronized)
3242{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003243 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003244 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003245 wl_list_for_each(view, &sub->surface->views, surface_link)
3246 weston_view_set_position(view,
3247 sub->position.x,
3248 sub->position.y);
3249
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003250 sub->position.set = 0;
3251 }
3252
3253 if (parent_is_synchronized || sub->synchronized)
3254 weston_subsurface_synchronized_commit(sub);
3255}
3256
Pekka Paalanen8274d902014-08-06 19:36:51 +03003257static int
3258subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3259{
3260 return snprintf(buf, len, "sub-surface");
3261}
3262
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003263static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003264subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003265{
3266 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003267 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003268
Jason Ekstranda7af7042013-10-12 22:38:11 -05003269 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003270 weston_view_set_position(view,
3271 view->geometry.x + dx,
3272 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003273
3274 /* No need to check parent mappedness, because if parent is not
3275 * mapped, parent is not in a visible layer, so this sub-surface
3276 * will not be drawn either.
3277 */
3278 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003279 struct weston_output *output;
3280
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003281 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003282 * because that would call it also for the parent surface,
3283 * which might not be mapped yet. That would lead to
3284 * inconsistent state, where the window could never be
3285 * mapped.
3286 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003287 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003288 * weston_surface_is_mapped() return true, so that when the
3289 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003290 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003291 */
3292 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003293 output = container_of(compositor->output_list.next,
3294 struct weston_output, link);
3295
3296 surface->output = output;
Bryce Harrington89324ce2015-12-23 18:38:07 -08003297 weston_surface_update_output_mask(surface, 1u << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003298 }
3299}
3300
3301static struct weston_subsurface *
3302weston_surface_to_subsurface(struct weston_surface *surface)
3303{
3304 if (surface->configure == subsurface_configure)
3305 return surface->configure_private;
3306
3307 return NULL;
3308}
3309
Pekka Paalanen01388e22013-04-25 13:57:44 +03003310WL_EXPORT struct weston_surface *
3311weston_surface_get_main_surface(struct weston_surface *surface)
3312{
3313 struct weston_subsurface *sub;
3314
3315 while (surface && (sub = weston_surface_to_subsurface(surface)))
3316 surface = sub->parent;
3317
3318 return surface;
3319}
3320
Pekka Paalanen50b67472014-10-01 15:02:41 +03003321WL_EXPORT int
3322weston_surface_set_role(struct weston_surface *surface,
3323 const char *role_name,
3324 struct wl_resource *error_resource,
3325 uint32_t error_code)
3326{
3327 assert(role_name);
3328
3329 if (surface->role_name == NULL ||
3330 surface->role_name == role_name ||
3331 strcmp(surface->role_name, role_name) == 0) {
3332 surface->role_name = role_name;
3333
3334 return 0;
3335 }
3336
3337 wl_resource_post_error(error_resource, error_code,
3338 "Cannot assign role %s to wl_surface@%d,"
3339 " already has role %s\n",
3340 role_name,
3341 wl_resource_get_id(surface->resource),
3342 surface->role_name);
3343 return -1;
3344}
3345
Pekka Paalanen8274d902014-08-06 19:36:51 +03003346WL_EXPORT void
3347weston_surface_set_label_func(struct weston_surface *surface,
3348 int (*desc)(struct weston_surface *,
3349 char *, size_t))
3350{
3351 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003352 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003353}
3354
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003355/** Get the size of surface contents
3356 *
3357 * \param surface The surface to query.
3358 * \param width Returns the width of raw contents.
3359 * \param height Returns the height of raw contents.
3360 *
3361 * Retrieves the raw surface content size in pixels for the given surface.
3362 * This is the whole content size in buffer pixels. If the surface
3363 * has no content or the renderer does not implement this feature,
3364 * zeroes are returned.
3365 *
3366 * This function is used to determine the buffer size needed for
3367 * a weston_surface_copy_content() call.
3368 */
3369WL_EXPORT void
3370weston_surface_get_content_size(struct weston_surface *surface,
3371 int *width, int *height)
3372{
3373 struct weston_renderer *rer = surface->compositor->renderer;
3374
3375 if (!rer->surface_get_content_size) {
3376 *width = 0;
3377 *height = 0;
3378 return;
3379 }
3380
3381 rer->surface_get_content_size(surface, width, height);
3382}
3383
3384/** Copy surface contents to system memory.
3385 *
3386 * \param surface The surface to copy from.
3387 * \param target Pointer to the target memory buffer.
3388 * \param size Size of the target buffer in bytes.
3389 * \param src_x X location on contents to copy from.
3390 * \param src_y Y location on contents to copy from.
3391 * \param width Width in pixels of the area to copy.
3392 * \param height Height in pixels of the area to copy.
3393 * \return 0 for success, -1 for failure.
3394 *
3395 * Surface contents are maintained by the renderer. They can be in a
3396 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3397 * else.
3398 *
3399 * Surface contents are copied into memory pointed to by target,
3400 * which has size bytes of space available. The target memory
3401 * may be larger than needed, but being smaller returns an error.
3402 * The extra bytes in target may or may not be written; their content is
3403 * unspecified. Size must be large enough to hold the image.
3404 *
3405 * The image in the target memory will be arranged in rows from
3406 * top to bottom, and pixels on a row from left to right. The pixel
3407 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3408 * width * 4.
3409 *
3410 * Parameters src_x and src_y define the upper-left corner in buffer
3411 * coordinates (pixels) to copy from. Parameters width and height
3412 * define the size of the area to copy in pixels.
3413 *
3414 * The rectangle defined by src_x, src_y, width, height must fit in
3415 * the surface contents. Otherwise an error is returned.
3416 *
3417 * Use surface_get_data_size to determine the content size; the
3418 * needed target buffer size and rectangle limits.
3419 *
3420 * CURRENT IMPLEMENTATION RESTRICTIONS:
3421 * - the machine must be little-endian due to Pixman formats.
3422 *
3423 * NOTE: Pixman formats are premultiplied.
3424 */
3425WL_EXPORT int
3426weston_surface_copy_content(struct weston_surface *surface,
3427 void *target, size_t size,
3428 int src_x, int src_y,
3429 int width, int height)
3430{
3431 struct weston_renderer *rer = surface->compositor->renderer;
3432 int cw, ch;
3433 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3434
3435 if (!rer->surface_copy_content)
3436 return -1;
3437
3438 weston_surface_get_content_size(surface, &cw, &ch);
3439
3440 if (src_x < 0 || src_y < 0)
3441 return -1;
3442
3443 if (width <= 0 || height <= 0)
3444 return -1;
3445
3446 if (src_x + width > cw || src_y + height > ch)
3447 return -1;
3448
3449 if (width * bytespp * height > size)
3450 return -1;
3451
3452 return rer->surface_copy_content(surface, target, size,
3453 src_x, src_y, width, height);
3454}
3455
Pekka Paalanene67858b2013-04-25 13:57:42 +03003456static void
3457subsurface_set_position(struct wl_client *client,
3458 struct wl_resource *resource, int32_t x, int32_t y)
3459{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003460 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003461
3462 if (!sub)
3463 return;
3464
3465 sub->position.x = x;
3466 sub->position.y = y;
3467 sub->position.set = 1;
3468}
3469
3470static struct weston_subsurface *
3471subsurface_from_surface(struct weston_surface *surface)
3472{
3473 struct weston_subsurface *sub;
3474
3475 sub = weston_surface_to_subsurface(surface);
3476 if (sub)
3477 return sub;
3478
3479 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3480 if (sub->surface == surface)
3481 return sub;
3482
3483 return NULL;
3484}
3485
3486static struct weston_subsurface *
3487subsurface_sibling_check(struct weston_subsurface *sub,
3488 struct weston_surface *surface,
3489 const char *request)
3490{
3491 struct weston_subsurface *sibling;
3492
3493 sibling = subsurface_from_surface(surface);
3494
3495 if (!sibling) {
3496 wl_resource_post_error(sub->resource,
3497 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3498 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003499 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003500 return NULL;
3501 }
3502
3503 if (sibling->parent != sub->parent) {
3504 wl_resource_post_error(sub->resource,
3505 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3506 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003507 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003508 return NULL;
3509 }
3510
3511 return sibling;
3512}
3513
3514static void
3515subsurface_place_above(struct wl_client *client,
3516 struct wl_resource *resource,
3517 struct wl_resource *sibling_resource)
3518{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003519 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003520 struct weston_surface *surface =
3521 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003522 struct weston_subsurface *sibling;
3523
3524 if (!sub)
3525 return;
3526
3527 sibling = subsurface_sibling_check(sub, surface, "place_above");
3528 if (!sibling)
3529 return;
3530
3531 wl_list_remove(&sub->parent_link_pending);
3532 wl_list_insert(sibling->parent_link_pending.prev,
3533 &sub->parent_link_pending);
3534}
3535
3536static void
3537subsurface_place_below(struct wl_client *client,
3538 struct wl_resource *resource,
3539 struct wl_resource *sibling_resource)
3540{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003541 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003542 struct weston_surface *surface =
3543 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003544 struct weston_subsurface *sibling;
3545
3546 if (!sub)
3547 return;
3548
3549 sibling = subsurface_sibling_check(sub, surface, "place_below");
3550 if (!sibling)
3551 return;
3552
3553 wl_list_remove(&sub->parent_link_pending);
3554 wl_list_insert(&sibling->parent_link_pending,
3555 &sub->parent_link_pending);
3556}
3557
3558static void
3559subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3560{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003561 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003562
3563 if (sub)
3564 sub->synchronized = 1;
3565}
3566
3567static void
3568subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3569{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003570 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003571
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003572 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003573 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003574
3575 /* If sub became effectively desynchronized, flush. */
3576 if (!weston_subsurface_is_synchronized(sub))
3577 weston_subsurface_synchronized_commit(sub);
3578 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003579}
3580
3581static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003582weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3583{
3584 wl_list_remove(&sub->parent_link);
3585 wl_list_remove(&sub->parent_link_pending);
3586 wl_list_remove(&sub->parent_destroy_listener.link);
3587 sub->parent = NULL;
3588}
3589
3590static void
3591weston_subsurface_destroy(struct weston_subsurface *sub);
3592
3593static void
3594subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3595{
3596 struct weston_subsurface *sub =
3597 container_of(listener, struct weston_subsurface,
3598 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003599 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003600
3601 /* The protocol object (wl_resource) is left inert. */
3602 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003603 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003604
3605 weston_subsurface_destroy(sub);
3606}
3607
3608static void
3609subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3610{
3611 struct weston_subsurface *sub =
3612 container_of(listener, struct weston_subsurface,
3613 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003614 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003615 assert(sub->surface != sub->parent);
3616
3617 if (weston_surface_is_mapped(sub->surface))
3618 weston_surface_unmap(sub->surface);
3619
3620 weston_subsurface_unlink_parent(sub);
3621}
3622
3623static void
3624subsurface_resource_destroy(struct wl_resource *resource)
3625{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003626 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003627
3628 if (sub)
3629 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003630}
3631
3632static void
3633subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3634{
3635 wl_resource_destroy(resource);
3636}
3637
3638static void
3639weston_subsurface_link_parent(struct weston_subsurface *sub,
3640 struct weston_surface *parent)
3641{
3642 sub->parent = parent;
3643 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003644 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003645 &sub->parent_destroy_listener);
3646
3647 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3648 wl_list_insert(&parent->subsurface_list_pending,
3649 &sub->parent_link_pending);
3650}
3651
3652static void
3653weston_subsurface_link_surface(struct weston_subsurface *sub,
3654 struct weston_surface *surface)
3655{
3656 sub->surface = surface;
3657 sub->surface_destroy_listener.notify =
3658 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003659 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003660 &sub->surface_destroy_listener);
3661}
3662
3663static void
3664weston_subsurface_destroy(struct weston_subsurface *sub)
3665{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003666 struct weston_view *view, *next;
3667
Pekka Paalanene67858b2013-04-25 13:57:42 +03003668 assert(sub->surface);
3669
3670 if (sub->resource) {
3671 assert(weston_surface_to_subsurface(sub->surface) == sub);
3672 assert(sub->parent_destroy_listener.notify ==
3673 subsurface_handle_parent_destroy);
3674
George Kiagiadakised04d382014-06-13 18:10:26 +02003675 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3676 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003677 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003678 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003679
Pekka Paalanene67858b2013-04-25 13:57:42 +03003680 if (sub->parent)
3681 weston_subsurface_unlink_parent(sub);
3682
Jason Ekstrand7b982072014-05-20 14:33:03 -05003683 weston_surface_state_fini(&sub->cached);
3684 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003685
3686 sub->surface->configure = NULL;
3687 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003688 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003689 } else {
3690 /* the dummy weston_subsurface for the parent itself */
3691 assert(sub->parent_destroy_listener.notify == NULL);
3692 wl_list_remove(&sub->parent_link);
3693 wl_list_remove(&sub->parent_link_pending);
3694 }
3695
3696 wl_list_remove(&sub->surface_destroy_listener.link);
3697 free(sub);
3698}
3699
3700static const struct wl_subsurface_interface subsurface_implementation = {
3701 subsurface_destroy,
3702 subsurface_set_position,
3703 subsurface_place_above,
3704 subsurface_place_below,
3705 subsurface_set_sync,
3706 subsurface_set_desync
3707};
3708
3709static struct weston_subsurface *
3710weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3711 struct weston_surface *parent)
3712{
3713 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003714 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003715
Bryce Harringtonde16d892014-11-20 22:21:57 -08003716 sub = zalloc(sizeof *sub);
3717 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003718 return NULL;
3719
Jason Ekstranda7af7042013-10-12 22:38:11 -05003720 wl_list_init(&sub->unused_views);
3721
Jason Ekstranda85118c2013-06-27 20:17:02 -05003722 sub->resource =
3723 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003724 if (!sub->resource) {
3725 free(sub);
3726 return NULL;
3727 }
3728
Jason Ekstranda85118c2013-06-27 20:17:02 -05003729 wl_resource_set_implementation(sub->resource,
3730 &subsurface_implementation,
3731 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003732 weston_subsurface_link_surface(sub, surface);
3733 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003734 weston_surface_state_init(&sub->cached);
3735 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003736 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003737
3738 return sub;
3739}
3740
3741/* Create a dummy subsurface for having the parent itself in its
3742 * sub-surface lists. Makes stacking order manipulation easy.
3743 */
3744static struct weston_subsurface *
3745weston_subsurface_create_for_parent(struct weston_surface *parent)
3746{
3747 struct weston_subsurface *sub;
3748
Bryce Harringtonde16d892014-11-20 22:21:57 -08003749 sub = zalloc(sizeof *sub);
3750 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003751 return NULL;
3752
3753 weston_subsurface_link_surface(sub, parent);
3754 sub->parent = parent;
3755 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3756 wl_list_insert(&parent->subsurface_list_pending,
3757 &sub->parent_link_pending);
3758
3759 return sub;
3760}
3761
3762static void
3763subcompositor_get_subsurface(struct wl_client *client,
3764 struct wl_resource *resource,
3765 uint32_t id,
3766 struct wl_resource *surface_resource,
3767 struct wl_resource *parent_resource)
3768{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003769 struct weston_surface *surface =
3770 wl_resource_get_user_data(surface_resource);
3771 struct weston_surface *parent =
3772 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003773 struct weston_subsurface *sub;
3774 static const char where[] = "get_subsurface: wl_subsurface@";
3775
3776 if (surface == parent) {
3777 wl_resource_post_error(resource,
3778 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3779 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003780 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003781 return;
3782 }
3783
3784 if (weston_surface_to_subsurface(surface)) {
3785 wl_resource_post_error(resource,
3786 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3787 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003788 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003789 return;
3790 }
3791
Pekka Paalanen50b67472014-10-01 15:02:41 +03003792 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3793 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003794 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003795
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003796 if (weston_surface_get_main_surface(parent) == surface) {
3797 wl_resource_post_error(resource,
3798 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3799 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003800 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003801 return;
3802 }
3803
Pekka Paalanene67858b2013-04-25 13:57:42 +03003804 /* make sure the parent is in its own list */
3805 if (wl_list_empty(&parent->subsurface_list)) {
3806 if (!weston_subsurface_create_for_parent(parent)) {
3807 wl_resource_post_no_memory(resource);
3808 return;
3809 }
3810 }
3811
3812 sub = weston_subsurface_create(id, surface, parent);
3813 if (!sub) {
3814 wl_resource_post_no_memory(resource);
3815 return;
3816 }
3817
3818 surface->configure = subsurface_configure;
3819 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003820 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003821}
3822
3823static void
3824subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3825{
3826 wl_resource_destroy(resource);
3827}
3828
3829static const struct wl_subcompositor_interface subcompositor_interface = {
3830 subcompositor_destroy,
3831 subcompositor_get_subsurface
3832};
3833
3834static void
3835bind_subcompositor(struct wl_client *client,
3836 void *data, uint32_t version, uint32_t id)
3837{
3838 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003839 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003840
Jason Ekstranda85118c2013-06-27 20:17:02 -05003841 resource =
3842 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003843 if (resource == NULL) {
3844 wl_client_post_no_memory(client);
3845 return;
3846 }
3847 wl_resource_set_implementation(resource, &subcompositor_interface,
3848 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003849}
3850
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003851/** Set a DPMS mode on all of the compositor's outputs
3852 *
3853 * \param compositor The compositor instance
3854 * \param state The DPMS state the outputs will be set to
3855 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003856static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003857weston_compositor_dpms(struct weston_compositor *compositor,
3858 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003859{
3860 struct weston_output *output;
3861
3862 wl_list_for_each(output, &compositor->output_list, link)
3863 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003864 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003865}
3866
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003867/** Restores the compositor to active status
3868 *
3869 * \param compositor The compositor instance
3870 *
3871 * If the compositor was in a sleeping mode, all outputs are powered
3872 * back on via DPMS. Otherwise if the compositor was inactive
3873 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3874 * signal will fire.
3875 *
3876 * Restarts the idle timer.
3877 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003878WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003879weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003880{
Neil Roberts8b62e202013-09-30 13:14:47 +01003881 uint32_t old_state = compositor->state;
3882
3883 /* The state needs to be changed before emitting the wake
3884 * signal because that may try to schedule a repaint which
3885 * will not work if the compositor is still sleeping */
3886 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3887
3888 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003889 case WESTON_COMPOSITOR_SLEEPING:
3890 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3891 /* fall through */
3892 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003893 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003894 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003895 /* fall through */
3896 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003897 wl_event_source_timer_update(compositor->idle_source,
3898 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003899 }
3900}
3901
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003902/** Turns off rendering and frame events for the compositor.
3903 *
3904 * \param compositor The compositor instance
3905 *
3906 * This is used for example to prevent further rendering while the
3907 * compositor is shutting down.
3908 *
3909 * \note When offscreen state is entered, outputs will be powered
3910 * back on if they were sleeping (in DPMS off mode), even though
3911 * no rendering will be performed.
3912 *
3913 * Stops the idle timer.
3914 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003915WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003916weston_compositor_offscreen(struct weston_compositor *compositor)
3917{
3918 switch (compositor->state) {
3919 case WESTON_COMPOSITOR_OFFSCREEN:
3920 return;
3921 case WESTON_COMPOSITOR_SLEEPING:
3922 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3923 /* fall through */
3924 default:
3925 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3926 wl_event_source_timer_update(compositor->idle_source, 0);
3927 }
3928}
3929
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003930/** Powers down all attached output devices
3931 *
3932 * \param compositor The compositor instance
3933 *
3934 * Causes rendering to the outputs to cease, and no frame events to be
3935 * sent. Only powers down the outputs if the compositor is not already
3936 * in sleep mode.
3937 *
3938 * Stops the idle timer.
3939 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003940WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003941weston_compositor_sleep(struct weston_compositor *compositor)
3942{
3943 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3944 return;
3945
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003946 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003947 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3948 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3949}
3950
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003951/** Sets compositor to idle mode
3952 *
3953 * \param data The compositor instance
3954 *
3955 * This is called when the idle timer fires. Once the compositor is in
3956 * idle mode it requires a wake action (e.g. via
3957 * weston_compositor_wake()) to restore it. The compositor's
3958 * idle_signal will be triggered when the idle event occurs.
3959 *
3960 * Idleness can be inhibited by setting the compositor's idle_inhibit
3961 * property.
3962 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003963static int
3964idle_handler(void *data)
3965{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003966 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003967
3968 if (compositor->idle_inhibit)
3969 return 1;
3970
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003971 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003972 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003973
3974 return 1;
3975}
3976
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003977WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003978weston_plane_init(struct weston_plane *plane,
3979 struct weston_compositor *ec,
3980 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003981{
3982 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003983 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003984 plane->x = x;
3985 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003986 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003987
3988 /* Init the link so that the call to wl_list_remove() when releasing
3989 * the plane without ever stacking doesn't lead to a crash */
3990 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003991}
3992
3993WL_EXPORT void
3994weston_plane_release(struct weston_plane *plane)
3995{
Xiong Zhang97116532013-10-23 13:58:31 +08003996 struct weston_view *view;
3997
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003998 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003999 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004000
Xiong Zhang97116532013-10-23 13:58:31 +08004001 wl_list_for_each(view, &plane->compositor->view_list, link) {
4002 if (view->plane == plane)
4003 view->plane = NULL;
4004 }
4005
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004006 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004007}
4008
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004009WL_EXPORT void
4010weston_compositor_stack_plane(struct weston_compositor *ec,
4011 struct weston_plane *plane,
4012 struct weston_plane *above)
4013{
4014 if (above)
4015 wl_list_insert(above->link.prev, &plane->link);
4016 else
4017 wl_list_insert(&ec->plane_list, &plane->link);
4018}
4019
Casey Dahlin9074db52012-04-19 22:50:09 -04004020static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004021{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004022 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004023}
4024
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004025static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004026bind_output(struct wl_client *client,
4027 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004028{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004029 struct weston_output *output = data;
4030 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004031 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004032
Jason Ekstranda85118c2013-06-27 20:17:02 -05004033 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004034 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004035 if (resource == NULL) {
4036 wl_client_post_no_memory(client);
4037 return;
4038 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004039
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004040 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05004041 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004042
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004043 wl_output_send_geometry(resource,
4044 output->x,
4045 output->y,
4046 output->mm_width,
4047 output->mm_height,
4048 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004049 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004050 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004051 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004052 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004053 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004054
4055 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004056 wl_output_send_mode(resource,
4057 mode->flags,
4058 mode->width,
4059 mode->height,
4060 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004061 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004062
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004063 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004064 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004065}
4066
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004067/* Move other outputs when one is removed so the space remains contiguos. */
4068static void
4069weston_compositor_remove_output(struct weston_compositor *compositor,
4070 struct weston_output *remove_output)
4071{
4072 struct weston_output *output;
4073 int offset = 0;
4074
4075 wl_list_for_each(output, &compositor->output_list, link) {
4076 if (output == remove_output) {
4077 offset = output->width;
4078 continue;
4079 }
4080
4081 if (offset > 0) {
4082 weston_output_move(output,
4083 output->x - offset, output->y);
4084 output->dirty = 1;
4085 }
4086 }
4087}
4088
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004089WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004090weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004091{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004092 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004093 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004094
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02004095 output->destroying = 1;
4096
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004097 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08004098 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004099 weston_view_assign_output(view);
4100 }
4101
Pekka Paalanen0513a952014-05-21 16:17:27 +03004102 wl_event_source_remove(output->repaint_timer);
4103
Pekka Paalanen133e4392014-09-23 22:08:46 -04004104 weston_presentation_feedback_discard_list(&output->feedback_list);
4105
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004106 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004107 wl_list_remove(&output->link);
4108
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004109 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004110 wl_signal_emit(&output->destroy_signal, output);
4111
Richard Hughesafe690c2013-05-02 10:10:04 +01004112 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004113 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004114 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08004115 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004116
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004117 wl_resource_for_each(resource, &output->resource_list) {
4118 wl_resource_set_destructor(resource, NULL);
4119 }
4120
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004121 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004122}
4123
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004124WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004125weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004126{
Scott Moreau850ca422012-05-21 15:21:25 -06004127 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004128
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004129 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004130 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004131
Scott Moreauccbf29d2012-02-22 14:21:41 -07004132 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004133 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004134 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004135 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004136 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004137 weston_matrix_scale(&output->matrix, magnification,
4138 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004139 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004140
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004141 switch (output->transform) {
4142 case WL_OUTPUT_TRANSFORM_FLIPPED:
4143 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4144 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4145 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4146 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4147 weston_matrix_scale(&output->matrix, -1, 1, 1);
4148 break;
4149 }
4150
4151 switch (output->transform) {
4152 default:
4153 case WL_OUTPUT_TRANSFORM_NORMAL:
4154 case WL_OUTPUT_TRANSFORM_FLIPPED:
4155 break;
4156 case WL_OUTPUT_TRANSFORM_90:
4157 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4158 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4159 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4160 break;
4161 case WL_OUTPUT_TRANSFORM_180:
4162 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4163 weston_matrix_translate(&output->matrix,
4164 -output->width, -output->height, 0);
4165 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4166 break;
4167 case WL_OUTPUT_TRANSFORM_270:
4168 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4169 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4170 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4171 break;
4172 }
4173
4174 if (output->current_scale != 1)
4175 weston_matrix_scale(&output->matrix,
4176 output->current_scale,
4177 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004178
Scott Moreauccbf29d2012-02-22 14:21:41 -07004179 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004180
4181 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004182}
4183
Scott Moreau1bad5db2012-08-18 01:04:05 -06004184static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004185weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004186{
4187 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004188 output->native_scale = scale;
4189 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004190
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004191 convert_size_by_transform_scale(&output->width, &output->height,
4192 output->current_mode->width,
4193 output->current_mode->height,
4194 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004195}
4196
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004197static void
4198weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004199{
4200 output->x = x;
4201 output->y = y;
4202
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004203 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004204 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004205 output->width,
4206 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004207}
4208
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004209WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004210weston_output_move(struct weston_output *output, int x, int y)
4211{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004212 struct wl_resource *resource;
4213
4214 output->move_x = x - output->x;
4215 output->move_y = y - output->y;
4216
4217 if (output->move_x == 0 && output->move_y == 0)
4218 return;
4219
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004220 weston_output_init_geometry(output, x, y);
4221
4222 output->dirty = 1;
4223
4224 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004225 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004226
4227 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004228 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004229 wl_output_send_geometry(resource,
4230 output->x,
4231 output->y,
4232 output->mm_width,
4233 output->mm_height,
4234 output->subpixel,
4235 output->make,
4236 output->model,
4237 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004238
4239 if (wl_resource_get_version(resource) >= 2)
4240 wl_output_send_done(resource);
4241 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004242}
4243
Bryce Harrington3f650b82015-12-23 11:01:58 -08004244/** Initialize a weston_output object's parameters
4245 *
4246 * \param output The weston_output object to initialize
4247 * \param c The output's compositor
4248 * \param x x coordinate for the output in global coordinate space
4249 * \param y y coordinate for the output in global coordinate space
4250 * \param mm_width Physical width of the output as reported by the backend
4251 * \param mm_height Physical height of the output as reported by the backend
4252 * \param transform Rotation of the output
4253 * \param scale Native scaling factor for the output
4254 *
4255 * Sets up the transformation, zoom, and geometry of the output using
4256 * the input properties.
4257 *
4258 * Establishes a repaint timer for the output with the relevant display
4259 * object's event loop. See output_repaint_timer_handler().
4260 *
4261 * The output is assigned an ID. Weston can support up to 32 distinct
4262 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4263 * is referred to and used to find the first available ID number, and
4264 * then this ID is marked as used in output_id_pool.
4265 *
4266 * The output is also assigned a Wayland global with the wl_output
4267 * external interface.
4268 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004269WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004270weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004271 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004272 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004273{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004274 struct wl_event_loop *loop;
4275
Bryce Harrington18e45732015-12-23 20:53:53 -08004276 /* Verify we haven't reached the limit of 32 available output IDs */
4277 assert(ffs(~c->output_id_pool) > 0);
4278
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004279 output->compositor = c;
4280 output->x = x;
4281 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004282 output->mm_width = mm_width;
4283 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004284 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004285 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004286
Alexander Larsson0b135062013-05-28 16:23:36 +02004287 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004288 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004289
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004290 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004291 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004292
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004293 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004294 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004295 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004296 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004297 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004298 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004299
Pekka Paalanen0513a952014-05-21 16:17:27 +03004300 loop = wl_display_get_event_loop(c->wl_display);
4301 output->repaint_timer = wl_event_loop_add_timer(loop,
4302 output_repaint_timer_handler, output);
4303
Bryce Harrington3f650b82015-12-23 11:01:58 -08004304 /* Invert the output id pool and look for the lowest numbered
4305 * switch (the least significant bit). Take that bit's position
4306 * as our ID, and mark it used in the compositor's output_id_pool.
4307 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004308 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004309 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004310
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004311 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004312 wl_global_create(c->wl_display, &wl_output_interface, 2,
4313 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004314}
4315
4316/** Adds an output to the compositor's output list and
4317 * send the compositor's output_created signal.
4318 *
4319 * \param compositor The compositor instance.
4320 * \param output The output to be added.
4321 */
4322WL_EXPORT void
4323weston_compositor_add_output(struct weston_compositor *compositor,
4324 struct weston_output *output)
4325{
4326 wl_list_insert(compositor->output_list.prev, &output->link);
4327 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004328}
4329
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004330WL_EXPORT void
4331weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004332 double device_x, double device_y,
4333 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004334{
Derek Foreman0f679412014-10-02 13:41:17 -05004335 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004336 device_x,
4337 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004338 0.0,
4339 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004340
Derek Foreman67a18b92015-03-24 11:36:14 -05004341 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004342
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004343 *x = p.f[0] / p.f[3];
4344 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004345}
4346
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004347static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004348destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004349{
Jonny Lamb74130762013-11-26 18:19:46 +01004350 struct weston_surface *surface =
4351 wl_resource_get_user_data(resource);
4352
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004353 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004354 surface->pending.buffer_viewport.buffer.src_width =
4355 wl_fixed_from_int(-1);
4356 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004357 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004358}
4359
4360static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004361viewport_destroy(struct wl_client *client,
4362 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004363{
4364 wl_resource_destroy(resource);
4365}
4366
4367static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004368viewport_set(struct wl_client *client,
4369 struct wl_resource *resource,
4370 wl_fixed_t src_x,
4371 wl_fixed_t src_y,
4372 wl_fixed_t src_width,
4373 wl_fixed_t src_height,
4374 int32_t dst_width,
4375 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004376{
Jonny Lamb74130762013-11-26 18:19:46 +01004377 struct weston_surface *surface =
4378 wl_resource_get_user_data(resource);
4379
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004380 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004381
Jonny Lamb8ae35902013-11-26 18:19:45 +01004382 if (wl_fixed_to_double(src_width) < 0 ||
4383 wl_fixed_to_double(src_height) < 0) {
4384 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004385 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004386 "source dimensions must be non-negative (%fx%f)",
4387 wl_fixed_to_double(src_width),
4388 wl_fixed_to_double(src_height));
4389 return;
4390 }
4391
4392 if (dst_width <= 0 || dst_height <= 0) {
4393 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004394 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004395 "destination dimensions must be positive (%dx%d)",
4396 dst_width, dst_height);
4397 return;
4398 }
4399
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004400 surface->pending.buffer_viewport.buffer.src_x = src_x;
4401 surface->pending.buffer_viewport.buffer.src_y = src_y;
4402 surface->pending.buffer_viewport.buffer.src_width = src_width;
4403 surface->pending.buffer_viewport.buffer.src_height = src_height;
4404 surface->pending.buffer_viewport.surface.width = dst_width;
4405 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004406 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004407}
4408
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004409static void
4410viewport_set_source(struct wl_client *client,
4411 struct wl_resource *resource,
4412 wl_fixed_t src_x,
4413 wl_fixed_t src_y,
4414 wl_fixed_t src_width,
4415 wl_fixed_t src_height)
4416{
4417 struct weston_surface *surface =
4418 wl_resource_get_user_data(resource);
4419
4420 assert(surface->viewport_resource != NULL);
4421
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004422 if (src_width == wl_fixed_from_int(-1) &&
4423 src_height == wl_fixed_from_int(-1)) {
4424 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004425 surface->pending.buffer_viewport.buffer.src_width =
4426 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004427 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004428 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004429 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004430
4431 if (src_width <= 0 || src_height <= 0) {
4432 wl_resource_post_error(resource,
4433 WL_VIEWPORT_ERROR_BAD_VALUE,
4434 "source size must be positive (%fx%f)",
4435 wl_fixed_to_double(src_width),
4436 wl_fixed_to_double(src_height));
4437 return;
4438 }
4439
4440 surface->pending.buffer_viewport.buffer.src_x = src_x;
4441 surface->pending.buffer_viewport.buffer.src_y = src_y;
4442 surface->pending.buffer_viewport.buffer.src_width = src_width;
4443 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004444 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004445}
4446
4447static void
4448viewport_set_destination(struct wl_client *client,
4449 struct wl_resource *resource,
4450 int32_t dst_width,
4451 int32_t dst_height)
4452{
4453 struct weston_surface *surface =
4454 wl_resource_get_user_data(resource);
4455
4456 assert(surface->viewport_resource != NULL);
4457
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004458 if (dst_width == -1 && dst_height == -1) {
4459 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004460 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004461 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004462 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004463 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004464
4465 if (dst_width <= 0 || dst_height <= 0) {
4466 wl_resource_post_error(resource,
4467 WL_VIEWPORT_ERROR_BAD_VALUE,
4468 "destination size must be positive (%dx%d)",
4469 dst_width, dst_height);
4470 return;
4471 }
4472
4473 surface->pending.buffer_viewport.surface.width = dst_width;
4474 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004475 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004476}
4477
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004478static const struct wl_viewport_interface viewport_interface = {
4479 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004480 viewport_set,
4481 viewport_set_source,
4482 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004483};
4484
4485static void
4486scaler_destroy(struct wl_client *client,
4487 struct wl_resource *resource)
4488{
4489 wl_resource_destroy(resource);
4490}
4491
4492static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004493scaler_get_viewport(struct wl_client *client,
4494 struct wl_resource *scaler,
4495 uint32_t id,
4496 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004497{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004498 int version = wl_resource_get_version(scaler);
4499 struct weston_surface *surface =
4500 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004501 struct wl_resource *resource;
4502
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004503 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004504 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004505 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4506 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004507 return;
4508 }
4509
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004510 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004511 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004512 if (resource == NULL) {
4513 wl_client_post_no_memory(client);
4514 return;
4515 }
4516
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004517 wl_resource_set_implementation(resource, &viewport_interface,
4518 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004519
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004520 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004521}
4522
4523static const struct wl_scaler_interface scaler_interface = {
4524 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004525 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004526};
4527
4528static void
4529bind_scaler(struct wl_client *client,
4530 void *data, uint32_t version, uint32_t id)
4531{
4532 struct wl_resource *resource;
4533
4534 resource = wl_resource_create(client, &wl_scaler_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004535 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004536 if (resource == NULL) {
4537 wl_client_post_no_memory(client);
4538 return;
4539 }
4540
4541 wl_resource_set_implementation(resource, &scaler_interface,
4542 NULL, NULL);
4543}
4544
4545static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004546destroy_presentation_feedback(struct wl_resource *feedback_resource)
4547{
4548 struct weston_presentation_feedback *feedback;
4549
4550 feedback = wl_resource_get_user_data(feedback_resource);
4551
4552 wl_list_remove(&feedback->link);
4553 free(feedback);
4554}
4555
4556static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004557presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4558{
4559 wl_resource_destroy(resource);
4560}
4561
4562static void
4563presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004564 struct wl_resource *presentation_resource,
4565 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004566 uint32_t callback)
4567{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004568 struct weston_surface *surface;
4569 struct weston_presentation_feedback *feedback;
4570
4571 surface = wl_resource_get_user_data(surface_resource);
4572
Bryce Harringtonde16d892014-11-20 22:21:57 -08004573 feedback = zalloc(sizeof *feedback);
4574 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004575 goto err_calloc;
4576
4577 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004578 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004579 1, callback);
4580 if (!feedback->resource)
4581 goto err_create;
4582
4583 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4584 destroy_presentation_feedback);
4585
4586 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4587
4588 return;
4589
4590err_create:
4591 free(feedback);
4592
4593err_calloc:
4594 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004595}
4596
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004597static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004598 presentation_destroy,
4599 presentation_feedback
4600};
4601
4602static void
4603bind_presentation(struct wl_client *client,
4604 void *data, uint32_t version, uint32_t id)
4605{
4606 struct weston_compositor *compositor = data;
4607 struct wl_resource *resource;
4608
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004609 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004610 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004611 if (resource == NULL) {
4612 wl_client_post_no_memory(client);
4613 return;
4614 }
4615
4616 wl_resource_set_implementation(resource, &presentation_implementation,
4617 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004618 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004619}
4620
4621static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004622compositor_bind(struct wl_client *client,
4623 void *data, uint32_t version, uint32_t id)
4624{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004625 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004626 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004627
Jason Ekstranda85118c2013-06-27 20:17:02 -05004628 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004629 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004630 if (resource == NULL) {
4631 wl_client_post_no_memory(client);
4632 return;
4633 }
4634
4635 wl_resource_set_implementation(resource, &compositor_interface,
4636 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004637}
4638
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004639WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004640weston_environment_get_fd(const char *env)
4641{
4642 char *e, *end;
4643 int fd, flags;
4644
4645 e = getenv(env);
4646 if (!e)
4647 return -1;
4648 fd = strtol(e, &end, 0);
4649 if (*end != '\0')
4650 return -1;
4651
4652 flags = fcntl(fd, F_GETFD);
4653 if (flags == -1)
4654 return -1;
4655
4656 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4657 unsetenv(env);
4658
4659 return fd;
4660}
4661
Pekka Paalanenb5026542014-11-12 15:09:24 +02004662static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004663timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004664 uint32_t key, void *data)
4665{
4666 struct weston_compositor *compositor = data;
4667
4668 if (weston_timeline_enabled_)
4669 weston_timeline_close();
4670 else
4671 weston_timeline_open(compositor);
4672}
4673
Giulio Camuffo459137b2014-10-11 23:56:24 +03004674/** Create the compositor.
4675 *
4676 * This functions creates and initializes a compositor instance.
4677 *
4678 * \param display The Wayland display to be used.
4679 * \param user_data A pointer to an object that can later be retrieved
4680 * using the \ref weston_compositor_get_user_data function.
4681 * \return The compositor instance on success or NULL on failure.
4682 */
4683WL_EXPORT struct weston_compositor *
4684weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004685{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004686 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004687 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004688
Giulio Camuffo459137b2014-10-11 23:56:24 +03004689 ec = zalloc(sizeof *ec);
4690 if (!ec)
4691 return NULL;
4692
4693 ec->wl_display = display;
4694 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004695 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004696 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004697 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004698 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004699 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004700 wl_signal_init(&ec->idle_signal);
4701 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004702 wl_signal_init(&ec->show_input_panel_signal);
4703 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004704 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004705 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004706 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004707 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004708 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004709 wl_signal_init(&ec->session_signal);
4710 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004711
Casey Dahlin58ba1372012-04-19 22:50:08 -04004712 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004713 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004714
Derek Foreman152254b2015-11-26 14:17:48 -06004715 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004716 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004717 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004718
Giulio Camuffo954f1832014-10-11 18:27:30 +03004719 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004720 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004721 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004722
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004723 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004724 ec, bind_scaler))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004725 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004726
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004727 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004728 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004729 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004730
Jason Ekstranda7af7042013-10-12 22:38:11 -05004731 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004732 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004733 wl_list_init(&ec->layer_list);
4734 wl_list_init(&ec->seat_list);
4735 wl_list_init(&ec->output_list);
4736 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004737 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004738 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004739 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004740 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004741 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004742
Xiong Zhang97116532013-10-23 13:58:31 +08004743 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004744 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004745
Giulio Camuffo459137b2014-10-11 23:56:24 +03004746 wl_data_device_manager_init(ec->wl_display);
4747
4748 wl_display_init_shm(ec->wl_display);
4749
4750 loop = wl_display_get_event_loop(ec->wl_display);
4751 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004752
Giulio Camuffo459137b2014-10-11 23:56:24 +03004753 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4754 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4755
4756 weston_compositor_add_debug_binding(ec, KEY_T,
4757 timeline_key_binding_handler, ec);
4758
Giulio Camuffo459137b2014-10-11 23:56:24 +03004759 return ec;
4760
4761fail:
4762 free(ec);
4763 return NULL;
4764}
4765
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004766WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004767weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004768{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004769 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004770
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004771 wl_event_source_remove(ec->idle_source);
4772
Matt Roper361d2ad2011-08-29 13:52:23 -07004773 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004774 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004775 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004776
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004777 if (ec->renderer)
4778 ec->renderer->destroy(ec);
4779
Daniel Stone325fc2d2012-05-30 16:31:58 +01004780 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004781 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004782 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004783 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004784 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004785 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004786
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004787 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004788}
4789
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004790WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004791weston_compositor_exit_with_code(struct weston_compositor *compositor,
4792 int exit_code)
4793{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004794 if (compositor->exit_code == EXIT_SUCCESS)
4795 compositor->exit_code = exit_code;
4796
Giulio Camuffo459137b2014-10-11 23:56:24 +03004797 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004798}
4799
4800WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004801weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4802 const struct weston_pointer_grab_interface *interface)
4803{
4804 struct weston_seat *seat;
4805
4806 ec->default_pointer_grab = interface;
4807 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004808 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4809
4810 if (pointer)
4811 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004812 }
4813}
4814
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004815WL_EXPORT int
4816weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4817 clockid_t clk_id)
4818{
4819 struct timespec ts;
4820
4821 if (clock_gettime(clk_id, &ts) < 0)
4822 return -1;
4823
4824 compositor->presentation_clock = clk_id;
4825
4826 return 0;
4827}
4828
4829/*
4830 * For choosing the software clock, when the display hardware or API
4831 * does not expose a compatible presentation timestamp.
4832 */
4833WL_EXPORT int
4834weston_compositor_set_presentation_clock_software(
4835 struct weston_compositor *compositor)
4836{
4837 /* In order of preference */
4838 static const clockid_t clocks[] = {
4839 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4840 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4841 CLOCK_MONOTONIC, /* no jumps, may crawl */
4842 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4843 CLOCK_REALTIME /* may jump and crawl */
4844 };
4845 unsigned i;
4846
4847 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4848 if (weston_compositor_set_presentation_clock(compositor,
4849 clocks[i]) == 0)
4850 return 0;
4851
4852 weston_log("Error: no suitable presentation clock available.\n");
4853
4854 return -1;
4855}
4856
Pekka Paalanen662f3842015-03-18 12:17:26 +02004857/** Read the current time from the Presentation clock
4858 *
4859 * \param compositor
4860 * \param ts[out] The current time.
4861 *
4862 * \note Reading the current time in user space is always imprecise to some
4863 * degree.
4864 *
4865 * This function is never meant to fail. If reading the clock does fail,
4866 * an error message is logged and a zero time is returned. Callers are not
4867 * supposed to detect or react to failures.
4868 */
4869WL_EXPORT void
4870weston_compositor_read_presentation_clock(
4871 const struct weston_compositor *compositor,
4872 struct timespec *ts)
4873{
4874 static bool warned;
4875 int ret;
4876
4877 ret = clock_gettime(compositor->presentation_clock, ts);
4878 if (ret < 0) {
4879 ts->tv_sec = 0;
4880 ts->tv_nsec = 0;
4881
4882 if (!warned)
4883 weston_log("Error: failure to read "
4884 "the presentation clock %#x: '%m' (%d)\n",
4885 compositor->presentation_clock, errno);
4886 warned = true;
4887 }
4888}
4889
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004890/** Import dmabuf buffer into current renderer
4891 *
4892 * \param compositor
4893 * \param buffer the dmabuf buffer to import
4894 * \return true on usable buffers, false otherwise
4895 *
4896 * This function tests that the linux_dmabuf_buffer is usable
4897 * for the current renderer. Returns false on unusable buffers. Usually
4898 * usability is tested by importing the dmabufs for composition.
4899 *
4900 * This hook is also used for detecting if the renderer supports
4901 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4902 * supported.
4903 * */
4904WL_EXPORT bool
4905weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4906 struct linux_dmabuf_buffer *buffer)
4907{
4908 struct weston_renderer *renderer;
4909
4910 renderer = compositor->renderer;
4911
4912 if (renderer->import_dmabuf == NULL)
4913 return false;
4914
4915 return renderer->import_dmabuf(compositor, buffer);
4916}
4917
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004918WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004919weston_version(int *major, int *minor, int *micro)
4920{
4921 *major = WESTON_VERSION_MAJOR;
4922 *minor = WESTON_VERSION_MINOR;
4923 *micro = WESTON_VERSION_MICRO;
4924}
4925
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004926WL_EXPORT void *
4927weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004928{
Derek Foreman3f86e502015-06-08 11:46:54 -05004929 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004930 char path[PATH_MAX];
4931 void *module, *init;
4932
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004933 if (name == NULL)
4934 return NULL;
4935
Derek Foreman3f86e502015-06-08 11:46:54 -05004936 if (name[0] != '/') {
4937 if (builddir)
4938 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4939 else
4940 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
4941 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004942 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004943 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004944
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004945 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4946 if (module) {
4947 weston_log("Module '%s' already loaded\n", path);
4948 dlclose(module);
4949 return NULL;
4950 }
4951
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004952 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004953 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004954 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004955 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004956 return NULL;
4957 }
4958
4959 init = dlsym(module, entrypoint);
4960 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004961 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004962 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004963 return NULL;
4964 }
4965
4966 return init;
4967}
4968
Giulio Camuffo459137b2014-10-11 23:56:24 +03004969
4970/** Destroys the compositor.
4971 *
4972 * This function cleans up the compositor state and destroys it.
4973 *
4974 * \param compositor The compositor to be destroyed.
4975 */
4976WL_EXPORT void
4977weston_compositor_destroy(struct weston_compositor *compositor)
4978{
4979 /* prevent further rendering while shutting down */
4980 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4981
4982 wl_signal_emit(&compositor->destroy_signal, compositor);
4983
4984 weston_compositor_xkb_destroy(compositor);
4985
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004986 if (compositor->backend)
4987 compositor->backend->destroy(compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004988 free(compositor);
4989}
4990
4991/** Instruct the compositor to exit.
4992 *
4993 * This functions does not directly destroy the compositor object, it merely
4994 * command it to start the tear down process. It is not guaranteed that the
4995 * tear down will happen immediately.
4996 *
4997 * \param compositor The compositor to tear down.
4998 */
4999WL_EXPORT void
5000weston_compositor_exit(struct weston_compositor *compositor)
5001{
5002 compositor->exit(compositor);
5003}
5004
5005/** Return the user data stored in the compositor.
5006 *
5007 * This function returns the user data pointer set with user_data parameter
5008 * to the \ref weston_compositor_create function.
5009 */
5010WL_EXPORT void *
5011weston_compositor_get_user_data(struct weston_compositor *compositor)
5012{
5013 return compositor->user_data;
5014}