2 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
4 * Copyright © 2012-2015 Collabora, Ltd.
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:
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.
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
38 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <sys/utsname.h>
46 #include <linux/input.h>
56 #include "compositor.h"
57 #include "viewporter-server-protocol.h"
58 #include "presentation-time-server-protocol.h"
59 #include "shared/helpers.h"
60 #include "shared/os-compatibility.h"
61 #include "shared/string-helpers.h"
62 #include "shared/timespec-util.h"
63 #include "git-version.h"
65 #include "plugin-registry.h"
67 #define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
70 weston_output_transform_scale_init(struct weston_output *output,
71 uint32_t transform, uint32_t scale);
74 weston_compositor_build_view_list(struct weston_compositor *compositor);
76 static void weston_mode_switch_finish(struct weston_output *output,
80 struct weston_seat *seat;
81 struct wl_resource *resource;
82 pixman_region32_t old_output_region;
85 pixman_region32_init(&old_output_region);
86 pixman_region32_copy(&old_output_region, &output->region);
88 /* Update output region and transformation matrix */
89 weston_output_transform_scale_init(output, output->transform, output->current_scale);
91 pixman_region32_init(&output->previous_damage);
92 pixman_region32_init_rect(&output->region, output->x, output->y,
93 output->width, output->height);
95 weston_output_update_matrix(output);
97 /* If a pointer falls outside the outputs new geometry, move it to its
98 * lower-right corner */
99 wl_list_for_each(seat, &output->compositor->seat_list, link) {
100 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
106 x = wl_fixed_to_int(pointer->x);
107 y = wl_fixed_to_int(pointer->y);
109 if (!pixman_region32_contains_point(&old_output_region,
111 pixman_region32_contains_point(&output->region,
115 if (x >= output->x + output->width)
116 x = output->x + output->width - 1;
117 if (y >= output->y + output->height)
118 y = output->y + output->height - 1;
120 pointer->x = wl_fixed_from_int(x);
121 pointer->y = wl_fixed_from_int(y);
124 pixman_region32_fini(&old_output_region);
126 if (!mode_changed && !scale_changed)
129 /* notify clients of the changes */
130 wl_resource_for_each(resource, &output->resource_list) {
132 wl_output_send_mode(resource,
133 output->current_mode->flags,
134 output->current_mode->width,
135 output->current_mode->height,
136 output->current_mode->refresh);
139 version = wl_resource_get_version(resource);
140 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
141 wl_output_send_scale(resource, output->current_scale);
143 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
144 wl_output_send_done(resource);
150 weston_compositor_reflow_outputs(struct weston_compositor *compositor,
151 struct weston_output *resized_output, int delta_width);
154 weston_output_mode_set_native(struct weston_output *output,
155 struct weston_mode *mode,
159 int mode_changed = 0, scale_changed = 0;
162 if (!output->switch_mode)
165 if (!output->original_mode) {
167 ret = output->switch_mode(output, mode);
170 if (output->current_scale != scale) {
172 output->current_scale = scale;
176 old_width = output->width;
177 output->native_mode = mode;
178 output->native_scale = scale;
180 weston_mode_switch_finish(output, mode_changed, scale_changed);
182 if (mode_changed || scale_changed) {
183 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
185 wl_signal_emit(&output->compositor->output_resized_signal, output);
191 weston_output_mode_switch_to_native(struct weston_output *output)
194 int mode_changed = 0, scale_changed = 0;
196 if (!output->switch_mode)
199 if (!output->original_mode) {
200 weston_log("already in the native mode\n");
203 /* the non fullscreen clients haven't seen a mode set since we
204 * switched into a temporary, so we need to notify them if the
205 * mode at that time is different from the native mode now.
207 mode_changed = (output->original_mode != output->native_mode);
208 scale_changed = (output->original_scale != output->native_scale);
210 ret = output->switch_mode(output, output->native_mode);
214 output->current_scale = output->native_scale;
216 output->original_mode = NULL;
217 output->original_scale = 0;
219 weston_mode_switch_finish(output, mode_changed, scale_changed);
225 weston_output_mode_switch_to_temporary(struct weston_output *output,
226 struct weston_mode *mode,
231 if (!output->switch_mode)
234 /* original_mode is the last mode non full screen clients have seen,
235 * so we shouldn't change it if we already have one set.
237 if (!output->original_mode) {
238 output->original_mode = output->native_mode;
239 output->original_scale = output->native_scale;
241 ret = output->switch_mode(output, mode);
245 output->current_scale = scale;
247 weston_mode_switch_finish(output, 0, 0);
253 region_init_infinite(pixman_region32_t *region)
255 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
256 UINT32_MAX, UINT32_MAX);
259 static struct weston_subsurface *
260 weston_surface_to_subsurface(struct weston_surface *surface);
262 WL_EXPORT struct weston_view *
263 weston_view_create(struct weston_surface *surface)
265 struct weston_view *view;
267 view = zalloc(sizeof *view);
271 view->surface = surface;
272 view->plane = &surface->compositor->primary_plane;
274 /* Assign to surface */
275 wl_list_insert(&surface->views, &view->surface_link);
277 wl_signal_init(&view->destroy_signal);
278 wl_list_init(&view->link);
279 wl_list_init(&view->layer_link.link);
281 pixman_region32_init(&view->clip);
284 pixman_region32_init(&view->transform.opaque);
286 wl_list_init(&view->geometry.transformation_list);
287 wl_list_insert(&view->geometry.transformation_list,
288 &view->transform.position.link);
289 weston_matrix_init(&view->transform.position.matrix);
290 wl_list_init(&view->geometry.child_list);
291 pixman_region32_init(&view->geometry.scissor);
292 pixman_region32_init(&view->transform.boundingbox);
293 view->transform.dirty = 1;
298 struct weston_frame_callback {
299 struct wl_resource *resource;
303 struct weston_presentation_feedback {
304 struct wl_resource *resource;
306 /* XXX: could use just wl_resource_get_link() instead */
309 /* The per-surface feedback flags */
314 weston_presentation_feedback_discard(
315 struct weston_presentation_feedback *feedback)
317 wp_presentation_feedback_send_discarded(feedback->resource);
318 wl_resource_destroy(feedback->resource);
322 weston_presentation_feedback_discard_list(struct wl_list *list)
324 struct weston_presentation_feedback *feedback, *tmp;
326 wl_list_for_each_safe(feedback, tmp, list, link)
327 weston_presentation_feedback_discard(feedback);
331 weston_presentation_feedback_present(
332 struct weston_presentation_feedback *feedback,
333 struct weston_output *output,
334 uint32_t refresh_nsec,
335 const struct timespec *ts,
339 struct wl_client *client = wl_resource_get_client(feedback->resource);
340 struct wl_resource *o;
343 wl_resource_for_each(o, &output->resource_list) {
344 if (wl_resource_get_client(o) != client)
347 wp_presentation_feedback_send_sync_output(feedback->resource, o);
351 wp_presentation_feedback_send_presented(feedback->resource,
352 secs >> 32, secs & 0xffffffff,
355 seq >> 32, seq & 0xffffffff,
356 flags | feedback->psf_flags);
357 wl_resource_destroy(feedback->resource);
361 weston_presentation_feedback_present_list(struct wl_list *list,
362 struct weston_output *output,
363 uint32_t refresh_nsec,
364 const struct timespec *ts,
368 struct weston_presentation_feedback *feedback, *tmp;
370 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
371 wl_list_empty(list));
373 wl_list_for_each_safe(feedback, tmp, list, link)
374 weston_presentation_feedback_present(feedback, output,
375 refresh_nsec, ts, seq,
380 surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
382 struct weston_surface_state *state =
383 container_of(listener, struct weston_surface_state,
384 buffer_destroy_listener);
386 state->buffer = NULL;
390 weston_surface_state_init(struct weston_surface_state *state)
392 state->newly_attached = 0;
393 state->buffer = NULL;
394 state->buffer_destroy_listener.notify =
395 surface_state_handle_buffer_destroy;
399 pixman_region32_init(&state->damage_surface);
400 pixman_region32_init(&state->damage_buffer);
401 pixman_region32_init(&state->opaque);
402 region_init_infinite(&state->input);
404 wl_list_init(&state->frame_callback_list);
405 wl_list_init(&state->feedback_list);
407 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
408 state->buffer_viewport.buffer.scale = 1;
409 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
410 state->buffer_viewport.surface.width = -1;
411 state->buffer_viewport.changed = 0;
415 weston_surface_state_fini(struct weston_surface_state *state)
417 struct weston_frame_callback *cb, *next;
419 wl_list_for_each_safe(cb, next,
420 &state->frame_callback_list, link)
421 wl_resource_destroy(cb->resource);
423 weston_presentation_feedback_discard_list(&state->feedback_list);
425 pixman_region32_fini(&state->input);
426 pixman_region32_fini(&state->opaque);
427 pixman_region32_fini(&state->damage_surface);
428 pixman_region32_fini(&state->damage_buffer);
431 wl_list_remove(&state->buffer_destroy_listener.link);
432 state->buffer = NULL;
436 weston_surface_state_set_buffer(struct weston_surface_state *state,
437 struct weston_buffer *buffer)
439 if (state->buffer == buffer)
443 wl_list_remove(&state->buffer_destroy_listener.link);
444 state->buffer = buffer;
446 wl_signal_add(&state->buffer->destroy_signal,
447 &state->buffer_destroy_listener);
450 WL_EXPORT struct weston_surface *
451 weston_surface_create(struct weston_compositor *compositor)
453 struct weston_surface *surface;
455 surface = zalloc(sizeof *surface);
459 wl_signal_init(&surface->destroy_signal);
460 wl_signal_init(&surface->commit_signal);
462 surface->compositor = compositor;
463 surface->ref_count = 1;
465 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
466 surface->buffer_viewport.buffer.scale = 1;
467 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
468 surface->buffer_viewport.surface.width = -1;
470 weston_surface_state_init(&surface->pending);
472 pixman_region32_init(&surface->damage);
473 pixman_region32_init(&surface->opaque);
474 region_init_infinite(&surface->input);
476 wl_list_init(&surface->views);
478 wl_list_init(&surface->frame_callback_list);
479 wl_list_init(&surface->feedback_list);
481 wl_list_init(&surface->subsurface_list);
482 wl_list_init(&surface->subsurface_list_pending);
484 weston_matrix_init(&surface->buffer_to_surface_matrix);
485 weston_matrix_init(&surface->surface_to_buffer_matrix);
487 wl_list_init(&surface->pointer_constraints);
493 weston_surface_set_color(struct weston_surface *surface,
494 float red, float green, float blue, float alpha)
496 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
500 weston_view_to_global_float(struct weston_view *view,
501 float sx, float sy, float *x, float *y)
503 if (view->transform.enabled) {
504 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
506 weston_matrix_transform(&view->transform.matrix, &v);
508 if (fabsf(v.f[3]) < 1e-6) {
509 weston_log("warning: numerical instability in "
510 "%s(), divisor = %g\n", __func__,
517 *x = v.f[0] / v.f[3];
518 *y = v.f[1] / v.f[3];
520 *x = sx + view->geometry.x;
521 *y = sy + view->geometry.y;
526 weston_transformed_coord(int width, int height,
527 enum wl_output_transform transform,
529 float sx, float sy, float *bx, float *by)
532 case WL_OUTPUT_TRANSFORM_NORMAL:
537 case WL_OUTPUT_TRANSFORM_FLIPPED:
541 case WL_OUTPUT_TRANSFORM_90:
545 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
549 case WL_OUTPUT_TRANSFORM_180:
553 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
557 case WL_OUTPUT_TRANSFORM_270:
561 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
571 WL_EXPORT pixman_box32_t
572 weston_transformed_rect(int width, int height,
573 enum wl_output_transform transform,
577 float x1, x2, y1, y2;
581 weston_transformed_coord(width, height, transform, scale,
582 rect.x1, rect.y1, &x1, &y1);
583 weston_transformed_coord(width, height, transform, scale,
584 rect.x2, rect.y2, &x2, &y2);
605 /** Transform a region by a matrix, restricted to axis-aligned transformations
607 * Warning: This function does not work for projective, affine, or matrices
608 * that encode arbitrary rotations. Only 90-degree step rotations are
612 weston_matrix_transform_region(pixman_region32_t *dest,
613 struct weston_matrix *matrix,
614 pixman_region32_t *src)
616 pixman_box32_t *src_rects, *dest_rects;
619 src_rects = pixman_region32_rectangles(src, &nrects);
620 dest_rects = malloc(nrects * sizeof(*dest_rects));
624 for (i = 0; i < nrects; i++) {
625 struct weston_vector vec1 = {{
626 src_rects[i].x1, src_rects[i].y1, 0, 1
628 weston_matrix_transform(matrix, &vec1);
629 vec1.f[0] /= vec1.f[3];
630 vec1.f[1] /= vec1.f[3];
632 struct weston_vector vec2 = {{
633 src_rects[i].x2, src_rects[i].y2, 0, 1
635 weston_matrix_transform(matrix, &vec2);
636 vec2.f[0] /= vec2.f[3];
637 vec2.f[1] /= vec2.f[3];
639 if (vec1.f[0] < vec2.f[0]) {
640 dest_rects[i].x1 = floor(vec1.f[0]);
641 dest_rects[i].x2 = ceil(vec2.f[0]);
643 dest_rects[i].x1 = floor(vec2.f[0]);
644 dest_rects[i].x2 = ceil(vec1.f[0]);
647 if (vec1.f[1] < vec2.f[1]) {
648 dest_rects[i].y1 = floor(vec1.f[1]);
649 dest_rects[i].y2 = ceil(vec2.f[1]);
651 dest_rects[i].y1 = floor(vec2.f[1]);
652 dest_rects[i].y2 = ceil(vec1.f[1]);
656 pixman_region32_clear(dest);
657 pixman_region32_init_rects(dest, dest_rects, nrects);
662 weston_transformed_region(int width, int height,
663 enum wl_output_transform transform,
665 pixman_region32_t *src, pixman_region32_t *dest)
667 pixman_box32_t *src_rects, *dest_rects;
670 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
672 pixman_region32_copy(dest, src);
676 src_rects = pixman_region32_rectangles(src, &nrects);
677 dest_rects = malloc(nrects * sizeof(*dest_rects));
681 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
682 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
684 for (i = 0; i < nrects; i++) {
687 case WL_OUTPUT_TRANSFORM_NORMAL:
688 dest_rects[i].x1 = src_rects[i].x1;
689 dest_rects[i].y1 = src_rects[i].y1;
690 dest_rects[i].x2 = src_rects[i].x2;
691 dest_rects[i].y2 = src_rects[i].y2;
693 case WL_OUTPUT_TRANSFORM_90:
694 dest_rects[i].x1 = height - src_rects[i].y2;
695 dest_rects[i].y1 = src_rects[i].x1;
696 dest_rects[i].x2 = height - src_rects[i].y1;
697 dest_rects[i].y2 = src_rects[i].x2;
699 case WL_OUTPUT_TRANSFORM_180:
700 dest_rects[i].x1 = width - src_rects[i].x2;
701 dest_rects[i].y1 = height - src_rects[i].y2;
702 dest_rects[i].x2 = width - src_rects[i].x1;
703 dest_rects[i].y2 = height - src_rects[i].y1;
705 case WL_OUTPUT_TRANSFORM_270:
706 dest_rects[i].x1 = src_rects[i].y1;
707 dest_rects[i].y1 = width - src_rects[i].x2;
708 dest_rects[i].x2 = src_rects[i].y2;
709 dest_rects[i].y2 = width - src_rects[i].x1;
711 case WL_OUTPUT_TRANSFORM_FLIPPED:
712 dest_rects[i].x1 = width - src_rects[i].x2;
713 dest_rects[i].y1 = src_rects[i].y1;
714 dest_rects[i].x2 = width - src_rects[i].x1;
715 dest_rects[i].y2 = src_rects[i].y2;
717 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
718 dest_rects[i].x1 = height - src_rects[i].y2;
719 dest_rects[i].y1 = width - src_rects[i].x2;
720 dest_rects[i].x2 = height - src_rects[i].y1;
721 dest_rects[i].y2 = width - src_rects[i].x1;
723 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
724 dest_rects[i].x1 = src_rects[i].x1;
725 dest_rects[i].y1 = height - src_rects[i].y2;
726 dest_rects[i].x2 = src_rects[i].x2;
727 dest_rects[i].y2 = height - src_rects[i].y1;
729 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
730 dest_rects[i].x1 = src_rects[i].y1;
731 dest_rects[i].y1 = src_rects[i].x1;
732 dest_rects[i].x2 = src_rects[i].y2;
733 dest_rects[i].y2 = src_rects[i].x2;
740 for (i = 0; i < nrects; i++) {
741 dest_rects[i].x1 *= scale;
742 dest_rects[i].x2 *= scale;
743 dest_rects[i].y1 *= scale;
744 dest_rects[i].y2 *= scale;
748 pixman_region32_clear(dest);
749 pixman_region32_init_rects(dest, dest_rects, nrects);
754 viewport_surface_to_buffer(struct weston_surface *surface,
755 float sx, float sy, float *bx, float *by)
757 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
758 double src_width, src_height;
761 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
762 if (vp->surface.width == -1) {
770 src_width = surface->width_from_buffer;
771 src_height = surface->height_from_buffer;
773 src_x = wl_fixed_to_double(vp->buffer.src_x);
774 src_y = wl_fixed_to_double(vp->buffer.src_y);
775 src_width = wl_fixed_to_double(vp->buffer.src_width);
776 src_height = wl_fixed_to_double(vp->buffer.src_height);
779 *bx = sx * src_width / surface->width + src_x;
780 *by = sy * src_height / surface->height + src_y;
784 weston_surface_to_buffer_float(struct weston_surface *surface,
785 float sx, float sy, float *bx, float *by)
787 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
789 /* first transform coordinates if the viewport is set */
790 viewport_surface_to_buffer(surface, sx, sy, bx, by);
792 weston_transformed_coord(surface->width_from_buffer,
793 surface->height_from_buffer,
794 vp->buffer.transform, vp->buffer.scale,
798 /** Transform a rectangle from surface coordinates to buffer coordinates
800 * \param surface The surface to fetch wp_viewport and buffer transformation
802 * \param rect The rectangle to transform.
803 * \return The transformed rectangle.
805 * Viewport and buffer transformations can only do translation, scaling,
806 * and rotations in 90-degree steps. Therefore the only loss in the
807 * conversion is coordinate rounding.
809 * However, some coordinate rounding takes place as an intermediate
810 * step before the buffer scale factor is applied, so the rectangle
811 * boundary may not be exactly as expected.
813 * This is OK for damage tracking since a little extra coverage is
816 WL_EXPORT pixman_box32_t
817 weston_surface_to_buffer_rect(struct weston_surface *surface,
820 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
823 /* first transform box coordinates if the viewport is set */
824 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
825 rect.x1 = floorf(xf);
826 rect.y1 = floorf(yf);
828 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
832 return weston_transformed_rect(surface->width_from_buffer,
833 surface->height_from_buffer,
834 vp->buffer.transform, vp->buffer.scale,
838 /** Transform a region from surface coordinates to buffer coordinates
840 * \param surface The surface to fetch wp_viewport and buffer transformation
842 * \param surface_region[in] The region in surface coordinates.
843 * \param buffer_region[out] The region converted to buffer coordinates.
845 * Buffer_region must be init'd, but will be completely overwritten.
847 * Viewport and buffer transformations can only do translation, scaling,
848 * and rotations in 90-degree steps. Therefore the only loss in the
849 * conversion is from the coordinate rounding that takes place in
850 * \ref weston_surface_to_buffer_rect.
853 weston_surface_to_buffer_region(struct weston_surface *surface,
854 pixman_region32_t *surface_region,
855 pixman_region32_t *buffer_region)
857 pixman_box32_t *src_rects, *dest_rects;
860 src_rects = pixman_region32_rectangles(surface_region, &nrects);
861 dest_rects = malloc(nrects * sizeof(*dest_rects));
865 for (i = 0; i < nrects; i++) {
866 dest_rects[i] = weston_surface_to_buffer_rect(surface,
870 pixman_region32_fini(buffer_region);
871 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
876 weston_view_move_to_plane(struct weston_view *view,
877 struct weston_plane *plane)
879 if (view->plane == plane)
882 weston_view_damage_below(view);
884 weston_surface_damage(view->surface);
887 /** Inflict damage on the plane where the view is visible.
889 * \param view The view that causes the damage.
891 * If the view is currently on a plane (including the primary plane),
892 * take the view's boundingbox, subtract all the opaque views that cover it,
893 * and add the remaining region as damage to the plane. This corresponds
894 * to the damage inflicted to the plane if this view disappeared.
896 * A repaint is scheduled for this view.
898 * The region of all opaque views covering this view is stored in
899 * weston_view::clip and updated by view_accumulate_damage() during
900 * weston_output_repaint(). Specifically, that region matches the
901 * scenegraph as it was last painted.
904 weston_view_damage_below(struct weston_view *view)
906 pixman_region32_t damage;
908 pixman_region32_init(&damage);
909 pixman_region32_subtract(&damage, &view->transform.boundingbox,
912 pixman_region32_union(&view->plane->damage,
913 &view->plane->damage, &damage);
914 pixman_region32_fini(&damage);
915 weston_view_schedule_repaint(view);
919 * \param es The surface
920 * \param mask The new set of outputs for the surface
922 * Sets the surface's set of outputs to the ones specified by
923 * the new output mask provided. Identifies the outputs that
924 * have changed, the posts enter and leave events for these
925 * outputs as appropriate.
928 weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
930 uint32_t different = es->output_mask ^ mask;
931 uint32_t entered = mask & different;
932 uint32_t left = es->output_mask & different;
933 struct weston_output *output;
934 struct wl_resource *resource = NULL;
935 struct wl_client *client;
937 es->output_mask = mask;
938 if (es->resource == NULL)
943 client = wl_resource_get_client(es->resource);
945 wl_list_for_each(output, &es->compositor->output_list, link) {
946 if (1u << output->id & different)
948 wl_resource_find_for_client(&output->resource_list,
950 if (resource == NULL)
952 if (1u << output->id & entered)
953 wl_surface_send_enter(es->resource, resource);
954 if (1u << output->id & left)
955 wl_surface_send_leave(es->resource, resource);
959 /** Recalculate which output(s) the surface has views displayed on
961 * \param es The surface to remap to outputs
963 * Finds the output that is showing the largest amount of one
964 * of the surface's various views. This output becomes the
965 * surface's primary output for vsync and frame callback purposes.
967 * Also notes all outputs of all of the surface's views
968 * in the output_mask for the surface.
971 weston_surface_assign_output(struct weston_surface *es)
973 struct weston_output *new_output;
974 struct weston_view *view;
975 pixman_region32_t region;
976 uint32_t max, area, mask;
982 pixman_region32_init(®ion);
983 wl_list_for_each(view, &es->views, surface_link) {
987 pixman_region32_intersect(®ion, &view->transform.boundingbox,
988 &view->output->region);
990 e = pixman_region32_extents(®ion);
991 area = (e->x2 - e->x1) * (e->y2 - e->y1);
993 mask |= view->output_mask;
996 new_output = view->output;
1000 pixman_region32_fini(®ion);
1002 es->output = new_output;
1003 weston_surface_update_output_mask(es, mask);
1006 /** Recalculate which output(s) the view is displayed on
1008 * \param ev The view to remap to outputs
1010 * Identifies the set of outputs that the view is visible on,
1011 * noting them into the output_mask. The output that the view
1012 * is most visible on is set as the view's primary output.
1014 * Also does the same for the view's surface. See
1015 * weston_surface_assign_output().
1018 weston_view_assign_output(struct weston_view *ev)
1020 struct weston_compositor *ec = ev->surface->compositor;
1021 struct weston_output *output, *new_output;
1022 pixman_region32_t region;
1023 uint32_t max, area, mask;
1029 pixman_region32_init(®ion);
1030 wl_list_for_each(output, &ec->output_list, link) {
1031 if (output->destroying)
1034 pixman_region32_intersect(®ion, &ev->transform.boundingbox,
1037 e = pixman_region32_extents(®ion);
1038 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1041 mask |= 1u << output->id;
1044 new_output = output;
1048 pixman_region32_fini(®ion);
1050 ev->output = new_output;
1051 ev->output_mask = mask;
1053 weston_surface_assign_output(ev->surface);
1057 weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1058 int from_x, int from_y, int *to_x, int *to_y)
1062 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1063 weston_view_from_global_float(to, x, y, &x, &y);
1070 weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1075 a = pixman_region32_extents(&from->geometry.scissor);
1077 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1078 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1080 pixman_region32_fini(&to->geometry.scissor);
1081 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1085 view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
1086 pixman_region32_t *bbox)
1088 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1089 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
1091 { inbox->x1, inbox->y1 },
1092 { inbox->x1, inbox->y2 },
1093 { inbox->x2, inbox->y1 },
1094 { inbox->x2, inbox->y2 },
1099 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
1100 /* avoid rounding empty bbox to 1x1 */
1101 pixman_region32_init(bbox);
1105 for (i = 0; i < 4; ++i) {
1107 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
1118 int_x = floorf(min_x);
1119 int_y = floorf(min_y);
1120 pixman_region32_init_rect(bbox, int_x, int_y,
1121 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
1125 weston_view_update_transform_disable(struct weston_view *view)
1127 view->transform.enabled = 0;
1129 /* round off fractions when not transformed */
1130 view->geometry.x = roundf(view->geometry.x);
1131 view->geometry.y = roundf(view->geometry.y);
1133 /* Otherwise identity matrix, but with x and y translation. */
1134 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1135 view->transform.position.matrix.d[12] = view->geometry.x;
1136 view->transform.position.matrix.d[13] = view->geometry.y;
1138 view->transform.matrix = view->transform.position.matrix;
1140 view->transform.inverse = view->transform.position.matrix;
1141 view->transform.inverse.d[12] = -view->geometry.x;
1142 view->transform.inverse.d[13] = -view->geometry.y;
1144 pixman_region32_init_rect(&view->transform.boundingbox,
1146 view->surface->width,
1147 view->surface->height);
1148 if (view->geometry.scissor_enabled)
1149 pixman_region32_intersect(&view->transform.boundingbox,
1150 &view->transform.boundingbox,
1151 &view->geometry.scissor);
1153 pixman_region32_translate(&view->transform.boundingbox,
1154 view->geometry.x, view->geometry.y);
1156 if (view->alpha == 1.0) {
1157 pixman_region32_copy(&view->transform.opaque,
1158 &view->surface->opaque);
1159 pixman_region32_translate(&view->transform.opaque,
1166 weston_view_update_transform_enable(struct weston_view *view)
1168 struct weston_view *parent = view->geometry.parent;
1169 struct weston_matrix *matrix = &view->transform.matrix;
1170 struct weston_matrix *inverse = &view->transform.inverse;
1171 struct weston_transform *tform;
1172 pixman_region32_t surfregion;
1173 const pixman_box32_t *surfbox;
1175 view->transform.enabled = 1;
1177 /* Otherwise identity matrix, but with x and y translation. */
1178 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1179 view->transform.position.matrix.d[12] = view->geometry.x;
1180 view->transform.position.matrix.d[13] = view->geometry.y;
1182 weston_matrix_init(matrix);
1183 wl_list_for_each(tform, &view->geometry.transformation_list, link)
1184 weston_matrix_multiply(matrix, &tform->matrix);
1187 weston_matrix_multiply(matrix, &parent->transform.matrix);
1189 if (weston_matrix_invert(inverse, matrix) < 0) {
1190 /* Oops, bad total transformation, not invertible */
1191 weston_log("error: weston_view %p"
1192 " transformation not invertible.\n", view);
1196 if (view->alpha == 1.0 &&
1197 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1198 pixman_region32_copy(&view->transform.opaque,
1199 &view->surface->opaque);
1200 pixman_region32_translate(&view->transform.opaque,
1205 pixman_region32_init_rect(&surfregion, 0, 0,
1206 view->surface->width, view->surface->height);
1207 if (view->geometry.scissor_enabled)
1208 pixman_region32_intersect(&surfregion, &surfregion,
1209 &view->geometry.scissor);
1210 surfbox = pixman_region32_extents(&surfregion);
1212 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1213 pixman_region32_fini(&surfregion);
1218 static struct weston_layer *
1219 get_view_layer(struct weston_view *view)
1221 if (view->parent_view)
1222 return get_view_layer(view->parent_view);
1223 return view->layer_link.layer;
1227 weston_view_update_transform(struct weston_view *view)
1229 struct weston_view *parent = view->geometry.parent;
1230 struct weston_layer *layer;
1231 pixman_region32_t mask;
1233 if (!view->transform.dirty)
1237 weston_view_update_transform(parent);
1239 view->transform.dirty = 0;
1241 weston_view_damage_below(view);
1243 pixman_region32_fini(&view->transform.boundingbox);
1244 pixman_region32_fini(&view->transform.opaque);
1245 pixman_region32_init(&view->transform.opaque);
1247 /* transform.position is always in transformation_list */
1248 if (view->geometry.transformation_list.next ==
1249 &view->transform.position.link &&
1250 view->geometry.transformation_list.prev ==
1251 &view->transform.position.link &&
1253 weston_view_update_transform_disable(view);
1255 if (weston_view_update_transform_enable(view) < 0)
1256 weston_view_update_transform_disable(view);
1259 layer = get_view_layer(view);
1261 pixman_region32_init_with_extents(&mask, &layer->mask);
1262 pixman_region32_intersect(&view->transform.boundingbox,
1263 &view->transform.boundingbox, &mask);
1264 pixman_region32_intersect(&view->transform.opaque,
1265 &view->transform.opaque, &mask);
1266 pixman_region32_fini(&mask);
1270 if (parent->geometry.scissor_enabled) {
1271 view->geometry.scissor_enabled = true;
1272 weston_view_transfer_scissor(parent, view);
1274 view->geometry.scissor_enabled = false;
1278 weston_view_damage_below(view);
1280 weston_view_assign_output(view);
1282 wl_signal_emit(&view->surface->compositor->transform_signal,
1287 weston_view_geometry_dirty(struct weston_view *view)
1289 struct weston_view *child;
1292 * The invariant: if view->geometry.dirty, then all views
1293 * in view->geometry.child_list have geometry.dirty too.
1294 * Corollary: if not parent->geometry.dirty, then all ancestors
1298 if (view->transform.dirty)
1301 view->transform.dirty = 1;
1303 wl_list_for_each(child, &view->geometry.child_list,
1304 geometry.parent_link)
1305 weston_view_geometry_dirty(child);
1309 weston_view_to_global_fixed(struct weston_view *view,
1310 wl_fixed_t vx, wl_fixed_t vy,
1311 wl_fixed_t *x, wl_fixed_t *y)
1315 weston_view_to_global_float(view,
1316 wl_fixed_to_double(vx),
1317 wl_fixed_to_double(vy),
1319 *x = wl_fixed_from_double(xf);
1320 *y = wl_fixed_from_double(yf);
1324 weston_view_from_global_float(struct weston_view *view,
1325 float x, float y, float *vx, float *vy)
1327 if (view->transform.enabled) {
1328 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1330 weston_matrix_transform(&view->transform.inverse, &v);
1332 if (fabsf(v.f[3]) < 1e-6) {
1333 weston_log("warning: numerical instability in "
1334 "weston_view_from_global(), divisor = %g\n",
1341 *vx = v.f[0] / v.f[3];
1342 *vy = v.f[1] / v.f[3];
1344 *vx = x - view->geometry.x;
1345 *vy = y - view->geometry.y;
1350 weston_view_from_global_fixed(struct weston_view *view,
1351 wl_fixed_t x, wl_fixed_t y,
1352 wl_fixed_t *vx, wl_fixed_t *vy)
1356 weston_view_from_global_float(view,
1357 wl_fixed_to_double(x),
1358 wl_fixed_to_double(y),
1360 *vx = wl_fixed_from_double(vxf);
1361 *vy = wl_fixed_from_double(vyf);
1365 weston_view_from_global(struct weston_view *view,
1366 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
1370 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1376 * \param surface The surface to be repainted
1378 * Marks the output(s) that the surface is shown on as needing to be
1379 * repainted. See weston_output_schedule_repaint().
1382 weston_surface_schedule_repaint(struct weston_surface *surface)
1384 struct weston_output *output;
1386 wl_list_for_each(output, &surface->compositor->output_list, link)
1387 if (surface->output_mask & (1u << output->id))
1388 weston_output_schedule_repaint(output);
1392 * \param view The view to be repainted
1394 * Marks the output(s) that the view is shown on as needing to be
1395 * repainted. See weston_output_schedule_repaint().
1398 weston_view_schedule_repaint(struct weston_view *view)
1400 struct weston_output *output;
1402 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1403 if (view->output_mask & (1u << output->id))
1404 weston_output_schedule_repaint(output);
1408 * XXX: This function does it the wrong way.
1409 * surface->damage is the damage from the client, and causes
1410 * surface_flush_damage() to copy pixels. No window management action can
1411 * cause damage to the client-provided content, warranting re-upload!
1413 * Instead of surface->damage, this function should record the damage
1414 * with all the views for this surface to avoid extraneous texture
1418 weston_surface_damage(struct weston_surface *surface)
1420 pixman_region32_union_rect(&surface->damage, &surface->damage,
1421 0, 0, surface->width,
1424 weston_surface_schedule_repaint(surface);
1428 weston_view_set_position(struct weston_view *view, float x, float y)
1430 if (view->geometry.x == x && view->geometry.y == y)
1433 view->geometry.x = x;
1434 view->geometry.y = y;
1435 weston_view_geometry_dirty(view);
1439 transform_parent_handle_parent_destroy(struct wl_listener *listener,
1442 struct weston_view *view =
1443 container_of(listener, struct weston_view,
1444 geometry.parent_destroy_listener);
1446 weston_view_set_transform_parent(view, NULL);
1450 weston_view_set_transform_parent(struct weston_view *view,
1451 struct weston_view *parent)
1453 if (view->geometry.parent) {
1454 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1455 wl_list_remove(&view->geometry.parent_link);
1458 view->geometry.scissor_enabled = false;
1461 view->geometry.parent = parent;
1463 view->geometry.parent_destroy_listener.notify =
1464 transform_parent_handle_parent_destroy;
1466 wl_signal_add(&parent->destroy_signal,
1467 &view->geometry.parent_destroy_listener);
1468 wl_list_insert(&parent->geometry.child_list,
1469 &view->geometry.parent_link);
1472 weston_view_geometry_dirty(view);
1475 /** Set a clip mask rectangle on a view
1477 * \param view The view to set the clip mask on.
1478 * \param x Top-left corner X coordinate of the clip rectangle.
1479 * \param y Top-left corner Y coordinate of the clip rectangle.
1480 * \param width Width of the clip rectangle, non-negative.
1481 * \param height Height of the clip rectangle, non-negative.
1483 * A shell may set a clip mask rectangle on a view. Everything outside
1484 * the rectangle is cut away for input and output purposes: it is
1485 * not drawn and cannot be hit by hit-test based input like pointer
1486 * motion or touch-downs. Everything inside the rectangle will behave
1487 * normally. Clients are unaware of clipping.
1489 * The rectangle is set in surface-local coordinates. Setting a clip
1490 * mask rectangle does not affect the view position, the view is positioned
1491 * as it would be without a clip. The clip also does not change
1492 * weston_surface::width,height.
1494 * The clip mask rectangle is part of transformation inheritance
1495 * (weston_view_set_transform_parent()). A clip set in the root of the
1496 * transformation inheritance tree will affect all views in the tree.
1497 * A clip can be set only on the root view. Attempting to set a clip
1498 * on view that has a transformation parent will fail. Assigning a parent
1499 * to a view that has a clip set will cause the clip to be forgotten.
1501 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1502 * on the additional transformations in the child views. These transformations
1503 * may not rotate the coordinate axes, i.e., only translation and scaling
1504 * are allowed. Violating this restriction causes the clipping to malfunction.
1505 * Furthermore, using scaling may cause rounding errors in child clipping.
1507 * The clip mask rectangle is not automatically adjusted based on
1508 * wl_surface.attach dx and dy arguments.
1510 * A clip mask rectangle can be set only if the compositor capability
1511 * WESTON_CAP_VIEW_CLIP_MASK is present.
1513 * This function sets the clip mask rectangle and schedules a repaint for
1517 weston_view_set_mask(struct weston_view *view,
1518 int x, int y, int width, int height)
1520 struct weston_compositor *compositor = view->surface->compositor;
1522 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1523 weston_log("%s not allowed without capability!\n", __func__);
1527 if (view->geometry.parent) {
1528 weston_log("view %p has a parent, clip forbidden!\n", view);
1532 if (width < 0 || height < 0) {
1533 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1534 x, y, width, height);
1538 pixman_region32_fini(&view->geometry.scissor);
1539 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1540 view->geometry.scissor_enabled = true;
1541 weston_view_geometry_dirty(view);
1542 weston_view_schedule_repaint(view);
1545 /** Remove the clip mask from a view
1547 * \param view The view to remove the clip mask from.
1549 * Removed the clip mask rectangle and schedules a repaint.
1551 * \sa weston_view_set_mask
1554 weston_view_set_mask_infinite(struct weston_view *view)
1556 view->geometry.scissor_enabled = false;
1557 weston_view_geometry_dirty(view);
1558 weston_view_schedule_repaint(view);
1561 /* Check if view should be displayed
1563 * The indicator is set manually when assigning
1564 * a view to a surface.
1566 * This needs reworking. See the thread starting at:
1568 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1571 weston_view_is_mapped(struct weston_view *view)
1573 return view->is_mapped;
1576 /* Check if a surface has a view assigned to it
1578 * The indicator is set manually when mapping
1579 * a surface and creating a view for it.
1581 * This needs to go. See the thread starting at:
1583 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1587 weston_surface_is_mapped(struct weston_surface *surface)
1589 return surface->is_mapped;
1593 surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
1595 struct weston_view *view;
1597 if (surface->width == width && surface->height == height)
1600 surface->width = width;
1601 surface->height = height;
1603 wl_list_for_each(view, &surface->views, surface_link)
1604 weston_view_geometry_dirty(view);
1608 weston_surface_set_size(struct weston_surface *surface,
1609 int32_t width, int32_t height)
1611 assert(!surface->resource);
1612 surface_set_size(surface, width, height);
1616 fixed_round_up_to_int(wl_fixed_t f)
1618 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1622 convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1623 int32_t width, int32_t height,
1629 switch (transform) {
1630 case WL_OUTPUT_TRANSFORM_NORMAL:
1631 case WL_OUTPUT_TRANSFORM_180:
1632 case WL_OUTPUT_TRANSFORM_FLIPPED:
1633 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1634 *width_out = width / scale;
1635 *height_out = height / scale;
1637 case WL_OUTPUT_TRANSFORM_90:
1638 case WL_OUTPUT_TRANSFORM_270:
1639 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1640 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1641 *width_out = height / scale;
1642 *height_out = width / scale;
1645 assert(0 && "invalid transform");
1650 weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
1652 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1654 if (!surface->buffer_ref.buffer) {
1655 surface->width_from_buffer = 0;
1656 surface->height_from_buffer = 0;
1660 convert_size_by_transform_scale(&surface->width_from_buffer,
1661 &surface->height_from_buffer,
1662 surface->buffer_ref.buffer->width,
1663 surface->buffer_ref.buffer->height,
1664 vp->buffer.transform,
1669 weston_surface_update_size(struct weston_surface *surface)
1671 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1672 int32_t width, height;
1674 width = surface->width_from_buffer;
1675 height = surface->height_from_buffer;
1677 if (width != 0 && vp->surface.width != -1) {
1678 surface_set_size(surface,
1679 vp->surface.width, vp->surface.height);
1683 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
1684 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1685 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1687 surface_set_size(surface, w ?: 1, h ?: 1);
1691 surface_set_size(surface, width, height);
1695 weston_compositor_get_time(void)
1699 gettimeofday(&tv, NULL);
1701 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
1704 WL_EXPORT struct weston_view *
1705 weston_compositor_pick_view(struct weston_compositor *compositor,
1706 wl_fixed_t x, wl_fixed_t y,
1707 wl_fixed_t *vx, wl_fixed_t *vy)
1709 struct weston_view *view;
1710 wl_fixed_t view_x, view_y;
1711 int view_ix, view_iy;
1712 int ix = wl_fixed_to_int(x);
1713 int iy = wl_fixed_to_int(y);
1715 wl_list_for_each(view, &compositor->view_list, link) {
1716 if (!pixman_region32_contains_point(
1717 &view->transform.boundingbox, ix, iy, NULL))
1720 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1721 view_ix = wl_fixed_to_int(view_x);
1722 view_iy = wl_fixed_to_int(view_y);
1724 if (!pixman_region32_contains_point(&view->surface->input,
1725 view_ix, view_iy, NULL))
1728 if (view->geometry.scissor_enabled &&
1729 !pixman_region32_contains_point(&view->geometry.scissor,
1730 view_ix, view_iy, NULL))
1738 *vx = wl_fixed_from_int(-1000000);
1739 *vy = wl_fixed_from_int(-1000000);
1744 weston_compositor_repick(struct weston_compositor *compositor)
1746 struct weston_seat *seat;
1748 if (!compositor->session_active)
1751 wl_list_for_each(seat, &compositor->seat_list, link)
1752 weston_seat_repick(seat);
1756 weston_view_unmap(struct weston_view *view)
1758 struct weston_seat *seat;
1760 if (!weston_view_is_mapped(view))
1763 weston_view_damage_below(view);
1764 view->output = NULL;
1766 view->is_mapped = false;
1767 weston_layer_entry_remove(&view->layer_link);
1768 wl_list_remove(&view->link);
1769 wl_list_init(&view->link);
1770 view->output_mask = 0;
1771 weston_surface_assign_output(view->surface);
1773 if (weston_surface_is_mapped(view->surface))
1776 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1777 struct weston_touch *touch = weston_seat_get_touch(seat);
1778 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1779 struct weston_keyboard *keyboard =
1780 weston_seat_get_keyboard(seat);
1782 if (keyboard && keyboard->focus == view->surface)
1783 weston_keyboard_set_focus(keyboard, NULL);
1784 if (pointer && pointer->focus == view)
1785 weston_pointer_clear_focus(pointer);
1786 if (touch && touch->focus == view)
1787 weston_touch_set_focus(touch, NULL);
1792 weston_surface_unmap(struct weston_surface *surface)
1794 struct weston_view *view;
1796 surface->is_mapped = false;
1797 wl_list_for_each(view, &surface->views, surface_link)
1798 weston_view_unmap(view);
1799 surface->output = NULL;
1803 weston_surface_reset_pending_buffer(struct weston_surface *surface)
1805 weston_surface_state_set_buffer(&surface->pending, NULL);
1806 surface->pending.sx = 0;
1807 surface->pending.sy = 0;
1808 surface->pending.newly_attached = 0;
1809 surface->pending.buffer_viewport.changed = 0;
1813 weston_view_destroy(struct weston_view *view)
1815 wl_signal_emit(&view->destroy_signal, view);
1817 assert(wl_list_empty(&view->geometry.child_list));
1819 if (weston_view_is_mapped(view)) {
1820 weston_view_unmap(view);
1821 weston_compositor_build_view_list(view->surface->compositor);
1824 wl_list_remove(&view->link);
1825 weston_layer_entry_remove(&view->layer_link);
1827 pixman_region32_fini(&view->clip);
1828 pixman_region32_fini(&view->geometry.scissor);
1829 pixman_region32_fini(&view->transform.boundingbox);
1830 pixman_region32_fini(&view->transform.opaque);
1832 weston_view_set_transform_parent(view, NULL);
1834 wl_list_remove(&view->surface_link);
1840 weston_surface_destroy(struct weston_surface *surface)
1842 struct weston_frame_callback *cb, *next;
1843 struct weston_view *ev, *nv;
1844 struct weston_pointer_constraint *constraint, *next_constraint;
1846 if (--surface->ref_count > 0)
1849 assert(surface->resource == NULL);
1851 wl_signal_emit(&surface->destroy_signal, surface);
1853 assert(wl_list_empty(&surface->subsurface_list_pending));
1854 assert(wl_list_empty(&surface->subsurface_list));
1856 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1857 weston_view_destroy(ev);
1859 weston_surface_state_fini(&surface->pending);
1861 weston_buffer_reference(&surface->buffer_ref, NULL);
1863 pixman_region32_fini(&surface->damage);
1864 pixman_region32_fini(&surface->opaque);
1865 pixman_region32_fini(&surface->input);
1867 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
1868 wl_resource_destroy(cb->resource);
1870 weston_presentation_feedback_discard_list(&surface->feedback_list);
1872 wl_list_for_each_safe(constraint, next_constraint,
1873 &surface->pointer_constraints,
1875 weston_pointer_constraint_destroy(constraint);
1881 destroy_surface(struct wl_resource *resource)
1883 struct weston_surface *surface = wl_resource_get_user_data(resource);
1887 /* Set the resource to NULL, since we don't want to leave a
1888 * dangling pointer if the surface was refcounted and survives
1889 * the weston_surface_destroy() call. */
1890 surface->resource = NULL;
1892 if (surface->viewport_resource)
1893 wl_resource_set_user_data(surface->viewport_resource, NULL);
1895 weston_surface_destroy(surface);
1899 weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1901 struct weston_buffer *buffer =
1902 container_of(listener, struct weston_buffer, destroy_listener);
1904 wl_signal_emit(&buffer->destroy_signal, buffer);
1908 WL_EXPORT struct weston_buffer *
1909 weston_buffer_from_resource(struct wl_resource *resource)
1911 struct weston_buffer *buffer;
1912 struct wl_listener *listener;
1914 listener = wl_resource_get_destroy_listener(resource,
1915 weston_buffer_destroy_handler);
1918 return container_of(listener, struct weston_buffer,
1921 buffer = zalloc(sizeof *buffer);
1925 buffer->resource = resource;
1926 wl_signal_init(&buffer->destroy_signal);
1927 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
1928 buffer->y_inverted = 1;
1929 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
1935 weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1938 struct weston_buffer_reference *ref =
1939 container_of(listener, struct weston_buffer_reference,
1942 assert((struct weston_buffer *)data == ref->buffer);
1947 weston_buffer_reference(struct weston_buffer_reference *ref,
1948 struct weston_buffer *buffer)
1950 if (ref->buffer && buffer != ref->buffer) {
1951 ref->buffer->busy_count--;
1952 if (ref->buffer->busy_count == 0) {
1953 assert(wl_resource_get_client(ref->buffer->resource));
1954 wl_resource_queue_event(ref->buffer->resource,
1957 wl_list_remove(&ref->destroy_listener.link);
1960 if (buffer && buffer != ref->buffer) {
1961 buffer->busy_count++;
1962 wl_signal_add(&buffer->destroy_signal,
1963 &ref->destroy_listener);
1966 ref->buffer = buffer;
1967 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1971 weston_surface_attach(struct weston_surface *surface,
1972 struct weston_buffer *buffer)
1974 weston_buffer_reference(&surface->buffer_ref, buffer);
1977 if (weston_surface_is_mapped(surface))
1978 weston_surface_unmap(surface);
1981 surface->compositor->renderer->attach(surface, buffer);
1983 weston_surface_calculate_size_from_buffer(surface);
1984 weston_presentation_feedback_discard_list(&surface->feedback_list);
1988 weston_compositor_damage_all(struct weston_compositor *compositor)
1990 struct weston_output *output;
1992 wl_list_for_each(output, &compositor->output_list, link)
1993 weston_output_damage(output);
1997 weston_output_damage(struct weston_output *output)
1999 struct weston_compositor *compositor = output->compositor;
2001 pixman_region32_union(&compositor->primary_plane.damage,
2002 &compositor->primary_plane.damage,
2004 weston_output_schedule_repaint(output);
2008 surface_flush_damage(struct weston_surface *surface)
2010 if (surface->buffer_ref.buffer &&
2011 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
2012 surface->compositor->renderer->flush_damage(surface);
2014 if (weston_timeline_enabled_ &&
2015 pixman_region32_not_empty(&surface->damage))
2016 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2017 TLP_OUTPUT(surface->output), TLP_END);
2019 pixman_region32_clear(&surface->damage);
2023 view_accumulate_damage(struct weston_view *view,
2024 pixman_region32_t *opaque)
2026 pixman_region32_t damage;
2028 pixman_region32_init(&damage);
2029 if (view->transform.enabled) {
2030 pixman_box32_t *extents;
2032 extents = pixman_region32_extents(&view->surface->damage);
2033 view_compute_bbox(view, extents, &damage);
2035 pixman_region32_copy(&damage, &view->surface->damage);
2036 pixman_region32_translate(&damage,
2037 view->geometry.x, view->geometry.y);
2040 pixman_region32_intersect(&damage, &damage,
2041 &view->transform.boundingbox);
2042 pixman_region32_subtract(&damage, &damage, opaque);
2043 pixman_region32_union(&view->plane->damage,
2044 &view->plane->damage, &damage);
2045 pixman_region32_fini(&damage);
2046 pixman_region32_copy(&view->clip, opaque);
2047 pixman_region32_union(opaque, opaque, &view->transform.opaque);
2051 compositor_accumulate_damage(struct weston_compositor *ec)
2053 struct weston_plane *plane;
2054 struct weston_view *ev;
2055 pixman_region32_t opaque, clip;
2057 pixman_region32_init(&clip);
2059 wl_list_for_each(plane, &ec->plane_list, link) {
2060 pixman_region32_copy(&plane->clip, &clip);
2062 pixman_region32_init(&opaque);
2064 wl_list_for_each(ev, &ec->view_list, link) {
2065 if (ev->plane != plane)
2068 view_accumulate_damage(ev, &opaque);
2071 pixman_region32_union(&clip, &clip, &opaque);
2072 pixman_region32_fini(&opaque);
2075 pixman_region32_fini(&clip);
2077 wl_list_for_each(ev, &ec->view_list, link)
2078 ev->surface->touched = false;
2080 wl_list_for_each(ev, &ec->view_list, link) {
2081 if (ev->surface->touched)
2083 ev->surface->touched = true;
2085 surface_flush_damage(ev->surface);
2087 /* Both the renderer and the backend have seen the buffer
2088 * by now. If renderer needs the buffer, it has its own
2089 * reference set. If the backend wants to keep the buffer
2090 * around for migrating the surface into a non-primary plane
2091 * later, keep_buffer is true. Otherwise, drop the core
2092 * reference now, and allow early buffer release. This enables
2093 * clients to use single-buffering.
2095 if (!ev->surface->keep_buffer)
2096 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
2101 surface_stash_subsurface_views(struct weston_surface *surface)
2103 struct weston_subsurface *sub;
2105 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2106 if (sub->surface == surface)
2109 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2110 wl_list_init(&sub->surface->views);
2112 surface_stash_subsurface_views(sub->surface);
2117 surface_free_unused_subsurface_views(struct weston_surface *surface)
2119 struct weston_subsurface *sub;
2120 struct weston_view *view, *nv;
2122 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2123 if (sub->surface == surface)
2126 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2127 weston_view_unmap (view);
2128 weston_view_destroy(view);
2131 surface_free_unused_subsurface_views(sub->surface);
2136 view_list_add_subsurface_view(struct weston_compositor *compositor,
2137 struct weston_subsurface *sub,
2138 struct weston_view *parent)
2140 struct weston_subsurface *child;
2141 struct weston_view *view = NULL, *iv;
2143 if (!weston_surface_is_mapped(sub->surface))
2146 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2147 if (iv->geometry.parent == parent) {
2154 /* Put it back in the surface's list of views */
2155 wl_list_remove(&view->surface_link);
2156 wl_list_insert(&sub->surface->views, &view->surface_link);
2158 view = weston_view_create(sub->surface);
2159 weston_view_set_position(view,
2162 weston_view_set_transform_parent(view, parent);
2165 view->parent_view = parent;
2166 weston_view_update_transform(view);
2167 view->is_mapped = true;
2169 if (wl_list_empty(&sub->surface->subsurface_list)) {
2170 wl_list_insert(compositor->view_list.prev, &view->link);
2174 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2175 if (child->surface == sub->surface)
2176 wl_list_insert(compositor->view_list.prev, &view->link);
2178 view_list_add_subsurface_view(compositor, child, view);
2182 /* This recursively adds the sub-surfaces for a view, relying on the
2183 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2184 * change first happens to the sub-surface list, and then automatically
2185 * propagates here. See weston_surface_damage_subsurfaces() for how the
2186 * sub-surfaces receive damage when the client changes the state.
2189 view_list_add(struct weston_compositor *compositor,
2190 struct weston_view *view)
2192 struct weston_subsurface *sub;
2194 weston_view_update_transform(view);
2196 if (wl_list_empty(&view->surface->subsurface_list)) {
2197 wl_list_insert(compositor->view_list.prev, &view->link);
2201 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2202 if (sub->surface == view->surface)
2203 wl_list_insert(compositor->view_list.prev, &view->link);
2205 view_list_add_subsurface_view(compositor, sub, view);
2210 weston_compositor_build_view_list(struct weston_compositor *compositor)
2212 struct weston_view *view;
2213 struct weston_layer *layer;
2215 wl_list_for_each(layer, &compositor->layer_list, link)
2216 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
2217 surface_stash_subsurface_views(view->surface);
2219 wl_list_init(&compositor->view_list);
2220 wl_list_for_each(layer, &compositor->layer_list, link) {
2221 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2222 view_list_add(compositor, view);
2226 wl_list_for_each(layer, &compositor->layer_list, link)
2227 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
2228 surface_free_unused_subsurface_views(view->surface);
2232 weston_output_take_feedback_list(struct weston_output *output,
2233 struct weston_surface *surface)
2235 struct weston_view *view;
2236 struct weston_presentation_feedback *feedback;
2237 uint32_t flags = 0xffffffff;
2239 if (wl_list_empty(&surface->feedback_list))
2242 /* All views must have the flag for the flag to survive. */
2243 wl_list_for_each(view, &surface->views, surface_link) {
2244 /* ignore views that are not on this output at all */
2245 if (view->output_mask & (1u << output->id))
2246 flags &= view->psf_flags;
2249 wl_list_for_each(feedback, &surface->feedback_list, link)
2250 feedback->psf_flags = flags;
2252 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2253 wl_list_init(&surface->feedback_list);
2257 weston_output_repaint(struct weston_output *output)
2259 struct weston_compositor *ec = output->compositor;
2260 struct weston_view *ev;
2261 struct weston_animation *animation, *next;
2262 struct weston_frame_callback *cb, *cnext;
2263 struct wl_list frame_callback_list;
2264 pixman_region32_t output_damage;
2267 if (output->destroying)
2270 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2272 /* Rebuild the surface list and update surface transforms up front. */
2273 weston_compositor_build_view_list(ec);
2275 if (output->assign_planes && !output->disable_planes) {
2276 output->assign_planes(output);
2278 wl_list_for_each(ev, &ec->view_list, link) {
2279 weston_view_move_to_plane(ev, &ec->primary_plane);
2284 wl_list_init(&frame_callback_list);
2285 wl_list_for_each(ev, &ec->view_list, link) {
2286 /* Note: This operation is safe to do multiple times on the
2289 if (ev->surface->output == output) {
2290 wl_list_insert_list(&frame_callback_list,
2291 &ev->surface->frame_callback_list);
2292 wl_list_init(&ev->surface->frame_callback_list);
2294 weston_output_take_feedback_list(output, ev->surface);
2298 compositor_accumulate_damage(ec);
2300 pixman_region32_init(&output_damage);
2301 pixman_region32_intersect(&output_damage,
2302 &ec->primary_plane.damage, &output->region);
2303 pixman_region32_subtract(&output_damage,
2304 &output_damage, &ec->primary_plane.clip);
2307 weston_output_update_matrix(output);
2309 r = output->repaint(output, &output_damage);
2311 pixman_region32_fini(&output_damage);
2313 output->repaint_needed = false;
2315 output->repaint_status = REPAINT_AWAITING_COMPLETION;
2317 weston_compositor_repick(ec);
2319 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
2320 wl_callback_send_done(cb->resource, output->frame_time);
2321 wl_resource_destroy(cb->resource);
2324 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
2325 animation->frame_counter++;
2326 animation->frame(animation, output, output->frame_time);
2329 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2335 weston_output_schedule_repaint_reset(struct weston_output *output)
2337 output->repaint_status = REPAINT_NOT_SCHEDULED;
2338 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2342 output_repaint_timer_handler(void *data)
2344 struct weston_output *output = data;
2345 struct weston_compositor *compositor = output->compositor;
2348 assert(output->repaint_status == REPAINT_SCHEDULED);
2350 /* If we're sleeping, drop the repaint machinery entirely; we will
2351 * explicitly repaint all outputs when we come back. */
2352 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2353 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2356 /* We don't actually need to repaint this output; drop it from
2357 * repaint until something causes damage. */
2358 if (!output->repaint_needed)
2361 /* If repaint fails, we aren't going to get weston_output_finish_frame
2362 * to trigger a new repaint, so drop it from repaint and hope
2363 * something schedules a successful repaint later. */
2364 ret = weston_output_repaint(output);
2371 weston_output_schedule_repaint_reset(output);
2376 weston_output_finish_frame(struct weston_output *output,
2377 const struct timespec *stamp,
2378 uint32_t presented_flags)
2380 struct weston_compositor *compositor = output->compositor;
2381 int32_t refresh_nsec;
2382 struct timespec now;
2383 struct timespec next;
2384 struct timespec remain;
2387 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2388 TLP_VBLANK(stamp), TLP_END);
2390 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
2391 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2393 /* If we haven't been supplied any timestamp at all, we don't have a
2394 * timebase to work against, so any delay just wastes time. Push a
2395 * repaint as soon as possible so we can get on with it. */
2399 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
2400 weston_presentation_feedback_present_list(&output->feedback_list,
2401 output, refresh_nsec, stamp,
2405 output->frame_time = timespec_to_msec(stamp);
2406 weston_compositor_read_presentation_clock(compositor, &now);
2408 timespec_add_nsec(&next, stamp, refresh_nsec);
2409 timespec_add_msec(&next, &next, -compositor->repaint_msec);
2410 timespec_sub(&remain, &next, &now);
2411 msec_rel = timespec_to_msec(&remain);
2413 if (msec_rel < -1000 || msec_rel > 1000) {
2417 weston_log("Warning: computed repaint delay is "
2418 "insane: %d msec\n", msec_rel);
2424 /* Called from restart_repaint_loop and restart happens already after
2425 * the deadline given by repaint_msec? In that case we delay until
2426 * the deadline of the next frame, to give clients a more predictable
2427 * timing of the repaint cycle to lock on. */
2428 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec_rel < 0)
2429 msec_rel += refresh_nsec / 1000000;
2432 output->repaint_status = REPAINT_SCHEDULED;
2435 output_repaint_timer_handler(output);
2437 wl_event_source_timer_update(output->repaint_timer, msec_rel);
2441 idle_repaint(void *data)
2443 struct weston_output *output = data;
2445 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2446 output->repaint_status = REPAINT_AWAITING_COMPLETION;
2447 output->start_repaint_loop(output);
2451 weston_layer_entry_insert(struct weston_layer_entry *list,
2452 struct weston_layer_entry *entry)
2454 wl_list_insert(&list->link, &entry->link);
2455 entry->layer = list->layer;
2459 weston_layer_entry_remove(struct weston_layer_entry *entry)
2461 wl_list_remove(&entry->link);
2462 wl_list_init(&entry->link);
2463 entry->layer = NULL;
2467 /** Initialize the weston_layer struct.
2469 * \param compositor The compositor instance
2470 * \param layer The layer to initialize
2473 weston_layer_init(struct weston_layer *layer,
2474 struct weston_compositor *compositor)
2476 layer->compositor = compositor;
2477 wl_list_init(&layer->link);
2478 wl_list_init(&layer->view_list.link);
2479 layer->view_list.layer = layer;
2480 weston_layer_set_mask_infinite(layer);
2483 /** Sets the position of the layer in the layer list. The layer will be placed
2484 * below any layer with the same position value, if any.
2485 * This function is safe to call if the layer is already on the list, but the
2486 * layer may be moved below other layers at the same position, if any.
2488 * \param layer The layer to modify
2489 * \param position The position the layer will be placed at
2492 weston_layer_set_position(struct weston_layer *layer,
2493 enum weston_layer_position position)
2495 struct weston_layer *below;
2497 wl_list_remove(&layer->link);
2499 /* layer_list is ordered from top to bottom, the last layer being the
2500 * background with the smallest position value */
2502 layer->position = position;
2503 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2504 if (below->position >= layer->position) {
2505 wl_list_insert(&below->link, &layer->link);
2509 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2512 /** Hide a layer by taking it off the layer list.
2513 * This function is safe to call if the layer is not on the list.
2515 * \param layer The layer to hide
2518 weston_layer_unset_position(struct weston_layer *layer)
2520 wl_list_remove(&layer->link);
2521 wl_list_init(&layer->link);
2525 weston_layer_set_mask(struct weston_layer *layer,
2526 int x, int y, int width, int height)
2528 struct weston_view *view;
2531 layer->mask.x2 = x + width;
2533 layer->mask.y2 = y + height;
2535 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2536 weston_view_geometry_dirty(view);
2541 weston_layer_set_mask_infinite(struct weston_layer *layer)
2543 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2544 UINT32_MAX, UINT32_MAX);
2548 weston_output_schedule_repaint(struct weston_output *output)
2550 struct weston_compositor *compositor = output->compositor;
2551 struct wl_event_loop *loop;
2553 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2554 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2557 if (!output->repaint_needed)
2558 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2560 loop = wl_display_get_event_loop(compositor->wl_display);
2561 output->repaint_needed = true;
2563 /* If we already have a repaint scheduled for our idle handler,
2564 * no need to set it again. If the repaint has been called but
2565 * not finished, then weston_output_finish_frame() will notice
2566 * that a repaint is needed and schedule one. */
2567 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
2570 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
2571 wl_event_loop_add_idle(loop, idle_repaint, output);
2572 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2576 weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2578 struct weston_output *output;
2580 wl_list_for_each(output, &compositor->output_list, link)
2581 weston_output_schedule_repaint(output);
2585 surface_destroy(struct wl_client *client, struct wl_resource *resource)
2587 wl_resource_destroy(resource);
2591 surface_attach(struct wl_client *client,
2592 struct wl_resource *resource,
2593 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2595 struct weston_surface *surface = wl_resource_get_user_data(resource);
2596 struct weston_buffer *buffer = NULL;
2598 if (buffer_resource) {
2599 buffer = weston_buffer_from_resource(buffer_resource);
2600 if (buffer == NULL) {
2601 wl_client_post_no_memory(client);
2606 /* Attach, attach, without commit in between does not send
2607 * wl_buffer.release. */
2608 weston_surface_state_set_buffer(&surface->pending, buffer);
2610 surface->pending.sx = sx;
2611 surface->pending.sy = sy;
2612 surface->pending.newly_attached = 1;
2616 surface_damage(struct wl_client *client,
2617 struct wl_resource *resource,
2618 int32_t x, int32_t y, int32_t width, int32_t height)
2620 struct weston_surface *surface = wl_resource_get_user_data(resource);
2622 if (width <= 0 || height <= 0)
2625 pixman_region32_union_rect(&surface->pending.damage_surface,
2626 &surface->pending.damage_surface,
2627 x, y, width, height);
2631 surface_damage_buffer(struct wl_client *client,
2632 struct wl_resource *resource,
2633 int32_t x, int32_t y, int32_t width, int32_t height)
2635 struct weston_surface *surface = wl_resource_get_user_data(resource);
2637 if (width <= 0 || height <= 0)
2640 pixman_region32_union_rect(&surface->pending.damage_buffer,
2641 &surface->pending.damage_buffer,
2642 x, y, width, height);
2646 destroy_frame_callback(struct wl_resource *resource)
2648 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
2650 wl_list_remove(&cb->link);
2655 surface_frame(struct wl_client *client,
2656 struct wl_resource *resource, uint32_t callback)
2658 struct weston_frame_callback *cb;
2659 struct weston_surface *surface = wl_resource_get_user_data(resource);
2661 cb = malloc(sizeof *cb);
2663 wl_resource_post_no_memory(resource);
2667 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2669 if (cb->resource == NULL) {
2671 wl_resource_post_no_memory(resource);
2675 wl_resource_set_implementation(cb->resource, NULL, cb,
2676 destroy_frame_callback);
2678 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
2682 surface_set_opaque_region(struct wl_client *client,
2683 struct wl_resource *resource,
2684 struct wl_resource *region_resource)
2686 struct weston_surface *surface = wl_resource_get_user_data(resource);
2687 struct weston_region *region;
2689 if (region_resource) {
2690 region = wl_resource_get_user_data(region_resource);
2691 pixman_region32_copy(&surface->pending.opaque,
2694 pixman_region32_clear(&surface->pending.opaque);
2699 surface_set_input_region(struct wl_client *client,
2700 struct wl_resource *resource,
2701 struct wl_resource *region_resource)
2703 struct weston_surface *surface = wl_resource_get_user_data(resource);
2704 struct weston_region *region;
2706 if (region_resource) {
2707 region = wl_resource_get_user_data(region_resource);
2708 pixman_region32_copy(&surface->pending.input,
2711 pixman_region32_fini(&surface->pending.input);
2712 region_init_infinite(&surface->pending.input);
2716 /* Cause damage to this sub-surface and all its children.
2718 * This is useful when there are state changes that need an implicit
2719 * damage, e.g. a z-order change.
2722 weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2724 struct weston_subsurface *child;
2726 weston_surface_damage(sub->surface);
2727 sub->reordered = false;
2729 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2731 weston_surface_damage_subsurfaces(child);
2735 weston_surface_commit_subsurface_order(struct weston_surface *surface)
2737 struct weston_subsurface *sub;
2739 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2740 parent_link_pending) {
2741 wl_list_remove(&sub->parent_link);
2742 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2745 weston_surface_damage_subsurfaces(sub);
2750 weston_surface_build_buffer_matrix(const struct weston_surface *surface,
2751 struct weston_matrix *matrix)
2753 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2754 double src_width, src_height, dest_width, dest_height;
2756 weston_matrix_init(matrix);
2758 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2759 src_width = surface->width_from_buffer;
2760 src_height = surface->height_from_buffer;
2762 src_width = wl_fixed_to_double(vp->buffer.src_width);
2763 src_height = wl_fixed_to_double(vp->buffer.src_height);
2766 if (vp->surface.width == -1) {
2767 dest_width = src_width;
2768 dest_height = src_height;
2770 dest_width = vp->surface.width;
2771 dest_height = vp->surface.height;
2774 if (src_width != dest_width || src_height != dest_height)
2775 weston_matrix_scale(matrix,
2776 src_width / dest_width,
2777 src_height / dest_height, 1);
2779 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2780 weston_matrix_translate(matrix,
2781 wl_fixed_to_double(vp->buffer.src_x),
2782 wl_fixed_to_double(vp->buffer.src_y),
2785 switch (vp->buffer.transform) {
2786 case WL_OUTPUT_TRANSFORM_FLIPPED:
2787 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2788 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2789 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2790 weston_matrix_scale(matrix, -1, 1, 1);
2791 weston_matrix_translate(matrix,
2792 surface->width_from_buffer, 0, 0);
2796 switch (vp->buffer.transform) {
2798 case WL_OUTPUT_TRANSFORM_NORMAL:
2799 case WL_OUTPUT_TRANSFORM_FLIPPED:
2801 case WL_OUTPUT_TRANSFORM_90:
2802 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2803 weston_matrix_rotate_xy(matrix, 0, 1);
2804 weston_matrix_translate(matrix,
2805 surface->height_from_buffer, 0, 0);
2807 case WL_OUTPUT_TRANSFORM_180:
2808 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2809 weston_matrix_rotate_xy(matrix, -1, 0);
2810 weston_matrix_translate(matrix,
2811 surface->width_from_buffer,
2812 surface->height_from_buffer, 0);
2814 case WL_OUTPUT_TRANSFORM_270:
2815 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2816 weston_matrix_rotate_xy(matrix, 0, -1);
2817 weston_matrix_translate(matrix,
2818 0, surface->width_from_buffer, 0);
2822 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2826 * Compute a + b > c while being safe to overflows.
2829 fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2831 return (int64_t)a + (int64_t)b > (int64_t)c;
2835 weston_surface_is_pending_viewport_source_valid(
2836 const struct weston_surface *surface)
2838 const struct weston_surface_state *pend = &surface->pending;
2839 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2840 int width_from_buffer = 0;
2841 int height_from_buffer = 0;
2845 /* If viewport source rect is not set, it is always ok. */
2846 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2849 if (pend->newly_attached) {
2851 convert_size_by_transform_scale(&width_from_buffer,
2852 &height_from_buffer,
2853 pend->buffer->width,
2854 pend->buffer->height,
2855 vp->buffer.transform,
2858 /* No buffer: viewport is irrelevant. */
2862 width_from_buffer = surface->width_from_buffer;
2863 height_from_buffer = surface->height_from_buffer;
2866 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2867 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2869 /* No buffer: viewport is irrelevant. */
2870 if (width_from_buffer == 0 || height_from_buffer == 0)
2873 /* overflow checks for wl_fixed_from_int() */
2874 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2876 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2879 w = wl_fixed_from_int(width_from_buffer);
2880 h = wl_fixed_from_int(height_from_buffer);
2882 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2884 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2891 fixed_is_integer(wl_fixed_t v)
2893 return (v & 0xff) == 0;
2897 weston_surface_is_pending_viewport_dst_size_int(
2898 const struct weston_surface *surface)
2900 const struct weston_buffer_viewport *vp =
2901 &surface->pending.buffer_viewport;
2903 if (vp->surface.width != -1) {
2904 assert(vp->surface.width > 0 && vp->surface.height > 0);
2908 return fixed_is_integer(vp->buffer.src_width) &&
2909 fixed_is_integer(vp->buffer.src_height);
2912 /* Translate pending damage in buffer co-ordinates to surface
2913 * co-ordinates and union it with a pixman_region32_t.
2914 * This should only be called after the buffer is attached.
2917 apply_damage_buffer(pixman_region32_t *dest,
2918 struct weston_surface *surface,
2919 struct weston_surface_state *state)
2921 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2923 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2924 * translated into surface co-ordinates and unioned with
2925 * any other surface damage.
2926 * None of this makes sense if there is no buffer though.
2928 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2929 pixman_region32_t buffer_damage;
2931 pixman_region32_intersect_rect(&state->damage_buffer,
2932 &state->damage_buffer,
2933 0, 0, buffer->width,
2935 pixman_region32_init(&buffer_damage);
2936 weston_matrix_transform_region(&buffer_damage,
2937 &surface->buffer_to_surface_matrix,
2938 &state->damage_buffer);
2939 pixman_region32_union(dest, dest, &buffer_damage);
2940 pixman_region32_fini(&buffer_damage);
2942 /* We should clear this on commit even if there was no buffer */
2943 pixman_region32_clear(&state->damage_buffer);
2947 weston_surface_commit_state(struct weston_surface *surface,
2948 struct weston_surface_state *state)
2950 struct weston_view *view;
2951 pixman_region32_t opaque;
2953 /* wl_surface.set_buffer_transform */
2954 /* wl_surface.set_buffer_scale */
2955 /* wp_viewport.set_source */
2956 /* wp_viewport.set_destination */
2957 surface->buffer_viewport = state->buffer_viewport;
2959 /* wl_surface.attach */
2960 if (state->newly_attached)
2961 weston_surface_attach(surface, state->buffer);
2962 weston_surface_state_set_buffer(state, NULL);
2964 weston_surface_build_buffer_matrix(surface,
2965 &surface->surface_to_buffer_matrix);
2966 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2967 &surface->surface_to_buffer_matrix);
2969 if (state->newly_attached || state->buffer_viewport.changed) {
2970 weston_surface_update_size(surface);
2971 if (surface->committed)
2972 surface->committed(surface, state->sx, state->sy);
2977 state->newly_attached = 0;
2978 state->buffer_viewport.changed = 0;
2980 /* wl_surface.damage and wl_surface.damage_buffer */
2981 if (weston_timeline_enabled_ &&
2982 (pixman_region32_not_empty(&state->damage_surface) ||
2983 pixman_region32_not_empty(&state->damage_buffer)))
2984 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
2986 pixman_region32_union(&surface->damage, &surface->damage,
2987 &state->damage_surface);
2989 apply_damage_buffer(&surface->damage, surface, state);
2991 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2992 0, 0, surface->width, surface->height);
2993 pixman_region32_clear(&state->damage_surface);
2995 /* wl_surface.set_opaque_region */
2996 pixman_region32_init(&opaque);
2997 pixman_region32_intersect_rect(&opaque, &state->opaque,
2998 0, 0, surface->width, surface->height);
3000 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3001 pixman_region32_copy(&surface->opaque, &opaque);
3002 wl_list_for_each(view, &surface->views, surface_link)
3003 weston_view_geometry_dirty(view);
3006 pixman_region32_fini(&opaque);
3008 /* wl_surface.set_input_region */
3009 pixman_region32_intersect_rect(&surface->input, &state->input,
3010 0, 0, surface->width, surface->height);
3012 /* wl_surface.frame */
3013 wl_list_insert_list(&surface->frame_callback_list,
3014 &state->frame_callback_list);
3015 wl_list_init(&state->frame_callback_list);
3018 * What should happen with a feedback request, if there
3019 * is no wl_buffer attached for this commit?
3022 /* presentation.feedback */
3023 wl_list_insert_list(&surface->feedback_list,
3024 &state->feedback_list);
3025 wl_list_init(&state->feedback_list);
3027 wl_signal_emit(&surface->commit_signal, surface);
3031 weston_surface_commit(struct weston_surface *surface)
3033 weston_surface_commit_state(surface, &surface->pending);
3035 weston_surface_commit_subsurface_order(surface);
3037 weston_surface_schedule_repaint(surface);
3041 weston_subsurface_commit(struct weston_subsurface *sub);
3044 weston_subsurface_parent_commit(struct weston_subsurface *sub,
3045 int parent_is_synchronized);
3048 surface_commit(struct wl_client *client, struct wl_resource *resource)
3050 struct weston_surface *surface = wl_resource_get_user_data(resource);
3051 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3053 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3054 assert(surface->viewport_resource);
3056 wl_resource_post_error(surface->viewport_resource,
3057 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3058 "wl_surface@%d has viewport source outside buffer",
3059 wl_resource_get_id(resource));
3063 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3064 assert(surface->viewport_resource);
3066 wl_resource_post_error(surface->viewport_resource,
3067 WP_VIEWPORT_ERROR_BAD_SIZE,
3068 "wl_surface@%d viewport dst size not integer",
3069 wl_resource_get_id(resource));
3074 weston_subsurface_commit(sub);
3078 weston_surface_commit(surface);
3080 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3081 if (sub->surface != surface)
3082 weston_subsurface_parent_commit(sub, 0);
3087 surface_set_buffer_transform(struct wl_client *client,
3088 struct wl_resource *resource, int transform)
3090 struct weston_surface *surface = wl_resource_get_user_data(resource);
3092 /* if wl_output.transform grows more members this will need to be updated. */
3093 if (transform < 0 ||
3094 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3095 wl_resource_post_error(resource,
3096 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3097 "buffer transform must be a valid transform "
3098 "('%d' specified)", transform);
3102 surface->pending.buffer_viewport.buffer.transform = transform;
3103 surface->pending.buffer_viewport.changed = 1;
3107 surface_set_buffer_scale(struct wl_client *client,
3108 struct wl_resource *resource,
3111 struct weston_surface *surface = wl_resource_get_user_data(resource);
3114 wl_resource_post_error(resource,
3115 WL_SURFACE_ERROR_INVALID_SCALE,
3116 "buffer scale must be at least one "
3117 "('%d' specified)", scale);
3121 surface->pending.buffer_viewport.buffer.scale = scale;
3122 surface->pending.buffer_viewport.changed = 1;
3125 static const struct wl_surface_interface surface_interface = {
3130 surface_set_opaque_region,
3131 surface_set_input_region,
3133 surface_set_buffer_transform,
3134 surface_set_buffer_scale,
3135 surface_damage_buffer
3139 compositor_create_surface(struct wl_client *client,
3140 struct wl_resource *resource, uint32_t id)
3142 struct weston_compositor *ec = wl_resource_get_user_data(resource);
3143 struct weston_surface *surface;
3145 surface = weston_surface_create(ec);
3146 if (surface == NULL) {
3147 wl_resource_post_no_memory(resource);
3152 wl_resource_create(client, &wl_surface_interface,
3153 wl_resource_get_version(resource), id);
3154 if (surface->resource == NULL) {
3155 weston_surface_destroy(surface);
3156 wl_resource_post_no_memory(resource);
3159 wl_resource_set_implementation(surface->resource, &surface_interface,
3160 surface, destroy_surface);
3162 wl_signal_emit(&ec->create_surface_signal, surface);
3166 destroy_region(struct wl_resource *resource)
3168 struct weston_region *region = wl_resource_get_user_data(resource);
3170 pixman_region32_fini(®ion->region);
3175 region_destroy(struct wl_client *client, struct wl_resource *resource)
3177 wl_resource_destroy(resource);
3181 region_add(struct wl_client *client, struct wl_resource *resource,
3182 int32_t x, int32_t y, int32_t width, int32_t height)
3184 struct weston_region *region = wl_resource_get_user_data(resource);
3186 pixman_region32_union_rect(®ion->region, ®ion->region,
3187 x, y, width, height);
3191 region_subtract(struct wl_client *client, struct wl_resource *resource,
3192 int32_t x, int32_t y, int32_t width, int32_t height)
3194 struct weston_region *region = wl_resource_get_user_data(resource);
3195 pixman_region32_t rect;
3197 pixman_region32_init_rect(&rect, x, y, width, height);
3198 pixman_region32_subtract(®ion->region, ®ion->region, &rect);
3199 pixman_region32_fini(&rect);
3202 static const struct wl_region_interface region_interface = {
3209 compositor_create_region(struct wl_client *client,
3210 struct wl_resource *resource, uint32_t id)
3212 struct weston_region *region;
3214 region = malloc(sizeof *region);
3215 if (region == NULL) {
3216 wl_resource_post_no_memory(resource);
3220 pixman_region32_init(®ion->region);
3223 wl_resource_create(client, &wl_region_interface, 1, id);
3224 if (region->resource == NULL) {
3226 wl_resource_post_no_memory(resource);
3229 wl_resource_set_implementation(region->resource, ®ion_interface,
3230 region, destroy_region);
3233 static const struct wl_compositor_interface compositor_interface = {
3234 compositor_create_surface,
3235 compositor_create_region
3239 weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3241 struct weston_surface *surface = sub->surface;
3243 weston_surface_commit_state(surface, &sub->cached);
3244 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
3246 weston_surface_commit_subsurface_order(surface);
3248 weston_surface_schedule_repaint(surface);
3250 sub->has_cached_data = 0;
3254 weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3256 struct weston_surface *surface = sub->surface;
3259 * If this commit would cause the surface to move by the
3260 * attach(dx, dy) parameters, the old damage region must be
3261 * translated to correspond to the new surface coordinate system
3264 pixman_region32_translate(&sub->cached.damage_surface,
3265 -surface->pending.sx, -surface->pending.sy);
3266 pixman_region32_union(&sub->cached.damage_surface,
3267 &sub->cached.damage_surface,
3268 &surface->pending.damage_surface);
3269 pixman_region32_clear(&surface->pending.damage_surface);
3271 if (surface->pending.newly_attached) {
3272 sub->cached.newly_attached = 1;
3273 weston_surface_state_set_buffer(&sub->cached,
3274 surface->pending.buffer);
3275 weston_buffer_reference(&sub->cached_buffer_ref,
3276 surface->pending.buffer);
3277 weston_presentation_feedback_discard_list(
3278 &sub->cached.feedback_list);
3280 sub->cached.sx += surface->pending.sx;
3281 sub->cached.sy += surface->pending.sy;
3283 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3285 sub->cached.buffer_viewport.changed |=
3286 surface->pending.buffer_viewport.changed;
3287 sub->cached.buffer_viewport.buffer =
3288 surface->pending.buffer_viewport.buffer;
3289 sub->cached.buffer_viewport.surface =
3290 surface->pending.buffer_viewport.surface;
3292 weston_surface_reset_pending_buffer(surface);
3294 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3296 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3298 wl_list_insert_list(&sub->cached.frame_callback_list,
3299 &surface->pending.frame_callback_list);
3300 wl_list_init(&surface->pending.frame_callback_list);
3302 wl_list_insert_list(&sub->cached.feedback_list,
3303 &surface->pending.feedback_list);
3304 wl_list_init(&surface->pending.feedback_list);
3306 sub->has_cached_data = 1;
3310 weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3313 if (sub->synchronized)
3319 sub = weston_surface_to_subsurface(sub->parent);
3326 weston_subsurface_commit(struct weston_subsurface *sub)
3328 struct weston_surface *surface = sub->surface;
3329 struct weston_subsurface *tmp;
3331 /* Recursive check for effectively synchronized. */
3332 if (weston_subsurface_is_synchronized(sub)) {
3333 weston_subsurface_commit_to_cache(sub);
3335 if (sub->has_cached_data) {
3336 /* flush accumulated state from cache */
3337 weston_subsurface_commit_to_cache(sub);
3338 weston_subsurface_commit_from_cache(sub);
3340 weston_surface_commit(surface);
3343 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3344 if (tmp->surface != surface)
3345 weston_subsurface_parent_commit(tmp, 0);
3351 weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
3353 struct weston_surface *surface = sub->surface;
3354 struct weston_subsurface *tmp;
3356 /* From now on, commit_from_cache the whole sub-tree, regardless of
3357 * the synchronized mode of each child. This sub-surface or some
3358 * of its ancestors were synchronized, so we are synchronized
3362 if (sub->has_cached_data)
3363 weston_subsurface_commit_from_cache(sub);
3365 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3366 if (tmp->surface != surface)
3367 weston_subsurface_parent_commit(tmp, 1);
3372 weston_subsurface_parent_commit(struct weston_subsurface *sub,
3373 int parent_is_synchronized)
3375 struct weston_view *view;
3376 if (sub->position.set) {
3377 wl_list_for_each(view, &sub->surface->views, surface_link)
3378 weston_view_set_position(view,
3382 sub->position.set = 0;
3385 if (parent_is_synchronized || sub->synchronized)
3386 weston_subsurface_synchronized_commit(sub);
3390 subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3392 return snprintf(buf, len, "sub-surface");
3396 subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
3398 struct weston_view *view;
3400 wl_list_for_each(view, &surface->views, surface_link)
3401 weston_view_set_position(view,
3402 view->geometry.x + dx,
3403 view->geometry.y + dy);
3405 /* No need to check parent mappedness, because if parent is not
3406 * mapped, parent is not in a visible layer, so this sub-surface
3407 * will not be drawn either.
3410 if (!weston_surface_is_mapped(surface)) {
3411 surface->is_mapped = true;
3413 /* Cannot call weston_view_update_transform(),
3414 * because that would call it also for the parent surface,
3415 * which might not be mapped yet. That would lead to
3416 * inconsistent state, where the window could never be
3419 * Instead just force the is_mapped flag on, to make
3420 * weston_surface_is_mapped() return true, so that when the
3421 * parent surface does get mapped, this one will get
3422 * included, too. See view_list_add().
3427 static struct weston_subsurface *
3428 weston_surface_to_subsurface(struct weston_surface *surface)
3430 if (surface->committed == subsurface_committed)
3431 return surface->committed_private;
3436 WL_EXPORT struct weston_surface *
3437 weston_surface_get_main_surface(struct weston_surface *surface)
3439 struct weston_subsurface *sub;
3441 while (surface && (sub = weston_surface_to_subsurface(surface)))
3442 surface = sub->parent;
3448 weston_surface_set_role(struct weston_surface *surface,
3449 const char *role_name,
3450 struct wl_resource *error_resource,
3451 uint32_t error_code)
3455 if (surface->role_name == NULL ||
3456 surface->role_name == role_name ||
3457 strcmp(surface->role_name, role_name) == 0) {
3458 surface->role_name = role_name;
3463 wl_resource_post_error(error_resource, error_code,
3464 "Cannot assign role %s to wl_surface@%d,"
3465 " already has role %s\n",
3467 wl_resource_get_id(surface->resource),
3468 surface->role_name);
3472 WL_EXPORT const char *
3473 weston_surface_get_role(struct weston_surface *surface)
3475 return surface->role_name;
3479 weston_surface_set_label_func(struct weston_surface *surface,
3480 int (*desc)(struct weston_surface *,
3483 surface->get_label = desc;
3484 surface->timeline.force_refresh = 1;
3487 /** Get the size of surface contents
3489 * \param surface The surface to query.
3490 * \param width Returns the width of raw contents.
3491 * \param height Returns the height of raw contents.
3493 * Retrieves the raw surface content size in pixels for the given surface.
3494 * This is the whole content size in buffer pixels. If the surface
3495 * has no content or the renderer does not implement this feature,
3496 * zeroes are returned.
3498 * This function is used to determine the buffer size needed for
3499 * a weston_surface_copy_content() call.
3502 weston_surface_get_content_size(struct weston_surface *surface,
3503 int *width, int *height)
3505 struct weston_renderer *rer = surface->compositor->renderer;
3507 if (!rer->surface_get_content_size) {
3513 rer->surface_get_content_size(surface, width, height);
3516 /** Get the bounding box of a surface and its subsurfaces
3518 * \param surface The surface to query.
3519 * \return The bounding box relative to the surface origin.
3522 WL_EXPORT struct weston_geometry
3523 weston_surface_get_bounding_box(struct weston_surface *surface)
3525 pixman_region32_t region;
3526 pixman_box32_t *box;
3527 struct weston_subsurface *subsurface;
3529 pixman_region32_init_rect(®ion,
3531 surface->width, surface->height);
3533 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3534 pixman_region32_union_rect(®ion, ®ion,
3535 subsurface->position.x,
3536 subsurface->position.y,
3537 subsurface->surface->width,
3538 subsurface->surface->height);
3540 box = pixman_region32_extents(®ion);
3541 struct weston_geometry geometry = {
3544 .width = box->x2 - box->x1,
3545 .height = box->y2 - box->y1,
3548 pixman_region32_fini(®ion);
3553 /** Copy surface contents to system memory.
3555 * \param surface The surface to copy from.
3556 * \param target Pointer to the target memory buffer.
3557 * \param size Size of the target buffer in bytes.
3558 * \param src_x X location on contents to copy from.
3559 * \param src_y Y location on contents to copy from.
3560 * \param width Width in pixels of the area to copy.
3561 * \param height Height in pixels of the area to copy.
3562 * \return 0 for success, -1 for failure.
3564 * Surface contents are maintained by the renderer. They can be in a
3565 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3568 * Surface contents are copied into memory pointed to by target,
3569 * which has size bytes of space available. The target memory
3570 * may be larger than needed, but being smaller returns an error.
3571 * The extra bytes in target may or may not be written; their content is
3572 * unspecified. Size must be large enough to hold the image.
3574 * The image in the target memory will be arranged in rows from
3575 * top to bottom, and pixels on a row from left to right. The pixel
3576 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3579 * Parameters src_x and src_y define the upper-left corner in buffer
3580 * coordinates (pixels) to copy from. Parameters width and height
3581 * define the size of the area to copy in pixels.
3583 * The rectangle defined by src_x, src_y, width, height must fit in
3584 * the surface contents. Otherwise an error is returned.
3586 * Use surface_get_data_size to determine the content size; the
3587 * needed target buffer size and rectangle limits.
3589 * CURRENT IMPLEMENTATION RESTRICTIONS:
3590 * - the machine must be little-endian due to Pixman formats.
3592 * NOTE: Pixman formats are premultiplied.
3595 weston_surface_copy_content(struct weston_surface *surface,
3596 void *target, size_t size,
3597 int src_x, int src_y,
3598 int width, int height)
3600 struct weston_renderer *rer = surface->compositor->renderer;
3602 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3604 if (!rer->surface_copy_content)
3607 weston_surface_get_content_size(surface, &cw, &ch);
3609 if (src_x < 0 || src_y < 0)
3612 if (width <= 0 || height <= 0)
3615 if (src_x + width > cw || src_y + height > ch)
3618 if (width * bytespp * height > size)
3621 return rer->surface_copy_content(surface, target, size,
3622 src_x, src_y, width, height);
3626 subsurface_set_position(struct wl_client *client,
3627 struct wl_resource *resource, int32_t x, int32_t y)
3629 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3634 sub->position.x = x;
3635 sub->position.y = y;
3636 sub->position.set = 1;
3639 static struct weston_subsurface *
3640 subsurface_find_sibling(struct weston_subsurface *sub,
3641 struct weston_surface *surface)
3643 struct weston_surface *parent = sub->parent;
3644 struct weston_subsurface *sibling;
3646 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3647 if (sibling->surface == surface && sibling != sub)
3654 static struct weston_subsurface *
3655 subsurface_sibling_check(struct weston_subsurface *sub,
3656 struct weston_surface *surface,
3657 const char *request)
3659 struct weston_subsurface *sibling;
3661 sibling = subsurface_find_sibling(sub, surface);
3663 wl_resource_post_error(sub->resource,
3664 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3665 "%s: wl_surface@%d is not a parent or sibling",
3666 request, wl_resource_get_id(surface->resource));
3670 assert(sibling->parent == sub->parent);
3676 subsurface_place_above(struct wl_client *client,
3677 struct wl_resource *resource,
3678 struct wl_resource *sibling_resource)
3680 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3681 struct weston_surface *surface =
3682 wl_resource_get_user_data(sibling_resource);
3683 struct weston_subsurface *sibling;
3688 sibling = subsurface_sibling_check(sub, surface, "place_above");
3692 wl_list_remove(&sub->parent_link_pending);
3693 wl_list_insert(sibling->parent_link_pending.prev,
3694 &sub->parent_link_pending);
3696 sub->reordered = true;
3700 subsurface_place_below(struct wl_client *client,
3701 struct wl_resource *resource,
3702 struct wl_resource *sibling_resource)
3704 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3705 struct weston_surface *surface =
3706 wl_resource_get_user_data(sibling_resource);
3707 struct weston_subsurface *sibling;
3712 sibling = subsurface_sibling_check(sub, surface, "place_below");
3716 wl_list_remove(&sub->parent_link_pending);
3717 wl_list_insert(&sibling->parent_link_pending,
3718 &sub->parent_link_pending);
3720 sub->reordered = true;
3724 subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3726 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3729 sub->synchronized = 1;
3733 subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3735 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3737 if (sub && sub->synchronized) {
3738 sub->synchronized = 0;
3740 /* If sub became effectively desynchronized, flush. */
3741 if (!weston_subsurface_is_synchronized(sub))
3742 weston_subsurface_synchronized_commit(sub);
3747 weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3749 wl_list_remove(&sub->parent_link);
3750 wl_list_remove(&sub->parent_link_pending);
3751 wl_list_remove(&sub->parent_destroy_listener.link);
3756 weston_subsurface_destroy(struct weston_subsurface *sub);
3759 subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3761 struct weston_subsurface *sub =
3762 container_of(listener, struct weston_subsurface,
3763 surface_destroy_listener);
3764 assert(data == sub->surface);
3766 /* The protocol object (wl_resource) is left inert. */
3768 wl_resource_set_user_data(sub->resource, NULL);
3770 weston_subsurface_destroy(sub);
3774 subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3776 struct weston_subsurface *sub =
3777 container_of(listener, struct weston_subsurface,
3778 parent_destroy_listener);
3779 assert(data == sub->parent);
3780 assert(sub->surface != sub->parent);
3782 if (weston_surface_is_mapped(sub->surface))
3783 weston_surface_unmap(sub->surface);
3785 weston_subsurface_unlink_parent(sub);
3789 subsurface_resource_destroy(struct wl_resource *resource)
3791 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3794 weston_subsurface_destroy(sub);
3798 subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3800 wl_resource_destroy(resource);
3804 weston_subsurface_link_parent(struct weston_subsurface *sub,
3805 struct weston_surface *parent)
3807 sub->parent = parent;
3808 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
3809 wl_signal_add(&parent->destroy_signal,
3810 &sub->parent_destroy_listener);
3812 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3813 wl_list_insert(&parent->subsurface_list_pending,
3814 &sub->parent_link_pending);
3818 weston_subsurface_link_surface(struct weston_subsurface *sub,
3819 struct weston_surface *surface)
3821 sub->surface = surface;
3822 sub->surface_destroy_listener.notify =
3823 subsurface_handle_surface_destroy;
3824 wl_signal_add(&surface->destroy_signal,
3825 &sub->surface_destroy_listener);
3829 weston_subsurface_destroy(struct weston_subsurface *sub)
3831 struct weston_view *view, *next;
3833 assert(sub->surface);
3835 if (sub->resource) {
3836 assert(weston_surface_to_subsurface(sub->surface) == sub);
3837 assert(sub->parent_destroy_listener.notify ==
3838 subsurface_handle_parent_destroy);
3840 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3841 weston_view_unmap(view);
3842 weston_view_destroy(view);
3846 weston_subsurface_unlink_parent(sub);
3848 weston_surface_state_fini(&sub->cached);
3849 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
3851 sub->surface->committed = NULL;
3852 sub->surface->committed_private = NULL;
3853 weston_surface_set_label_func(sub->surface, NULL);
3855 /* the dummy weston_subsurface for the parent itself */
3856 assert(sub->parent_destroy_listener.notify == NULL);
3857 wl_list_remove(&sub->parent_link);
3858 wl_list_remove(&sub->parent_link_pending);
3861 wl_list_remove(&sub->surface_destroy_listener.link);
3865 static const struct wl_subsurface_interface subsurface_implementation = {
3867 subsurface_set_position,
3868 subsurface_place_above,
3869 subsurface_place_below,
3870 subsurface_set_sync,
3871 subsurface_set_desync
3874 static struct weston_subsurface *
3875 weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3876 struct weston_surface *parent)
3878 struct weston_subsurface *sub;
3879 struct wl_client *client = wl_resource_get_client(surface->resource);
3881 sub = zalloc(sizeof *sub);
3885 wl_list_init(&sub->unused_views);
3888 wl_resource_create(client, &wl_subsurface_interface, 1, id);
3889 if (!sub->resource) {
3894 wl_resource_set_implementation(sub->resource,
3895 &subsurface_implementation,
3896 sub, subsurface_resource_destroy);
3897 weston_subsurface_link_surface(sub, surface);
3898 weston_subsurface_link_parent(sub, parent);
3899 weston_surface_state_init(&sub->cached);
3900 sub->cached_buffer_ref.buffer = NULL;
3901 sub->synchronized = 1;
3906 /* Create a dummy subsurface for having the parent itself in its
3907 * sub-surface lists. Makes stacking order manipulation easy.
3909 static struct weston_subsurface *
3910 weston_subsurface_create_for_parent(struct weston_surface *parent)
3912 struct weston_subsurface *sub;
3914 sub = zalloc(sizeof *sub);
3918 weston_subsurface_link_surface(sub, parent);
3919 sub->parent = parent;
3920 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3921 wl_list_insert(&parent->subsurface_list_pending,
3922 &sub->parent_link_pending);
3928 subcompositor_get_subsurface(struct wl_client *client,
3929 struct wl_resource *resource,
3931 struct wl_resource *surface_resource,
3932 struct wl_resource *parent_resource)
3934 struct weston_surface *surface =
3935 wl_resource_get_user_data(surface_resource);
3936 struct weston_surface *parent =
3937 wl_resource_get_user_data(parent_resource);
3938 struct weston_subsurface *sub;
3939 static const char where[] = "get_subsurface: wl_subsurface@";
3941 if (surface == parent) {
3942 wl_resource_post_error(resource,
3943 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3944 "%s%d: wl_surface@%d cannot be its own parent",
3945 where, id, wl_resource_get_id(surface_resource));
3949 if (weston_surface_to_subsurface(surface)) {
3950 wl_resource_post_error(resource,
3951 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3952 "%s%d: wl_surface@%d is already a sub-surface",
3953 where, id, wl_resource_get_id(surface_resource));
3957 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3958 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
3961 if (weston_surface_get_main_surface(parent) == surface) {
3962 wl_resource_post_error(resource,
3963 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3964 "%s%d: wl_surface@%d is an ancestor of parent",
3965 where, id, wl_resource_get_id(surface_resource));
3969 /* make sure the parent is in its own list */
3970 if (wl_list_empty(&parent->subsurface_list)) {
3971 if (!weston_subsurface_create_for_parent(parent)) {
3972 wl_resource_post_no_memory(resource);
3977 sub = weston_subsurface_create(id, surface, parent);
3979 wl_resource_post_no_memory(resource);
3983 surface->committed = subsurface_committed;
3984 surface->committed_private = sub;
3985 weston_surface_set_label_func(surface, subsurface_get_label);
3989 subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3991 wl_resource_destroy(resource);
3994 static const struct wl_subcompositor_interface subcompositor_interface = {
3995 subcompositor_destroy,
3996 subcompositor_get_subsurface
4000 bind_subcompositor(struct wl_client *client,
4001 void *data, uint32_t version, uint32_t id)
4003 struct weston_compositor *compositor = data;
4004 struct wl_resource *resource;
4007 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
4008 if (resource == NULL) {
4009 wl_client_post_no_memory(client);
4012 wl_resource_set_implementation(resource, &subcompositor_interface,
4016 /** Set a DPMS mode on all of the compositor's outputs
4018 * \param compositor The compositor instance
4019 * \param state The DPMS state the outputs will be set to
4022 weston_compositor_dpms(struct weston_compositor *compositor,
4023 enum dpms_enum state)
4025 struct weston_output *output;
4027 wl_list_for_each(output, &compositor->output_list, link)
4028 if (output->set_dpms)
4029 output->set_dpms(output, state);
4032 /** Restores the compositor to active status
4034 * \param compositor The compositor instance
4036 * If the compositor was in a sleeping mode, all outputs are powered
4037 * back on via DPMS. Otherwise if the compositor was inactive
4038 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4041 * Restarts the idle timer.
4044 weston_compositor_wake(struct weston_compositor *compositor)
4046 uint32_t old_state = compositor->state;
4048 /* The state needs to be changed before emitting the wake
4049 * signal because that may try to schedule a repaint which
4050 * will not work if the compositor is still sleeping */
4051 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4053 switch (old_state) {
4054 case WESTON_COMPOSITOR_SLEEPING:
4055 case WESTON_COMPOSITOR_IDLE:
4056 case WESTON_COMPOSITOR_OFFSCREEN:
4057 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
4058 wl_signal_emit(&compositor->wake_signal, compositor);
4061 wl_event_source_timer_update(compositor->idle_source,
4062 compositor->idle_time * 1000);
4066 /** Turns off rendering and frame events for the compositor.
4068 * \param compositor The compositor instance
4070 * This is used for example to prevent further rendering while the
4071 * compositor is shutting down.
4073 * Stops the idle timer.
4076 weston_compositor_offscreen(struct weston_compositor *compositor)
4078 switch (compositor->state) {
4079 case WESTON_COMPOSITOR_OFFSCREEN:
4081 case WESTON_COMPOSITOR_SLEEPING:
4083 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4084 wl_event_source_timer_update(compositor->idle_source, 0);
4088 /** Powers down all attached output devices
4090 * \param compositor The compositor instance
4092 * Causes rendering to the outputs to cease, and no frame events to be
4093 * sent. Only powers down the outputs if the compositor is not already
4096 * Stops the idle timer.
4099 weston_compositor_sleep(struct weston_compositor *compositor)
4101 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4104 wl_event_source_timer_update(compositor->idle_source, 0);
4105 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4106 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4109 /** Sets compositor to idle mode
4111 * \param data The compositor instance
4113 * This is called when the idle timer fires. Once the compositor is in
4114 * idle mode it requires a wake action (e.g. via
4115 * weston_compositor_wake()) to restore it. The compositor's
4116 * idle_signal will be triggered when the idle event occurs.
4118 * Idleness can be inhibited by setting the compositor's idle_inhibit
4122 idle_handler(void *data)
4124 struct weston_compositor *compositor = data;
4126 if (compositor->idle_inhibit)
4129 compositor->state = WESTON_COMPOSITOR_IDLE;
4130 wl_signal_emit(&compositor->idle_signal, compositor);
4136 weston_plane_init(struct weston_plane *plane,
4137 struct weston_compositor *ec,
4138 int32_t x, int32_t y)
4140 pixman_region32_init(&plane->damage);
4141 pixman_region32_init(&plane->clip);
4144 plane->compositor = ec;
4146 /* Init the link so that the call to wl_list_remove() when releasing
4147 * the plane without ever stacking doesn't lead to a crash */
4148 wl_list_init(&plane->link);
4152 weston_plane_release(struct weston_plane *plane)
4154 struct weston_view *view;
4156 pixman_region32_fini(&plane->damage);
4157 pixman_region32_fini(&plane->clip);
4159 wl_list_for_each(view, &plane->compositor->view_list, link) {
4160 if (view->plane == plane)
4164 wl_list_remove(&plane->link);
4168 weston_compositor_stack_plane(struct weston_compositor *ec,
4169 struct weston_plane *plane,
4170 struct weston_plane *above)
4173 wl_list_insert(above->link.prev, &plane->link);
4175 wl_list_insert(&ec->plane_list, &plane->link);
4179 output_release(struct wl_client *client, struct wl_resource *resource)
4181 wl_resource_destroy(resource);
4184 static const struct wl_output_interface output_interface = {
4189 static void unbind_resource(struct wl_resource *resource)
4191 wl_list_remove(wl_resource_get_link(resource));
4195 bind_output(struct wl_client *client,
4196 void *data, uint32_t version, uint32_t id)
4198 struct weston_output *output = data;
4199 struct weston_mode *mode;
4200 struct wl_resource *resource;
4202 resource = wl_resource_create(client, &wl_output_interface,
4204 if (resource == NULL) {
4205 wl_client_post_no_memory(client);
4209 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
4210 wl_resource_set_implementation(resource, &output_interface, data, unbind_resource);
4212 wl_output_send_geometry(resource,
4218 output->make, output->model,
4220 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
4221 wl_output_send_scale(resource,
4222 output->current_scale);
4224 wl_list_for_each (mode, &output->mode_list, link) {
4225 wl_output_send_mode(resource,
4232 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
4233 wl_output_send_done(resource);
4236 /* Move other outputs when one is resized so the space remains contiguous. */
4238 weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4239 struct weston_output *resized_output, int delta_width)
4241 struct weston_output *output;
4242 bool start_resizing = false;
4247 wl_list_for_each(output, &compositor->output_list, link) {
4248 if (output == resized_output) {
4249 start_resizing = true;
4253 if (start_resizing) {
4254 weston_output_move(output, output->x + delta_width, output->y);
4261 weston_output_update_matrix(struct weston_output *output)
4263 float magnification;
4265 weston_matrix_init(&output->matrix);
4266 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
4268 if (output->zoom.active) {
4269 magnification = 1 / (1 - output->zoom.spring_z.current);
4270 weston_output_update_zoom(output);
4271 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
4272 -output->zoom.trans_y, 0);
4273 weston_matrix_scale(&output->matrix, magnification,
4274 magnification, 1.0);
4277 switch (output->transform) {
4278 case WL_OUTPUT_TRANSFORM_FLIPPED:
4279 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4280 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4281 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4282 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4283 weston_matrix_scale(&output->matrix, -1, 1, 1);
4287 switch (output->transform) {
4289 case WL_OUTPUT_TRANSFORM_NORMAL:
4290 case WL_OUTPUT_TRANSFORM_FLIPPED:
4292 case WL_OUTPUT_TRANSFORM_90:
4293 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4294 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4295 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4297 case WL_OUTPUT_TRANSFORM_180:
4298 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4299 weston_matrix_translate(&output->matrix,
4300 -output->width, -output->height, 0);
4301 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4303 case WL_OUTPUT_TRANSFORM_270:
4304 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4305 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4306 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4310 if (output->current_scale != 1)
4311 weston_matrix_scale(&output->matrix,
4312 output->current_scale,
4313 output->current_scale, 1);
4317 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
4321 weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
4323 output->transform = transform;
4324 output->native_scale = scale;
4325 output->current_scale = scale;
4327 convert_size_by_transform_scale(&output->width, &output->height,
4328 output->current_mode->width,
4329 output->current_mode->height,
4334 weston_output_init_geometry(struct weston_output *output, int x, int y)
4339 pixman_region32_init(&output->previous_damage);
4340 pixman_region32_init_rect(&output->region, x, y,
4346 weston_output_move(struct weston_output *output, int x, int y)
4348 struct wl_resource *resource;
4350 output->move_x = x - output->x;
4351 output->move_y = y - output->y;
4353 if (output->move_x == 0 && output->move_y == 0)
4356 weston_output_init_geometry(output, x, y);
4360 /* Move views on this output. */
4361 wl_signal_emit(&output->compositor->output_moved_signal, output);
4363 /* Notify clients of the change for output position. */
4364 wl_resource_for_each(resource, &output->resource_list) {
4365 wl_output_send_geometry(resource,
4375 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
4376 wl_output_send_done(resource);
4380 /** Adds an output to the compositor's output list and
4381 * send the compositor's output_created signal.
4383 * \param compositor The compositor instance.
4384 * \param output The output to be added.
4387 weston_compositor_add_output(struct weston_compositor *compositor,
4388 struct weston_output *output)
4390 struct weston_view *view, *next;
4392 wl_list_insert(compositor->output_list.prev, &output->link);
4393 wl_signal_emit(&compositor->output_created_signal, output);
4395 wl_list_for_each_safe(view, next, &compositor->view_list, link)
4396 weston_view_geometry_dirty(view);
4400 weston_output_transform_coordinate(struct weston_output *output,
4401 double device_x, double device_y,
4402 double *x, double *y)
4404 struct weston_vector p = { {
4410 weston_matrix_transform(&output->inverse_matrix, &p);
4412 *x = p.f[0] / p.f[3];
4413 *y = p.f[1] / p.f[3];
4416 /** Undoes changes to an output done by weston_output_enable()
4418 * \param output The weston_output object that needs the changes undone.
4420 * Removes the repaint timer.
4421 * Destroys the Wayland global assigned to the output.
4422 * Destroys pixman regions allocated to the output.
4423 * Deallocates output's ID and updates compositor's output_id_pool.
4426 weston_output_enable_undo(struct weston_output *output)
4428 wl_event_source_remove(output->repaint_timer);
4430 wl_global_destroy(output->global);
4432 pixman_region32_fini(&output->region);
4433 pixman_region32_fini(&output->previous_damage);
4434 output->compositor->output_id_pool &= ~(1u << output->id);
4436 output->enabled = false;
4439 /** Removes output from compositor's output list
4441 * \param output The weston_output object that is being removed.
4443 * Presentation feedback is discarded.
4444 * Compositor is notified that outputs were changed and
4445 * applies the necessary changes.
4446 * All views assigned to the weston_output object are
4447 * moved to a new output.
4448 * Signal is emitted to notify all users of the weston_output
4449 * object that the output is being destroyed.
4450 * wl_output protocol objects referencing this weston_output
4454 weston_compositor_remove_output(struct weston_output *output)
4456 struct wl_resource *resource;
4457 struct weston_view *view;
4459 assert(output->destroying);
4461 wl_list_for_each(view, &output->compositor->view_list, link) {
4462 if (view->output_mask & (1u << output->id))
4463 weston_view_assign_output(view);
4466 weston_presentation_feedback_discard_list(&output->feedback_list);
4468 weston_compositor_reflow_outputs(output->compositor, output, output->width);
4469 wl_list_remove(&output->link);
4471 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
4472 wl_signal_emit(&output->destroy_signal, output);
4474 wl_resource_for_each(resource, &output->resource_list) {
4475 wl_resource_set_destructor(resource, NULL);
4479 /** Sets the output scale for a given output.
4481 * \param output The weston_output object that the scale is set for.
4482 * \param scale Scale factor for the given output.
4484 * It only supports setting scale for an output that
4485 * is not enabled and it can only be ran once.
4488 weston_output_set_scale(struct weston_output *output,
4491 /* We can only set scale on a disabled output */
4492 assert(!output->enabled);
4494 /* We only want to set scale once */
4495 assert(!output->scale);
4497 output->scale = scale;
4500 /** Sets the output transform for a given output.
4502 * \param output The weston_output object that the transform is set for.
4503 * \param transform Transform value for the given output.
4505 * It only supports setting transform for an output that is
4506 * not enabled and it can only be ran once.
4508 * Refer to wl_output::transform section located at
4509 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
4510 * for list of values that can be passed to this function.
4513 weston_output_set_transform(struct weston_output *output,
4516 /* We can only set transform on a disabled output */
4517 assert(!output->enabled);
4519 /* We only want to set transform once */
4520 assert(output->transform == UINT32_MAX);
4522 output->transform = transform;
4525 /** Initializes a weston_output object with enough data so
4526 ** an output can be configured.
4528 * \param output The weston_output object to initialize
4529 * \param compositor The compositor instance.
4531 * Sets initial values for fields that are expected to be
4532 * configured either by compositors or backends.
4535 weston_output_init(struct weston_output *output,
4536 struct weston_compositor *compositor)
4538 output->compositor = compositor;
4539 output->destroying = 0;
4541 /* Backends must set output->name */
4542 assert(output->name);
4544 wl_list_init(&output->link);
4546 output->enabled = false;
4548 /* Add some (in)sane defaults which can be used
4549 * for checking if an output was properly configured
4551 output->mm_width = 0;
4552 output->mm_height = 0;
4554 /* Can't use -1 on uint32_t and 0 is valid enum value */
4555 output->transform = UINT32_MAX;
4558 /** Adds weston_output object to pending output list.
4560 * \param output The weston_output object to add
4561 * \param compositor The compositor instance.
4563 * Also notifies the compositor that an output is pending for
4567 weston_compositor_add_pending_output(struct weston_output *output,
4568 struct weston_compositor *compositor)
4570 wl_list_insert(compositor->pending_output_list.prev, &output->link);
4571 wl_signal_emit(&compositor->output_pending_signal, output);
4574 /** Constructs a weston_output object that can be used by the compositor.
4576 * \param output The weston_output object that needs to be enabled.
4578 * Output coordinates are calculated and each new output is by default
4579 * assigned to the right of previous one.
4581 * Sets up the transformation, zoom, and geometry of the output using
4582 * the properties that need to be configured by the compositor.
4584 * Establishes a repaint timer for the output with the relevant display
4585 * object's event loop. See output_repaint_timer_handler().
4587 * The output is assigned an ID. Weston can support up to 32 distinct
4588 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4589 * is referred to and used to find the first available ID number, and
4590 * then this ID is marked as used in output_id_pool.
4592 * The output is also assigned a Wayland global with the wl_output
4593 * external interface.
4595 * Backend specific function is called to set up the output output.
4597 * Output is added to the compositor's output list
4599 * If the backend specific function fails, the weston_output object
4600 * is returned to a state it was before calling this function and
4601 * is added to the compositor's pending_output_list in case it needs
4602 * to be reconfigured or just so it can be destroyed at shutdown.
4604 * 0 is returned on success, -1 on failure.
4607 weston_output_enable(struct weston_output *output)
4609 struct weston_compositor *c = output->compositor;
4610 struct weston_output *iterator;
4611 struct wl_event_loop *loop;
4614 assert(output->enable);
4616 iterator = container_of(c->output_list.prev,
4617 struct weston_output, link);
4619 if (!wl_list_empty(&c->output_list))
4620 x = iterator->x + iterator->width;
4622 /* Make sure the scale is set up */
4623 assert(output->scale);
4625 /* Make sure we have a transform set */
4626 assert(output->transform != UINT32_MAX);
4628 /* Remove it from pending/disabled output list */
4629 wl_list_remove(&output->link);
4631 /* Verify we haven't reached the limit of 32 available output IDs */
4632 assert(ffs(~c->output_id_pool) > 0);
4637 output->original_scale = output->scale;
4639 weston_output_transform_scale_init(output, output->transform, output->scale);
4640 weston_output_init_zoom(output);
4642 weston_output_init_geometry(output, x, y);
4643 weston_output_damage(output);
4645 wl_signal_init(&output->frame_signal);
4646 wl_signal_init(&output->destroy_signal);
4647 wl_list_init(&output->animation_list);
4648 wl_list_init(&output->resource_list);
4649 wl_list_init(&output->feedback_list);
4650 wl_list_init(&output->link);
4652 loop = wl_display_get_event_loop(c->wl_display);
4653 output->repaint_timer = wl_event_loop_add_timer(loop,
4654 output_repaint_timer_handler, output);
4656 /* Invert the output id pool and look for the lowest numbered
4657 * switch (the least significant bit). Take that bit's position
4658 * as our ID, and mark it used in the compositor's output_id_pool.
4660 output->id = ffs(~output->compositor->output_id_pool) - 1;
4661 output->compositor->output_id_pool |= 1u << output->id;
4664 wl_global_create(c->wl_display, &wl_output_interface, 3,
4665 output, bind_output);
4667 output->enabled = true;
4669 /* Enable the output (set up the crtc or create a
4670 * window representing the output, set up the
4673 if (output->enable(output) < 0) {
4674 weston_log("Enabling output \"%s\" failed.\n", output->name);
4676 weston_output_enable_undo(output);
4677 wl_list_insert(output->compositor->pending_output_list.prev,
4682 weston_compositor_add_output(output->compositor, output);
4687 /** Converts a weston_output object to a pending output state, so it
4688 ** can be configured again or destroyed.
4690 * \param output The weston_output object that needs to be disabled.
4692 * See weston_output_init() for more information on the
4693 * state output is returned to.
4695 * Calls a backend specific function to disable an output, in case
4696 * such function exists.
4698 * If the output is being used by the compositor, it is first removed
4699 * from weston's output_list (see weston_compositor_remove_output())
4700 * and is returned to a state it was before weston_output_enable()
4701 * was ran (see weston_output_enable_undo()).
4703 * Output is added to pending_output_list so it will get destroyed
4704 * if the output does not get configured again when the compositor
4705 * shuts down. If an output is to be used immediately, it needs to
4706 * be manually removed from the list (the compositor specific functions
4707 * for handling pending outputs will take care of that).
4709 * If backend specific disable function returns negative value,
4710 * this function will return too. It can be used as an indicator
4711 * that output cannot be disabled at the present time. In that case
4712 * backend needs to make sure the output is disabled when it is
4716 weston_output_disable(struct weston_output *output)
4718 assert(output->disable);
4720 /* Should we rename this? */
4721 output->destroying = 1;
4723 if (output->disable(output) < 0)
4726 if (output->enabled) {
4727 weston_compositor_remove_output(output);
4728 weston_output_enable_undo(output);
4730 /* We need to preserve it somewhere so it can be destroyed on shutdown
4731 if nobody wants to configure it again */
4732 wl_list_insert(output->compositor->pending_output_list.prev, &output->link);
4735 output->destroying = 0;
4738 /** Emits a signal to indicate that there are outputs waiting to be configured.
4740 * \param compositor The compositor instance
4743 weston_pending_output_coldplug(struct weston_compositor *compositor)
4745 struct weston_output *output, *next;
4747 wl_list_for_each_safe(output, next, &compositor->pending_output_list, link)
4748 wl_signal_emit(&compositor->output_pending_signal, output);
4752 weston_output_destroy(struct weston_output *output)
4754 output->destroying = 1;
4756 if (output->enabled) {
4757 weston_compositor_remove_output(output);
4758 weston_output_enable_undo(output);
4765 destroy_viewport(struct wl_resource *resource)
4767 struct weston_surface *surface =
4768 wl_resource_get_user_data(resource);
4773 surface->viewport_resource = NULL;
4774 surface->pending.buffer_viewport.buffer.src_width =
4775 wl_fixed_from_int(-1);
4776 surface->pending.buffer_viewport.surface.width = -1;
4777 surface->pending.buffer_viewport.changed = 1;
4781 viewport_destroy(struct wl_client *client,
4782 struct wl_resource *resource)
4784 wl_resource_destroy(resource);
4788 viewport_set_source(struct wl_client *client,
4789 struct wl_resource *resource,
4792 wl_fixed_t src_width,
4793 wl_fixed_t src_height)
4795 struct weston_surface *surface =
4796 wl_resource_get_user_data(resource);
4799 wl_resource_post_error(resource,
4800 WP_VIEWPORT_ERROR_NO_SURFACE,
4801 "wl_surface for this viewport is no longer exists");
4805 assert(surface->viewport_resource == resource);
4806 assert(surface->resource);
4808 if (src_width == wl_fixed_from_int(-1) &&
4809 src_height == wl_fixed_from_int(-1) &&
4810 src_x == wl_fixed_from_int(-1) &&
4811 src_y == wl_fixed_from_int(-1)) {
4812 /* unset source rect */
4813 surface->pending.buffer_viewport.buffer.src_width =
4814 wl_fixed_from_int(-1);
4815 surface->pending.buffer_viewport.changed = 1;
4819 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
4820 wl_resource_post_error(resource,
4821 WP_VIEWPORT_ERROR_BAD_VALUE,
4822 "wl_surface@%d viewport source "
4823 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4824 wl_resource_get_id(surface->resource),
4825 wl_fixed_to_double(src_width),
4826 wl_fixed_to_double(src_height),
4827 wl_fixed_to_double(src_x),
4828 wl_fixed_to_double(src_y));
4832 surface->pending.buffer_viewport.buffer.src_x = src_x;
4833 surface->pending.buffer_viewport.buffer.src_y = src_y;
4834 surface->pending.buffer_viewport.buffer.src_width = src_width;
4835 surface->pending.buffer_viewport.buffer.src_height = src_height;
4836 surface->pending.buffer_viewport.changed = 1;
4840 viewport_set_destination(struct wl_client *client,
4841 struct wl_resource *resource,
4845 struct weston_surface *surface =
4846 wl_resource_get_user_data(resource);
4849 wl_resource_post_error(resource,
4850 WP_VIEWPORT_ERROR_NO_SURFACE,
4851 "wl_surface for this viewport no longer exists");
4855 assert(surface->viewport_resource == resource);
4857 if (dst_width == -1 && dst_height == -1) {
4858 /* unset destination size */
4859 surface->pending.buffer_viewport.surface.width = -1;
4860 surface->pending.buffer_viewport.changed = 1;
4864 if (dst_width <= 0 || dst_height <= 0) {
4865 wl_resource_post_error(resource,
4866 WP_VIEWPORT_ERROR_BAD_VALUE,
4867 "destination size must be positive (%dx%d)",
4868 dst_width, dst_height);
4872 surface->pending.buffer_viewport.surface.width = dst_width;
4873 surface->pending.buffer_viewport.surface.height = dst_height;
4874 surface->pending.buffer_viewport.changed = 1;
4877 static const struct wp_viewport_interface viewport_interface = {
4879 viewport_set_source,
4880 viewport_set_destination
4884 viewporter_destroy(struct wl_client *client,
4885 struct wl_resource *resource)
4887 wl_resource_destroy(resource);
4891 viewporter_get_viewport(struct wl_client *client,
4892 struct wl_resource *viewporter,
4894 struct wl_resource *surface_resource)
4896 int version = wl_resource_get_version(viewporter);
4897 struct weston_surface *surface =
4898 wl_resource_get_user_data(surface_resource);
4899 struct wl_resource *resource;
4901 if (surface->viewport_resource) {
4902 wl_resource_post_error(viewporter,
4903 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
4904 "a viewport for that surface already exists");
4908 resource = wl_resource_create(client, &wp_viewport_interface,
4910 if (resource == NULL) {
4911 wl_client_post_no_memory(client);
4915 wl_resource_set_implementation(resource, &viewport_interface,
4916 surface, destroy_viewport);
4918 surface->viewport_resource = resource;
4921 static const struct wp_viewporter_interface viewporter_interface = {
4923 viewporter_get_viewport
4927 bind_viewporter(struct wl_client *client,
4928 void *data, uint32_t version, uint32_t id)
4930 struct wl_resource *resource;
4932 resource = wl_resource_create(client, &wp_viewporter_interface,
4934 if (resource == NULL) {
4935 wl_client_post_no_memory(client);
4939 wl_resource_set_implementation(resource, &viewporter_interface,
4944 destroy_presentation_feedback(struct wl_resource *feedback_resource)
4946 struct weston_presentation_feedback *feedback;
4948 feedback = wl_resource_get_user_data(feedback_resource);
4950 wl_list_remove(&feedback->link);
4955 presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4957 wl_resource_destroy(resource);
4961 presentation_feedback(struct wl_client *client,
4962 struct wl_resource *presentation_resource,
4963 struct wl_resource *surface_resource,
4966 struct weston_surface *surface;
4967 struct weston_presentation_feedback *feedback;
4969 surface = wl_resource_get_user_data(surface_resource);
4971 feedback = zalloc(sizeof *feedback);
4972 if (feedback == NULL)
4975 feedback->resource = wl_resource_create(client,
4976 &wp_presentation_feedback_interface,
4978 if (!feedback->resource)
4981 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4982 destroy_presentation_feedback);
4984 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4992 wl_client_post_no_memory(client);
4995 static const struct wp_presentation_interface presentation_implementation = {
4996 presentation_destroy,
4997 presentation_feedback
5001 bind_presentation(struct wl_client *client,
5002 void *data, uint32_t version, uint32_t id)
5004 struct weston_compositor *compositor = data;
5005 struct wl_resource *resource;
5007 resource = wl_resource_create(client, &wp_presentation_interface,
5009 if (resource == NULL) {
5010 wl_client_post_no_memory(client);
5014 wl_resource_set_implementation(resource, &presentation_implementation,
5016 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
5020 compositor_bind(struct wl_client *client,
5021 void *data, uint32_t version, uint32_t id)
5023 struct weston_compositor *compositor = data;
5024 struct wl_resource *resource;
5026 resource = wl_resource_create(client, &wl_compositor_interface,
5028 if (resource == NULL) {
5029 wl_client_post_no_memory(client);
5033 wl_resource_set_implementation(resource, &compositor_interface,
5038 weston_environment_get_fd(const char *env)
5044 if (!e || !safe_strtoint(e, &fd))
5047 flags = fcntl(fd, F_GETFD);
5051 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
5058 timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
5059 uint32_t key, void *data)
5061 struct weston_compositor *compositor = data;
5063 if (weston_timeline_enabled_)
5064 weston_timeline_close();
5066 weston_timeline_open(compositor);
5069 /** Create the compositor.
5071 * This functions creates and initializes a compositor instance.
5073 * \param display The Wayland display to be used.
5074 * \param user_data A pointer to an object that can later be retrieved
5075 * using the \ref weston_compositor_get_user_data function.
5076 * \return The compositor instance on success or NULL on failure.
5078 WL_EXPORT struct weston_compositor *
5079 weston_compositor_create(struct wl_display *display, void *user_data)
5081 struct weston_compositor *ec;
5082 struct wl_event_loop *loop;
5084 ec = zalloc(sizeof *ec);
5088 ec->wl_display = display;
5089 ec->user_data = user_data;
5090 wl_signal_init(&ec->destroy_signal);
5091 wl_signal_init(&ec->create_surface_signal);
5092 wl_signal_init(&ec->activate_signal);
5093 wl_signal_init(&ec->transform_signal);
5094 wl_signal_init(&ec->kill_signal);
5095 wl_signal_init(&ec->idle_signal);
5096 wl_signal_init(&ec->wake_signal);
5097 wl_signal_init(&ec->show_input_panel_signal);
5098 wl_signal_init(&ec->hide_input_panel_signal);
5099 wl_signal_init(&ec->update_input_panel_signal);
5100 wl_signal_init(&ec->seat_created_signal);
5101 wl_signal_init(&ec->output_pending_signal);
5102 wl_signal_init(&ec->output_created_signal);
5103 wl_signal_init(&ec->output_destroyed_signal);
5104 wl_signal_init(&ec->output_moved_signal);
5105 wl_signal_init(&ec->output_resized_signal);
5106 wl_signal_init(&ec->session_signal);
5107 ec->session_active = 1;
5109 ec->output_id_pool = 0;
5110 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
5112 ec->activate_serial = 1;
5114 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
5115 ec, compositor_bind))
5118 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
5119 ec, bind_subcompositor))
5122 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
5123 ec, bind_viewporter))
5126 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
5127 ec, bind_presentation))
5130 if (weston_input_init(ec) != 0)
5133 wl_list_init(&ec->view_list);
5134 wl_list_init(&ec->plane_list);
5135 wl_list_init(&ec->layer_list);
5136 wl_list_init(&ec->seat_list);
5137 wl_list_init(&ec->pending_output_list);
5138 wl_list_init(&ec->output_list);
5139 wl_list_init(&ec->key_binding_list);
5140 wl_list_init(&ec->modifier_binding_list);
5141 wl_list_init(&ec->button_binding_list);
5142 wl_list_init(&ec->touch_binding_list);
5143 wl_list_init(&ec->axis_binding_list);
5144 wl_list_init(&ec->debug_binding_list);
5146 wl_list_init(&ec->plugin_api_list);
5148 weston_plane_init(&ec->primary_plane, ec, 0, 0);
5149 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
5151 wl_data_device_manager_init(ec->wl_display);
5153 wl_display_init_shm(ec->wl_display);
5155 loop = wl_display_get_event_loop(ec->wl_display);
5156 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
5158 weston_layer_init(&ec->fade_layer, ec);
5159 weston_layer_init(&ec->cursor_layer, ec);
5161 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
5162 weston_layer_set_position(&ec->cursor_layer,
5163 WESTON_LAYER_POSITION_CURSOR);
5165 weston_compositor_add_debug_binding(ec, KEY_T,
5166 timeline_key_binding_handler, ec);
5176 weston_compositor_shutdown(struct weston_compositor *ec)
5178 struct weston_output *output, *next;
5180 wl_event_source_remove(ec->idle_source);
5182 /* Destroy all outputs associated with this compositor */
5183 wl_list_for_each_safe(output, next, &ec->output_list, link)
5184 output->destroy(output);
5186 /* Destroy all pending outputs associated with this compositor */
5187 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
5188 output->destroy(output);
5191 ec->renderer->destroy(ec);
5193 weston_binding_list_destroy_all(&ec->key_binding_list);
5194 weston_binding_list_destroy_all(&ec->modifier_binding_list);
5195 weston_binding_list_destroy_all(&ec->button_binding_list);
5196 weston_binding_list_destroy_all(&ec->touch_binding_list);
5197 weston_binding_list_destroy_all(&ec->axis_binding_list);
5198 weston_binding_list_destroy_all(&ec->debug_binding_list);
5200 weston_plane_release(&ec->primary_plane);
5204 weston_compositor_exit_with_code(struct weston_compositor *compositor,
5207 if (compositor->exit_code == EXIT_SUCCESS)
5208 compositor->exit_code = exit_code;
5210 weston_compositor_exit(compositor);
5214 weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
5215 const struct weston_pointer_grab_interface *interface)
5217 struct weston_seat *seat;
5219 ec->default_pointer_grab = interface;
5220 wl_list_for_each(seat, &ec->seat_list, link) {
5221 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5224 weston_pointer_set_default_grab(pointer, interface);
5229 weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
5234 if (clock_gettime(clk_id, &ts) < 0)
5237 compositor->presentation_clock = clk_id;
5243 * For choosing the software clock, when the display hardware or API
5244 * does not expose a compatible presentation timestamp.
5247 weston_compositor_set_presentation_clock_software(
5248 struct weston_compositor *compositor)
5250 /* In order of preference */
5251 static const clockid_t clocks[] = {
5252 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
5253 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
5254 CLOCK_MONOTONIC, /* no jumps, may crawl */
5255 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
5256 CLOCK_REALTIME /* may jump and crawl */
5260 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
5261 if (weston_compositor_set_presentation_clock(compositor,
5265 weston_log("Error: no suitable presentation clock available.\n");
5270 /** Read the current time from the Presentation clock
5273 * \param ts[out] The current time.
5275 * \note Reading the current time in user space is always imprecise to some
5278 * This function is never meant to fail. If reading the clock does fail,
5279 * an error message is logged and a zero time is returned. Callers are not
5280 * supposed to detect or react to failures.
5283 weston_compositor_read_presentation_clock(
5284 const struct weston_compositor *compositor,
5285 struct timespec *ts)
5290 ret = clock_gettime(compositor->presentation_clock, ts);
5296 weston_log("Error: failure to read "
5297 "the presentation clock %#x: '%m' (%d)\n",
5298 compositor->presentation_clock, errno);
5303 /** Import dmabuf buffer into current renderer
5306 * \param buffer the dmabuf buffer to import
5307 * \return true on usable buffers, false otherwise
5309 * This function tests that the linux_dmabuf_buffer is usable
5310 * for the current renderer. Returns false on unusable buffers. Usually
5311 * usability is tested by importing the dmabufs for composition.
5313 * This hook is also used for detecting if the renderer supports
5314 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
5318 weston_compositor_import_dmabuf(struct weston_compositor *compositor,
5319 struct linux_dmabuf_buffer *buffer)
5321 struct weston_renderer *renderer;
5323 renderer = compositor->renderer;
5325 if (renderer->import_dmabuf == NULL)
5328 return renderer->import_dmabuf(compositor, buffer);
5332 weston_version(int *major, int *minor, int *micro)
5334 *major = WESTON_VERSION_MAJOR;
5335 *minor = WESTON_VERSION_MINOR;
5336 *micro = WESTON_VERSION_MICRO;
5340 weston_load_module(const char *name, const char *entrypoint)
5342 const char *builddir = getenv("WESTON_BUILD_DIR");
5343 char path[PATH_MAX];
5344 void *module, *init;
5350 if (name[0] != '/') {
5352 len = snprintf(path, sizeof path, "%s/.libs/%s",
5355 len = snprintf(path, sizeof path, "%s/%s",
5356 LIBWESTON_MODULEDIR, name);
5358 len = snprintf(path, sizeof path, "%s", name);
5361 /* snprintf returns the length of the string it would've written,
5362 * _excluding_ the NUL byte. So even being equal to the size of
5363 * our buffer is an error here. */
5364 if (len >= sizeof path)
5367 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
5369 weston_log("Module '%s' already loaded\n", path);
5374 weston_log("Loading module '%s'\n", path);
5375 module = dlopen(path, RTLD_NOW);
5377 weston_log("Failed to load module: %s\n", dlerror());
5381 init = dlsym(module, entrypoint);
5383 weston_log("Failed to lookup init function: %s\n", dlerror());
5392 /** Destroys the compositor.
5394 * This function cleans up the compositor state and destroys it.
5396 * \param compositor The compositor to be destroyed.
5399 weston_compositor_destroy(struct weston_compositor *compositor)
5401 /* prevent further rendering while shutting down */
5402 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5404 wl_signal_emit(&compositor->destroy_signal, compositor);
5406 weston_compositor_xkb_destroy(compositor);
5408 if (compositor->backend)
5409 compositor->backend->destroy(compositor);
5411 weston_plugin_api_destroy_list(compositor);
5416 /** Instruct the compositor to exit.
5418 * This functions does not directly destroy the compositor object, it merely
5419 * command it to start the tear down process. It is not guaranteed that the
5420 * tear down will happen immediately.
5422 * \param compositor The compositor to tear down.
5425 weston_compositor_exit(struct weston_compositor *compositor)
5427 compositor->exit(compositor);
5430 /** Return the user data stored in the compositor.
5432 * This function returns the user data pointer set with user_data parameter
5433 * to the \ref weston_compositor_create function.
5436 weston_compositor_get_user_data(struct weston_compositor *compositor)
5438 return compositor->user_data;
5441 static const char * const backend_map[] = {
5442 [WESTON_BACKEND_DRM] = "drm-backend.so",
5443 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5444 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5445 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5446 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5447 [WESTON_BACKEND_X11] = "x11-backend.so",
5450 /** Load a backend into a weston_compositor
5452 * A backend must be loaded to make a weston_compositor work. A backend
5453 * provides input and output capabilities, and determines the renderer to use.
5455 * \param compositor A compositor that has not had a backend loaded yet.
5456 * \param backend Name of the backend file.
5457 * \param config_base A pointer to a backend-specific configuration
5458 * structure's 'base' member.
5460 * \return 0 on success, or -1 on error.
5463 weston_compositor_load_backend(struct weston_compositor *compositor,
5464 enum weston_compositor_backend backend,
5465 struct weston_backend_config *config_base)
5467 int (*backend_init)(struct weston_compositor *c,
5468 struct weston_backend_config *config_base);
5470 if (backend >= ARRAY_LENGTH(backend_map))
5473 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
5477 return backend_init(compositor, config_base);
5481 weston_compositor_load_xwayland(struct weston_compositor *compositor)
5483 int (*module_init)(struct weston_compositor *ec);
5485 module_init = weston_load_module("xwayland.so", "weston_module_init");
5488 if (module_init(compositor) < 0)