1 /*
2 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
4 * Copyright © 2012-2015 Collabora, Ltd.
5 *
6 * 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:
13 *
14 * 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.
26 */
27
28 #include "config.h"
29
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <stdint.h>
35 #include <limits.h>
36 #include <stdarg.h>
37 #include <assert.h>
38 #include <sys/ioctl.h>
39 #include <sys/mman.h>
40 #include <sys/wait.h>
41 #include <sys/socket.h>
42 #include <sys/utsname.h>
43 #include <sys/stat.h>
44 #include <unistd.h>
45 #include <math.h>
46 #include <linux/input.h>
47 #include <dlfcn.h>
48 #include <signal.h>
49 #include <setjmp.h>
50 #include <sys/time.h>
51 #include <time.h>
52 #include <errno.h>
53
54 #include "timeline.h"
55
56 #include "compositor.h"
57 #include "scaler-server-protocol.h"
58 #include "presentation_timing-server-protocol.h"
59 #include "shared/helpers.h"
60 #include "shared/os-compatibility.h"
61 #include "shared/timespec-util.h"
62 #include "git-version.h"
63 #include "version.h"
64
65 #define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
66
67 static void
68 weston_output_transform_scale_init(struct weston_output *output,
69 uint32_t transform, uint32_t scale);
70
71 static void
72 weston_compositor_build_view_list(struct weston_compositor *compositor);
73
weston_mode_switch_finish(struct weston_output * output,int mode_changed,int scale_changed)74 static void weston_mode_switch_finish(struct weston_output *output,
75 int mode_changed,
76 int scale_changed)
77 {
78 struct weston_seat *seat;
79 struct wl_resource *resource;
80 pixman_region32_t old_output_region;
81 int version;
82
83 pixman_region32_init(&old_output_region);
84 pixman_region32_copy(&old_output_region, &output->region);
85
86 /* Update output region and transformation matrix */
87 weston_output_transform_scale_init(output, output->transform, output->current_scale);
88
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
95 /* 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) {
98 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
99 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
124 if (!mode_changed && !scale_changed)
125 return;
126
127 /* notify clients of the changes */
128 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 }
136
137 version = wl_resource_get_version(resource);
138 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
139 wl_output_send_scale(resource, output->current_scale);
140
141 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
142 wl_output_send_done(resource);
143 }
144 }
145
146 WL_EXPORT int
weston_output_mode_set_native(struct weston_output * output,struct weston_mode * mode,int32_t scale)147 weston_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;
165 }
166 }
167
168 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
176 WL_EXPORT int
weston_output_mode_switch_to_native(struct weston_output * output)177 weston_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
210 WL_EXPORT int
weston_output_mode_switch_to_temporary(struct weston_output * output,struct weston_mode * mode,int32_t scale)211 weston_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;
236 }
237
238 static void
child_client_exec(int sockfd,const char * path)239 child_client_exec(int sockfd, const char *path)
240 {
241 int clientfd;
242 char s[32];
243 sigset_t allsigs;
244
245 /* do not give our signal mask to the new process */
246 sigfillset(&allsigs);
247 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
248
249 /* 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 }
254
255 /* 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) {
259 weston_log("compositor: dup failed: %s\n", strerror(errno));
260 return;
261 }
262
263 snprintf(s, sizeof s, "%d", clientfd);
264 setenv("WAYLAND_SOCKET", s, 1);
265
266 if (execl(path, path, NULL) < 0)
267 weston_log("compositor: executing '%s' failed: %s\n",
268 path, strerror(errno));
269 }
270
271 WL_EXPORT struct wl_client *
weston_client_launch(struct weston_compositor * compositor,struct weston_process * proc,const char * path,weston_process_cleanup_func_t cleanup)272 weston_client_launch(struct weston_compositor *compositor,
273 struct weston_process *proc,
274 const char *path,
275 weston_process_cleanup_func_t cleanup)
276 {
277 int sv[2];
278 pid_t pid;
279 struct wl_client *client;
280
281 weston_log("launching '%s'\n", path);
282
283 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
284 weston_log("weston_client_launch: "
285 "socketpair failed while launching '%s': %s\n",
286 path, strerror(errno));
287 return NULL;
288 }
289
290 pid = fork();
291 if (pid == -1) {
292 close(sv[0]);
293 close(sv[1]);
294 weston_log("weston_client_launch: "
295 "fork failed while launching '%s': %s\n", path,
296 strerror(errno));
297 return NULL;
298 }
299
300 if (pid == 0) {
301 child_client_exec(sv[1], path);
302 _exit(-1);
303 }
304
305 close(sv[1]);
306
307 client = wl_client_create(compositor->wl_display, sv[0]);
308 if (!client) {
309 close(sv[0]);
310 weston_log("weston_client_launch: "
311 "wl_client_create failed while launching '%s'.\n",
312 path);
313 return NULL;
314 }
315
316 proc->pid = pid;
317 proc->cleanup = cleanup;
318 weston_watch_process(proc);
319
320 return client;
321 }
322
323 struct process_info {
324 struct weston_process proc;
325 char *path;
326 };
327
328 static void
process_handle_sigchld(struct weston_process * process,int status)329 process_handle_sigchld(struct weston_process *process, int status)
330 {
331 struct process_info *pinfo =
332 container_of(process, struct process_info, proc);
333
334 /*
335 * There are no guarantees whether this runs before or after
336 * the wl_client destructor.
337 */
338
339 if (WIFEXITED(status)) {
340 weston_log("%s exited with status %d\n", pinfo->path,
341 WEXITSTATUS(status));
342 } else if (WIFSIGNALED(status)) {
343 weston_log("%s died on signal %d\n", pinfo->path,
344 WTERMSIG(status));
345 } else {
346 weston_log("%s disappeared\n", pinfo->path);
347 }
348
349 free(pinfo->path);
350 free(pinfo);
351 }
352
353 WL_EXPORT struct wl_client *
weston_client_start(struct weston_compositor * compositor,const char * path)354 weston_client_start(struct weston_compositor *compositor, const char *path)
355 {
356 struct process_info *pinfo;
357 struct wl_client *client;
358
359 pinfo = zalloc(sizeof *pinfo);
360 if (!pinfo)
361 return NULL;
362
363 pinfo->path = strdup(path);
364 if (!pinfo->path)
365 goto out_free;
366
367 client = weston_client_launch(compositor, &pinfo->proc, path,
368 process_handle_sigchld);
369 if (!client)
370 goto out_str;
371
372 return client;
373
374 out_str:
375 free(pinfo->path);
376
377 out_free:
378 free(pinfo);
379
380 return NULL;
381 }
382
383 static void
region_init_infinite(pixman_region32_t * region)384 region_init_infinite(pixman_region32_t *region)
385 {
386 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
387 UINT32_MAX, UINT32_MAX);
388 }
389
390 static struct weston_subsurface *
391 weston_surface_to_subsurface(struct weston_surface *surface);
392
393 WL_EXPORT struct weston_view *
weston_view_create(struct weston_surface * surface)394 weston_view_create(struct weston_surface *surface)
395 {
396 struct weston_view *view;
397
398 view = zalloc(sizeof *view);
399 if (view == NULL)
400 return NULL;
401
402 view->surface = surface;
403
404 /* Assign to surface */
405 wl_list_insert(&surface->views, &view->surface_link);
406
407 wl_signal_init(&view->destroy_signal);
408 wl_list_init(&view->link);
409 wl_list_init(&view->layer_link.link);
410
411 pixman_region32_init(&view->clip);
412
413 view->alpha = 1.0;
414 pixman_region32_init(&view->transform.opaque);
415
416 wl_list_init(&view->geometry.transformation_list);
417 wl_list_insert(&view->geometry.transformation_list,
418 &view->transform.position.link);
419 weston_matrix_init(&view->transform.position.matrix);
420 wl_list_init(&view->geometry.child_list);
421 pixman_region32_init(&view->geometry.scissor);
422 pixman_region32_init(&view->transform.boundingbox);
423 view->transform.dirty = 1;
424
425 return view;
426 }
427
428 struct weston_frame_callback {
429 struct wl_resource *resource;
430 struct wl_list link;
431 };
432
433 struct weston_presentation_feedback {
434 struct wl_resource *resource;
435
436 /* XXX: could use just wl_resource_get_link() instead */
437 struct wl_list link;
438
439 /* The per-surface feedback flags */
440 uint32_t psf_flags;
441 };
442
443 static void
weston_presentation_feedback_discard(struct weston_presentation_feedback * feedback)444 weston_presentation_feedback_discard(
445 struct weston_presentation_feedback *feedback)
446 {
447 presentation_feedback_send_discarded(feedback->resource);
448 wl_resource_destroy(feedback->resource);
449 }
450
451 static void
weston_presentation_feedback_discard_list(struct wl_list * list)452 weston_presentation_feedback_discard_list(struct wl_list *list)
453 {
454 struct weston_presentation_feedback *feedback, *tmp;
455
456 wl_list_for_each_safe(feedback, tmp, list, link)
457 weston_presentation_feedback_discard(feedback);
458 }
459
460 static void
weston_presentation_feedback_present(struct weston_presentation_feedback * feedback,struct weston_output * output,uint32_t refresh_nsec,const struct timespec * ts,uint64_t seq,uint32_t flags)461 weston_presentation_feedback_present(
462 struct weston_presentation_feedback *feedback,
463 struct weston_output *output,
464 uint32_t refresh_nsec,
465 const struct timespec *ts,
466 uint64_t seq,
467 uint32_t flags)
468 {
469 struct wl_client *client = wl_resource_get_client(feedback->resource);
470 struct wl_resource *o;
471 uint64_t secs;
472
473 wl_resource_for_each(o, &output->resource_list) {
474 if (wl_resource_get_client(o) != client)
475 continue;
476
477 presentation_feedback_send_sync_output(feedback->resource, o);
478 }
479
480 secs = ts->tv_sec;
481 presentation_feedback_send_presented(feedback->resource,
482 secs >> 32, secs & 0xffffffff,
483 ts->tv_nsec,
484 refresh_nsec,
485 seq >> 32, seq & 0xffffffff,
486 flags | feedback->psf_flags);
487 wl_resource_destroy(feedback->resource);
488 }
489
490 static void
weston_presentation_feedback_present_list(struct wl_list * list,struct weston_output * output,uint32_t refresh_nsec,const struct timespec * ts,uint64_t seq,uint32_t flags)491 weston_presentation_feedback_present_list(struct wl_list *list,
492 struct weston_output *output,
493 uint32_t refresh_nsec,
494 const struct timespec *ts,
495 uint64_t seq,
496 uint32_t flags)
497 {
498 struct weston_presentation_feedback *feedback, *tmp;
499
500 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
501 wl_list_empty(list));
502
503 wl_list_for_each_safe(feedback, tmp, list, link)
504 weston_presentation_feedback_present(feedback, output,
505 refresh_nsec, ts, seq,
506 flags);
507 }
508
509 static void
surface_state_handle_buffer_destroy(struct wl_listener * listener,void * data)510 surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
511 {
512 struct weston_surface_state *state =
513 container_of(listener, struct weston_surface_state,
514 buffer_destroy_listener);
515
516 state->buffer = NULL;
517 }
518
519 static void
weston_surface_state_init(struct weston_surface_state * state)520 weston_surface_state_init(struct weston_surface_state *state)
521 {
522 state->newly_attached = 0;
523 state->buffer = NULL;
524 state->buffer_destroy_listener.notify =
525 surface_state_handle_buffer_destroy;
526 state->sx = 0;
527 state->sy = 0;
528
529 pixman_region32_init(&state->damage);
530 pixman_region32_init(&state->opaque);
531 region_init_infinite(&state->input);
532
533 wl_list_init(&state->frame_callback_list);
534 wl_list_init(&state->feedback_list);
535
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
543 static void
weston_surface_state_fini(struct weston_surface_state * state)544 weston_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
552 weston_presentation_feedback_discard_list(&state->feedback_list);
553
554 pixman_region32_fini(&state->input);
555 pixman_region32_fini(&state->opaque);
556 pixman_region32_fini(&state->damage);
557
558 if (state->buffer)
559 wl_list_remove(&state->buffer_destroy_listener.link);
560 state->buffer = NULL;
561 }
562
563 static void
weston_surface_state_set_buffer(struct weston_surface_state * state,struct weston_buffer * buffer)564 weston_surface_state_set_buffer(struct weston_surface_state *state,
565 struct weston_buffer *buffer)
566 {
567 if (state->buffer == buffer)
568 return;
569
570 if (state->buffer)
571 wl_list_remove(&state->buffer_destroy_listener.link);
572 state->buffer = buffer;
573 if (state->buffer)
574 wl_signal_add(&state->buffer->destroy_signal,
575 &state->buffer_destroy_listener);
576 }
577
578 WL_EXPORT struct weston_surface *
weston_surface_create(struct weston_compositor * compositor)579 weston_surface_create(struct weston_compositor *compositor)
580 {
581 struct weston_surface *surface;
582
583 surface = zalloc(sizeof *surface);
584 if (surface == NULL)
585 return NULL;
586
587 wl_signal_init(&surface->destroy_signal);
588
589 surface->compositor = compositor;
590 surface->ref_count = 1;
591
592 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
593 surface->buffer_viewport.buffer.scale = 1;
594 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
595 surface->buffer_viewport.surface.width = -1;
596
597 weston_surface_state_init(&surface->pending);
598
599 pixman_region32_init(&surface->damage);
600 pixman_region32_init(&surface->opaque);
601 region_init_infinite(&surface->input);
602
603 wl_list_init(&surface->views);
604
605 wl_list_init(&surface->frame_callback_list);
606 wl_list_init(&surface->feedback_list);
607
608 wl_list_init(&surface->subsurface_list);
609 wl_list_init(&surface->subsurface_list_pending);
610
611 weston_matrix_init(&surface->buffer_to_surface_matrix);
612 weston_matrix_init(&surface->surface_to_buffer_matrix);
613
614 return surface;
615 }
616
617 WL_EXPORT void
weston_surface_set_color(struct weston_surface * surface,float red,float green,float blue,float alpha)618 weston_surface_set_color(struct weston_surface *surface,
619 float red, float green, float blue, float alpha)
620 {
621 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
622 }
623
624 WL_EXPORT void
weston_view_to_global_float(struct weston_view * view,float sx,float sy,float * x,float * y)625 weston_view_to_global_float(struct weston_view *view,
626 float sx, float sy, float *x, float *y)
627 {
628 if (view->transform.enabled) {
629 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
630
631 weston_matrix_transform(&view->transform.matrix, &v);
632
633 if (fabsf(v.f[3]) < 1e-6) {
634 weston_log("warning: numerical instability in "
635 "%s(), divisor = %g\n", __func__,
636 v.f[3]);
637 *x = 0;
638 *y = 0;
639 return;
640 }
641
642 *x = v.f[0] / v.f[3];
643 *y = v.f[1] / v.f[3];
644 } else {
645 *x = sx + view->geometry.x;
646 *y = sy + view->geometry.y;
647 }
648 }
649
650 WL_EXPORT void
weston_transformed_coord(int width,int height,enum wl_output_transform transform,int32_t scale,float sx,float sy,float * bx,float * by)651 weston_transformed_coord(int width, int height,
652 enum wl_output_transform transform,
653 int32_t scale,
654 float sx, float sy, float *bx, float *by)
655 {
656 switch (transform) {
657 case WL_OUTPUT_TRANSFORM_NORMAL:
658 default:
659 *bx = sx;
660 *by = sy;
661 break;
662 case WL_OUTPUT_TRANSFORM_FLIPPED:
663 *bx = width - sx;
664 *by = sy;
665 break;
666 case WL_OUTPUT_TRANSFORM_90:
667 *bx = height - sy;
668 *by = sx;
669 break;
670 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
671 *bx = height - sy;
672 *by = width - sx;
673 break;
674 case WL_OUTPUT_TRANSFORM_180:
675 *bx = width - sx;
676 *by = height - sy;
677 break;
678 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
679 *bx = sx;
680 *by = height - sy;
681 break;
682 case WL_OUTPUT_TRANSFORM_270:
683 *bx = sy;
684 *by = width - sx;
685 break;
686 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
687 *bx = sy;
688 *by = sx;
689 break;
690 }
691
692 *bx *= scale;
693 *by *= scale;
694 }
695
696 WL_EXPORT pixman_box32_t
weston_transformed_rect(int width,int height,enum wl_output_transform transform,int32_t scale,pixman_box32_t rect)697 weston_transformed_rect(int width, int height,
698 enum wl_output_transform transform,
699 int32_t scale,
700 pixman_box32_t rect)
701 {
702 float x1, x2, y1, y2;
703
704 pixman_box32_t ret;
705
706 weston_transformed_coord(width, height, transform, scale,
707 rect.x1, rect.y1, &x1, &y1);
708 weston_transformed_coord(width, height, transform, scale,
709 rect.x2, rect.y2, &x2, &y2);
710
711 if (x1 <= x2) {
712 ret.x1 = x1;
713 ret.x2 = x2;
714 } else {
715 ret.x1 = x2;
716 ret.x2 = x1;
717 }
718
719 if (y1 <= y2) {
720 ret.y1 = y1;
721 ret.y2 = y2;
722 } else {
723 ret.y1 = y2;
724 ret.y2 = y1;
725 }
726
727 return ret;
728 }
729
730 WL_EXPORT void
weston_transformed_region(int width,int height,enum wl_output_transform transform,int32_t scale,pixman_region32_t * src,pixman_region32_t * dest)731 weston_transformed_region(int width, int height,
732 enum wl_output_transform transform,
733 int32_t scale,
734 pixman_region32_t *src, pixman_region32_t *dest)
735 {
736 pixman_box32_t *src_rects, *dest_rects;
737 int nrects, i;
738
739 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
740 if (src != dest)
741 pixman_region32_copy(dest, src);
742 return;
743 }
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 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
751 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
752 } else {
753 for (i = 0; i < nrects; i++) {
754 switch (transform) {
755 default:
756 case WL_OUTPUT_TRANSFORM_NORMAL:
757 dest_rects[i].x1 = src_rects[i].x1;
758 dest_rects[i].y1 = src_rects[i].y1;
759 dest_rects[i].x2 = src_rects[i].x2;
760 dest_rects[i].y2 = src_rects[i].y2;
761 break;
762 case WL_OUTPUT_TRANSFORM_90:
763 dest_rects[i].x1 = height - src_rects[i].y2;
764 dest_rects[i].y1 = src_rects[i].x1;
765 dest_rects[i].x2 = height - src_rects[i].y1;
766 dest_rects[i].y2 = src_rects[i].x2;
767 break;
768 case WL_OUTPUT_TRANSFORM_180:
769 dest_rects[i].x1 = width - src_rects[i].x2;
770 dest_rects[i].y1 = height - src_rects[i].y2;
771 dest_rects[i].x2 = width - src_rects[i].x1;
772 dest_rects[i].y2 = height - src_rects[i].y1;
773 break;
774 case WL_OUTPUT_TRANSFORM_270:
775 dest_rects[i].x1 = src_rects[i].y1;
776 dest_rects[i].y1 = width - src_rects[i].x2;
777 dest_rects[i].x2 = src_rects[i].y2;
778 dest_rects[i].y2 = width - src_rects[i].x1;
779 break;
780 case WL_OUTPUT_TRANSFORM_FLIPPED:
781 dest_rects[i].x1 = width - src_rects[i].x2;
782 dest_rects[i].y1 = src_rects[i].y1;
783 dest_rects[i].x2 = width - src_rects[i].x1;
784 dest_rects[i].y2 = src_rects[i].y2;
785 break;
786 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
787 dest_rects[i].x1 = height - src_rects[i].y2;
788 dest_rects[i].y1 = width - src_rects[i].x2;
789 dest_rects[i].x2 = height - src_rects[i].y1;
790 dest_rects[i].y2 = width - src_rects[i].x1;
791 break;
792 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
793 dest_rects[i].x1 = src_rects[i].x1;
794 dest_rects[i].y1 = height - src_rects[i].y2;
795 dest_rects[i].x2 = src_rects[i].x2;
796 dest_rects[i].y2 = height - src_rects[i].y1;
797 break;
798 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
799 dest_rects[i].x1 = src_rects[i].y1;
800 dest_rects[i].y1 = src_rects[i].x1;
801 dest_rects[i].x2 = src_rects[i].y2;
802 dest_rects[i].y2 = src_rects[i].x2;
803 break;
804 }
805 }
806 }
807
808 if (scale != 1) {
809 for (i = 0; i < nrects; i++) {
810 dest_rects[i].x1 *= scale;
811 dest_rects[i].x2 *= scale;
812 dest_rects[i].y1 *= scale;
813 dest_rects[i].y2 *= scale;
814 }
815 }
816
817 pixman_region32_clear(dest);
818 pixman_region32_init_rects(dest, dest_rects, nrects);
819 free(dest_rects);
820 }
821
822 static void
scaler_surface_to_buffer(struct weston_surface * surface,float sx,float sy,float * bx,float * by)823 scaler_surface_to_buffer(struct weston_surface *surface,
824 float sx, float sy, float *bx, float *by)
825 {
826 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
827 double src_width, src_height;
828 double src_x, src_y;
829
830 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
831 if (vp->surface.width == -1) {
832 *bx = sx;
833 *by = sy;
834 return;
835 }
836
837 src_x = 0.0;
838 src_y = 0.0;
839 src_width = surface->width_from_buffer;
840 src_height = surface->height_from_buffer;
841 } else {
842 src_x = wl_fixed_to_double(vp->buffer.src_x);
843 src_y = wl_fixed_to_double(vp->buffer.src_y);
844 src_width = wl_fixed_to_double(vp->buffer.src_width);
845 src_height = wl_fixed_to_double(vp->buffer.src_height);
846 }
847
848 *bx = sx * src_width / surface->width + src_x;
849 *by = sy * src_height / surface->height + src_y;
850 }
851
852 WL_EXPORT void
weston_surface_to_buffer_float(struct weston_surface * surface,float sx,float sy,float * bx,float * by)853 weston_surface_to_buffer_float(struct weston_surface *surface,
854 float sx, float sy, float *bx, float *by)
855 {
856 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
857
858 /* first transform coordinates if the scaler is set */
859 scaler_surface_to_buffer(surface, sx, sy, bx, by);
860
861 weston_transformed_coord(surface->width_from_buffer,
862 surface->height_from_buffer,
863 vp->buffer.transform, vp->buffer.scale,
864 *bx, *by, bx, by);
865 }
866
867 WL_EXPORT void
weston_surface_to_buffer(struct weston_surface * surface,int sx,int sy,int * bx,int * by)868 weston_surface_to_buffer(struct weston_surface *surface,
869 int sx, int sy, int *bx, int *by)
870 {
871 float bxf, byf;
872
873 weston_surface_to_buffer_float(surface,
874 sx, sy, &bxf, &byf);
875
876 *bx = floorf(bxf);
877 *by = floorf(byf);
878 }
879
880 WL_EXPORT pixman_box32_t
weston_surface_to_buffer_rect(struct weston_surface * surface,pixman_box32_t rect)881 weston_surface_to_buffer_rect(struct weston_surface *surface,
882 pixman_box32_t rect)
883 {
884 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
885 float xf, yf;
886
887 /* first transform box coordinates if the scaler is set */
888 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
889 rect.x1 = floorf(xf);
890 rect.y1 = floorf(yf);
891
892 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
893 rect.x2 = floorf(xf);
894 rect.y2 = floorf(yf);
895
896 return weston_transformed_rect(surface->width_from_buffer,
897 surface->height_from_buffer,
898 vp->buffer.transform, vp->buffer.scale,
899 rect);
900 }
901
902 /** Transform a region from surface coordinates to buffer coordinates
903 *
904 * \param surface The surface to fetch wl_viewport and buffer transformation
905 * from.
906 * \param surface_region[in] The region in surface coordinates.
907 * \param buffer_region[out] The region converted to buffer coordinates.
908 *
909 * Buffer_region must be init'd, but will be completely overwritten.
910 *
911 * Viewport and buffer transformations can only do translation, scaling,
912 * and rotations in 90-degree steps. Therefore the only loss in the
913 * conversion is coordinate flooring (rounding).
914 */
915 WL_EXPORT void
weston_surface_to_buffer_region(struct weston_surface * surface,pixman_region32_t * surface_region,pixman_region32_t * buffer_region)916 weston_surface_to_buffer_region(struct weston_surface *surface,
917 pixman_region32_t *surface_region,
918 pixman_region32_t *buffer_region)
919 {
920 pixman_box32_t *src_rects, *dest_rects;
921 int nrects, i;
922
923 src_rects = pixman_region32_rectangles(surface_region, &nrects);
924 dest_rects = malloc(nrects * sizeof(*dest_rects));
925 if (!dest_rects)
926 return;
927
928 for (i = 0; i < nrects; i++) {
929 dest_rects[i] = weston_surface_to_buffer_rect(surface,
930 src_rects[i]);
931 }
932
933 pixman_region32_fini(buffer_region);
934 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
935 free(dest_rects);
936 }
937
938 WL_EXPORT void
weston_view_move_to_plane(struct weston_view * view,struct weston_plane * plane)939 weston_view_move_to_plane(struct weston_view *view,
940 struct weston_plane *plane)
941 {
942 if (view->plane == plane)
943 return;
944
945 weston_view_damage_below(view);
946 view->plane = plane;
947 weston_surface_damage(view->surface);
948 }
949
950 /** Inflict damage on the plane where the view is visible.
951 *
952 * \param view The view that causes the damage.
953 *
954 * If the view is currently on a plane (including the primary plane),
955 * take the view's boundingbox, subtract all the opaque views that cover it,
956 * and add the remaining region as damage to the plane. This corresponds
957 * to the damage inflicted to the plane if this view disappeared.
958 *
959 * A repaint is scheduled for this view.
960 *
961 * The region of all opaque views covering this view is stored in
962 * weston_view::clip and updated by view_accumulate_damage() during
963 * weston_output_repaint(). Specifically, that region matches the
964 * scenegraph as it was last painted.
965 */
966 WL_EXPORT void
weston_view_damage_below(struct weston_view * view)967 weston_view_damage_below(struct weston_view *view)
968 {
969 pixman_region32_t damage;
970
971 pixman_region32_init(&damage);
972 pixman_region32_subtract(&damage, &view->transform.boundingbox,
973 &view->clip);
974 if (view->plane)
975 pixman_region32_union(&view->plane->damage,
976 &view->plane->damage, &damage);
977 pixman_region32_fini(&damage);
978 weston_view_schedule_repaint(view);
979 }
980
981 static void
weston_surface_update_output_mask(struct weston_surface * es,uint32_t mask)982 weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
983 {
984 uint32_t different = es->output_mask ^ mask;
985 uint32_t entered = mask & different;
986 uint32_t left = es->output_mask & different;
987 struct weston_output *output;
988 struct wl_resource *resource = NULL;
989 struct wl_client *client;
990
991 es->output_mask = mask;
992 if (es->resource == NULL)
993 return;
994 if (different == 0)
995 return;
996
997 client = wl_resource_get_client(es->resource);
998
999 wl_list_for_each(output, &es->compositor->output_list, link) {
1000 if (1 << output->id & different)
1001 resource =
1002 wl_resource_find_for_client(&output->resource_list,
1003 client);
1004 if (resource == NULL)
1005 continue;
1006 if (1 << output->id & entered)
1007 wl_surface_send_enter(es->resource, resource);
1008 if (1 << output->id & left)
1009 wl_surface_send_leave(es->resource, resource);
1010 }
1011 }
1012
1013
1014 static void
weston_surface_assign_output(struct weston_surface * es)1015 weston_surface_assign_output(struct weston_surface *es)
1016 {
1017 struct weston_output *new_output;
1018 struct weston_view *view;
1019 pixman_region32_t region;
1020 uint32_t max, area, mask;
1021 pixman_box32_t *e;
1022
1023 new_output = NULL;
1024 max = 0;
1025 mask = 0;
1026 pixman_region32_init(®ion);
1027 wl_list_for_each(view, &es->views, surface_link) {
1028 if (!view->output)
1029 continue;
1030
1031 pixman_region32_intersect(®ion, &view->transform.boundingbox,
1032 &view->output->region);
1033
1034 e = pixman_region32_extents(®ion);
1035 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1036
1037 mask |= view->output_mask;
1038
1039 if (area >= max) {
1040 new_output = view->output;
1041 max = area;
1042 }
1043 }
1044 pixman_region32_fini(®ion);
1045
1046 es->output = new_output;
1047 weston_surface_update_output_mask(es, mask);
1048 }
1049
1050 static void
weston_view_assign_output(struct weston_view * ev)1051 weston_view_assign_output(struct weston_view *ev)
1052 {
1053 struct weston_compositor *ec = ev->surface->compositor;
1054 struct weston_output *output, *new_output;
1055 pixman_region32_t region;
1056 uint32_t max, area, mask;
1057 pixman_box32_t *e;
1058
1059 new_output = NULL;
1060 max = 0;
1061 mask = 0;
1062 pixman_region32_init(®ion);
1063 wl_list_for_each(output, &ec->output_list, link) {
1064 if (output->destroying)
1065 continue;
1066
1067 pixman_region32_intersect(®ion, &ev->transform.boundingbox,
1068 &output->region);
1069
1070 e = pixman_region32_extents(®ion);
1071 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1072
1073 if (area > 0)
1074 mask |= 1 << output->id;
1075
1076 if (area >= max) {
1077 new_output = output;
1078 max = area;
1079 }
1080 }
1081 pixman_region32_fini(®ion);
1082
1083 ev->output = new_output;
1084 ev->output_mask = mask;
1085
1086 weston_surface_assign_output(ev->surface);
1087 }
1088
1089 static void
weston_view_to_view_map(struct weston_view * from,struct weston_view * to,int from_x,int from_y,int * to_x,int * to_y)1090 weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1091 int from_x, int from_y, int *to_x, int *to_y)
1092 {
1093 float x, y;
1094
1095 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1096 weston_view_from_global_float(to, x, y, &x, &y);
1097
1098 *to_x = round(x);
1099 *to_y = round(y);
1100 }
1101
1102 static void
weston_view_transfer_scissor(struct weston_view * from,struct weston_view * to)1103 weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1104 {
1105 pixman_box32_t *a;
1106 pixman_box32_t b;
1107
1108 a = pixman_region32_extents(&from->geometry.scissor);
1109
1110 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1111 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1112
1113 pixman_region32_fini(&to->geometry.scissor);
1114 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1115 }
1116
1117 static void
view_compute_bbox(struct weston_view * view,const pixman_box32_t * inbox,pixman_region32_t * bbox)1118 view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
1119 pixman_region32_t *bbox)
1120 {
1121 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1122 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
1123 int32_t s[4][2] = {
1124 { inbox->x1, inbox->y1 },
1125 { inbox->x1, inbox->y2 },
1126 { inbox->x2, inbox->y1 },
1127 { inbox->x2, inbox->y2 },
1128 };
1129 float int_x, int_y;
1130 int i;
1131
1132 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
1133 /* avoid rounding empty bbox to 1x1 */
1134 pixman_region32_init(bbox);
1135 return;
1136 }
1137
1138 for (i = 0; i < 4; ++i) {
1139 float x, y;
1140 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
1141 if (x < min_x)
1142 min_x = x;
1143 if (x > max_x)
1144 max_x = x;
1145 if (y < min_y)
1146 min_y = y;
1147 if (y > max_y)
1148 max_y = y;
1149 }
1150
1151 int_x = floorf(min_x);
1152 int_y = floorf(min_y);
1153 pixman_region32_init_rect(bbox, int_x, int_y,
1154 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
1155 }
1156
1157 static void
weston_view_update_transform_disable(struct weston_view * view)1158 weston_view_update_transform_disable(struct weston_view *view)
1159 {
1160 view->transform.enabled = 0;
1161
1162 /* round off fractions when not transformed */
1163 view->geometry.x = roundf(view->geometry.x);
1164 view->geometry.y = roundf(view->geometry.y);
1165
1166 /* Otherwise identity matrix, but with x and y translation. */
1167 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1168 view->transform.position.matrix.d[12] = view->geometry.x;
1169 view->transform.position.matrix.d[13] = view->geometry.y;
1170
1171 view->transform.matrix = view->transform.position.matrix;
1172
1173 view->transform.inverse = view->transform.position.matrix;
1174 view->transform.inverse.d[12] = -view->geometry.x;
1175 view->transform.inverse.d[13] = -view->geometry.y;
1176
1177 pixman_region32_init_rect(&view->transform.boundingbox,
1178 0, 0,
1179 view->surface->width,
1180 view->surface->height);
1181 if (view->geometry.scissor_enabled)
1182 pixman_region32_intersect(&view->transform.boundingbox,
1183 &view->transform.boundingbox,
1184 &view->geometry.scissor);
1185
1186 pixman_region32_translate(&view->transform.boundingbox,
1187 view->geometry.x, view->geometry.y);
1188
1189 if (view->alpha == 1.0) {
1190 pixman_region32_copy(&view->transform.opaque,
1191 &view->surface->opaque);
1192 pixman_region32_translate(&view->transform.opaque,
1193 view->geometry.x,
1194 view->geometry.y);
1195 }
1196 }
1197
1198 static int
weston_view_update_transform_enable(struct weston_view * view)1199 weston_view_update_transform_enable(struct weston_view *view)
1200 {
1201 struct weston_view *parent = view->geometry.parent;
1202 struct weston_matrix *matrix = &view->transform.matrix;
1203 struct weston_matrix *inverse = &view->transform.inverse;
1204 struct weston_transform *tform;
1205 pixman_region32_t surfregion;
1206 const pixman_box32_t *surfbox;
1207
1208 view->transform.enabled = 1;
1209
1210 /* Otherwise identity matrix, but with x and y translation. */
1211 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1212 view->transform.position.matrix.d[12] = view->geometry.x;
1213 view->transform.position.matrix.d[13] = view->geometry.y;
1214
1215 weston_matrix_init(matrix);
1216 wl_list_for_each(tform, &view->geometry.transformation_list, link)
1217 weston_matrix_multiply(matrix, &tform->matrix);
1218
1219 if (parent)
1220 weston_matrix_multiply(matrix, &parent->transform.matrix);
1221
1222 if (weston_matrix_invert(inverse, matrix) < 0) {
1223 /* Oops, bad total transformation, not invertible */
1224 weston_log("error: weston_view %p"
1225 " transformation not invertible.\n", view);
1226 return -1;
1227 }
1228
1229 pixman_region32_init_rect(&surfregion, 0, 0,
1230 view->surface->width, view->surface->height);
1231 if (view->geometry.scissor_enabled)
1232 pixman_region32_intersect(&surfregion, &surfregion,
1233 &view->geometry.scissor);
1234 surfbox = pixman_region32_extents(&surfregion);
1235
1236 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1237 pixman_region32_fini(&surfregion);
1238
1239 return 0;
1240 }
1241
1242 static struct weston_layer *
get_view_layer(struct weston_view * view)1243 get_view_layer(struct weston_view *view)
1244 {
1245 if (view->parent_view)
1246 return get_view_layer(view->parent_view);
1247 return view->layer_link.layer;
1248 }
1249
1250 WL_EXPORT void
weston_view_update_transform(struct weston_view * view)1251 weston_view_update_transform(struct weston_view *view)
1252 {
1253 struct weston_view *parent = view->geometry.parent;
1254 struct weston_layer *layer;
1255 pixman_region32_t mask;
1256
1257 if (!view->transform.dirty)
1258 return;
1259
1260 if (parent)
1261 weston_view_update_transform(parent);
1262
1263 view->transform.dirty = 0;
1264
1265 weston_view_damage_below(view);
1266
1267 pixman_region32_fini(&view->transform.boundingbox);
1268 pixman_region32_fini(&view->transform.opaque);
1269 pixman_region32_init(&view->transform.opaque);
1270
1271 /* transform.position is always in transformation_list */
1272 if (view->geometry.transformation_list.next ==
1273 &view->transform.position.link &&
1274 view->geometry.transformation_list.prev ==
1275 &view->transform.position.link &&
1276 !parent) {
1277 weston_view_update_transform_disable(view);
1278 } else {
1279 if (weston_view_update_transform_enable(view) < 0)
1280 weston_view_update_transform_disable(view);
1281 }
1282
1283 layer = get_view_layer(view);
1284 if (layer) {
1285 pixman_region32_init_with_extents(&mask, &layer->mask);
1286 pixman_region32_intersect(&view->transform.boundingbox,
1287 &view->transform.boundingbox, &mask);
1288 pixman_region32_intersect(&view->transform.opaque,
1289 &view->transform.opaque, &mask);
1290 pixman_region32_fini(&mask);
1291 }
1292
1293 if (parent) {
1294 if (parent->geometry.scissor_enabled) {
1295 view->geometry.scissor_enabled = true;
1296 weston_view_transfer_scissor(parent, view);
1297 } else {
1298 view->geometry.scissor_enabled = false;
1299 }
1300 }
1301
1302 weston_view_damage_below(view);
1303
1304 weston_view_assign_output(view);
1305
1306 wl_signal_emit(&view->surface->compositor->transform_signal,
1307 view->surface);
1308 }
1309
1310 WL_EXPORT void
weston_view_geometry_dirty(struct weston_view * view)1311 weston_view_geometry_dirty(struct weston_view *view)
1312 {
1313 struct weston_view *child;
1314
1315 /*
1316 * The invariant: if view->geometry.dirty, then all views
1317 * in view->geometry.child_list have geometry.dirty too.
1318 * Corollary: if not parent->geometry.dirty, then all ancestors
1319 * are not dirty.
1320 */
1321
1322 if (view->transform.dirty)
1323 return;
1324
1325 view->transform.dirty = 1;
1326
1327 wl_list_for_each(child, &view->geometry.child_list,
1328 geometry.parent_link)
1329 weston_view_geometry_dirty(child);
1330 }
1331
1332 WL_EXPORT void
weston_view_to_global_fixed(struct weston_view * view,wl_fixed_t vx,wl_fixed_t vy,wl_fixed_t * x,wl_fixed_t * y)1333 weston_view_to_global_fixed(struct weston_view *view,
1334 wl_fixed_t vx, wl_fixed_t vy,
1335 wl_fixed_t *x, wl_fixed_t *y)
1336 {
1337 float xf, yf;
1338
1339 weston_view_to_global_float(view,
1340 wl_fixed_to_double(vx),
1341 wl_fixed_to_double(vy),
1342 &xf, &yf);
1343 *x = wl_fixed_from_double(xf);
1344 *y = wl_fixed_from_double(yf);
1345 }
1346
1347 WL_EXPORT void
weston_view_from_global_float(struct weston_view * view,float x,float y,float * vx,float * vy)1348 weston_view_from_global_float(struct weston_view *view,
1349 float x, float y, float *vx, float *vy)
1350 {
1351 if (view->transform.enabled) {
1352 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1353
1354 weston_matrix_transform(&view->transform.inverse, &v);
1355
1356 if (fabsf(v.f[3]) < 1e-6) {
1357 weston_log("warning: numerical instability in "
1358 "weston_view_from_global(), divisor = %g\n",
1359 v.f[3]);
1360 *vx = 0;
1361 *vy = 0;
1362 return;
1363 }
1364
1365 *vx = v.f[0] / v.f[3];
1366 *vy = v.f[1] / v.f[3];
1367 } else {
1368 *vx = x - view->geometry.x;
1369 *vy = y - view->geometry.y;
1370 }
1371 }
1372
1373 WL_EXPORT void
weston_view_from_global_fixed(struct weston_view * view,wl_fixed_t x,wl_fixed_t y,wl_fixed_t * vx,wl_fixed_t * vy)1374 weston_view_from_global_fixed(struct weston_view *view,
1375 wl_fixed_t x, wl_fixed_t y,
1376 wl_fixed_t *vx, wl_fixed_t *vy)
1377 {
1378 float vxf, vyf;
1379
1380 weston_view_from_global_float(view,
1381 wl_fixed_to_double(x),
1382 wl_fixed_to_double(y),
1383 &vxf, &vyf);
1384 *vx = wl_fixed_from_double(vxf);
1385 *vy = wl_fixed_from_double(vyf);
1386 }
1387
1388 WL_EXPORT void
weston_view_from_global(struct weston_view * view,int32_t x,int32_t y,int32_t * vx,int32_t * vy)1389 weston_view_from_global(struct weston_view *view,
1390 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
1391 {
1392 float vxf, vyf;
1393
1394 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1395 *vx = floorf(vxf);
1396 *vy = floorf(vyf);
1397 }
1398
1399 WL_EXPORT void
weston_surface_schedule_repaint(struct weston_surface * surface)1400 weston_surface_schedule_repaint(struct weston_surface *surface)
1401 {
1402 struct weston_output *output;
1403
1404 wl_list_for_each(output, &surface->compositor->output_list, link)
1405 if (surface->output_mask & (1 << output->id))
1406 weston_output_schedule_repaint(output);
1407 }
1408
1409 WL_EXPORT void
weston_view_schedule_repaint(struct weston_view * view)1410 weston_view_schedule_repaint(struct weston_view *view)
1411 {
1412 struct weston_output *output;
1413
1414 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1415 if (view->output_mask & (1 << output->id))
1416 weston_output_schedule_repaint(output);
1417 }
1418
1419 /**
1420 * XXX: This function does it the wrong way.
1421 * surface->damage is the damage from the client, and causes
1422 * surface_flush_damage() to copy pixels. No window management action can
1423 * cause damage to the client-provided content, warranting re-upload!
1424 *
1425 * Instead of surface->damage, this function should record the damage
1426 * with all the views for this surface to avoid extraneous texture
1427 * uploads.
1428 */
1429 WL_EXPORT void
weston_surface_damage(struct weston_surface * surface)1430 weston_surface_damage(struct weston_surface *surface)
1431 {
1432 pixman_region32_union_rect(&surface->damage, &surface->damage,
1433 0, 0, surface->width,
1434 surface->height);
1435
1436 weston_surface_schedule_repaint(surface);
1437 }
1438
1439 WL_EXPORT void
weston_view_set_position(struct weston_view * view,float x,float y)1440 weston_view_set_position(struct weston_view *view, float x, float y)
1441 {
1442 if (view->geometry.x == x && view->geometry.y == y)
1443 return;
1444
1445 view->geometry.x = x;
1446 view->geometry.y = y;
1447 weston_view_geometry_dirty(view);
1448 }
1449
1450 static void
transform_parent_handle_parent_destroy(struct wl_listener * listener,void * data)1451 transform_parent_handle_parent_destroy(struct wl_listener *listener,
1452 void *data)
1453 {
1454 struct weston_view *view =
1455 container_of(listener, struct weston_view,
1456 geometry.parent_destroy_listener);
1457
1458 weston_view_set_transform_parent(view, NULL);
1459 }
1460
1461 WL_EXPORT void
weston_view_set_transform_parent(struct weston_view * view,struct weston_view * parent)1462 weston_view_set_transform_parent(struct weston_view *view,
1463 struct weston_view *parent)
1464 {
1465 if (view->geometry.parent) {
1466 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1467 wl_list_remove(&view->geometry.parent_link);
1468
1469 if (!parent)
1470 view->geometry.scissor_enabled = false;
1471 }
1472
1473 view->geometry.parent = parent;
1474
1475 view->geometry.parent_destroy_listener.notify =
1476 transform_parent_handle_parent_destroy;
1477 if (parent) {
1478 wl_signal_add(&parent->destroy_signal,
1479 &view->geometry.parent_destroy_listener);
1480 wl_list_insert(&parent->geometry.child_list,
1481 &view->geometry.parent_link);
1482 }
1483
1484 weston_view_geometry_dirty(view);
1485 }
1486
1487 /** Set a clip mask rectangle on a view
1488 *
1489 * \param view The view to set the clip mask on.
1490 * \param x Top-left corner X coordinate of the clip rectangle.
1491 * \param y Top-left corner Y coordinate of the clip rectangle.
1492 * \param width Width of the clip rectangle, non-negative.
1493 * \param height Height of the clip rectangle, non-negative.
1494 *
1495 * A shell may set a clip mask rectangle on a view. Everything outside
1496 * the rectangle is cut away for input and output purposes: it is
1497 * not drawn and cannot be hit by hit-test based input like pointer
1498 * motion or touch-downs. Everything inside the rectangle will behave
1499 * normally. Clients are unaware of clipping.
1500 *
1501 * The rectangle is set in the surface local coordinates. Setting a clip
1502 * mask rectangle does not affect the view position, the view is positioned
1503 * as it would be without a clip. The clip also does not change
1504 * weston_surface::width,height.
1505 *
1506 * The clip mask rectangle is part of transformation inheritance
1507 * (weston_view_set_transform_parent()). A clip set in the root of the
1508 * transformation inheritance tree will affect all views in the tree.
1509 * A clip can be set only on the root view. Attempting to set a clip
1510 * on view that has a transformation parent will fail. Assigning a parent
1511 * to a view that has a clip set will cause the clip to be forgotten.
1512 *
1513 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1514 * on the additional transformations in the child views. These transformations
1515 * may not rotate the coordinate axes, i.e., only translation and scaling
1516 * are allowed. Violating this restriction causes the clipping to malfunction.
1517 * Furthermore, using scaling may cause rounding errors in child clipping.
1518 *
1519 * The clip mask rectangle is not automatically adjusted based on
1520 * wl_surface.attach dx and dy arguments.
1521 *
1522 * A clip mask rectangle can be set only if the compositor capability
1523 * WESTON_CAP_VIEW_CLIP_MASK is present.
1524 *
1525 * This function sets the clip mask rectangle and schedules a repaint for
1526 * the view.
1527 */
1528 WL_EXPORT void
weston_view_set_mask(struct weston_view * view,int x,int y,int width,int height)1529 weston_view_set_mask(struct weston_view *view,
1530 int x, int y, int width, int height)
1531 {
1532 struct weston_compositor *compositor = view->surface->compositor;
1533
1534 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1535 weston_log("%s not allowed without capability!\n", __func__);
1536 return;
1537 }
1538
1539 if (view->geometry.parent) {
1540 weston_log("view %p has a parent, clip forbidden!\n", view);
1541 return;
1542 }
1543
1544 if (width < 0 || height < 0) {
1545 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1546 x, y, width, height);
1547 return;
1548 }
1549
1550 pixman_region32_fini(&view->geometry.scissor);
1551 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1552 view->geometry.scissor_enabled = true;
1553 weston_view_geometry_dirty(view);
1554 weston_view_schedule_repaint(view);
1555 }
1556
1557 /** Remove the clip mask from a view
1558 *
1559 * \param view The view to remove the clip mask from.
1560 *
1561 * Removed the clip mask rectangle and schedules a repaint.
1562 *
1563 * \sa weston_view_set_mask
1564 */
1565 WL_EXPORT void
weston_view_set_mask_infinite(struct weston_view * view)1566 weston_view_set_mask_infinite(struct weston_view *view)
1567 {
1568 view->geometry.scissor_enabled = false;
1569 weston_view_geometry_dirty(view);
1570 weston_view_schedule_repaint(view);
1571 }
1572
1573 WL_EXPORT bool
weston_view_is_mapped(struct weston_view * view)1574 weston_view_is_mapped(struct weston_view *view)
1575 {
1576 if (view->output)
1577 return true;
1578 else
1579 return false;
1580 }
1581
1582 WL_EXPORT bool
weston_surface_is_mapped(struct weston_surface * surface)1583 weston_surface_is_mapped(struct weston_surface *surface)
1584 {
1585 if (surface->output)
1586 return true;
1587 else
1588 return false;
1589 }
1590
1591 static void
surface_set_size(struct weston_surface * surface,int32_t width,int32_t height)1592 surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
1593 {
1594 struct weston_view *view;
1595
1596 if (surface->width == width && surface->height == height)
1597 return;
1598
1599 surface->width = width;
1600 surface->height = height;
1601
1602 wl_list_for_each(view, &surface->views, surface_link)
1603 weston_view_geometry_dirty(view);
1604 }
1605
1606 WL_EXPORT void
weston_surface_set_size(struct weston_surface * surface,int32_t width,int32_t height)1607 weston_surface_set_size(struct weston_surface *surface,
1608 int32_t width, int32_t height)
1609 {
1610 assert(!surface->resource);
1611 surface_set_size(surface, width, height);
1612 }
1613
1614 static int
fixed_round_up_to_int(wl_fixed_t f)1615 fixed_round_up_to_int(wl_fixed_t f)
1616 {
1617 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1618 }
1619
1620 static void
weston_surface_calculate_size_from_buffer(struct weston_surface * surface)1621 weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
1622 {
1623 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1624 int32_t width, height;
1625
1626 if (!surface->buffer_ref.buffer) {
1627 surface->width_from_buffer = 0;
1628 surface->height_from_buffer = 0;
1629 return;
1630 }
1631
1632 switch (vp->buffer.transform) {
1633 case WL_OUTPUT_TRANSFORM_90:
1634 case WL_OUTPUT_TRANSFORM_270:
1635 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1636 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1637 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1638 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
1639 break;
1640 default:
1641 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1642 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
1643 break;
1644 }
1645
1646 surface->width_from_buffer = width;
1647 surface->height_from_buffer = height;
1648 }
1649
1650 static void
weston_surface_update_size(struct weston_surface * surface)1651 weston_surface_update_size(struct weston_surface *surface)
1652 {
1653 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1654 int32_t width, height;
1655
1656 width = surface->width_from_buffer;
1657 height = surface->height_from_buffer;
1658
1659 if (width != 0 && vp->surface.width != -1) {
1660 surface_set_size(surface,
1661 vp->surface.width, vp->surface.height);
1662 return;
1663 }
1664
1665 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
1666 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1667 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1668
1669 surface_set_size(surface, w ?: 1, h ?: 1);
1670 return;
1671 }
1672
1673 surface_set_size(surface, width, height);
1674 }
1675
1676 WL_EXPORT uint32_t
weston_compositor_get_time(void)1677 weston_compositor_get_time(void)
1678 {
1679 struct timeval tv;
1680
1681 gettimeofday(&tv, NULL);
1682
1683 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
1684 }
1685
1686 WL_EXPORT struct weston_view *
weston_compositor_pick_view(struct weston_compositor * compositor,wl_fixed_t x,wl_fixed_t y,wl_fixed_t * vx,wl_fixed_t * vy)1687 weston_compositor_pick_view(struct weston_compositor *compositor,
1688 wl_fixed_t x, wl_fixed_t y,
1689 wl_fixed_t *vx, wl_fixed_t *vy)
1690 {
1691 struct weston_view *view;
1692 wl_fixed_t view_x, view_y;
1693 int view_ix, view_iy;
1694 int ix = wl_fixed_to_int(x);
1695 int iy = wl_fixed_to_int(y);
1696
1697 wl_list_for_each(view, &compositor->view_list, link) {
1698 if (!pixman_region32_contains_point(
1699 &view->transform.boundingbox, ix, iy, NULL))
1700 continue;
1701
1702 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1703 view_ix = wl_fixed_to_int(view_x);
1704 view_iy = wl_fixed_to_int(view_y);
1705
1706 if (!pixman_region32_contains_point(&view->surface->input,
1707 view_ix, view_iy, NULL))
1708 continue;
1709
1710 if (view->geometry.scissor_enabled &&
1711 !pixman_region32_contains_point(&view->geometry.scissor,
1712 view_ix, view_iy, NULL))
1713 continue;
1714
1715 *vx = view_x;
1716 *vy = view_y;
1717 return view;
1718 }
1719
1720 *vx = wl_fixed_from_int(-1000000);
1721 *vy = wl_fixed_from_int(-1000000);
1722 return NULL;
1723 }
1724
1725 static void
weston_compositor_repick(struct weston_compositor * compositor)1726 weston_compositor_repick(struct weston_compositor *compositor)
1727 {
1728 struct weston_seat *seat;
1729
1730 if (!compositor->session_active)
1731 return;
1732
1733 wl_list_for_each(seat, &compositor->seat_list, link)
1734 weston_seat_repick(seat);
1735 }
1736
1737 WL_EXPORT void
weston_view_unmap(struct weston_view * view)1738 weston_view_unmap(struct weston_view *view)
1739 {
1740 struct weston_seat *seat;
1741
1742 if (!weston_view_is_mapped(view))
1743 return;
1744
1745 weston_view_damage_below(view);
1746 view->output = NULL;
1747 view->plane = NULL;
1748 weston_layer_entry_remove(&view->layer_link);
1749 wl_list_remove(&view->link);
1750 wl_list_init(&view->link);
1751 view->output_mask = 0;
1752 weston_surface_assign_output(view->surface);
1753
1754 if (weston_surface_is_mapped(view->surface))
1755 return;
1756
1757 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1758 struct weston_touch *touch = weston_seat_get_touch(seat);
1759 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1760 struct weston_keyboard *keyboard =
1761 weston_seat_get_keyboard(seat);
1762
1763 if (keyboard && keyboard->focus == view->surface)
1764 weston_keyboard_set_focus(keyboard, NULL);
1765 if (pointer && pointer->focus == view)
1766 weston_pointer_clear_focus(pointer);
1767 if (touch && touch->focus == view)
1768 weston_touch_set_focus(touch, NULL);
1769 }
1770 }
1771
1772 WL_EXPORT void
weston_surface_unmap(struct weston_surface * surface)1773 weston_surface_unmap(struct weston_surface *surface)
1774 {
1775 struct weston_view *view;
1776
1777 wl_list_for_each(view, &surface->views, surface_link)
1778 weston_view_unmap(view);
1779 surface->output = NULL;
1780 }
1781
1782 static void
weston_surface_reset_pending_buffer(struct weston_surface * surface)1783 weston_surface_reset_pending_buffer(struct weston_surface *surface)
1784 {
1785 weston_surface_state_set_buffer(&surface->pending, NULL);
1786 surface->pending.sx = 0;
1787 surface->pending.sy = 0;
1788 surface->pending.newly_attached = 0;
1789 surface->pending.buffer_viewport.changed = 0;
1790 }
1791
1792 WL_EXPORT void
weston_view_destroy(struct weston_view * view)1793 weston_view_destroy(struct weston_view *view)
1794 {
1795 wl_signal_emit(&view->destroy_signal, view);
1796
1797 assert(wl_list_empty(&view->geometry.child_list));
1798
1799 if (weston_view_is_mapped(view)) {
1800 weston_view_unmap(view);
1801 weston_compositor_build_view_list(view->surface->compositor);
1802 }
1803
1804 wl_list_remove(&view->link);
1805 weston_layer_entry_remove(&view->layer_link);
1806
1807 pixman_region32_fini(&view->clip);
1808 pixman_region32_fini(&view->geometry.scissor);
1809 pixman_region32_fini(&view->transform.boundingbox);
1810 pixman_region32_fini(&view->transform.opaque);
1811
1812 weston_view_set_transform_parent(view, NULL);
1813
1814 wl_list_remove(&view->surface_link);
1815
1816 free(view);
1817 }
1818
1819 WL_EXPORT void
weston_surface_destroy(struct weston_surface * surface)1820 weston_surface_destroy(struct weston_surface *surface)
1821 {
1822 struct weston_frame_callback *cb, *next;
1823 struct weston_view *ev, *nv;
1824
1825 if (--surface->ref_count > 0)
1826 return;
1827
1828 assert(surface->resource == NULL);
1829
1830 wl_signal_emit(&surface->destroy_signal, surface);
1831
1832 assert(wl_list_empty(&surface->subsurface_list_pending));
1833 assert(wl_list_empty(&surface->subsurface_list));
1834
1835 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1836 weston_view_destroy(ev);
1837
1838 weston_surface_state_fini(&surface->pending);
1839
1840 weston_buffer_reference(&surface->buffer_ref, NULL);
1841
1842 pixman_region32_fini(&surface->damage);
1843 pixman_region32_fini(&surface->opaque);
1844 pixman_region32_fini(&surface->input);
1845
1846 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
1847 wl_resource_destroy(cb->resource);
1848
1849 weston_presentation_feedback_discard_list(&surface->feedback_list);
1850
1851 free(surface);
1852 }
1853
1854 static void
destroy_surface(struct wl_resource * resource)1855 destroy_surface(struct wl_resource *resource)
1856 {
1857 struct weston_surface *surface = wl_resource_get_user_data(resource);
1858
1859 assert(surface);
1860
1861 /* Set the resource to NULL, since we don't want to leave a
1862 * dangling pointer if the surface was refcounted and survives
1863 * the weston_surface_destroy() call. */
1864 surface->resource = NULL;
1865 weston_surface_destroy(surface);
1866 }
1867
1868 static void
weston_buffer_destroy_handler(struct wl_listener * listener,void * data)1869 weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1870 {
1871 struct weston_buffer *buffer =
1872 container_of(listener, struct weston_buffer, destroy_listener);
1873
1874 wl_signal_emit(&buffer->destroy_signal, buffer);
1875 free(buffer);
1876 }
1877
1878 WL_EXPORT struct weston_buffer *
weston_buffer_from_resource(struct wl_resource * resource)1879 weston_buffer_from_resource(struct wl_resource *resource)
1880 {
1881 struct weston_buffer *buffer;
1882 struct wl_listener *listener;
1883
1884 listener = wl_resource_get_destroy_listener(resource,
1885 weston_buffer_destroy_handler);
1886
1887 if (listener)
1888 return container_of(listener, struct weston_buffer,
1889 destroy_listener);
1890
1891 buffer = zalloc(sizeof *buffer);
1892 if (buffer == NULL)
1893 return NULL;
1894
1895 buffer->resource = resource;
1896 wl_signal_init(&buffer->destroy_signal);
1897 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
1898 buffer->y_inverted = 1;
1899 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
1900
1901 return buffer;
1902 }
1903
1904 static void
weston_buffer_reference_handle_destroy(struct wl_listener * listener,void * data)1905 weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1906 void *data)
1907 {
1908 struct weston_buffer_reference *ref =
1909 container_of(listener, struct weston_buffer_reference,
1910 destroy_listener);
1911
1912 assert((struct weston_buffer *)data == ref->buffer);
1913 ref->buffer = NULL;
1914 }
1915
1916 WL_EXPORT void
weston_buffer_reference(struct weston_buffer_reference * ref,struct weston_buffer * buffer)1917 weston_buffer_reference(struct weston_buffer_reference *ref,
1918 struct weston_buffer *buffer)
1919 {
1920 if (ref->buffer && buffer != ref->buffer) {
1921 ref->buffer->busy_count--;
1922 if (ref->buffer->busy_count == 0) {
1923 assert(wl_resource_get_client(ref->buffer->resource));
1924 wl_resource_queue_event(ref->buffer->resource,
1925 WL_BUFFER_RELEASE);
1926 }
1927 wl_list_remove(&ref->destroy_listener.link);
1928 }
1929
1930 if (buffer && buffer != ref->buffer) {
1931 buffer->busy_count++;
1932 wl_signal_add(&buffer->destroy_signal,
1933 &ref->destroy_listener);
1934 }
1935
1936 ref->buffer = buffer;
1937 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1938 }
1939
1940 static void
weston_surface_attach(struct weston_surface * surface,struct weston_buffer * buffer)1941 weston_surface_attach(struct weston_surface *surface,
1942 struct weston_buffer *buffer)
1943 {
1944 weston_buffer_reference(&surface->buffer_ref, buffer);
1945
1946 if (!buffer) {
1947 if (weston_surface_is_mapped(surface))
1948 weston_surface_unmap(surface);
1949 }
1950
1951 surface->compositor->renderer->attach(surface, buffer);
1952
1953 weston_surface_calculate_size_from_buffer(surface);
1954 weston_presentation_feedback_discard_list(&surface->feedback_list);
1955 }
1956
1957 WL_EXPORT void
weston_compositor_damage_all(struct weston_compositor * compositor)1958 weston_compositor_damage_all(struct weston_compositor *compositor)
1959 {
1960 struct weston_output *output;
1961
1962 wl_list_for_each(output, &compositor->output_list, link)
1963 weston_output_damage(output);
1964 }
1965
1966 WL_EXPORT void
weston_output_damage(struct weston_output * output)1967 weston_output_damage(struct weston_output *output)
1968 {
1969 struct weston_compositor *compositor = output->compositor;
1970
1971 pixman_region32_union(&compositor->primary_plane.damage,
1972 &compositor->primary_plane.damage,
1973 &output->region);
1974 weston_output_schedule_repaint(output);
1975 }
1976
1977 static void
surface_flush_damage(struct weston_surface * surface)1978 surface_flush_damage(struct weston_surface *surface)
1979 {
1980 if (surface->buffer_ref.buffer &&
1981 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
1982 surface->compositor->renderer->flush_damage(surface);
1983
1984 if (weston_timeline_enabled_ &&
1985 pixman_region32_not_empty(&surface->damage))
1986 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1987 TLP_OUTPUT(surface->output), TLP_END);
1988
1989 pixman_region32_clear(&surface->damage);
1990 }
1991
1992 static void
view_accumulate_damage(struct weston_view * view,pixman_region32_t * opaque)1993 view_accumulate_damage(struct weston_view *view,
1994 pixman_region32_t *opaque)
1995 {
1996 pixman_region32_t damage;
1997
1998 pixman_region32_init(&damage);
1999 if (view->transform.enabled) {
2000 pixman_box32_t *extents;
2001
2002 extents = pixman_region32_extents(&view->surface->damage);
2003 view_compute_bbox(view, extents, &damage);
2004 } else {
2005 pixman_region32_copy(&damage, &view->surface->damage);
2006 pixman_region32_translate(&damage,
2007 view->geometry.x, view->geometry.y);
2008 }
2009
2010 pixman_region32_intersect(&damage, &damage,
2011 &view->transform.boundingbox);
2012 pixman_region32_subtract(&damage, &damage, opaque);
2013 pixman_region32_union(&view->plane->damage,
2014 &view->plane->damage, &damage);
2015 pixman_region32_fini(&damage);
2016 pixman_region32_copy(&view->clip, opaque);
2017 pixman_region32_union(opaque, opaque, &view->transform.opaque);
2018 }
2019
2020 static void
compositor_accumulate_damage(struct weston_compositor * ec)2021 compositor_accumulate_damage(struct weston_compositor *ec)
2022 {
2023 struct weston_plane *plane;
2024 struct weston_view *ev;
2025 pixman_region32_t opaque, clip;
2026
2027 pixman_region32_init(&clip);
2028
2029 wl_list_for_each(plane, &ec->plane_list, link) {
2030 pixman_region32_copy(&plane->clip, &clip);
2031
2032 pixman_region32_init(&opaque);
2033
2034 wl_list_for_each(ev, &ec->view_list, link) {
2035 if (ev->plane != plane)
2036 continue;
2037
2038 view_accumulate_damage(ev, &opaque);
2039 }
2040
2041 pixman_region32_union(&clip, &clip, &opaque);
2042 pixman_region32_fini(&opaque);
2043 }
2044
2045 pixman_region32_fini(&clip);
2046
2047 wl_list_for_each(ev, &ec->view_list, link)
2048 ev->surface->touched = 0;
2049
2050 wl_list_for_each(ev, &ec->view_list, link) {
2051 if (ev->surface->touched)
2052 continue;
2053 ev->surface->touched = 1;
2054
2055 surface_flush_damage(ev->surface);
2056
2057 /* Both the renderer and the backend have seen the buffer
2058 * by now. If renderer needs the buffer, it has its own
2059 * reference set. If the backend wants to keep the buffer
2060 * around for migrating the surface into a non-primary plane
2061 * later, keep_buffer is true. Otherwise, drop the core
2062 * reference now, and allow early buffer release. This enables
2063 * clients to use single-buffering.
2064 */
2065 if (!ev->surface->keep_buffer)
2066 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
2067 }
2068 }
2069
2070 static void
surface_stash_subsurface_views(struct weston_surface * surface)2071 surface_stash_subsurface_views(struct weston_surface *surface)
2072 {
2073 struct weston_subsurface *sub;
2074
2075 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2076 if (sub->surface == surface)
2077 continue;
2078
2079 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2080 wl_list_init(&sub->surface->views);
2081
2082 surface_stash_subsurface_views(sub->surface);
2083 }
2084 }
2085
2086 static void
surface_free_unused_subsurface_views(struct weston_surface * surface)2087 surface_free_unused_subsurface_views(struct weston_surface *surface)
2088 {
2089 struct weston_subsurface *sub;
2090 struct weston_view *view, *nv;
2091
2092 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2093 if (sub->surface == surface)
2094 continue;
2095
2096 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2097 weston_view_unmap (view);
2098 weston_view_destroy(view);
2099 }
2100
2101 surface_free_unused_subsurface_views(sub->surface);
2102 }
2103 }
2104
2105 static void
view_list_add_subsurface_view(struct weston_compositor * compositor,struct weston_subsurface * sub,struct weston_view * parent)2106 view_list_add_subsurface_view(struct weston_compositor *compositor,
2107 struct weston_subsurface *sub,
2108 struct weston_view *parent)
2109 {
2110 struct weston_subsurface *child;
2111 struct weston_view *view = NULL, *iv;
2112
2113 if (!weston_surface_is_mapped(sub->surface))
2114 return;
2115
2116 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2117 if (iv->geometry.parent == parent) {
2118 view = iv;
2119 break;
2120 }
2121 }
2122
2123 if (view) {
2124 /* Put it back in the surface's list of views */
2125 wl_list_remove(&view->surface_link);
2126 wl_list_insert(&sub->surface->views, &view->surface_link);
2127 } else {
2128 view = weston_view_create(sub->surface);
2129 weston_view_set_position(view,
2130 sub->position.x,
2131 sub->position.y);
2132 weston_view_set_transform_parent(view, parent);
2133 }
2134
2135 view->parent_view = parent;
2136 weston_view_update_transform(view);
2137
2138 if (wl_list_empty(&sub->surface->subsurface_list)) {
2139 wl_list_insert(compositor->view_list.prev, &view->link);
2140 return;
2141 }
2142
2143 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2144 if (child->surface == sub->surface)
2145 wl_list_insert(compositor->view_list.prev, &view->link);
2146 else
2147 view_list_add_subsurface_view(compositor, child, view);
2148 }
2149 }
2150
2151 static void
view_list_add(struct weston_compositor * compositor,struct weston_view * view)2152 view_list_add(struct weston_compositor *compositor,
2153 struct weston_view *view)
2154 {
2155 struct weston_subsurface *sub;
2156
2157 weston_view_update_transform(view);
2158
2159 if (wl_list_empty(&view->surface->subsurface_list)) {
2160 wl_list_insert(compositor->view_list.prev, &view->link);
2161 return;
2162 }
2163
2164 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2165 if (sub->surface == view->surface)
2166 wl_list_insert(compositor->view_list.prev, &view->link);
2167 else
2168 view_list_add_subsurface_view(compositor, sub, view);
2169 }
2170 }
2171
2172 static void
weston_compositor_build_view_list(struct weston_compositor * compositor)2173 weston_compositor_build_view_list(struct weston_compositor *compositor)
2174 {
2175 struct weston_view *view;
2176 struct weston_layer *layer;
2177
2178 wl_list_for_each(layer, &compositor->layer_list, link)
2179 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
2180 surface_stash_subsurface_views(view->surface);
2181
2182 wl_list_init(&compositor->view_list);
2183 wl_list_for_each(layer, &compositor->layer_list, link) {
2184 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2185 view_list_add(compositor, view);
2186 }
2187 }
2188
2189 wl_list_for_each(layer, &compositor->layer_list, link)
2190 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
2191 surface_free_unused_subsurface_views(view->surface);
2192 }
2193
2194 static void
weston_output_take_feedback_list(struct weston_output * output,struct weston_surface * surface)2195 weston_output_take_feedback_list(struct weston_output *output,
2196 struct weston_surface *surface)
2197 {
2198 struct weston_view *view;
2199 struct weston_presentation_feedback *feedback;
2200 uint32_t flags = 0xffffffff;
2201
2202 if (wl_list_empty(&surface->feedback_list))
2203 return;
2204
2205 /* All views must have the flag for the flag to survive. */
2206 wl_list_for_each(view, &surface->views, surface_link) {
2207 /* ignore views that are not on this output at all */
2208 if (view->output_mask & (1u << output->id))
2209 flags &= view->psf_flags;
2210 }
2211
2212 wl_list_for_each(feedback, &surface->feedback_list, link)
2213 feedback->psf_flags = flags;
2214
2215 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2216 wl_list_init(&surface->feedback_list);
2217 }
2218
2219 static int
weston_output_repaint(struct weston_output * output)2220 weston_output_repaint(struct weston_output *output)
2221 {
2222 struct weston_compositor *ec = output->compositor;
2223 struct weston_view *ev;
2224 struct weston_animation *animation, *next;
2225 struct weston_frame_callback *cb, *cnext;
2226 struct wl_list frame_callback_list;
2227 pixman_region32_t output_damage;
2228 int r;
2229
2230 if (output->destroying)
2231 return 0;
2232
2233 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2234
2235 /* Rebuild the surface list and update surface transforms up front. */
2236 weston_compositor_build_view_list(ec);
2237
2238 if (output->assign_planes && !output->disable_planes) {
2239 output->assign_planes(output);
2240 } else {
2241 wl_list_for_each(ev, &ec->view_list, link) {
2242 weston_view_move_to_plane(ev, &ec->primary_plane);
2243 ev->psf_flags = 0;
2244 }
2245 }
2246
2247 wl_list_init(&frame_callback_list);
2248 wl_list_for_each(ev, &ec->view_list, link) {
2249 /* Note: This operation is safe to do multiple times on the
2250 * same surface.
2251 */
2252 if (ev->surface->output == output) {
2253 wl_list_insert_list(&frame_callback_list,
2254 &ev->surface->frame_callback_list);
2255 wl_list_init(&ev->surface->frame_callback_list);
2256
2257 weston_output_take_feedback_list(output, ev->surface);
2258 }
2259 }
2260
2261 compositor_accumulate_damage(ec);
2262
2263 pixman_region32_init(&output_damage);
2264 pixman_region32_intersect(&output_damage,
2265 &ec->primary_plane.damage, &output->region);
2266 pixman_region32_subtract(&output_damage,
2267 &output_damage, &ec->primary_plane.clip);
2268
2269 if (output->dirty)
2270 weston_output_update_matrix(output);
2271
2272 r = output->repaint(output, &output_damage);
2273
2274 pixman_region32_fini(&output_damage);
2275
2276 output->repaint_needed = 0;
2277
2278 weston_compositor_repick(ec);
2279 /* XXX can we do similar behaviour with the libevent2 event loop? */
2280 // wl_event_loop_dispatch(ec->input_loop, 0);
2281
2282 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
2283 wl_callback_send_done(cb->resource, output->frame_time);
2284 wl_resource_destroy(cb->resource);
2285 }
2286
2287 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
2288 animation->frame_counter++;
2289 animation->frame(animation, output, output->frame_time);
2290 }
2291
2292 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2293
2294 return r;
2295 }
2296
2297 static void
weston_output_schedule_repaint_reset(struct weston_output * output)2298 weston_output_schedule_repaint_reset(struct weston_output *output)
2299 {
2300 #if 0
2301 struct weston_compositor *compositor = output->compositor;
2302 struct wl_event_loop *loop;
2303 int fd;
2304 #endif
2305
2306 output->repaint_scheduled = 0;
2307 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2308
2309 /* XXX Implement similar behaviour with libevent */
2310 #if 0
2311 if (compositor->input_loop_source)
2312 return;
2313
2314 loop = wl_display_get_event_loop(compositor->wl_display);
2315 fd = wl_event_loop_get_fd(compositor->input_loop);
2316 compositor->input_loop_source =
2317 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2318 weston_compositor_read_input, compositor);
2319 #endif
2320 }
2321
2322 static int
output_repaint_timer_handler(void * data)2323 output_repaint_timer_handler(void *data)
2324 {
2325 struct weston_output *output = data;
2326 struct weston_compositor *compositor = output->compositor;
2327
2328 if (output->repaint_needed &&
2329 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2330 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2331 weston_output_repaint(output) == 0)
2332 return 0;
2333
2334 weston_output_schedule_repaint_reset(output);
2335
2336 return 0;
2337 }
2338
2339 WL_EXPORT void
weston_output_finish_frame(struct weston_output * output,const struct timespec * stamp,uint32_t presented_flags)2340 weston_output_finish_frame(struct weston_output *output,
2341 const struct timespec *stamp,
2342 uint32_t presented_flags)
2343 {
2344 struct weston_compositor *compositor = output->compositor;
2345 int32_t refresh_nsec;
2346 struct timespec now;
2347 struct timespec gone;
2348 int msec;
2349
2350 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2351 TLP_VBLANK(stamp), TLP_END);
2352
2353 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
2354 weston_presentation_feedback_present_list(&output->feedback_list,
2355 output, refresh_nsec, stamp,
2356 output->msc,
2357 presented_flags);
2358
2359 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
2360
2361 weston_compositor_read_presentation_clock(compositor, &now);
2362 timespec_sub(&gone, &now, stamp);
2363 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2364 msec -= compositor->repaint_msec;
2365
2366 if (msec < -1000 || msec > 1000) {
2367 static bool warned;
2368
2369 if (!warned)
2370 weston_log("Warning: computed repaint delay is "
2371 "insane: %d msec\n", msec);
2372 warned = true;
2373
2374 msec = 0;
2375 }
2376
2377 /* Called from restart_repaint_loop and restart happens already after
2378 * the deadline given by repaint_msec? In that case we delay until
2379 * the deadline of the next frame, to give clients a more predictable
2380 * timing of the repaint cycle to lock on. */
2381 if (presented_flags == PRESENTATION_FEEDBACK_INVALID && msec < 0)
2382 msec += refresh_nsec / 1000000;
2383
2384 if (msec < 1)
2385 output_repaint_timer_handler(output);
2386 else
2387 wl_event_source_timer_update(output->repaint_timer, msec);
2388 }
2389
2390 static void
idle_repaint(void * data)2391 idle_repaint(void *data)
2392 {
2393 struct weston_output *output = data;
2394
2395 output->start_repaint_loop(output);
2396 }
2397
2398 WL_EXPORT void
weston_layer_entry_insert(struct weston_layer_entry * list,struct weston_layer_entry * entry)2399 weston_layer_entry_insert(struct weston_layer_entry *list,
2400 struct weston_layer_entry *entry)
2401 {
2402 wl_list_insert(&list->link, &entry->link);
2403 entry->layer = list->layer;
2404 }
2405
2406 WL_EXPORT void
weston_layer_entry_remove(struct weston_layer_entry * entry)2407 weston_layer_entry_remove(struct weston_layer_entry *entry)
2408 {
2409 wl_list_remove(&entry->link);
2410 wl_list_init(&entry->link);
2411 entry->layer = NULL;
2412 }
2413
2414 WL_EXPORT void
weston_layer_init(struct weston_layer * layer,struct wl_list * below)2415 weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2416 {
2417 wl_list_init(&layer->view_list.link);
2418 layer->view_list.layer = layer;
2419 weston_layer_set_mask_infinite(layer);
2420 if (below != NULL)
2421 wl_list_insert(below, &layer->link);
2422 }
2423
2424 WL_EXPORT void
weston_layer_set_mask(struct weston_layer * layer,int x,int y,int width,int height)2425 weston_layer_set_mask(struct weston_layer *layer,
2426 int x, int y, int width, int height)
2427 {
2428 struct weston_view *view;
2429
2430 layer->mask.x1 = x;
2431 layer->mask.x2 = x + width;
2432 layer->mask.y1 = y;
2433 layer->mask.y2 = y + height;
2434
2435 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2436 weston_view_geometry_dirty(view);
2437 }
2438 }
2439
2440 WL_EXPORT void
weston_layer_set_mask_infinite(struct weston_layer * layer)2441 weston_layer_set_mask_infinite(struct weston_layer *layer)
2442 {
2443 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2444 UINT32_MAX, UINT32_MAX);
2445 }
2446
2447 WL_EXPORT void
weston_output_schedule_repaint(struct weston_output * output)2448 weston_output_schedule_repaint(struct weston_output *output)
2449 {
2450 struct weston_compositor *compositor = output->compositor;
2451 struct wl_event_loop *loop;
2452
2453 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2454 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2455 return;
2456
2457 if (!output->repaint_needed)
2458 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2459
2460 loop = wl_display_get_event_loop(compositor->wl_display);
2461 output->repaint_needed = 1;
2462 if (output->repaint_scheduled)
2463 return;
2464
2465 wl_event_loop_add_idle(loop, idle_repaint, output);
2466 output->repaint_scheduled = 1;
2467 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2468
2469
2470 /* XXX Implement similar behaviour with libevent */
2471 #if 0
2472 if (compositor->input_loop_source) {
2473 wl_event_source_remove(compositor->input_loop_source);
2474 compositor->input_loop_source = NULL;
2475 }
2476 #endif
2477 }
2478
2479 WL_EXPORT void
weston_compositor_schedule_repaint(struct weston_compositor * compositor)2480 weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2481 {
2482 struct weston_output *output;
2483
2484 wl_list_for_each(output, &compositor->output_list, link)
2485 weston_output_schedule_repaint(output);
2486 }
2487
2488 static void
surface_destroy(struct wl_client * client,struct wl_resource * resource)2489 surface_destroy(struct wl_client *client, struct wl_resource *resource)
2490 {
2491 wl_resource_destroy(resource);
2492 }
2493
2494 static void
surface_attach(struct wl_client * client,struct wl_resource * resource,struct wl_resource * buffer_resource,int32_t sx,int32_t sy)2495 surface_attach(struct wl_client *client,
2496 struct wl_resource *resource,
2497 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2498 {
2499 struct weston_surface *surface = wl_resource_get_user_data(resource);
2500 struct weston_buffer *buffer = NULL;
2501
2502 if (buffer_resource) {
2503 buffer = weston_buffer_from_resource(buffer_resource);
2504 if (buffer == NULL) {
2505 wl_client_post_no_memory(client);
2506 return;
2507 }
2508 }
2509
2510 /* Attach, attach, without commit in between does not send
2511 * wl_buffer.release. */
2512 weston_surface_state_set_buffer(&surface->pending, buffer);
2513
2514 surface->pending.sx = sx;
2515 surface->pending.sy = sy;
2516 surface->pending.newly_attached = 1;
2517 }
2518
2519 static void
surface_damage(struct wl_client * client,struct wl_resource * resource,int32_t x,int32_t y,int32_t width,int32_t height)2520 surface_damage(struct wl_client *client,
2521 struct wl_resource *resource,
2522 int32_t x, int32_t y, int32_t width, int32_t height)
2523 {
2524 struct weston_surface *surface = wl_resource_get_user_data(resource);
2525
2526 pixman_region32_union_rect(&surface->pending.damage,
2527 &surface->pending.damage,
2528 x, y, width, height);
2529 }
2530
2531 static void
destroy_frame_callback(struct wl_resource * resource)2532 destroy_frame_callback(struct wl_resource *resource)
2533 {
2534 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
2535
2536 wl_list_remove(&cb->link);
2537 free(cb);
2538 }
2539
2540 static void
surface_frame(struct wl_client * client,struct wl_resource * resource,uint32_t callback)2541 surface_frame(struct wl_client *client,
2542 struct wl_resource *resource, uint32_t callback)
2543 {
2544 struct weston_frame_callback *cb;
2545 struct weston_surface *surface = wl_resource_get_user_data(resource);
2546
2547 cb = malloc(sizeof *cb);
2548 if (cb == NULL) {
2549 wl_resource_post_no_memory(resource);
2550 return;
2551 }
2552
2553 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2554 callback);
2555 if (cb->resource == NULL) {
2556 free(cb);
2557 wl_resource_post_no_memory(resource);
2558 return;
2559 }
2560
2561 wl_resource_set_implementation(cb->resource, NULL, cb,
2562 destroy_frame_callback);
2563
2564 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
2565 }
2566
2567 static void
surface_set_opaque_region(struct wl_client * client,struct wl_resource * resource,struct wl_resource * region_resource)2568 surface_set_opaque_region(struct wl_client *client,
2569 struct wl_resource *resource,
2570 struct wl_resource *region_resource)
2571 {
2572 struct weston_surface *surface = wl_resource_get_user_data(resource);
2573 struct weston_region *region;
2574
2575 if (region_resource) {
2576 region = wl_resource_get_user_data(region_resource);
2577 pixman_region32_copy(&surface->pending.opaque,
2578 ®ion->region);
2579 } else {
2580 pixman_region32_clear(&surface->pending.opaque);
2581 }
2582 }
2583
2584 static void
surface_set_input_region(struct wl_client * client,struct wl_resource * resource,struct wl_resource * region_resource)2585 surface_set_input_region(struct wl_client *client,
2586 struct wl_resource *resource,
2587 struct wl_resource *region_resource)
2588 {
2589 struct weston_surface *surface = wl_resource_get_user_data(resource);
2590 struct weston_region *region;
2591
2592 if (region_resource) {
2593 region = wl_resource_get_user_data(region_resource);
2594 pixman_region32_copy(&surface->pending.input,
2595 ®ion->region);
2596 } else {
2597 pixman_region32_fini(&surface->pending.input);
2598 region_init_infinite(&surface->pending.input);
2599 }
2600 }
2601
2602 static void
weston_surface_commit_subsurface_order(struct weston_surface * surface)2603 weston_surface_commit_subsurface_order(struct weston_surface *surface)
2604 {
2605 struct weston_subsurface *sub;
2606
2607 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2608 parent_link_pending) {
2609 wl_list_remove(&sub->parent_link);
2610 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2611 }
2612 }
2613
2614 static void
weston_surface_build_buffer_matrix(struct weston_surface * surface,struct weston_matrix * matrix)2615 weston_surface_build_buffer_matrix(struct weston_surface *surface,
2616 struct weston_matrix *matrix)
2617 {
2618 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2619 double src_width, src_height, dest_width, dest_height;
2620
2621 weston_matrix_init(matrix);
2622
2623 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2624 src_width = surface->width_from_buffer;
2625 src_height = surface->height_from_buffer;
2626 } else {
2627 src_width = wl_fixed_to_double(vp->buffer.src_width);
2628 src_height = wl_fixed_to_double(vp->buffer.src_height);
2629 }
2630
2631 if (vp->surface.width == -1) {
2632 dest_width = src_width;
2633 dest_height = src_height;
2634 } else {
2635 dest_width = vp->surface.width;
2636 dest_height = vp->surface.height;
2637 }
2638
2639 if (src_width != dest_width || src_height != dest_height)
2640 weston_matrix_scale(matrix,
2641 src_width / dest_width,
2642 src_height / dest_height, 1);
2643
2644 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2645 weston_matrix_translate(matrix,
2646 wl_fixed_to_double(vp->buffer.src_x),
2647 wl_fixed_to_double(vp->buffer.src_y),
2648 0);
2649
2650 switch (vp->buffer.transform) {
2651 case WL_OUTPUT_TRANSFORM_FLIPPED:
2652 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2653 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2654 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2655 weston_matrix_scale(matrix, -1, 1, 1);
2656 weston_matrix_translate(matrix,
2657 surface->width_from_buffer, 0, 0);
2658 break;
2659 }
2660
2661 switch (vp->buffer.transform) {
2662 default:
2663 case WL_OUTPUT_TRANSFORM_NORMAL:
2664 case WL_OUTPUT_TRANSFORM_FLIPPED:
2665 break;
2666 case WL_OUTPUT_TRANSFORM_90:
2667 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2668 weston_matrix_rotate_xy(matrix, 0, 1);
2669 weston_matrix_translate(matrix,
2670 surface->height_from_buffer, 0, 0);
2671 break;
2672 case WL_OUTPUT_TRANSFORM_180:
2673 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2674 weston_matrix_rotate_xy(matrix, -1, 0);
2675 weston_matrix_translate(matrix,
2676 surface->width_from_buffer,
2677 surface->height_from_buffer, 0);
2678 break;
2679 case WL_OUTPUT_TRANSFORM_270:
2680 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2681 weston_matrix_rotate_xy(matrix, 0, -1);
2682 weston_matrix_translate(matrix,
2683 0, surface->width_from_buffer, 0);
2684 break;
2685 }
2686
2687 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2688 }
2689
2690 static void
weston_surface_commit_state(struct weston_surface * surface,struct weston_surface_state * state)2691 weston_surface_commit_state(struct weston_surface *surface,
2692 struct weston_surface_state *state)
2693 {
2694 struct weston_view *view;
2695 pixman_region32_t opaque;
2696
2697 /* wl_surface.set_buffer_transform */
2698 /* wl_surface.set_buffer_scale */
2699 /* wl_viewport.set */
2700 surface->buffer_viewport = state->buffer_viewport;
2701
2702 /* wl_surface.attach */
2703 if (state->newly_attached)
2704 weston_surface_attach(surface, state->buffer);
2705 weston_surface_state_set_buffer(state, NULL);
2706
2707 weston_surface_build_buffer_matrix(surface,
2708 &surface->surface_to_buffer_matrix);
2709 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2710 &surface->surface_to_buffer_matrix);
2711
2712 if (state->newly_attached || state->buffer_viewport.changed) {
2713 weston_surface_update_size(surface);
2714 if (surface->configure)
2715 surface->configure(surface, state->sx, state->sy);
2716 }
2717
2718 state->sx = 0;
2719 state->sy = 0;
2720 state->newly_attached = 0;
2721 state->buffer_viewport.changed = 0;
2722
2723 /* wl_surface.damage */
2724 if (weston_timeline_enabled_ &&
2725 pixman_region32_not_empty(&state->damage))
2726 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
2727 pixman_region32_union(&surface->damage, &surface->damage,
2728 &state->damage);
2729 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2730 0, 0, surface->width, surface->height);
2731 pixman_region32_clear(&state->damage);
2732
2733 /* wl_surface.set_opaque_region */
2734 pixman_region32_init(&opaque);
2735 pixman_region32_intersect_rect(&opaque, &state->opaque,
2736 0, 0, surface->width, surface->height);
2737
2738 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2739 pixman_region32_copy(&surface->opaque, &opaque);
2740 wl_list_for_each(view, &surface->views, surface_link)
2741 weston_view_geometry_dirty(view);
2742 }
2743
2744 pixman_region32_fini(&opaque);
2745
2746 /* wl_surface.set_input_region */
2747 pixman_region32_intersect_rect(&surface->input, &state->input,
2748 0, 0, surface->width, surface->height);
2749
2750 /* wl_surface.frame */
2751 wl_list_insert_list(&surface->frame_callback_list,
2752 &state->frame_callback_list);
2753 wl_list_init(&state->frame_callback_list);
2754
2755 /* XXX:
2756 * What should happen with a feedback request, if there
2757 * is no wl_buffer attached for this commit?
2758 */
2759
2760 /* presentation.feedback */
2761 wl_list_insert_list(&surface->feedback_list,
2762 &state->feedback_list);
2763 wl_list_init(&state->feedback_list);
2764 }
2765
2766 static void
weston_surface_commit(struct weston_surface * surface)2767 weston_surface_commit(struct weston_surface *surface)
2768 {
2769 weston_surface_commit_state(surface, &surface->pending);
2770
2771 weston_surface_commit_subsurface_order(surface);
2772
2773 weston_surface_schedule_repaint(surface);
2774 }
2775
2776 static void
2777 weston_subsurface_commit(struct weston_subsurface *sub);
2778
2779 static void
2780 weston_subsurface_parent_commit(struct weston_subsurface *sub,
2781 int parent_is_synchronized);
2782
2783 static void
surface_commit(struct wl_client * client,struct wl_resource * resource)2784 surface_commit(struct wl_client *client, struct wl_resource *resource)
2785 {
2786 struct weston_surface *surface = wl_resource_get_user_data(resource);
2787 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2788
2789 if (sub) {
2790 weston_subsurface_commit(sub);
2791 return;
2792 }
2793
2794 weston_surface_commit(surface);
2795
2796 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2797 if (sub->surface != surface)
2798 weston_subsurface_parent_commit(sub, 0);
2799 }
2800 }
2801
2802 static void
surface_set_buffer_transform(struct wl_client * client,struct wl_resource * resource,int transform)2803 surface_set_buffer_transform(struct wl_client *client,
2804 struct wl_resource *resource, int transform)
2805 {
2806 struct weston_surface *surface = wl_resource_get_user_data(resource);
2807
2808 /* if wl_output.transform grows more members this will need to be updated. */
2809 if (transform < 0 ||
2810 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2811 wl_resource_post_error(resource,
2812 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2813 "buffer transform must be a valid transform "
2814 "('%d' specified)", transform);
2815 return;
2816 }
2817
2818 surface->pending.buffer_viewport.buffer.transform = transform;
2819 surface->pending.buffer_viewport.changed = 1;
2820 }
2821
2822 static void
surface_set_buffer_scale(struct wl_client * client,struct wl_resource * resource,int32_t scale)2823 surface_set_buffer_scale(struct wl_client *client,
2824 struct wl_resource *resource,
2825 int32_t scale)
2826 {
2827 struct weston_surface *surface = wl_resource_get_user_data(resource);
2828
2829 if (scale < 1) {
2830 wl_resource_post_error(resource,
2831 WL_SURFACE_ERROR_INVALID_SCALE,
2832 "buffer scale must be at least one "
2833 "('%d' specified)", scale);
2834 return;
2835 }
2836
2837 surface->pending.buffer_viewport.buffer.scale = scale;
2838 surface->pending.buffer_viewport.changed = 1;
2839 }
2840
2841 static const struct wl_surface_interface surface_interface = {
2842 surface_destroy,
2843 surface_attach,
2844 surface_damage,
2845 surface_frame,
2846 surface_set_opaque_region,
2847 surface_set_input_region,
2848 surface_commit,
2849 surface_set_buffer_transform,
2850 surface_set_buffer_scale
2851 };
2852
2853 static void
compositor_create_surface(struct wl_client * client,struct wl_resource * resource,uint32_t id)2854 compositor_create_surface(struct wl_client *client,
2855 struct wl_resource *resource, uint32_t id)
2856 {
2857 struct weston_compositor *ec = wl_resource_get_user_data(resource);
2858 struct weston_surface *surface;
2859
2860 surface = weston_surface_create(ec);
2861 if (surface == NULL) {
2862 wl_resource_post_no_memory(resource);
2863 return;
2864 }
2865
2866 surface->resource =
2867 wl_resource_create(client, &wl_surface_interface,
2868 wl_resource_get_version(resource), id);
2869 if (surface->resource == NULL) {
2870 weston_surface_destroy(surface);
2871 wl_resource_post_no_memory(resource);
2872 return;
2873 }
2874 wl_resource_set_implementation(surface->resource, &surface_interface,
2875 surface, destroy_surface);
2876
2877 wl_signal_emit(&ec->create_surface_signal, surface);
2878 }
2879
2880 static void
destroy_region(struct wl_resource * resource)2881 destroy_region(struct wl_resource *resource)
2882 {
2883 struct weston_region *region = wl_resource_get_user_data(resource);
2884
2885 pixman_region32_fini(®ion->region);
2886 free(region);
2887 }
2888
2889 static void
region_destroy(struct wl_client * client,struct wl_resource * resource)2890 region_destroy(struct wl_client *client, struct wl_resource *resource)
2891 {
2892 wl_resource_destroy(resource);
2893 }
2894
2895 static void
region_add(struct wl_client * client,struct wl_resource * resource,int32_t x,int32_t y,int32_t width,int32_t height)2896 region_add(struct wl_client *client, struct wl_resource *resource,
2897 int32_t x, int32_t y, int32_t width, int32_t height)
2898 {
2899 struct weston_region *region = wl_resource_get_user_data(resource);
2900
2901 pixman_region32_union_rect(®ion->region, ®ion->region,
2902 x, y, width, height);
2903 }
2904
2905 static void
region_subtract(struct wl_client * client,struct wl_resource * resource,int32_t x,int32_t y,int32_t width,int32_t height)2906 region_subtract(struct wl_client *client, struct wl_resource *resource,
2907 int32_t x, int32_t y, int32_t width, int32_t height)
2908 {
2909 struct weston_region *region = wl_resource_get_user_data(resource);
2910 pixman_region32_t rect;
2911
2912 pixman_region32_init_rect(&rect, x, y, width, height);
2913 pixman_region32_subtract(®ion->region, ®ion->region, &rect);
2914 pixman_region32_fini(&rect);
2915 }
2916
2917 static const struct wl_region_interface region_interface = {
2918 region_destroy,
2919 region_add,
2920 region_subtract
2921 };
2922
2923 static void
compositor_create_region(struct wl_client * client,struct wl_resource * resource,uint32_t id)2924 compositor_create_region(struct wl_client *client,
2925 struct wl_resource *resource, uint32_t id)
2926 {
2927 struct weston_region *region;
2928
2929 region = malloc(sizeof *region);
2930 if (region == NULL) {
2931 wl_resource_post_no_memory(resource);
2932 return;
2933 }
2934
2935 pixman_region32_init(®ion->region);
2936
2937 region->resource =
2938 wl_resource_create(client, &wl_region_interface, 1, id);
2939 if (region->resource == NULL) {
2940 free(region);
2941 wl_resource_post_no_memory(resource);
2942 return;
2943 }
2944 wl_resource_set_implementation(region->resource, ®ion_interface,
2945 region, destroy_region);
2946 }
2947
2948 static const struct wl_compositor_interface compositor_interface = {
2949 compositor_create_surface,
2950 compositor_create_region
2951 };
2952
2953 static void
weston_subsurface_commit_from_cache(struct weston_subsurface * sub)2954 weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2955 {
2956 struct weston_surface *surface = sub->surface;
2957
2958 weston_surface_commit_state(surface, &sub->cached);
2959 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
2960
2961 weston_surface_commit_subsurface_order(surface);
2962
2963 weston_surface_schedule_repaint(surface);
2964
2965 sub->has_cached_data = 0;
2966 }
2967
2968 static void
weston_subsurface_commit_to_cache(struct weston_subsurface * sub)2969 weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2970 {
2971 struct weston_surface *surface = sub->surface;
2972
2973 /*
2974 * If this commit would cause the surface to move by the
2975 * attach(dx, dy) parameters, the old damage region must be
2976 * translated to correspond to the new surface coordinate system
2977 * original_mode.
2978 */
2979 pixman_region32_translate(&sub->cached.damage,
2980 -surface->pending.sx, -surface->pending.sy);
2981 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2982 &surface->pending.damage);
2983 pixman_region32_clear(&surface->pending.damage);
2984
2985 if (surface->pending.newly_attached) {
2986 sub->cached.newly_attached = 1;
2987 weston_surface_state_set_buffer(&sub->cached,
2988 surface->pending.buffer);
2989 weston_buffer_reference(&sub->cached_buffer_ref,
2990 surface->pending.buffer);
2991 weston_presentation_feedback_discard_list(
2992 &sub->cached.feedback_list);
2993 }
2994 sub->cached.sx += surface->pending.sx;
2995 sub->cached.sy += surface->pending.sy;
2996
2997 sub->cached.buffer_viewport.changed |=
2998 surface->pending.buffer_viewport.changed;
2999 sub->cached.buffer_viewport.buffer =
3000 surface->pending.buffer_viewport.buffer;
3001 sub->cached.buffer_viewport.surface =
3002 surface->pending.buffer_viewport.surface;
3003
3004 weston_surface_reset_pending_buffer(surface);
3005
3006 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3007
3008 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3009
3010 wl_list_insert_list(&sub->cached.frame_callback_list,
3011 &surface->pending.frame_callback_list);
3012 wl_list_init(&surface->pending.frame_callback_list);
3013
3014 wl_list_insert_list(&sub->cached.feedback_list,
3015 &surface->pending.feedback_list);
3016 wl_list_init(&surface->pending.feedback_list);
3017
3018 sub->has_cached_data = 1;
3019 }
3020
3021 static bool
weston_subsurface_is_synchronized(struct weston_subsurface * sub)3022 weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3023 {
3024 while (sub) {
3025 if (sub->synchronized)
3026 return true;
3027
3028 if (!sub->parent)
3029 return false;
3030
3031 sub = weston_surface_to_subsurface(sub->parent);
3032 }
3033
3034 return false;
3035 }
3036
3037 static void
weston_subsurface_commit(struct weston_subsurface * sub)3038 weston_subsurface_commit(struct weston_subsurface *sub)
3039 {
3040 struct weston_surface *surface = sub->surface;
3041 struct weston_subsurface *tmp;
3042
3043 /* Recursive check for effectively synchronized. */
3044 if (weston_subsurface_is_synchronized(sub)) {
3045 weston_subsurface_commit_to_cache(sub);
3046 } else {
3047 if (sub->has_cached_data) {
3048 /* flush accumulated state from cache */
3049 weston_subsurface_commit_to_cache(sub);
3050 weston_subsurface_commit_from_cache(sub);
3051 } else {
3052 weston_surface_commit(surface);
3053 }
3054
3055 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3056 if (tmp->surface != surface)
3057 weston_subsurface_parent_commit(tmp, 0);
3058 }
3059 }
3060 }
3061
3062 static void
weston_subsurface_synchronized_commit(struct weston_subsurface * sub)3063 weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
3064 {
3065 struct weston_surface *surface = sub->surface;
3066 struct weston_subsurface *tmp;
3067
3068 /* From now on, commit_from_cache the whole sub-tree, regardless of
3069 * the synchronized mode of each child. This sub-surface or some
3070 * of its ancestors were synchronized, so we are synchronized
3071 * all the way down.
3072 */
3073
3074 if (sub->has_cached_data)
3075 weston_subsurface_commit_from_cache(sub);
3076
3077 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3078 if (tmp->surface != surface)
3079 weston_subsurface_parent_commit(tmp, 1);
3080 }
3081 }
3082
3083 static void
weston_subsurface_parent_commit(struct weston_subsurface * sub,int parent_is_synchronized)3084 weston_subsurface_parent_commit(struct weston_subsurface *sub,
3085 int parent_is_synchronized)
3086 {
3087 struct weston_view *view;
3088 if (sub->position.set) {
3089 wl_list_for_each(view, &sub->surface->views, surface_link)
3090 weston_view_set_position(view,
3091 sub->position.x,
3092 sub->position.y);
3093
3094 sub->position.set = 0;
3095 }
3096
3097 if (parent_is_synchronized || sub->synchronized)
3098 weston_subsurface_synchronized_commit(sub);
3099 }
3100
3101 static int
subsurface_get_label(struct weston_surface * surface,char * buf,size_t len)3102 subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3103 {
3104 return snprintf(buf, len, "sub-surface");
3105 }
3106
3107 static void
subsurface_configure(struct weston_surface * surface,int32_t dx,int32_t dy)3108 subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
3109 {
3110 struct weston_compositor *compositor = surface->compositor;
3111 struct weston_view *view;
3112
3113 wl_list_for_each(view, &surface->views, surface_link)
3114 weston_view_set_position(view,
3115 view->geometry.x + dx,
3116 view->geometry.y + dy);
3117
3118 /* No need to check parent mappedness, because if parent is not
3119 * mapped, parent is not in a visible layer, so this sub-surface
3120 * will not be drawn either.
3121 */
3122 if (!weston_surface_is_mapped(surface)) {
3123 struct weston_output *output;
3124
3125 /* Cannot call weston_view_update_transform(),
3126 * because that would call it also for the parent surface,
3127 * which might not be mapped yet. That would lead to
3128 * inconsistent state, where the window could never be
3129 * mapped.
3130 *
3131 * Instead just assign any output, to make
3132 * weston_surface_is_mapped() return true, so that when the
3133 * parent surface does get mapped, this one will get
3134 * included, too. See view_list_add().
3135 */
3136 assert(!wl_list_empty(&compositor->output_list));
3137 output = container_of(compositor->output_list.next,
3138 struct weston_output, link);
3139
3140 surface->output = output;
3141 weston_surface_update_output_mask(surface, 1 << output->id);
3142 }
3143 }
3144
3145 static struct weston_subsurface *
weston_surface_to_subsurface(struct weston_surface * surface)3146 weston_surface_to_subsurface(struct weston_surface *surface)
3147 {
3148 if (surface->configure == subsurface_configure)
3149 return surface->configure_private;
3150
3151 return NULL;
3152 }
3153
3154 WL_EXPORT struct weston_surface *
weston_surface_get_main_surface(struct weston_surface * surface)3155 weston_surface_get_main_surface(struct weston_surface *surface)
3156 {
3157 struct weston_subsurface *sub;
3158
3159 while (surface && (sub = weston_surface_to_subsurface(surface)))
3160 surface = sub->parent;
3161
3162 return surface;
3163 }
3164
3165 WL_EXPORT int
weston_surface_set_role(struct weston_surface * surface,const char * role_name,struct wl_resource * error_resource,uint32_t error_code)3166 weston_surface_set_role(struct weston_surface *surface,
3167 const char *role_name,
3168 struct wl_resource *error_resource,
3169 uint32_t error_code)
3170 {
3171 assert(role_name);
3172
3173 if (surface->role_name == NULL ||
3174 surface->role_name == role_name ||
3175 strcmp(surface->role_name, role_name) == 0) {
3176 surface->role_name = role_name;
3177
3178 return 0;
3179 }
3180
3181 wl_resource_post_error(error_resource, error_code,
3182 "Cannot assign role %s to wl_surface@%d,"
3183 " already has role %s\n",
3184 role_name,
3185 wl_resource_get_id(surface->resource),
3186 surface->role_name);
3187 return -1;
3188 }
3189
3190 WL_EXPORT void
weston_surface_set_label_func(struct weston_surface * surface,int (* desc)(struct weston_surface *,char *,size_t))3191 weston_surface_set_label_func(struct weston_surface *surface,
3192 int (*desc)(struct weston_surface *,
3193 char *, size_t))
3194 {
3195 surface->get_label = desc;
3196 surface->timeline.force_refresh = 1;
3197 }
3198
3199 /** Get the size of surface contents
3200 *
3201 * \param surface The surface to query.
3202 * \param width Returns the width of raw contents.
3203 * \param height Returns the height of raw contents.
3204 *
3205 * Retrieves the raw surface content size in pixels for the given surface.
3206 * This is the whole content size in buffer pixels. If the surface
3207 * has no content or the renderer does not implement this feature,
3208 * zeroes are returned.
3209 *
3210 * This function is used to determine the buffer size needed for
3211 * a weston_surface_copy_content() call.
3212 */
3213 WL_EXPORT void
weston_surface_get_content_size(struct weston_surface * surface,int * width,int * height)3214 weston_surface_get_content_size(struct weston_surface *surface,
3215 int *width, int *height)
3216 {
3217 struct weston_renderer *rer = surface->compositor->renderer;
3218
3219 if (!rer->surface_get_content_size) {
3220 *width = 0;
3221 *height = 0;
3222 return;
3223 }
3224
3225 rer->surface_get_content_size(surface, width, height);
3226 }
3227
3228 /** Copy surface contents to system memory.
3229 *
3230 * \param surface The surface to copy from.
3231 * \param target Pointer to the target memory buffer.
3232 * \param size Size of the target buffer in bytes.
3233 * \param src_x X location on contents to copy from.
3234 * \param src_y Y location on contents to copy from.
3235 * \param width Width in pixels of the area to copy.
3236 * \param height Height in pixels of the area to copy.
3237 * \return 0 for success, -1 for failure.
3238 *
3239 * Surface contents are maintained by the renderer. They can be in a
3240 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3241 * else.
3242 *
3243 * Surface contents are copied into memory pointed to by target,
3244 * which has size bytes of space available. The target memory
3245 * may be larger than needed, but being smaller returns an error.
3246 * The extra bytes in target may or may not be written; their content is
3247 * unspecified. Size must be large enough to hold the image.
3248 *
3249 * The image in the target memory will be arranged in rows from
3250 * top to bottom, and pixels on a row from left to right. The pixel
3251 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3252 * width * 4.
3253 *
3254 * Parameters src_x and src_y define the upper-left corner in buffer
3255 * coordinates (pixels) to copy from. Parameters width and height
3256 * define the size of the area to copy in pixels.
3257 *
3258 * The rectangle defined by src_x, src_y, width, height must fit in
3259 * the surface contents. Otherwise an error is returned.
3260 *
3261 * Use surface_get_data_size to determine the content size; the
3262 * needed target buffer size and rectangle limits.
3263 *
3264 * CURRENT IMPLEMENTATION RESTRICTIONS:
3265 * - the machine must be little-endian due to Pixman formats.
3266 *
3267 * NOTE: Pixman formats are premultiplied.
3268 */
3269 WL_EXPORT int
weston_surface_copy_content(struct weston_surface * surface,void * target,size_t size,int src_x,int src_y,int width,int height)3270 weston_surface_copy_content(struct weston_surface *surface,
3271 void *target, size_t size,
3272 int src_x, int src_y,
3273 int width, int height)
3274 {
3275 struct weston_renderer *rer = surface->compositor->renderer;
3276 int cw, ch;
3277 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3278
3279 if (!rer->surface_copy_content)
3280 return -1;
3281
3282 weston_surface_get_content_size(surface, &cw, &ch);
3283
3284 if (src_x < 0 || src_y < 0)
3285 return -1;
3286
3287 if (width <= 0 || height <= 0)
3288 return -1;
3289
3290 if (src_x + width > cw || src_y + height > ch)
3291 return -1;
3292
3293 if (width * bytespp * height > size)
3294 return -1;
3295
3296 return rer->surface_copy_content(surface, target, size,
3297 src_x, src_y, width, height);
3298 }
3299
3300 static void
subsurface_set_position(struct wl_client * client,struct wl_resource * resource,int32_t x,int32_t y)3301 subsurface_set_position(struct wl_client *client,
3302 struct wl_resource *resource, int32_t x, int32_t y)
3303 {
3304 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3305
3306 if (!sub)
3307 return;
3308
3309 sub->position.x = x;
3310 sub->position.y = y;
3311 sub->position.set = 1;
3312 }
3313
3314 static struct weston_subsurface *
subsurface_from_surface(struct weston_surface * surface)3315 subsurface_from_surface(struct weston_surface *surface)
3316 {
3317 struct weston_subsurface *sub;
3318
3319 sub = weston_surface_to_subsurface(surface);
3320 if (sub)
3321 return sub;
3322
3323 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3324 if (sub->surface == surface)
3325 return sub;
3326
3327 return NULL;
3328 }
3329
3330 static struct weston_subsurface *
subsurface_sibling_check(struct weston_subsurface * sub,struct weston_surface * surface,const char * request)3331 subsurface_sibling_check(struct weston_subsurface *sub,
3332 struct weston_surface *surface,
3333 const char *request)
3334 {
3335 struct weston_subsurface *sibling;
3336
3337 sibling = subsurface_from_surface(surface);
3338
3339 if (!sibling) {
3340 wl_resource_post_error(sub->resource,
3341 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3342 "%s: wl_surface@%d is not a parent or sibling",
3343 request, wl_resource_get_id(surface->resource));
3344 return NULL;
3345 }
3346
3347 if (sibling->parent != sub->parent) {
3348 wl_resource_post_error(sub->resource,
3349 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3350 "%s: wl_surface@%d has a different parent",
3351 request, wl_resource_get_id(surface->resource));
3352 return NULL;
3353 }
3354
3355 return sibling;
3356 }
3357
3358 static void
subsurface_place_above(struct wl_client * client,struct wl_resource * resource,struct wl_resource * sibling_resource)3359 subsurface_place_above(struct wl_client *client,
3360 struct wl_resource *resource,
3361 struct wl_resource *sibling_resource)
3362 {
3363 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3364 struct weston_surface *surface =
3365 wl_resource_get_user_data(sibling_resource);
3366 struct weston_subsurface *sibling;
3367
3368 if (!sub)
3369 return;
3370
3371 sibling = subsurface_sibling_check(sub, surface, "place_above");
3372 if (!sibling)
3373 return;
3374
3375 wl_list_remove(&sub->parent_link_pending);
3376 wl_list_insert(sibling->parent_link_pending.prev,
3377 &sub->parent_link_pending);
3378 }
3379
3380 static void
subsurface_place_below(struct wl_client * client,struct wl_resource * resource,struct wl_resource * sibling_resource)3381 subsurface_place_below(struct wl_client *client,
3382 struct wl_resource *resource,
3383 struct wl_resource *sibling_resource)
3384 {
3385 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3386 struct weston_surface *surface =
3387 wl_resource_get_user_data(sibling_resource);
3388 struct weston_subsurface *sibling;
3389
3390 if (!sub)
3391 return;
3392
3393 sibling = subsurface_sibling_check(sub, surface, "place_below");
3394 if (!sibling)
3395 return;
3396
3397 wl_list_remove(&sub->parent_link_pending);
3398 wl_list_insert(&sibling->parent_link_pending,
3399 &sub->parent_link_pending);
3400 }
3401
3402 static void
subsurface_set_sync(struct wl_client * client,struct wl_resource * resource)3403 subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3404 {
3405 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3406
3407 if (sub)
3408 sub->synchronized = 1;
3409 }
3410
3411 static void
subsurface_set_desync(struct wl_client * client,struct wl_resource * resource)3412 subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3413 {
3414 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3415
3416 if (sub && sub->synchronized) {
3417 sub->synchronized = 0;
3418
3419 /* If sub became effectively desynchronized, flush. */
3420 if (!weston_subsurface_is_synchronized(sub))
3421 weston_subsurface_synchronized_commit(sub);
3422 }
3423 }
3424
3425 static void
weston_subsurface_unlink_parent(struct weston_subsurface * sub)3426 weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3427 {
3428 wl_list_remove(&sub->parent_link);
3429 wl_list_remove(&sub->parent_link_pending);
3430 wl_list_remove(&sub->parent_destroy_listener.link);
3431 sub->parent = NULL;
3432 }
3433
3434 static void
3435 weston_subsurface_destroy(struct weston_subsurface *sub);
3436
3437 static void
subsurface_handle_surface_destroy(struct wl_listener * listener,void * data)3438 subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3439 {
3440 struct weston_subsurface *sub =
3441 container_of(listener, struct weston_subsurface,
3442 surface_destroy_listener);
3443 assert(data == sub->surface);
3444
3445 /* The protocol object (wl_resource) is left inert. */
3446 if (sub->resource)
3447 wl_resource_set_user_data(sub->resource, NULL);
3448
3449 weston_subsurface_destroy(sub);
3450 }
3451
3452 static void
subsurface_handle_parent_destroy(struct wl_listener * listener,void * data)3453 subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3454 {
3455 struct weston_subsurface *sub =
3456 container_of(listener, struct weston_subsurface,
3457 parent_destroy_listener);
3458 assert(data == sub->parent);
3459 assert(sub->surface != sub->parent);
3460
3461 if (weston_surface_is_mapped(sub->surface))
3462 weston_surface_unmap(sub->surface);
3463
3464 weston_subsurface_unlink_parent(sub);
3465 }
3466
3467 static void
subsurface_resource_destroy(struct wl_resource * resource)3468 subsurface_resource_destroy(struct wl_resource *resource)
3469 {
3470 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3471
3472 if (sub)
3473 weston_subsurface_destroy(sub);
3474 }
3475
3476 static void
subsurface_destroy(struct wl_client * client,struct wl_resource * resource)3477 subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3478 {
3479 wl_resource_destroy(resource);
3480 }
3481
3482 static void
weston_subsurface_link_parent(struct weston_subsurface * sub,struct weston_surface * parent)3483 weston_subsurface_link_parent(struct weston_subsurface *sub,
3484 struct weston_surface *parent)
3485 {
3486 sub->parent = parent;
3487 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
3488 wl_signal_add(&parent->destroy_signal,
3489 &sub->parent_destroy_listener);
3490
3491 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3492 wl_list_insert(&parent->subsurface_list_pending,
3493 &sub->parent_link_pending);
3494 }
3495
3496 static void
weston_subsurface_link_surface(struct weston_subsurface * sub,struct weston_surface * surface)3497 weston_subsurface_link_surface(struct weston_subsurface *sub,
3498 struct weston_surface *surface)
3499 {
3500 sub->surface = surface;
3501 sub->surface_destroy_listener.notify =
3502 subsurface_handle_surface_destroy;
3503 wl_signal_add(&surface->destroy_signal,
3504 &sub->surface_destroy_listener);
3505 }
3506
3507 static void
weston_subsurface_destroy(struct weston_subsurface * sub)3508 weston_subsurface_destroy(struct weston_subsurface *sub)
3509 {
3510 struct weston_view *view, *next;
3511
3512 assert(sub->surface);
3513
3514 if (sub->resource) {
3515 assert(weston_surface_to_subsurface(sub->surface) == sub);
3516 assert(sub->parent_destroy_listener.notify ==
3517 subsurface_handle_parent_destroy);
3518
3519 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3520 weston_view_unmap(view);
3521 weston_view_destroy(view);
3522 }
3523
3524 if (sub->parent)
3525 weston_subsurface_unlink_parent(sub);
3526
3527 weston_surface_state_fini(&sub->cached);
3528 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
3529
3530 sub->surface->configure = NULL;
3531 sub->surface->configure_private = NULL;
3532 weston_surface_set_label_func(sub->surface, NULL);
3533 } else {
3534 /* the dummy weston_subsurface for the parent itself */
3535 assert(sub->parent_destroy_listener.notify == NULL);
3536 wl_list_remove(&sub->parent_link);
3537 wl_list_remove(&sub->parent_link_pending);
3538 }
3539
3540 wl_list_remove(&sub->surface_destroy_listener.link);
3541 free(sub);
3542 }
3543
3544 static const struct wl_subsurface_interface subsurface_implementation = {
3545 subsurface_destroy,
3546 subsurface_set_position,
3547 subsurface_place_above,
3548 subsurface_place_below,
3549 subsurface_set_sync,
3550 subsurface_set_desync
3551 };
3552
3553 static struct weston_subsurface *
weston_subsurface_create(uint32_t id,struct weston_surface * surface,struct weston_surface * parent)3554 weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3555 struct weston_surface *parent)
3556 {
3557 struct weston_subsurface *sub;
3558 struct wl_client *client = wl_resource_get_client(surface->resource);
3559
3560 sub = zalloc(sizeof *sub);
3561 if (sub == NULL)
3562 return NULL;
3563
3564 wl_list_init(&sub->unused_views);
3565
3566 sub->resource =
3567 wl_resource_create(client, &wl_subsurface_interface, 1, id);
3568 if (!sub->resource) {
3569 free(sub);
3570 return NULL;
3571 }
3572
3573 wl_resource_set_implementation(sub->resource,
3574 &subsurface_implementation,
3575 sub, subsurface_resource_destroy);
3576 weston_subsurface_link_surface(sub, surface);
3577 weston_subsurface_link_parent(sub, parent);
3578 weston_surface_state_init(&sub->cached);
3579 sub->cached_buffer_ref.buffer = NULL;
3580 sub->synchronized = 1;
3581
3582 return sub;
3583 }
3584
3585 /* Create a dummy subsurface for having the parent itself in its
3586 * sub-surface lists. Makes stacking order manipulation easy.
3587 */
3588 static struct weston_subsurface *
weston_subsurface_create_for_parent(struct weston_surface * parent)3589 weston_subsurface_create_for_parent(struct weston_surface *parent)
3590 {
3591 struct weston_subsurface *sub;
3592
3593 sub = zalloc(sizeof *sub);
3594 if (sub == NULL)
3595 return NULL;
3596
3597 weston_subsurface_link_surface(sub, parent);
3598 sub->parent = parent;
3599 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3600 wl_list_insert(&parent->subsurface_list_pending,
3601 &sub->parent_link_pending);
3602
3603 return sub;
3604 }
3605
3606 static void
subcompositor_get_subsurface(struct wl_client * client,struct wl_resource * resource,uint32_t id,struct wl_resource * surface_resource,struct wl_resource * parent_resource)3607 subcompositor_get_subsurface(struct wl_client *client,
3608 struct wl_resource *resource,
3609 uint32_t id,
3610 struct wl_resource *surface_resource,
3611 struct wl_resource *parent_resource)
3612 {
3613 struct weston_surface *surface =
3614 wl_resource_get_user_data(surface_resource);
3615 struct weston_surface *parent =
3616 wl_resource_get_user_data(parent_resource);
3617 struct weston_subsurface *sub;
3618 static const char where[] = "get_subsurface: wl_subsurface@";
3619
3620 if (surface == parent) {
3621 wl_resource_post_error(resource,
3622 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3623 "%s%d: wl_surface@%d cannot be its own parent",
3624 where, id, wl_resource_get_id(surface_resource));
3625 return;
3626 }
3627
3628 if (weston_surface_to_subsurface(surface)) {
3629 wl_resource_post_error(resource,
3630 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3631 "%s%d: wl_surface@%d is already a sub-surface",
3632 where, id, wl_resource_get_id(surface_resource));
3633 return;
3634 }
3635
3636 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3637 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
3638 return;
3639
3640 if (weston_surface_get_main_surface(parent) == surface) {
3641 wl_resource_post_error(resource,
3642 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3643 "%s%d: wl_surface@%d is an ancestor of parent",
3644 where, id, wl_resource_get_id(surface_resource));
3645 return;
3646 }
3647
3648 /* make sure the parent is in its own list */
3649 if (wl_list_empty(&parent->subsurface_list)) {
3650 if (!weston_subsurface_create_for_parent(parent)) {
3651 wl_resource_post_no_memory(resource);
3652 return;
3653 }
3654 }
3655
3656 sub = weston_subsurface_create(id, surface, parent);
3657 if (!sub) {
3658 wl_resource_post_no_memory(resource);
3659 return;
3660 }
3661
3662 surface->configure = subsurface_configure;
3663 surface->configure_private = sub;
3664 weston_surface_set_label_func(surface, subsurface_get_label);
3665 }
3666
3667 static void
subcompositor_destroy(struct wl_client * client,struct wl_resource * resource)3668 subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3669 {
3670 wl_resource_destroy(resource);
3671 }
3672
3673 static const struct wl_subcompositor_interface subcompositor_interface = {
3674 subcompositor_destroy,
3675 subcompositor_get_subsurface
3676 };
3677
3678 static void
bind_subcompositor(struct wl_client * client,void * data,uint32_t version,uint32_t id)3679 bind_subcompositor(struct wl_client *client,
3680 void *data, uint32_t version, uint32_t id)
3681 {
3682 struct weston_compositor *compositor = data;
3683 struct wl_resource *resource;
3684
3685 resource =
3686 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
3687 if (resource == NULL) {
3688 wl_client_post_no_memory(client);
3689 return;
3690 }
3691 wl_resource_set_implementation(resource, &subcompositor_interface,
3692 compositor, NULL);
3693 }
3694
3695 static void
weston_compositor_dpms(struct weston_compositor * compositor,enum dpms_enum state)3696 weston_compositor_dpms(struct weston_compositor *compositor,
3697 enum dpms_enum state)
3698 {
3699 struct weston_output *output;
3700
3701 wl_list_for_each(output, &compositor->output_list, link)
3702 if (output->set_dpms)
3703 output->set_dpms(output, state);
3704 }
3705
3706 WL_EXPORT void
weston_compositor_wake(struct weston_compositor * compositor)3707 weston_compositor_wake(struct weston_compositor *compositor)
3708 {
3709 uint32_t old_state = compositor->state;
3710
3711 /* The state needs to be changed before emitting the wake
3712 * signal because that may try to schedule a repaint which
3713 * will not work if the compositor is still sleeping */
3714 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3715
3716 switch (old_state) {
3717 case WESTON_COMPOSITOR_SLEEPING:
3718 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3719 /* fall through */
3720 case WESTON_COMPOSITOR_IDLE:
3721 case WESTON_COMPOSITOR_OFFSCREEN:
3722 wl_signal_emit(&compositor->wake_signal, compositor);
3723 /* fall through */
3724 default:
3725 wl_event_source_timer_update(compositor->idle_source,
3726 compositor->idle_time * 1000);
3727 }
3728 }
3729
3730 WL_EXPORT void
weston_compositor_offscreen(struct weston_compositor * compositor)3731 weston_compositor_offscreen(struct weston_compositor *compositor)
3732 {
3733 switch (compositor->state) {
3734 case WESTON_COMPOSITOR_OFFSCREEN:
3735 return;
3736 case WESTON_COMPOSITOR_SLEEPING:
3737 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3738 /* fall through */
3739 default:
3740 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3741 wl_event_source_timer_update(compositor->idle_source, 0);
3742 }
3743 }
3744
3745 WL_EXPORT void
weston_compositor_sleep(struct weston_compositor * compositor)3746 weston_compositor_sleep(struct weston_compositor *compositor)
3747 {
3748 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3749 return;
3750
3751 wl_event_source_timer_update(compositor->idle_source, 0);
3752 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3753 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3754 }
3755
3756 static int
idle_handler(void * data)3757 idle_handler(void *data)
3758 {
3759 struct weston_compositor *compositor = data;
3760
3761 if (compositor->idle_inhibit)
3762 return 1;
3763
3764 compositor->state = WESTON_COMPOSITOR_IDLE;
3765 wl_signal_emit(&compositor->idle_signal, compositor);
3766
3767 return 1;
3768 }
3769
3770 WL_EXPORT void
weston_plane_init(struct weston_plane * plane,struct weston_compositor * ec,int32_t x,int32_t y)3771 weston_plane_init(struct weston_plane *plane,
3772 struct weston_compositor *ec,
3773 int32_t x, int32_t y)
3774 {
3775 pixman_region32_init(&plane->damage);
3776 pixman_region32_init(&plane->clip);
3777 plane->x = x;
3778 plane->y = y;
3779 plane->compositor = ec;
3780
3781 /* Init the link so that the call to wl_list_remove() when releasing
3782 * the plane without ever stacking doesn't lead to a crash */
3783 wl_list_init(&plane->link);
3784 }
3785
3786 WL_EXPORT void
weston_plane_release(struct weston_plane * plane)3787 weston_plane_release(struct weston_plane *plane)
3788 {
3789 struct weston_view *view;
3790
3791 pixman_region32_fini(&plane->damage);
3792 pixman_region32_fini(&plane->clip);
3793
3794 wl_list_for_each(view, &plane->compositor->view_list, link) {
3795 if (view->plane == plane)
3796 view->plane = NULL;
3797 }
3798
3799 wl_list_remove(&plane->link);
3800 }
3801
3802 WL_EXPORT void
weston_compositor_stack_plane(struct weston_compositor * ec,struct weston_plane * plane,struct weston_plane * above)3803 weston_compositor_stack_plane(struct weston_compositor *ec,
3804 struct weston_plane *plane,
3805 struct weston_plane *above)
3806 {
3807 if (above)
3808 wl_list_insert(above->link.prev, &plane->link);
3809 else
3810 wl_list_insert(&ec->plane_list, &plane->link);
3811 }
3812
unbind_resource(struct wl_resource * resource)3813 static void unbind_resource(struct wl_resource *resource)
3814 {
3815 wl_list_remove(wl_resource_get_link(resource));
3816 }
3817
3818 static void
bind_output(struct wl_client * client,void * data,uint32_t version,uint32_t id)3819 bind_output(struct wl_client *client,
3820 void *data, uint32_t version, uint32_t id)
3821 {
3822 struct weston_output *output = data;
3823 struct weston_mode *mode;
3824 struct wl_resource *resource;
3825
3826 resource = wl_resource_create(client, &wl_output_interface,
3827 MIN(version, 2), id);
3828 if (resource == NULL) {
3829 wl_client_post_no_memory(client);
3830 return;
3831 }
3832
3833 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
3834 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
3835
3836 wl_output_send_geometry(resource,
3837 output->x,
3838 output->y,
3839 output->mm_width,
3840 output->mm_height,
3841 output->subpixel,
3842 output->make, output->model,
3843 output->transform);
3844 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
3845 wl_output_send_scale(resource,
3846 output->current_scale);
3847
3848 wl_list_for_each (mode, &output->mode_list, link) {
3849 wl_output_send_mode(resource,
3850 mode->flags,
3851 mode->width,
3852 mode->height,
3853 mode->refresh);
3854 }
3855
3856 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
3857 wl_output_send_done(resource);
3858 }
3859
3860 /* Move other outputs when one is removed so the space remains contiguos. */
3861 static void
weston_compositor_remove_output(struct weston_compositor * compositor,struct weston_output * remove_output)3862 weston_compositor_remove_output(struct weston_compositor *compositor,
3863 struct weston_output *remove_output)
3864 {
3865 struct weston_output *output;
3866 int offset = 0;
3867
3868 wl_list_for_each(output, &compositor->output_list, link) {
3869 if (output == remove_output) {
3870 offset = output->width;
3871 continue;
3872 }
3873
3874 if (offset > 0) {
3875 weston_output_move(output,
3876 output->x - offset, output->y);
3877 output->dirty = 1;
3878 }
3879 }
3880 }
3881
3882 WL_EXPORT void
weston_output_destroy(struct weston_output * output)3883 weston_output_destroy(struct weston_output *output)
3884 {
3885 struct wl_resource *resource;
3886 struct weston_view *view;
3887
3888 output->destroying = 1;
3889
3890 wl_list_for_each(view, &output->compositor->view_list, link) {
3891 if (view->output_mask & (1 << output->id))
3892 weston_view_assign_output(view);
3893 }
3894
3895 wl_event_source_remove(output->repaint_timer);
3896
3897 weston_presentation_feedback_discard_list(&output->feedback_list);
3898
3899 weston_compositor_remove_output(output->compositor, output);
3900 wl_list_remove(&output->link);
3901
3902 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
3903 wl_signal_emit(&output->destroy_signal, output);
3904
3905 free(output->name);
3906 pixman_region32_fini(&output->region);
3907 pixman_region32_fini(&output->previous_damage);
3908 output->compositor->output_id_pool &= ~(1 << output->id);
3909
3910 wl_resource_for_each(resource, &output->resource_list) {
3911 wl_resource_set_destructor(resource, NULL);
3912 }
3913
3914 wl_global_destroy(output->global);
3915 }
3916
3917 WL_EXPORT void
weston_output_update_matrix(struct weston_output * output)3918 weston_output_update_matrix(struct weston_output *output)
3919 {
3920 float magnification;
3921
3922 weston_matrix_init(&output->matrix);
3923 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
3924
3925 if (output->zoom.active) {
3926 magnification = 1 / (1 - output->zoom.spring_z.current);
3927 weston_output_update_zoom(output);
3928 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3929 -output->zoom.trans_y, 0);
3930 weston_matrix_scale(&output->matrix, magnification,
3931 magnification, 1.0);
3932 }
3933
3934 switch (output->transform) {
3935 case WL_OUTPUT_TRANSFORM_FLIPPED:
3936 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3937 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3938 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3939 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3940 weston_matrix_scale(&output->matrix, -1, 1, 1);
3941 break;
3942 }
3943
3944 switch (output->transform) {
3945 default:
3946 case WL_OUTPUT_TRANSFORM_NORMAL:
3947 case WL_OUTPUT_TRANSFORM_FLIPPED:
3948 break;
3949 case WL_OUTPUT_TRANSFORM_90:
3950 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3951 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
3952 weston_matrix_rotate_xy(&output->matrix, 0, 1);
3953 break;
3954 case WL_OUTPUT_TRANSFORM_180:
3955 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3956 weston_matrix_translate(&output->matrix,
3957 -output->width, -output->height, 0);
3958 weston_matrix_rotate_xy(&output->matrix, -1, 0);
3959 break;
3960 case WL_OUTPUT_TRANSFORM_270:
3961 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3962 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3963 weston_matrix_rotate_xy(&output->matrix, 0, -1);
3964 break;
3965 }
3966
3967 if (output->current_scale != 1)
3968 weston_matrix_scale(&output->matrix,
3969 output->current_scale,
3970 output->current_scale, 1);
3971
3972 output->dirty = 0;
3973
3974 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
3975 }
3976
3977 static void
weston_output_transform_scale_init(struct weston_output * output,uint32_t transform,uint32_t scale)3978 weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
3979 {
3980 output->transform = transform;
3981
3982 switch (transform) {
3983 case WL_OUTPUT_TRANSFORM_90:
3984 case WL_OUTPUT_TRANSFORM_270:
3985 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3986 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3987 /* Swap width and height */
3988 output->width = output->current_mode->height;
3989 output->height = output->current_mode->width;
3990 break;
3991 case WL_OUTPUT_TRANSFORM_NORMAL:
3992 case WL_OUTPUT_TRANSFORM_180:
3993 case WL_OUTPUT_TRANSFORM_FLIPPED:
3994 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3995 output->width = output->current_mode->width;
3996 output->height = output->current_mode->height;
3997 break;
3998 default:
3999 break;
4000 }
4001
4002 output->native_scale = output->current_scale = scale;
4003 output->width /= scale;
4004 output->height /= scale;
4005 }
4006
4007 static void
weston_output_init_geometry(struct weston_output * output,int x,int y)4008 weston_output_init_geometry(struct weston_output *output, int x, int y)
4009 {
4010 output->x = x;
4011 output->y = y;
4012
4013 pixman_region32_init(&output->previous_damage);
4014 pixman_region32_init_rect(&output->region, x, y,
4015 output->width,
4016 output->height);
4017 }
4018
4019 WL_EXPORT void
weston_output_move(struct weston_output * output,int x,int y)4020 weston_output_move(struct weston_output *output, int x, int y)
4021 {
4022 struct wl_resource *resource;
4023
4024 output->move_x = x - output->x;
4025 output->move_y = y - output->y;
4026
4027 if (output->move_x == 0 && output->move_y == 0)
4028 return;
4029
4030 weston_output_init_geometry(output, x, y);
4031
4032 output->dirty = 1;
4033
4034 /* Move views on this output. */
4035 wl_signal_emit(&output->compositor->output_moved_signal, output);
4036
4037 /* Notify clients of the change for output position. */
4038 wl_resource_for_each(resource, &output->resource_list) {
4039 wl_output_send_geometry(resource,
4040 output->x,
4041 output->y,
4042 output->mm_width,
4043 output->mm_height,
4044 output->subpixel,
4045 output->make,
4046 output->model,
4047 output->transform);
4048
4049 if (wl_resource_get_version(resource) >= 2)
4050 wl_output_send_done(resource);
4051 }
4052 }
4053
4054 WL_EXPORT void
weston_output_init(struct weston_output * output,struct weston_compositor * c,int x,int y,int mm_width,int mm_height,uint32_t transform,int32_t scale)4055 weston_output_init(struct weston_output *output, struct weston_compositor *c,
4056 int x, int y, int mm_width, int mm_height, uint32_t transform,
4057 int32_t scale)
4058 {
4059 struct wl_event_loop *loop;
4060
4061 output->compositor = c;
4062 output->x = x;
4063 output->y = y;
4064 output->mm_width = mm_width;
4065 output->mm_height = mm_height;
4066 output->dirty = 1;
4067 output->original_scale = scale;
4068
4069 weston_output_transform_scale_init(output, transform, scale);
4070 weston_output_init_zoom(output);
4071
4072 weston_output_init_geometry(output, x, y);
4073 weston_output_damage(output);
4074
4075 wl_signal_init(&output->frame_signal);
4076 wl_signal_init(&output->destroy_signal);
4077 wl_list_init(&output->animation_list);
4078 wl_list_init(&output->resource_list);
4079 wl_list_init(&output->feedback_list);
4080 wl_list_init(&output->link);
4081
4082 loop = wl_display_get_event_loop(c->wl_display);
4083 output->repaint_timer = wl_event_loop_add_timer(loop,
4084 output_repaint_timer_handler, output);
4085
4086 output->id = ffs(~output->compositor->output_id_pool) - 1;
4087 output->compositor->output_id_pool |= 1 << output->id;
4088
4089 output->global =
4090 wl_global_create(c->wl_display, &wl_output_interface, 2,
4091 output, bind_output);
4092 }
4093
4094 /** Adds an output to the compositor's output list and
4095 * send the compositor's output_created signal.
4096 *
4097 * \param compositor The compositor instance.
4098 * \param output The output to be added.
4099 */
4100 WL_EXPORT void
weston_compositor_add_output(struct weston_compositor * compositor,struct weston_output * output)4101 weston_compositor_add_output(struct weston_compositor *compositor,
4102 struct weston_output *output)
4103 {
4104 wl_list_insert(compositor->output_list.prev, &output->link);
4105 wl_signal_emit(&compositor->output_created_signal, output);
4106 }
4107
4108 WL_EXPORT void
weston_output_transform_coordinate(struct weston_output * output,wl_fixed_t device_x,wl_fixed_t device_y,wl_fixed_t * x,wl_fixed_t * y)4109 weston_output_transform_coordinate(struct weston_output *output,
4110 wl_fixed_t device_x, wl_fixed_t device_y,
4111 wl_fixed_t *x, wl_fixed_t *y)
4112 {
4113 struct weston_vector p = { {
4114 wl_fixed_to_double(device_x),
4115 wl_fixed_to_double(device_y),
4116 0.0,
4117 1.0 } };
4118
4119 weston_matrix_transform(&output->inverse_matrix, &p);
4120
4121 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4122 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
4123 }
4124
4125 static void
destroy_viewport(struct wl_resource * resource)4126 destroy_viewport(struct wl_resource *resource)
4127 {
4128 struct weston_surface *surface =
4129 wl_resource_get_user_data(resource);
4130
4131 surface->viewport_resource = NULL;
4132 surface->pending.buffer_viewport.buffer.src_width =
4133 wl_fixed_from_int(-1);
4134 surface->pending.buffer_viewport.surface.width = -1;
4135 surface->pending.buffer_viewport.changed = 1;
4136 }
4137
4138 static void
viewport_destroy(struct wl_client * client,struct wl_resource * resource)4139 viewport_destroy(struct wl_client *client,
4140 struct wl_resource *resource)
4141 {
4142 wl_resource_destroy(resource);
4143 }
4144
4145 static void
viewport_set(struct wl_client * client,struct wl_resource * resource,wl_fixed_t src_x,wl_fixed_t src_y,wl_fixed_t src_width,wl_fixed_t src_height,int32_t dst_width,int32_t dst_height)4146 viewport_set(struct wl_client *client,
4147 struct wl_resource *resource,
4148 wl_fixed_t src_x,
4149 wl_fixed_t src_y,
4150 wl_fixed_t src_width,
4151 wl_fixed_t src_height,
4152 int32_t dst_width,
4153 int32_t dst_height)
4154 {
4155 struct weston_surface *surface =
4156 wl_resource_get_user_data(resource);
4157
4158 assert(surface->viewport_resource != NULL);
4159
4160 if (wl_fixed_to_double(src_width) < 0 ||
4161 wl_fixed_to_double(src_height) < 0) {
4162 wl_resource_post_error(resource,
4163 WL_VIEWPORT_ERROR_BAD_VALUE,
4164 "source dimensions must be non-negative (%fx%f)",
4165 wl_fixed_to_double(src_width),
4166 wl_fixed_to_double(src_height));
4167 return;
4168 }
4169
4170 if (dst_width <= 0 || dst_height <= 0) {
4171 wl_resource_post_error(resource,
4172 WL_VIEWPORT_ERROR_BAD_VALUE,
4173 "destination dimensions must be positive (%dx%d)",
4174 dst_width, dst_height);
4175 return;
4176 }
4177
4178 surface->pending.buffer_viewport.buffer.src_x = src_x;
4179 surface->pending.buffer_viewport.buffer.src_y = src_y;
4180 surface->pending.buffer_viewport.buffer.src_width = src_width;
4181 surface->pending.buffer_viewport.buffer.src_height = src_height;
4182 surface->pending.buffer_viewport.surface.width = dst_width;
4183 surface->pending.buffer_viewport.surface.height = dst_height;
4184 surface->pending.buffer_viewport.changed = 1;
4185 }
4186
4187 static void
viewport_set_source(struct wl_client * client,struct wl_resource * resource,wl_fixed_t src_x,wl_fixed_t src_y,wl_fixed_t src_width,wl_fixed_t src_height)4188 viewport_set_source(struct wl_client *client,
4189 struct wl_resource *resource,
4190 wl_fixed_t src_x,
4191 wl_fixed_t src_y,
4192 wl_fixed_t src_width,
4193 wl_fixed_t src_height)
4194 {
4195 struct weston_surface *surface =
4196 wl_resource_get_user_data(resource);
4197
4198 assert(surface->viewport_resource != NULL);
4199
4200 if (src_width == wl_fixed_from_int(-1) &&
4201 src_height == wl_fixed_from_int(-1)) {
4202 /* unset source size */
4203 surface->pending.buffer_viewport.buffer.src_width =
4204 wl_fixed_from_int(-1);
4205 surface->pending.buffer_viewport.changed = 1;
4206 return;
4207 }
4208
4209 if (src_width <= 0 || src_height <= 0) {
4210 wl_resource_post_error(resource,
4211 WL_VIEWPORT_ERROR_BAD_VALUE,
4212 "source size must be positive (%fx%f)",
4213 wl_fixed_to_double(src_width),
4214 wl_fixed_to_double(src_height));
4215 return;
4216 }
4217
4218 surface->pending.buffer_viewport.buffer.src_x = src_x;
4219 surface->pending.buffer_viewport.buffer.src_y = src_y;
4220 surface->pending.buffer_viewport.buffer.src_width = src_width;
4221 surface->pending.buffer_viewport.buffer.src_height = src_height;
4222 surface->pending.buffer_viewport.changed = 1;
4223 }
4224
4225 static void
viewport_set_destination(struct wl_client * client,struct wl_resource * resource,int32_t dst_width,int32_t dst_height)4226 viewport_set_destination(struct wl_client *client,
4227 struct wl_resource *resource,
4228 int32_t dst_width,
4229 int32_t dst_height)
4230 {
4231 struct weston_surface *surface =
4232 wl_resource_get_user_data(resource);
4233
4234 assert(surface->viewport_resource != NULL);
4235
4236 if (dst_width == -1 && dst_height == -1) {
4237 /* unset destination size */
4238 surface->pending.buffer_viewport.surface.width = -1;
4239 surface->pending.buffer_viewport.changed = 1;
4240 return;
4241 }
4242
4243 if (dst_width <= 0 || dst_height <= 0) {
4244 wl_resource_post_error(resource,
4245 WL_VIEWPORT_ERROR_BAD_VALUE,
4246 "destination size must be positive (%dx%d)",
4247 dst_width, dst_height);
4248 return;
4249 }
4250
4251 surface->pending.buffer_viewport.surface.width = dst_width;
4252 surface->pending.buffer_viewport.surface.height = dst_height;
4253 surface->pending.buffer_viewport.changed = 1;
4254 }
4255
4256 static const struct wl_viewport_interface viewport_interface = {
4257 viewport_destroy,
4258 viewport_set,
4259 viewport_set_source,
4260 viewport_set_destination
4261 };
4262
4263 static void
scaler_destroy(struct wl_client * client,struct wl_resource * resource)4264 scaler_destroy(struct wl_client *client,
4265 struct wl_resource *resource)
4266 {
4267 wl_resource_destroy(resource);
4268 }
4269
4270 static void
scaler_get_viewport(struct wl_client * client,struct wl_resource * scaler,uint32_t id,struct wl_resource * surface_resource)4271 scaler_get_viewport(struct wl_client *client,
4272 struct wl_resource *scaler,
4273 uint32_t id,
4274 struct wl_resource *surface_resource)
4275 {
4276 int version = wl_resource_get_version(scaler);
4277 struct weston_surface *surface =
4278 wl_resource_get_user_data(surface_resource);
4279 struct wl_resource *resource;
4280
4281 if (surface->viewport_resource) {
4282 wl_resource_post_error(scaler,
4283 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4284 "a viewport for that surface already exists");
4285 return;
4286 }
4287
4288 resource = wl_resource_create(client, &wl_viewport_interface,
4289 version, id);
4290 if (resource == NULL) {
4291 wl_client_post_no_memory(client);
4292 return;
4293 }
4294
4295 wl_resource_set_implementation(resource, &viewport_interface,
4296 surface, destroy_viewport);
4297
4298 surface->viewport_resource = resource;
4299 }
4300
4301 static const struct wl_scaler_interface scaler_interface = {
4302 scaler_destroy,
4303 scaler_get_viewport
4304 };
4305
4306 static void
bind_scaler(struct wl_client * client,void * data,uint32_t version,uint32_t id)4307 bind_scaler(struct wl_client *client,
4308 void *data, uint32_t version, uint32_t id)
4309 {
4310 struct wl_resource *resource;
4311
4312 resource = wl_resource_create(client, &wl_scaler_interface,
4313 MIN(version, 2), id);
4314 if (resource == NULL) {
4315 wl_client_post_no_memory(client);
4316 return;
4317 }
4318
4319 wl_resource_set_implementation(resource, &scaler_interface,
4320 NULL, NULL);
4321 }
4322
4323 static void
destroy_presentation_feedback(struct wl_resource * feedback_resource)4324 destroy_presentation_feedback(struct wl_resource *feedback_resource)
4325 {
4326 struct weston_presentation_feedback *feedback;
4327
4328 feedback = wl_resource_get_user_data(feedback_resource);
4329
4330 wl_list_remove(&feedback->link);
4331 free(feedback);
4332 }
4333
4334 static void
presentation_destroy(struct wl_client * client,struct wl_resource * resource)4335 presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4336 {
4337 wl_resource_destroy(resource);
4338 }
4339
4340 static void
presentation_feedback(struct wl_client * client,struct wl_resource * presentation_resource,struct wl_resource * surface_resource,uint32_t callback)4341 presentation_feedback(struct wl_client *client,
4342 struct wl_resource *presentation_resource,
4343 struct wl_resource *surface_resource,
4344 uint32_t callback)
4345 {
4346 struct weston_surface *surface;
4347 struct weston_presentation_feedback *feedback;
4348
4349 surface = wl_resource_get_user_data(surface_resource);
4350
4351 feedback = zalloc(sizeof *feedback);
4352 if (feedback == NULL)
4353 goto err_calloc;
4354
4355 feedback->resource = wl_resource_create(client,
4356 &presentation_feedback_interface,
4357 1, callback);
4358 if (!feedback->resource)
4359 goto err_create;
4360
4361 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4362 destroy_presentation_feedback);
4363
4364 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4365
4366 return;
4367
4368 err_create:
4369 free(feedback);
4370
4371 err_calloc:
4372 wl_client_post_no_memory(client);
4373 }
4374
4375 static const struct presentation_interface presentation_implementation = {
4376 presentation_destroy,
4377 presentation_feedback
4378 };
4379
4380 static void
bind_presentation(struct wl_client * client,void * data,uint32_t version,uint32_t id)4381 bind_presentation(struct wl_client *client,
4382 void *data, uint32_t version, uint32_t id)
4383 {
4384 struct weston_compositor *compositor = data;
4385 struct wl_resource *resource;
4386
4387 resource = wl_resource_create(client, &presentation_interface,
4388 MIN(version, 1), id);
4389 if (resource == NULL) {
4390 wl_client_post_no_memory(client);
4391 return;
4392 }
4393
4394 wl_resource_set_implementation(resource, &presentation_implementation,
4395 compositor, NULL);
4396 presentation_send_clock_id(resource, compositor->presentation_clock);
4397 }
4398
4399 static void
compositor_bind(struct wl_client * client,void * data,uint32_t version,uint32_t id)4400 compositor_bind(struct wl_client *client,
4401 void *data, uint32_t version, uint32_t id)
4402 {
4403 struct weston_compositor *compositor = data;
4404 struct wl_resource *resource;
4405
4406 resource = wl_resource_create(client, &wl_compositor_interface,
4407 MIN(version, 3), id);
4408 if (resource == NULL) {
4409 wl_client_post_no_memory(client);
4410 return;
4411 }
4412
4413 wl_resource_set_implementation(resource, &compositor_interface,
4414 compositor, NULL);
4415 }
4416
4417 WL_EXPORT int
weston_environment_get_fd(const char * env)4418 weston_environment_get_fd(const char *env)
4419 {
4420 char *e, *end;
4421 int fd, flags;
4422
4423 e = getenv(env);
4424 if (!e)
4425 return -1;
4426 fd = strtol(e, &end, 0);
4427 if (*end != '\0')
4428 return -1;
4429
4430 flags = fcntl(fd, F_GETFD);
4431 if (flags == -1)
4432 return -1;
4433
4434 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4435 unsetenv(env);
4436
4437 return fd;
4438 }
4439
4440 static void
timeline_key_binding_handler(struct weston_keyboard * keyboard,uint32_t time,uint32_t key,void * data)4441 timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
4442 uint32_t key, void *data)
4443 {
4444 struct weston_compositor *compositor = data;
4445
4446 if (weston_timeline_enabled_)
4447 weston_timeline_close();
4448 else
4449 weston_timeline_open(compositor);
4450 }
4451
4452 /** Create the compositor.
4453 *
4454 * This functions creates and initializes a compositor instance.
4455 *
4456 * \param display The Wayland display to be used.
4457 * \param user_data A pointer to an object that can later be retrieved
4458 * using the \ref weston_compositor_get_user_data function.
4459 * \return The compositor instance on success or NULL on failure.
4460 */
4461 WL_EXPORT struct weston_compositor *
weston_compositor_create(struct wl_display * display,void * user_data)4462 weston_compositor_create(struct wl_display *display, void *user_data)
4463 {
4464 struct weston_compositor *ec;
4465 struct wl_event_loop *loop;
4466
4467 ec = zalloc(sizeof *ec);
4468 if (!ec)
4469 return NULL;
4470
4471 ec->wl_display = display;
4472 ec->user_data = user_data;
4473 wl_signal_init(&ec->destroy_signal);
4474 wl_signal_init(&ec->create_surface_signal);
4475 wl_signal_init(&ec->activate_signal);
4476 wl_signal_init(&ec->transform_signal);
4477 wl_signal_init(&ec->kill_signal);
4478 wl_signal_init(&ec->idle_signal);
4479 wl_signal_init(&ec->wake_signal);
4480 wl_signal_init(&ec->show_input_panel_signal);
4481 wl_signal_init(&ec->hide_input_panel_signal);
4482 wl_signal_init(&ec->update_input_panel_signal);
4483 wl_signal_init(&ec->seat_created_signal);
4484 wl_signal_init(&ec->output_created_signal);
4485 wl_signal_init(&ec->output_destroyed_signal);
4486 wl_signal_init(&ec->output_moved_signal);
4487 wl_signal_init(&ec->session_signal);
4488 ec->session_active = 1;
4489
4490 ec->output_id_pool = 0;
4491 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
4492
4493 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 3,
4494 ec, compositor_bind))
4495 goto fail;
4496
4497 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
4498 ec, bind_subcompositor))
4499 goto fail;
4500
4501 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
4502 ec, bind_scaler))
4503 goto fail;
4504
4505 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4506 ec, bind_presentation))
4507 goto fail;
4508
4509 wl_list_init(&ec->view_list);
4510 wl_list_init(&ec->plane_list);
4511 wl_list_init(&ec->layer_list);
4512 wl_list_init(&ec->seat_list);
4513 wl_list_init(&ec->output_list);
4514 wl_list_init(&ec->key_binding_list);
4515 wl_list_init(&ec->modifier_binding_list);
4516 wl_list_init(&ec->button_binding_list);
4517 wl_list_init(&ec->touch_binding_list);
4518 wl_list_init(&ec->axis_binding_list);
4519 wl_list_init(&ec->debug_binding_list);
4520
4521 weston_plane_init(&ec->primary_plane, ec, 0, 0);
4522 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
4523
4524 wl_data_device_manager_init(ec->wl_display);
4525
4526 wl_display_init_shm(ec->wl_display);
4527
4528 loop = wl_display_get_event_loop(ec->wl_display);
4529 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4530 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4531
4532 ec->input_loop = loop;
4533
4534 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4535 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4536
4537 weston_compositor_add_debug_binding(ec, KEY_T,
4538 timeline_key_binding_handler, ec);
4539
4540 return ec;
4541
4542 fail:
4543 free(ec);
4544 return NULL;
4545 }
4546
4547 WL_EXPORT void
weston_compositor_shutdown(struct weston_compositor * ec)4548 weston_compositor_shutdown(struct weston_compositor *ec)
4549 {
4550 struct weston_output *output, *next;
4551
4552 wl_event_source_remove(ec->idle_source);
4553 /* XXX Implement similar behaviour with libevent */
4554 #if 0
4555 if (ec->input_loop_source)
4556 wl_event_source_remove(ec->input_loop_source);
4557 #endif
4558
4559 /* Destroy all outputs associated with this compositor */
4560 wl_list_for_each_safe(output, next, &ec->output_list, link)
4561 output->destroy(output);
4562
4563 if (ec->renderer)
4564 ec->renderer->destroy(ec);
4565
4566 weston_binding_list_destroy_all(&ec->key_binding_list);
4567 weston_binding_list_destroy_all(&ec->modifier_binding_list);
4568 weston_binding_list_destroy_all(&ec->button_binding_list);
4569 weston_binding_list_destroy_all(&ec->touch_binding_list);
4570 weston_binding_list_destroy_all(&ec->axis_binding_list);
4571 weston_binding_list_destroy_all(&ec->debug_binding_list);
4572
4573 weston_plane_release(&ec->primary_plane);
4574
4575 /* XXX Implement similar behaviour with libevent */
4576 #if 0
4577 wl_event_loop_destroy(ec->input_loop);
4578 #endif
4579 }
4580
4581 WL_EXPORT void
weston_compositor_exit_with_code(struct weston_compositor * compositor,int exit_code)4582 weston_compositor_exit_with_code(struct weston_compositor *compositor,
4583 int exit_code)
4584 {
4585 if (compositor->exit_code == EXIT_SUCCESS)
4586 compositor->exit_code = exit_code;
4587
4588 weston_compositor_exit(compositor);
4589 }
4590
4591 WL_EXPORT void
weston_compositor_set_default_pointer_grab(struct weston_compositor * ec,const struct weston_pointer_grab_interface * interface)4592 weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4593 const struct weston_pointer_grab_interface *interface)
4594 {
4595 struct weston_seat *seat;
4596
4597 ec->default_pointer_grab = interface;
4598 wl_list_for_each(seat, &ec->seat_list, link) {
4599 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4600
4601 if (pointer)
4602 weston_pointer_set_default_grab(pointer, interface);
4603 }
4604 }
4605
4606 WL_EXPORT int
weston_compositor_set_presentation_clock(struct weston_compositor * compositor,clockid_t clk_id)4607 weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4608 clockid_t clk_id)
4609 {
4610 struct timespec ts;
4611
4612 if (clock_gettime(clk_id, &ts) < 0)
4613 return -1;
4614
4615 compositor->presentation_clock = clk_id;
4616
4617 return 0;
4618 }
4619
4620 /*
4621 * For choosing the software clock, when the display hardware or API
4622 * does not expose a compatible presentation timestamp.
4623 */
4624 WL_EXPORT int
weston_compositor_set_presentation_clock_software(struct weston_compositor * compositor)4625 weston_compositor_set_presentation_clock_software(
4626 struct weston_compositor *compositor)
4627 {
4628 /* In order of preference */
4629 static const clockid_t clocks[] = {
4630 #if defined(__FreeBSD__)
4631 CLOCK_MONOTONIC_FAST, /* no jumps, may crawl, fast & coarse */
4632 #else
4633 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4634 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4635 #endif
4636 CLOCK_MONOTONIC, /* no jumps, may crawl */
4637 #if defined(__FreeBSD__)
4638 CLOCK_REALTIME_FAST, /* may jump and crawl, fast & coarse */
4639 #else
4640 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4641 #endif
4642 CLOCK_REALTIME /* may jump and crawl */
4643 };
4644 unsigned i;
4645
4646 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4647 if (weston_compositor_set_presentation_clock(compositor,
4648 clocks[i]) == 0) return 0;
4649
4650 weston_log("Error: no suitable presentation clock available.\n");
4651
4652 return -1;
4653 }
4654
4655 /** Read the current time from the Presentation clock
4656 *
4657 * \param compositor
4658 * \param ts[out] The current time.
4659 *
4660 * \note Reading the current time in user space is always imprecise to some
4661 * degree.
4662 *
4663 * This function is never meant to fail. If reading the clock does fail,
4664 * an error message is logged and a zero time is returned. Callers are not
4665 * supposed to detect or react to failures.
4666 */
4667 WL_EXPORT void
weston_compositor_read_presentation_clock(const struct weston_compositor * compositor,struct timespec * ts)4668 weston_compositor_read_presentation_clock(
4669 const struct weston_compositor *compositor,
4670 struct timespec *ts)
4671 {
4672 static bool warned;
4673 int ret;
4674
4675 ret = clock_gettime(compositor->presentation_clock, ts);
4676 if (ret < 0) {
4677 ts->tv_sec = 0;
4678 ts->tv_nsec = 0;
4679
4680 if (!warned)
4681 weston_log("Error: failure to read "
4682 "the presentation clock %#lx: '%s' (%d)\n",
4683 compositor->presentation_clock,
4684 strerror(errno), errno);
4685 warned = true;
4686 }
4687 }
4688
4689 /** Import dmabuf buffer into current renderer
4690 *
4691 * \param compositor
4692 * \param buffer the dmabuf buffer to import
4693 * \return true on usable buffers, false otherwise
4694 *
4695 * This function tests that the linux_dmabuf_buffer is usable
4696 * for the current renderer. Returns false on unusable buffers. Usually
4697 * usability is tested by importing the dmabufs for composition.
4698 *
4699 * This hook is also used for detecting if the renderer supports
4700 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4701 * supported.
4702 * */
4703 WL_EXPORT bool
weston_compositor_import_dmabuf(struct weston_compositor * compositor,struct linux_dmabuf_buffer * buffer)4704 weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4705 struct linux_dmabuf_buffer *buffer)
4706 {
4707 struct weston_renderer *renderer;
4708
4709 renderer = compositor->renderer;
4710
4711 if (renderer->import_dmabuf == NULL)
4712 return false;
4713
4714 return renderer->import_dmabuf(compositor, buffer);
4715 }
4716
4717 WL_EXPORT void
weston_version(int * major,int * minor,int * micro)4718 weston_version(int *major, int *minor, int *micro)
4719 {
4720 *major = WESTON_VERSION_MAJOR;
4721 *minor = WESTON_VERSION_MINOR;
4722 *micro = WESTON_VERSION_MICRO;
4723 }
4724
4725 WL_EXPORT void *
weston_load_module(const char * name,const char * entrypoint)4726 weston_load_module(const char *name, const char *entrypoint)
4727 {
4728 const char *builddir = getenv("WESTON_BUILD_DIR");
4729 char path[PATH_MAX];
4730 void *module, *init;
4731
4732 if (name == NULL)
4733 return NULL;
4734
4735 if (name[0] != '/') {
4736 if (builddir)
4737 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4738 else
4739 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
4740 } else {
4741 snprintf(path, sizeof path, "%s", name);
4742 }
4743
4744 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4745 if (module) {
4746 weston_log("Module '%s' already loaded\n", path);
4747 dlclose(module);
4748 return NULL;
4749 }
4750
4751 weston_log("Loading module '%s'\n", path);
4752 module = dlopen(path, RTLD_NOW);
4753 if (!module) {
4754 weston_log("Failed to load module: %s\n", dlerror());
4755 return NULL;
4756 }
4757
4758 init = dlsym(module, entrypoint);
4759 if (!init) {
4760 weston_log("Failed to lookup init function: %s\n", dlerror());
4761 dlclose(module);
4762 return NULL;
4763 }
4764
4765 return init;
4766 }
4767
4768
4769 /** Destroys the compositor.
4770 *
4771 * This function cleans up the compositor state and destroys it.
4772 *
4773 * \param compositor The compositor to be destroyed.
4774 */
4775 WL_EXPORT void
weston_compositor_destroy(struct weston_compositor * compositor)4776 weston_compositor_destroy(struct weston_compositor *compositor)
4777 {
4778 /* prevent further rendering while shutting down */
4779 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4780
4781 wl_signal_emit(&compositor->destroy_signal, compositor);
4782
4783 weston_compositor_xkb_destroy(compositor);
4784
4785 compositor->backend->destroy(compositor);
4786 free(compositor);
4787 }
4788
4789 /** Instruct the compositor to exit.
4790 *
4791 * This functions does not directly destroy the compositor object, it merely
4792 * command it to start the tear down process. It is not guaranteed that the
4793 * tear down will happen immediately.
4794 *
4795 * \param compositor The compositor to tear down.
4796 */
4797 WL_EXPORT void
weston_compositor_exit(struct weston_compositor * compositor)4798 weston_compositor_exit(struct weston_compositor *compositor)
4799 {
4800 compositor->exit(compositor);
4801 }
4802
4803 /** Return the user data stored in the compositor.
4804 *
4805 * This function returns the user data pointer set with user_data parameter
4806 * to the \ref weston_compositor_create function.
4807 */
4808 WL_EXPORT void *
weston_compositor_get_user_data(struct weston_compositor * compositor)4809 weston_compositor_get_user_data(struct weston_compositor *compositor)
4810 {
4811 return compositor->user_data;
4812 }
4813