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;
273 /* Assign to surface */
274 wl_list_insert(&surface->views, &view->surface_link);
276 wl_signal_init(&view->destroy_signal);
277 wl_list_init(&view->link);
278 wl_list_init(&view->layer_link.link);
280 pixman_region32_init(&view->clip);
283 pixman_region32_init(&view->transform.opaque);
285 wl_list_init(&view->geometry.transformation_list);
286 wl_list_insert(&view->geometry.transformation_list,
287 &view->transform.position.link);
288 weston_matrix_init(&view->transform.position.matrix);
289 wl_list_init(&view->geometry.child_list);
290 pixman_region32_init(&view->geometry.scissor);
291 pixman_region32_init(&view->transform.boundingbox);
292 view->transform.dirty = 1;
297 struct weston_frame_callback {
298 struct wl_resource *resource;
302 struct weston_presentation_feedback {
303 struct wl_resource *resource;
305 /* XXX: could use just wl_resource_get_link() instead */
308 /* The per-surface feedback flags */
313 weston_presentation_feedback_discard(
314 struct weston_presentation_feedback *feedback)
316 wp_presentation_feedback_send_discarded(feedback->resource);
317 wl_resource_destroy(feedback->resource);
321 weston_presentation_feedback_discard_list(struct wl_list *list)
323 struct weston_presentation_feedback *feedback, *tmp;
325 wl_list_for_each_safe(feedback, tmp, list, link)
326 weston_presentation_feedback_discard(feedback);
330 weston_presentation_feedback_present(
331 struct weston_presentation_feedback *feedback,
332 struct weston_output *output,
333 uint32_t refresh_nsec,
334 const struct timespec *ts,
338 struct wl_client *client = wl_resource_get_client(feedback->resource);
339 struct wl_resource *o;
342 wl_resource_for_each(o, &output->resource_list) {
343 if (wl_resource_get_client(o) != client)
346 wp_presentation_feedback_send_sync_output(feedback->resource, o);
350 wp_presentation_feedback_send_presented(feedback->resource,
351 secs >> 32, secs & 0xffffffff,
354 seq >> 32, seq & 0xffffffff,
355 flags | feedback->psf_flags);
356 wl_resource_destroy(feedback->resource);
360 weston_presentation_feedback_present_list(struct wl_list *list,
361 struct weston_output *output,
362 uint32_t refresh_nsec,
363 const struct timespec *ts,
367 struct weston_presentation_feedback *feedback, *tmp;
369 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
370 wl_list_empty(list));
372 wl_list_for_each_safe(feedback, tmp, list, link)
373 weston_presentation_feedback_present(feedback, output,
374 refresh_nsec, ts, seq,
379 surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
381 struct weston_surface_state *state =
382 container_of(listener, struct weston_surface_state,
383 buffer_destroy_listener);
385 state->buffer = NULL;
389 weston_surface_state_init(struct weston_surface_state *state)
391 state->newly_attached = 0;
392 state->buffer = NULL;
393 state->buffer_destroy_listener.notify =
394 surface_state_handle_buffer_destroy;
398 pixman_region32_init(&state->damage_surface);
399 pixman_region32_init(&state->damage_buffer);
400 pixman_region32_init(&state->opaque);
401 region_init_infinite(&state->input);
403 wl_list_init(&state->frame_callback_list);
404 wl_list_init(&state->feedback_list);
406 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
407 state->buffer_viewport.buffer.scale = 1;
408 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
409 state->buffer_viewport.surface.width = -1;
410 state->buffer_viewport.changed = 0;
414 weston_surface_state_fini(struct weston_surface_state *state)
416 struct weston_frame_callback *cb, *next;
418 wl_list_for_each_safe(cb, next,
419 &state->frame_callback_list, link)
420 wl_resource_destroy(cb->resource);
422 weston_presentation_feedback_discard_list(&state->feedback_list);
424 pixman_region32_fini(&state->input);
425 pixman_region32_fini(&state->opaque);
426 pixman_region32_fini(&state->damage_surface);
427 pixman_region32_fini(&state->damage_buffer);
430 wl_list_remove(&state->buffer_destroy_listener.link);
431 state->buffer = NULL;
435 weston_surface_state_set_buffer(struct weston_surface_state *state,
436 struct weston_buffer *buffer)
438 if (state->buffer == buffer)
442 wl_list_remove(&state->buffer_destroy_listener.link);
443 state->buffer = buffer;
445 wl_signal_add(&state->buffer->destroy_signal,
446 &state->buffer_destroy_listener);
449 WL_EXPORT struct weston_surface *
450 weston_surface_create(struct weston_compositor *compositor)
452 struct weston_surface *surface;
454 surface = zalloc(sizeof *surface);
458 wl_signal_init(&surface->destroy_signal);
459 wl_signal_init(&surface->commit_signal);
461 surface->compositor = compositor;
462 surface->ref_count = 1;
464 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
465 surface->buffer_viewport.buffer.scale = 1;
466 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
467 surface->buffer_viewport.surface.width = -1;
469 weston_surface_state_init(&surface->pending);
471 pixman_region32_init(&surface->damage);
472 pixman_region32_init(&surface->opaque);
473 region_init_infinite(&surface->input);
475 wl_list_init(&surface->views);
477 wl_list_init(&surface->frame_callback_list);
478 wl_list_init(&surface->feedback_list);
480 wl_list_init(&surface->subsurface_list);
481 wl_list_init(&surface->subsurface_list_pending);
483 weston_matrix_init(&surface->buffer_to_surface_matrix);
484 weston_matrix_init(&surface->surface_to_buffer_matrix);
486 wl_list_init(&surface->pointer_constraints);
492 weston_surface_set_color(struct weston_surface *surface,
493 float red, float green, float blue, float alpha)
495 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
499 weston_view_to_global_float(struct weston_view *view,
500 float sx, float sy, float *x, float *y)
502 if (view->transform.enabled) {
503 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
505 weston_matrix_transform(&view->transform.matrix, &v);
507 if (fabsf(v.f[3]) < 1e-6) {
508 weston_log("warning: numerical instability in "
509 "%s(), divisor = %g\n", __func__,
516 *x = v.f[0] / v.f[3];
517 *y = v.f[1] / v.f[3];
519 *x = sx + view->geometry.x;
520 *y = sy + view->geometry.y;
525 weston_transformed_coord(int width, int height,
526 enum wl_output_transform transform,
528 float sx, float sy, float *bx, float *by)
531 case WL_OUTPUT_TRANSFORM_NORMAL:
536 case WL_OUTPUT_TRANSFORM_FLIPPED:
540 case WL_OUTPUT_TRANSFORM_90:
544 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
548 case WL_OUTPUT_TRANSFORM_180:
552 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
556 case WL_OUTPUT_TRANSFORM_270:
560 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
570 WL_EXPORT pixman_box32_t
571 weston_transformed_rect(int width, int height,
572 enum wl_output_transform transform,
576 float x1, x2, y1, y2;
580 weston_transformed_coord(width, height, transform, scale,
581 rect.x1, rect.y1, &x1, &y1);
582 weston_transformed_coord(width, height, transform, scale,
583 rect.x2, rect.y2, &x2, &y2);
604 /** Transform a region by a matrix, restricted to axis-aligned transformations
606 * Warning: This function does not work for projective, affine, or matrices
607 * that encode arbitrary rotations. Only 90-degree step rotations are
611 weston_matrix_transform_region(pixman_region32_t *dest,
612 struct weston_matrix *matrix,
613 pixman_region32_t *src)
615 pixman_box32_t *src_rects, *dest_rects;
618 src_rects = pixman_region32_rectangles(src, &nrects);
619 dest_rects = malloc(nrects * sizeof(*dest_rects));
623 for (i = 0; i < nrects; i++) {
624 struct weston_vector vec1 = {{
625 src_rects[i].x1, src_rects[i].y1, 0, 1
627 weston_matrix_transform(matrix, &vec1);
628 vec1.f[0] /= vec1.f[3];
629 vec1.f[1] /= vec1.f[3];
631 struct weston_vector vec2 = {{
632 src_rects[i].x2, src_rects[i].y2, 0, 1
634 weston_matrix_transform(matrix, &vec2);
635 vec2.f[0] /= vec2.f[3];
636 vec2.f[1] /= vec2.f[3];
638 if (vec1.f[0] < vec2.f[0]) {
639 dest_rects[i].x1 = floor(vec1.f[0]);
640 dest_rects[i].x2 = ceil(vec2.f[0]);
642 dest_rects[i].x1 = floor(vec2.f[0]);
643 dest_rects[i].x2 = ceil(vec1.f[0]);
646 if (vec1.f[1] < vec2.f[1]) {
647 dest_rects[i].y1 = floor(vec1.f[1]);
648 dest_rects[i].y2 = ceil(vec2.f[1]);
650 dest_rects[i].y1 = floor(vec2.f[1]);
651 dest_rects[i].y2 = ceil(vec1.f[1]);
655 pixman_region32_clear(dest);
656 pixman_region32_init_rects(dest, dest_rects, nrects);
661 weston_transformed_region(int width, int height,
662 enum wl_output_transform transform,
664 pixman_region32_t *src, pixman_region32_t *dest)
666 pixman_box32_t *src_rects, *dest_rects;
669 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
671 pixman_region32_copy(dest, src);
675 src_rects = pixman_region32_rectangles(src, &nrects);
676 dest_rects = malloc(nrects * sizeof(*dest_rects));
680 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
681 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
683 for (i = 0; i < nrects; i++) {
686 case WL_OUTPUT_TRANSFORM_NORMAL:
687 dest_rects[i].x1 = src_rects[i].x1;
688 dest_rects[i].y1 = src_rects[i].y1;
689 dest_rects[i].x2 = src_rects[i].x2;
690 dest_rects[i].y2 = src_rects[i].y2;
692 case WL_OUTPUT_TRANSFORM_90:
693 dest_rects[i].x1 = height - src_rects[i].y2;
694 dest_rects[i].y1 = src_rects[i].x1;
695 dest_rects[i].x2 = height - src_rects[i].y1;
696 dest_rects[i].y2 = src_rects[i].x2;
698 case WL_OUTPUT_TRANSFORM_180:
699 dest_rects[i].x1 = width - src_rects[i].x2;
700 dest_rects[i].y1 = height - src_rects[i].y2;
701 dest_rects[i].x2 = width - src_rects[i].x1;
702 dest_rects[i].y2 = height - src_rects[i].y1;
704 case WL_OUTPUT_TRANSFORM_270:
705 dest_rects[i].x1 = src_rects[i].y1;
706 dest_rects[i].y1 = width - src_rects[i].x2;
707 dest_rects[i].x2 = src_rects[i].y2;
708 dest_rects[i].y2 = width - src_rects[i].x1;
710 case WL_OUTPUT_TRANSFORM_FLIPPED:
711 dest_rects[i].x1 = width - src_rects[i].x2;
712 dest_rects[i].y1 = src_rects[i].y1;
713 dest_rects[i].x2 = width - src_rects[i].x1;
714 dest_rects[i].y2 = src_rects[i].y2;
716 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
717 dest_rects[i].x1 = height - src_rects[i].y2;
718 dest_rects[i].y1 = width - src_rects[i].x2;
719 dest_rects[i].x2 = height - src_rects[i].y1;
720 dest_rects[i].y2 = width - src_rects[i].x1;
722 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
723 dest_rects[i].x1 = src_rects[i].x1;
724 dest_rects[i].y1 = height - src_rects[i].y2;
725 dest_rects[i].x2 = src_rects[i].x2;
726 dest_rects[i].y2 = height - src_rects[i].y1;
728 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
729 dest_rects[i].x1 = src_rects[i].y1;
730 dest_rects[i].y1 = src_rects[i].x1;
731 dest_rects[i].x2 = src_rects[i].y2;
732 dest_rects[i].y2 = src_rects[i].x2;
739 for (i = 0; i < nrects; i++) {
740 dest_rects[i].x1 *= scale;
741 dest_rects[i].x2 *= scale;
742 dest_rects[i].y1 *= scale;
743 dest_rects[i].y2 *= scale;
747 pixman_region32_clear(dest);
748 pixman_region32_init_rects(dest, dest_rects, nrects);
753 viewport_surface_to_buffer(struct weston_surface *surface,
754 float sx, float sy, float *bx, float *by)
756 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
757 double src_width, src_height;
760 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
761 if (vp->surface.width == -1) {
769 src_width = surface->width_from_buffer;
770 src_height = surface->height_from_buffer;
772 src_x = wl_fixed_to_double(vp->buffer.src_x);
773 src_y = wl_fixed_to_double(vp->buffer.src_y);
774 src_width = wl_fixed_to_double(vp->buffer.src_width);
775 src_height = wl_fixed_to_double(vp->buffer.src_height);
778 *bx = sx * src_width / surface->width + src_x;
779 *by = sy * src_height / surface->height + src_y;
783 weston_surface_to_buffer_float(struct weston_surface *surface,
784 float sx, float sy, float *bx, float *by)
786 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
788 /* first transform coordinates if the viewport is set */
789 viewport_surface_to_buffer(surface, sx, sy, bx, by);
791 weston_transformed_coord(surface->width_from_buffer,
792 surface->height_from_buffer,
793 vp->buffer.transform, vp->buffer.scale,
797 /** Transform a rectangle from surface coordinates to buffer coordinates
799 * \param surface The surface to fetch wp_viewport and buffer transformation
801 * \param rect The rectangle to transform.
802 * \return The transformed rectangle.
804 * Viewport and buffer transformations can only do translation, scaling,
805 * and rotations in 90-degree steps. Therefore the only loss in the
806 * conversion is coordinate rounding.
808 * However, some coordinate rounding takes place as an intermediate
809 * step before the buffer scale factor is applied, so the rectangle
810 * boundary may not be exactly as expected.
812 * This is OK for damage tracking since a little extra coverage is
815 WL_EXPORT pixman_box32_t
816 weston_surface_to_buffer_rect(struct weston_surface *surface,
819 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
822 /* first transform box coordinates if the viewport is set */
823 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
824 rect.x1 = floorf(xf);
825 rect.y1 = floorf(yf);
827 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
831 return weston_transformed_rect(surface->width_from_buffer,
832 surface->height_from_buffer,
833 vp->buffer.transform, vp->buffer.scale,
837 /** Transform a region from surface coordinates to buffer coordinates
839 * \param surface The surface to fetch wp_viewport and buffer transformation
841 * \param surface_region[in] The region in surface coordinates.
842 * \param buffer_region[out] The region converted to buffer coordinates.
844 * Buffer_region must be init'd, but will be completely overwritten.
846 * Viewport and buffer transformations can only do translation, scaling,
847 * and rotations in 90-degree steps. Therefore the only loss in the
848 * conversion is from the coordinate rounding that takes place in
849 * \ref weston_surface_to_buffer_rect.
852 weston_surface_to_buffer_region(struct weston_surface *surface,
853 pixman_region32_t *surface_region,
854 pixman_region32_t *buffer_region)
856 pixman_box32_t *src_rects, *dest_rects;
859 src_rects = pixman_region32_rectangles(surface_region, &nrects);
860 dest_rects = malloc(nrects * sizeof(*dest_rects));
864 for (i = 0; i < nrects; i++) {
865 dest_rects[i] = weston_surface_to_buffer_rect(surface,
869 pixman_region32_fini(buffer_region);
870 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
875 weston_view_move_to_plane(struct weston_view *view,
876 struct weston_plane *plane)
878 if (view->plane == plane)
881 weston_view_damage_below(view);
883 weston_surface_damage(view->surface);
886 /** Inflict damage on the plane where the view is visible.
888 * \param view The view that causes the damage.
890 * If the view is currently on a plane (including the primary plane),
891 * take the view's boundingbox, subtract all the opaque views that cover it,
892 * and add the remaining region as damage to the plane. This corresponds
893 * to the damage inflicted to the plane if this view disappeared.
895 * A repaint is scheduled for this view.
897 * The region of all opaque views covering this view is stored in
898 * weston_view::clip and updated by view_accumulate_damage() during
899 * weston_output_repaint(). Specifically, that region matches the
900 * scenegraph as it was last painted.
903 weston_view_damage_below(struct weston_view *view)
905 pixman_region32_t damage;
907 pixman_region32_init(&damage);
908 pixman_region32_subtract(&damage, &view->transform.boundingbox,
911 pixman_region32_union(&view->plane->damage,
912 &view->plane->damage, &damage);
913 pixman_region32_fini(&damage);
914 weston_view_schedule_repaint(view);
918 * \param es The surface
919 * \param mask The new set of outputs for the surface
921 * Sets the surface's set of outputs to the ones specified by
922 * the new output mask provided. Identifies the outputs that
923 * have changed, the posts enter and leave events for these
924 * outputs as appropriate.
927 weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
929 uint32_t different = es->output_mask ^ mask;
930 uint32_t entered = mask & different;
931 uint32_t left = es->output_mask & different;
932 struct weston_output *output;
933 struct wl_resource *resource = NULL;
934 struct wl_client *client;
936 es->output_mask = mask;
937 if (es->resource == NULL)
942 client = wl_resource_get_client(es->resource);
944 wl_list_for_each(output, &es->compositor->output_list, link) {
945 if (1u << output->id & different)
947 wl_resource_find_for_client(&output->resource_list,
949 if (resource == NULL)
951 if (1u << output->id & entered)
952 wl_surface_send_enter(es->resource, resource);
953 if (1u << output->id & left)
954 wl_surface_send_leave(es->resource, resource);
958 /** Recalculate which output(s) the surface has views displayed on
960 * \param es The surface to remap to outputs
962 * Finds the output that is showing the largest amount of one
963 * of the surface's various views. This output becomes the
964 * surface's primary output for vsync and frame callback purposes.
966 * Also notes all outputs of all of the surface's views
967 * in the output_mask for the surface.
970 weston_surface_assign_output(struct weston_surface *es)
972 struct weston_output *new_output;
973 struct weston_view *view;
974 pixman_region32_t region;
975 uint32_t max, area, mask;
981 pixman_region32_init(®ion);
982 wl_list_for_each(view, &es->views, surface_link) {
986 pixman_region32_intersect(®ion, &view->transform.boundingbox,
987 &view->output->region);
989 e = pixman_region32_extents(®ion);
990 area = (e->x2 - e->x1) * (e->y2 - e->y1);
992 mask |= view->output_mask;
995 new_output = view->output;
999 pixman_region32_fini(®ion);
1001 es->output = new_output;
1002 weston_surface_update_output_mask(es, mask);
1005 /** Recalculate which output(s) the view is displayed on
1007 * \param ev The view to remap to outputs
1009 * Identifies the set of outputs that the view is visible on,
1010 * noting them into the output_mask. The output that the view
1011 * is most visible on is set as the view's primary output.
1013 * Also does the same for the view's surface. See
1014 * weston_surface_assign_output().
1017 weston_view_assign_output(struct weston_view *ev)
1019 struct weston_compositor *ec = ev->surface->compositor;
1020 struct weston_output *output, *new_output;
1021 pixman_region32_t region;
1022 uint32_t max, area, mask;
1028 pixman_region32_init(®ion);
1029 wl_list_for_each(output, &ec->output_list, link) {
1030 if (output->destroying)
1033 pixman_region32_intersect(®ion, &ev->transform.boundingbox,
1036 e = pixman_region32_extents(®ion);
1037 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1040 mask |= 1u << output->id;
1043 new_output = output;
1047 pixman_region32_fini(®ion);
1049 ev->output = new_output;
1050 ev->output_mask = mask;
1052 weston_surface_assign_output(ev->surface);
1056 weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1057 int from_x, int from_y, int *to_x, int *to_y)
1061 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1062 weston_view_from_global_float(to, x, y, &x, &y);
1069 weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1074 a = pixman_region32_extents(&from->geometry.scissor);
1076 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1077 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1079 pixman_region32_fini(&to->geometry.scissor);
1080 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1084 view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
1085 pixman_region32_t *bbox)
1087 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1088 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
1090 { inbox->x1, inbox->y1 },
1091 { inbox->x1, inbox->y2 },
1092 { inbox->x2, inbox->y1 },
1093 { inbox->x2, inbox->y2 },
1098 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
1099 /* avoid rounding empty bbox to 1x1 */
1100 pixman_region32_init(bbox);
1104 for (i = 0; i < 4; ++i) {
1106 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
1117 int_x = floorf(min_x);
1118 int_y = floorf(min_y);
1119 pixman_region32_init_rect(bbox, int_x, int_y,
1120 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
1124 weston_view_update_transform_disable(struct weston_view *view)
1126 view->transform.enabled = 0;
1128 /* round off fractions when not transformed */
1129 view->geometry.x = roundf(view->geometry.x);
1130 view->geometry.y = roundf(view->geometry.y);
1132 /* Otherwise identity matrix, but with x and y translation. */
1133 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1134 view->transform.position.matrix.d[12] = view->geometry.x;
1135 view->transform.position.matrix.d[13] = view->geometry.y;
1137 view->transform.matrix = view->transform.position.matrix;
1139 view->transform.inverse = view->transform.position.matrix;
1140 view->transform.inverse.d[12] = -view->geometry.x;
1141 view->transform.inverse.d[13] = -view->geometry.y;
1143 pixman_region32_init_rect(&view->transform.boundingbox,
1145 view->surface->width,
1146 view->surface->height);
1147 if (view->geometry.scissor_enabled)
1148 pixman_region32_intersect(&view->transform.boundingbox,
1149 &view->transform.boundingbox,
1150 &view->geometry.scissor);
1152 pixman_region32_translate(&view->transform.boundingbox,
1153 view->geometry.x, view->geometry.y);
1155 if (view->alpha == 1.0) {
1156 pixman_region32_copy(&view->transform.opaque,
1157 &view->surface->opaque);
1158 pixman_region32_translate(&view->transform.opaque,
1165 weston_view_update_transform_enable(struct weston_view *view)
1167 struct weston_view *parent = view->geometry.parent;
1168 struct weston_matrix *matrix = &view->transform.matrix;
1169 struct weston_matrix *inverse = &view->transform.inverse;
1170 struct weston_transform *tform;
1171 pixman_region32_t surfregion;
1172 const pixman_box32_t *surfbox;
1174 view->transform.enabled = 1;
1176 /* Otherwise identity matrix, but with x and y translation. */
1177 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1178 view->transform.position.matrix.d[12] = view->geometry.x;
1179 view->transform.position.matrix.d[13] = view->geometry.y;
1181 weston_matrix_init(matrix);
1182 wl_list_for_each(tform, &view->geometry.transformation_list, link)
1183 weston_matrix_multiply(matrix, &tform->matrix);
1186 weston_matrix_multiply(matrix, &parent->transform.matrix);
1188 if (weston_matrix_invert(inverse, matrix) < 0) {
1189 /* Oops, bad total transformation, not invertible */
1190 weston_log("error: weston_view %p"
1191 " transformation not invertible.\n", view);
1195 pixman_region32_init_rect(&surfregion, 0, 0,
1196 view->surface->width, view->surface->height);
1197 if (view->geometry.scissor_enabled)
1198 pixman_region32_intersect(&surfregion, &surfregion,
1199 &view->geometry.scissor);
1200 surfbox = pixman_region32_extents(&surfregion);
1202 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1203 pixman_region32_fini(&surfregion);
1208 static struct weston_layer *
1209 get_view_layer(struct weston_view *view)
1211 if (view->parent_view)
1212 return get_view_layer(view->parent_view);
1213 return view->layer_link.layer;
1217 weston_view_update_transform(struct weston_view *view)
1219 struct weston_view *parent = view->geometry.parent;
1220 struct weston_layer *layer;
1221 pixman_region32_t mask;
1223 if (!view->transform.dirty)
1227 weston_view_update_transform(parent);
1229 view->transform.dirty = 0;
1231 weston_view_damage_below(view);
1233 pixman_region32_fini(&view->transform.boundingbox);
1234 pixman_region32_fini(&view->transform.opaque);
1235 pixman_region32_init(&view->transform.opaque);
1237 /* transform.position is always in transformation_list */
1238 if (view->geometry.transformation_list.next ==
1239 &view->transform.position.link &&
1240 view->geometry.transformation_list.prev ==
1241 &view->transform.position.link &&
1243 weston_view_update_transform_disable(view);
1245 if (weston_view_update_transform_enable(view) < 0)
1246 weston_view_update_transform_disable(view);
1249 layer = get_view_layer(view);
1251 pixman_region32_init_with_extents(&mask, &layer->mask);
1252 pixman_region32_intersect(&view->transform.boundingbox,
1253 &view->transform.boundingbox, &mask);
1254 pixman_region32_intersect(&view->transform.opaque,
1255 &view->transform.opaque, &mask);
1256 pixman_region32_fini(&mask);
1260 if (parent->geometry.scissor_enabled) {
1261 view->geometry.scissor_enabled = true;
1262 weston_view_transfer_scissor(parent, view);
1264 view->geometry.scissor_enabled = false;
1268 weston_view_damage_below(view);
1270 weston_view_assign_output(view);
1272 wl_signal_emit(&view->surface->compositor->transform_signal,
1277 weston_view_geometry_dirty(struct weston_view *view)
1279 struct weston_view *child;
1282 * The invariant: if view->geometry.dirty, then all views
1283 * in view->geometry.child_list have geometry.dirty too.
1284 * Corollary: if not parent->geometry.dirty, then all ancestors
1288 if (view->transform.dirty)
1291 view->transform.dirty = 1;
1293 wl_list_for_each(child, &view->geometry.child_list,
1294 geometry.parent_link)
1295 weston_view_geometry_dirty(child);
1299 weston_view_to_global_fixed(struct weston_view *view,
1300 wl_fixed_t vx, wl_fixed_t vy,
1301 wl_fixed_t *x, wl_fixed_t *y)
1305 weston_view_to_global_float(view,
1306 wl_fixed_to_double(vx),
1307 wl_fixed_to_double(vy),
1309 *x = wl_fixed_from_double(xf);
1310 *y = wl_fixed_from_double(yf);
1314 weston_view_from_global_float(struct weston_view *view,
1315 float x, float y, float *vx, float *vy)
1317 if (view->transform.enabled) {
1318 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1320 weston_matrix_transform(&view->transform.inverse, &v);
1322 if (fabsf(v.f[3]) < 1e-6) {
1323 weston_log("warning: numerical instability in "
1324 "weston_view_from_global(), divisor = %g\n",
1331 *vx = v.f[0] / v.f[3];
1332 *vy = v.f[1] / v.f[3];
1334 *vx = x - view->geometry.x;
1335 *vy = y - view->geometry.y;
1340 weston_view_from_global_fixed(struct weston_view *view,
1341 wl_fixed_t x, wl_fixed_t y,
1342 wl_fixed_t *vx, wl_fixed_t *vy)
1346 weston_view_from_global_float(view,
1347 wl_fixed_to_double(x),
1348 wl_fixed_to_double(y),
1350 *vx = wl_fixed_from_double(vxf);
1351 *vy = wl_fixed_from_double(vyf);
1355 weston_view_from_global(struct weston_view *view,
1356 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
1360 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1366 * \param surface The surface to be repainted
1368 * Marks the output(s) that the surface is shown on as needing to be
1369 * repainted. See weston_output_schedule_repaint().
1372 weston_surface_schedule_repaint(struct weston_surface *surface)
1374 struct weston_output *output;
1376 wl_list_for_each(output, &surface->compositor->output_list, link)
1377 if (surface->output_mask & (1u << output->id))
1378 weston_output_schedule_repaint(output);
1382 * \param view The view to be repainted
1384 * Marks the output(s) that the view is shown on as needing to be
1385 * repainted. See weston_output_schedule_repaint().
1388 weston_view_schedule_repaint(struct weston_view *view)
1390 struct weston_output *output;
1392 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1393 if (view->output_mask & (1u << output->id))
1394 weston_output_schedule_repaint(output);
1398 * XXX: This function does it the wrong way.
1399 * surface->damage is the damage from the client, and causes
1400 * surface_flush_damage() to copy pixels. No window management action can
1401 * cause damage to the client-provided content, warranting re-upload!
1403 * Instead of surface->damage, this function should record the damage
1404 * with all the views for this surface to avoid extraneous texture
1408 weston_surface_damage(struct weston_surface *surface)
1410 pixman_region32_union_rect(&surface->damage, &surface->damage,
1411 0, 0, surface->width,
1414 weston_surface_schedule_repaint(surface);
1418 weston_view_set_position(struct weston_view *view, float x, float y)
1420 if (view->geometry.x == x && view->geometry.y == y)
1423 view->geometry.x = x;
1424 view->geometry.y = y;
1425 weston_view_geometry_dirty(view);
1429 transform_parent_handle_parent_destroy(struct wl_listener *listener,
1432 struct weston_view *view =
1433 container_of(listener, struct weston_view,
1434 geometry.parent_destroy_listener);
1436 weston_view_set_transform_parent(view, NULL);
1440 weston_view_set_transform_parent(struct weston_view *view,
1441 struct weston_view *parent)
1443 if (view->geometry.parent) {
1444 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1445 wl_list_remove(&view->geometry.parent_link);
1448 view->geometry.scissor_enabled = false;
1451 view->geometry.parent = parent;
1453 view->geometry.parent_destroy_listener.notify =
1454 transform_parent_handle_parent_destroy;
1456 wl_signal_add(&parent->destroy_signal,
1457 &view->geometry.parent_destroy_listener);
1458 wl_list_insert(&parent->geometry.child_list,
1459 &view->geometry.parent_link);
1462 weston_view_geometry_dirty(view);
1465 /** Set a clip mask rectangle on a view
1467 * \param view The view to set the clip mask on.
1468 * \param x Top-left corner X coordinate of the clip rectangle.
1469 * \param y Top-left corner Y coordinate of the clip rectangle.
1470 * \param width Width of the clip rectangle, non-negative.
1471 * \param height Height of the clip rectangle, non-negative.
1473 * A shell may set a clip mask rectangle on a view. Everything outside
1474 * the rectangle is cut away for input and output purposes: it is
1475 * not drawn and cannot be hit by hit-test based input like pointer
1476 * motion or touch-downs. Everything inside the rectangle will behave
1477 * normally. Clients are unaware of clipping.
1479 * The rectangle is set in surface-local coordinates. Setting a clip
1480 * mask rectangle does not affect the view position, the view is positioned
1481 * as it would be without a clip. The clip also does not change
1482 * weston_surface::width,height.
1484 * The clip mask rectangle is part of transformation inheritance
1485 * (weston_view_set_transform_parent()). A clip set in the root of the
1486 * transformation inheritance tree will affect all views in the tree.
1487 * A clip can be set only on the root view. Attempting to set a clip
1488 * on view that has a transformation parent will fail. Assigning a parent
1489 * to a view that has a clip set will cause the clip to be forgotten.
1491 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1492 * on the additional transformations in the child views. These transformations
1493 * may not rotate the coordinate axes, i.e., only translation and scaling
1494 * are allowed. Violating this restriction causes the clipping to malfunction.
1495 * Furthermore, using scaling may cause rounding errors in child clipping.
1497 * The clip mask rectangle is not automatically adjusted based on
1498 * wl_surface.attach dx and dy arguments.
1500 * A clip mask rectangle can be set only if the compositor capability
1501 * WESTON_CAP_VIEW_CLIP_MASK is present.
1503 * This function sets the clip mask rectangle and schedules a repaint for
1507 weston_view_set_mask(struct weston_view *view,
1508 int x, int y, int width, int height)
1510 struct weston_compositor *compositor = view->surface->compositor;
1512 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1513 weston_log("%s not allowed without capability!\n", __func__);
1517 if (view->geometry.parent) {
1518 weston_log("view %p has a parent, clip forbidden!\n", view);
1522 if (width < 0 || height < 0) {
1523 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1524 x, y, width, height);
1528 pixman_region32_fini(&view->geometry.scissor);
1529 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1530 view->geometry.scissor_enabled = true;
1531 weston_view_geometry_dirty(view);
1532 weston_view_schedule_repaint(view);
1535 /** Remove the clip mask from a view
1537 * \param view The view to remove the clip mask from.
1539 * Removed the clip mask rectangle and schedules a repaint.
1541 * \sa weston_view_set_mask
1544 weston_view_set_mask_infinite(struct weston_view *view)
1546 view->geometry.scissor_enabled = false;
1547 weston_view_geometry_dirty(view);
1548 weston_view_schedule_repaint(view);
1551 /* Check if view should be displayed
1553 * The indicator is set manually when assigning
1554 * a view to a surface.
1556 * This needs reworking. See the thread starting at:
1558 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1561 weston_view_is_mapped(struct weston_view *view)
1563 return view->is_mapped;
1566 /* Check if a surface has a view assigned to it
1568 * The indicator is set manually when mapping
1569 * a surface and creating a view for it.
1571 * This needs to go. See the thread starting at:
1573 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1577 weston_surface_is_mapped(struct weston_surface *surface)
1579 return surface->is_mapped;
1583 surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
1585 struct weston_view *view;
1587 if (surface->width == width && surface->height == height)
1590 surface->width = width;
1591 surface->height = height;
1593 wl_list_for_each(view, &surface->views, surface_link)
1594 weston_view_geometry_dirty(view);
1598 weston_surface_set_size(struct weston_surface *surface,
1599 int32_t width, int32_t height)
1601 assert(!surface->resource);
1602 surface_set_size(surface, width, height);
1606 fixed_round_up_to_int(wl_fixed_t f)
1608 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1612 convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1613 int32_t width, int32_t height,
1619 switch (transform) {
1620 case WL_OUTPUT_TRANSFORM_NORMAL:
1621 case WL_OUTPUT_TRANSFORM_180:
1622 case WL_OUTPUT_TRANSFORM_FLIPPED:
1623 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1624 *width_out = width / scale;
1625 *height_out = height / scale;
1627 case WL_OUTPUT_TRANSFORM_90:
1628 case WL_OUTPUT_TRANSFORM_270:
1629 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1630 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1631 *width_out = height / scale;
1632 *height_out = width / scale;
1635 assert(0 && "invalid transform");
1640 weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
1642 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1644 if (!surface->buffer_ref.buffer) {
1645 surface->width_from_buffer = 0;
1646 surface->height_from_buffer = 0;
1650 convert_size_by_transform_scale(&surface->width_from_buffer,
1651 &surface->height_from_buffer,
1652 surface->buffer_ref.buffer->width,
1653 surface->buffer_ref.buffer->height,
1654 vp->buffer.transform,
1659 weston_surface_update_size(struct weston_surface *surface)
1661 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1662 int32_t width, height;
1664 width = surface->width_from_buffer;
1665 height = surface->height_from_buffer;
1667 if (width != 0 && vp->surface.width != -1) {
1668 surface_set_size(surface,
1669 vp->surface.width, vp->surface.height);
1673 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
1674 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1675 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1677 surface_set_size(surface, w ?: 1, h ?: 1);
1681 surface_set_size(surface, width, height);
1685 weston_compositor_get_time(void)
1689 gettimeofday(&tv, NULL);
1691 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
1694 WL_EXPORT struct weston_view *
1695 weston_compositor_pick_view(struct weston_compositor *compositor,
1696 wl_fixed_t x, wl_fixed_t y,
1697 wl_fixed_t *vx, wl_fixed_t *vy)
1699 struct weston_view *view;
1700 wl_fixed_t view_x, view_y;
1701 int view_ix, view_iy;
1702 int ix = wl_fixed_to_int(x);
1703 int iy = wl_fixed_to_int(y);
1705 wl_list_for_each(view, &compositor->view_list, link) {
1706 if (!pixman_region32_contains_point(
1707 &view->transform.boundingbox, ix, iy, NULL))
1710 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1711 view_ix = wl_fixed_to_int(view_x);
1712 view_iy = wl_fixed_to_int(view_y);
1714 if (!pixman_region32_contains_point(&view->surface->input,
1715 view_ix, view_iy, NULL))
1718 if (view->geometry.scissor_enabled &&
1719 !pixman_region32_contains_point(&view->geometry.scissor,
1720 view_ix, view_iy, NULL))
1728 *vx = wl_fixed_from_int(-1000000);
1729 *vy = wl_fixed_from_int(-1000000);
1734 weston_compositor_repick(struct weston_compositor *compositor)
1736 struct weston_seat *seat;
1738 if (!compositor->session_active)
1741 wl_list_for_each(seat, &compositor->seat_list, link)
1742 weston_seat_repick(seat);
1746 weston_view_unmap(struct weston_view *view)
1748 struct weston_seat *seat;
1750 if (!weston_view_is_mapped(view))
1753 weston_view_damage_below(view);
1754 view->output = NULL;
1756 view->is_mapped = false;
1757 weston_layer_entry_remove(&view->layer_link);
1758 wl_list_remove(&view->link);
1759 wl_list_init(&view->link);
1760 view->output_mask = 0;
1761 weston_surface_assign_output(view->surface);
1763 if (weston_surface_is_mapped(view->surface))
1766 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1767 struct weston_touch *touch = weston_seat_get_touch(seat);
1768 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1769 struct weston_keyboard *keyboard =
1770 weston_seat_get_keyboard(seat);
1772 if (keyboard && keyboard->focus == view->surface)
1773 weston_keyboard_set_focus(keyboard, NULL);
1774 if (pointer && pointer->focus == view)
1775 weston_pointer_clear_focus(pointer);
1776 if (touch && touch->focus == view)
1777 weston_touch_set_focus(touch, NULL);
1782 weston_surface_unmap(struct weston_surface *surface)
1784 struct weston_view *view;
1786 surface->is_mapped = false;
1787 wl_list_for_each(view, &surface->views, surface_link)
1788 weston_view_unmap(view);
1789 surface->output = NULL;
1793 weston_surface_reset_pending_buffer(struct weston_surface *surface)
1795 weston_surface_state_set_buffer(&surface->pending, NULL);
1796 surface->pending.sx = 0;
1797 surface->pending.sy = 0;
1798 surface->pending.newly_attached = 0;
1799 surface->pending.buffer_viewport.changed = 0;
1803 weston_view_destroy(struct weston_view *view)
1805 wl_signal_emit(&view->destroy_signal, view);
1807 assert(wl_list_empty(&view->geometry.child_list));
1809 if (weston_view_is_mapped(view)) {
1810 weston_view_unmap(view);
1811 weston_compositor_build_view_list(view->surface->compositor);
1814 wl_list_remove(&view->link);
1815 weston_layer_entry_remove(&view->layer_link);
1817 pixman_region32_fini(&view->clip);
1818 pixman_region32_fini(&view->geometry.scissor);
1819 pixman_region32_fini(&view->transform.boundingbox);
1820 pixman_region32_fini(&view->transform.opaque);
1822 weston_view_set_transform_parent(view, NULL);
1824 wl_list_remove(&view->surface_link);
1830 weston_surface_destroy(struct weston_surface *surface)
1832 struct weston_frame_callback *cb, *next;
1833 struct weston_view *ev, *nv;
1834 struct weston_pointer_constraint *constraint, *next_constraint;
1836 if (--surface->ref_count > 0)
1839 assert(surface->resource == NULL);
1841 wl_signal_emit(&surface->destroy_signal, surface);
1843 assert(wl_list_empty(&surface->subsurface_list_pending));
1844 assert(wl_list_empty(&surface->subsurface_list));
1846 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1847 weston_view_destroy(ev);
1849 weston_surface_state_fini(&surface->pending);
1851 weston_buffer_reference(&surface->buffer_ref, NULL);
1853 pixman_region32_fini(&surface->damage);
1854 pixman_region32_fini(&surface->opaque);
1855 pixman_region32_fini(&surface->input);
1857 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
1858 wl_resource_destroy(cb->resource);
1860 weston_presentation_feedback_discard_list(&surface->feedback_list);
1862 wl_list_for_each_safe(constraint, next_constraint,
1863 &surface->pointer_constraints,
1865 weston_pointer_constraint_destroy(constraint);
1871 destroy_surface(struct wl_resource *resource)
1873 struct weston_surface *surface = wl_resource_get_user_data(resource);
1877 /* Set the resource to NULL, since we don't want to leave a
1878 * dangling pointer if the surface was refcounted and survives
1879 * the weston_surface_destroy() call. */
1880 surface->resource = NULL;
1882 if (surface->viewport_resource)
1883 wl_resource_set_user_data(surface->viewport_resource, NULL);
1885 weston_surface_destroy(surface);
1889 weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1891 struct weston_buffer *buffer =
1892 container_of(listener, struct weston_buffer, destroy_listener);
1894 wl_signal_emit(&buffer->destroy_signal, buffer);
1898 WL_EXPORT struct weston_buffer *
1899 weston_buffer_from_resource(struct wl_resource *resource)
1901 struct weston_buffer *buffer;
1902 struct wl_listener *listener;
1904 listener = wl_resource_get_destroy_listener(resource,
1905 weston_buffer_destroy_handler);
1908 return container_of(listener, struct weston_buffer,
1911 buffer = zalloc(sizeof *buffer);
1915 buffer->resource = resource;
1916 wl_signal_init(&buffer->destroy_signal);
1917 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
1918 buffer->y_inverted = 1;
1919 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
1925 weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1928 struct weston_buffer_reference *ref =
1929 container_of(listener, struct weston_buffer_reference,
1932 assert((struct weston_buffer *)data == ref->buffer);
1937 weston_buffer_reference(struct weston_buffer_reference *ref,
1938 struct weston_buffer *buffer)
1940 if (ref->buffer && buffer != ref->buffer) {
1941 ref->buffer->busy_count--;
1942 if (ref->buffer->busy_count == 0) {
1943 assert(wl_resource_get_client(ref->buffer->resource));
1944 wl_resource_queue_event(ref->buffer->resource,
1947 wl_list_remove(&ref->destroy_listener.link);
1950 if (buffer && buffer != ref->buffer) {
1951 buffer->busy_count++;
1952 wl_signal_add(&buffer->destroy_signal,
1953 &ref->destroy_listener);
1956 ref->buffer = buffer;
1957 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1961 weston_surface_attach(struct weston_surface *surface,
1962 struct weston_buffer *buffer)
1964 weston_buffer_reference(&surface->buffer_ref, buffer);
1967 if (weston_surface_is_mapped(surface))
1968 weston_surface_unmap(surface);
1971 surface->compositor->renderer->attach(surface, buffer);
1973 weston_surface_calculate_size_from_buffer(surface);
1974 weston_presentation_feedback_discard_list(&surface->feedback_list);
1978 weston_compositor_damage_all(struct weston_compositor *compositor)
1980 struct weston_output *output;
1982 wl_list_for_each(output, &compositor->output_list, link)
1983 weston_output_damage(output);
1987 weston_output_damage(struct weston_output *output)
1989 struct weston_compositor *compositor = output->compositor;
1991 pixman_region32_union(&compositor->primary_plane.damage,
1992 &compositor->primary_plane.damage,
1994 weston_output_schedule_repaint(output);
1998 surface_flush_damage(struct weston_surface *surface)
2000 if (surface->buffer_ref.buffer &&
2001 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
2002 surface->compositor->renderer->flush_damage(surface);
2004 if (weston_timeline_enabled_ &&
2005 pixman_region32_not_empty(&surface->damage))
2006 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2007 TLP_OUTPUT(surface->output), TLP_END);
2009 pixman_region32_clear(&surface->damage);
2013 view_accumulate_damage(struct weston_view *view,
2014 pixman_region32_t *opaque)
2016 pixman_region32_t damage;
2018 pixman_region32_init(&damage);
2019 if (view->transform.enabled) {
2020 pixman_box32_t *extents;
2022 extents = pixman_region32_extents(&view->surface->damage);
2023 view_compute_bbox(view, extents, &damage);
2025 pixman_region32_copy(&damage, &view->surface->damage);
2026 pixman_region32_translate(&damage,
2027 view->geometry.x, view->geometry.y);
2030 pixman_region32_intersect(&damage, &damage,
2031 &view->transform.boundingbox);
2032 pixman_region32_subtract(&damage, &damage, opaque);
2033 pixman_region32_union(&view->plane->damage,
2034 &view->plane->damage, &damage);
2035 pixman_region32_fini(&damage);
2036 pixman_region32_copy(&view->clip, opaque);
2037 pixman_region32_union(opaque, opaque, &view->transform.opaque);
2041 compositor_accumulate_damage(struct weston_compositor *ec)
2043 struct weston_plane *plane;
2044 struct weston_view *ev;
2045 pixman_region32_t opaque, clip;
2047 pixman_region32_init(&clip);
2049 wl_list_for_each(plane, &ec->plane_list, link) {
2050 pixman_region32_copy(&plane->clip, &clip);
2052 pixman_region32_init(&opaque);
2054 wl_list_for_each(ev, &ec->view_list, link) {
2055 if (ev->plane != plane)
2058 view_accumulate_damage(ev, &opaque);
2061 pixman_region32_union(&clip, &clip, &opaque);
2062 pixman_region32_fini(&opaque);
2065 pixman_region32_fini(&clip);
2067 wl_list_for_each(ev, &ec->view_list, link)
2068 ev->surface->touched = false;
2070 wl_list_for_each(ev, &ec->view_list, link) {
2071 if (ev->surface->touched)
2073 ev->surface->touched = true;
2075 surface_flush_damage(ev->surface);
2077 /* Both the renderer and the backend have seen the buffer
2078 * by now. If renderer needs the buffer, it has its own
2079 * reference set. If the backend wants to keep the buffer
2080 * around for migrating the surface into a non-primary plane
2081 * later, keep_buffer is true. Otherwise, drop the core
2082 * reference now, and allow early buffer release. This enables
2083 * clients to use single-buffering.
2085 if (!ev->surface->keep_buffer)
2086 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
2091 surface_stash_subsurface_views(struct weston_surface *surface)
2093 struct weston_subsurface *sub;
2095 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2096 if (sub->surface == surface)
2099 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2100 wl_list_init(&sub->surface->views);
2102 surface_stash_subsurface_views(sub->surface);
2107 surface_free_unused_subsurface_views(struct weston_surface *surface)
2109 struct weston_subsurface *sub;
2110 struct weston_view *view, *nv;
2112 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2113 if (sub->surface == surface)
2116 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2117 weston_view_unmap (view);
2118 weston_view_destroy(view);
2121 surface_free_unused_subsurface_views(sub->surface);
2126 view_list_add_subsurface_view(struct weston_compositor *compositor,
2127 struct weston_subsurface *sub,
2128 struct weston_view *parent)
2130 struct weston_subsurface *child;
2131 struct weston_view *view = NULL, *iv;
2133 if (!weston_surface_is_mapped(sub->surface))
2136 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2137 if (iv->geometry.parent == parent) {
2144 /* Put it back in the surface's list of views */
2145 wl_list_remove(&view->surface_link);
2146 wl_list_insert(&sub->surface->views, &view->surface_link);
2148 view = weston_view_create(sub->surface);
2149 weston_view_set_position(view,
2152 weston_view_set_transform_parent(view, parent);
2155 view->parent_view = parent;
2156 weston_view_update_transform(view);
2157 view->is_mapped = true;
2159 if (wl_list_empty(&sub->surface->subsurface_list)) {
2160 wl_list_insert(compositor->view_list.prev, &view->link);
2164 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2165 if (child->surface == sub->surface)
2166 wl_list_insert(compositor->view_list.prev, &view->link);
2168 view_list_add_subsurface_view(compositor, child, view);
2173 view_list_add(struct weston_compositor *compositor,
2174 struct weston_view *view)
2176 struct weston_subsurface *sub;
2178 weston_view_update_transform(view);
2180 if (wl_list_empty(&view->surface->subsurface_list)) {
2181 wl_list_insert(compositor->view_list.prev, &view->link);
2185 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2186 if (sub->surface == view->surface)
2187 wl_list_insert(compositor->view_list.prev, &view->link);
2189 view_list_add_subsurface_view(compositor, sub, view);
2194 weston_compositor_build_view_list(struct weston_compositor *compositor)
2196 struct weston_view *view;
2197 struct weston_layer *layer;
2199 wl_list_for_each(layer, &compositor->layer_list, link)
2200 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
2201 surface_stash_subsurface_views(view->surface);
2203 wl_list_init(&compositor->view_list);
2204 wl_list_for_each(layer, &compositor->layer_list, link) {
2205 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2206 view_list_add(compositor, view);
2210 wl_list_for_each(layer, &compositor->layer_list, link)
2211 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
2212 surface_free_unused_subsurface_views(view->surface);
2216 weston_output_take_feedback_list(struct weston_output *output,
2217 struct weston_surface *surface)
2219 struct weston_view *view;
2220 struct weston_presentation_feedback *feedback;
2221 uint32_t flags = 0xffffffff;
2223 if (wl_list_empty(&surface->feedback_list))
2226 /* All views must have the flag for the flag to survive. */
2227 wl_list_for_each(view, &surface->views, surface_link) {
2228 /* ignore views that are not on this output at all */
2229 if (view->output_mask & (1u << output->id))
2230 flags &= view->psf_flags;
2233 wl_list_for_each(feedback, &surface->feedback_list, link)
2234 feedback->psf_flags = flags;
2236 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2237 wl_list_init(&surface->feedback_list);
2241 weston_output_repaint(struct weston_output *output)
2243 struct weston_compositor *ec = output->compositor;
2244 struct weston_view *ev;
2245 struct weston_animation *animation, *next;
2246 struct weston_frame_callback *cb, *cnext;
2247 struct wl_list frame_callback_list;
2248 pixman_region32_t output_damage;
2251 if (output->destroying)
2254 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2256 /* Rebuild the surface list and update surface transforms up front. */
2257 weston_compositor_build_view_list(ec);
2259 if (output->assign_planes && !output->disable_planes) {
2260 output->assign_planes(output);
2262 wl_list_for_each(ev, &ec->view_list, link) {
2263 weston_view_move_to_plane(ev, &ec->primary_plane);
2268 wl_list_init(&frame_callback_list);
2269 wl_list_for_each(ev, &ec->view_list, link) {
2270 /* Note: This operation is safe to do multiple times on the
2273 if (ev->surface->output == output) {
2274 wl_list_insert_list(&frame_callback_list,
2275 &ev->surface->frame_callback_list);
2276 wl_list_init(&ev->surface->frame_callback_list);
2278 weston_output_take_feedback_list(output, ev->surface);
2282 compositor_accumulate_damage(ec);
2284 pixman_region32_init(&output_damage);
2285 pixman_region32_intersect(&output_damage,
2286 &ec->primary_plane.damage, &output->region);
2287 pixman_region32_subtract(&output_damage,
2288 &output_damage, &ec->primary_plane.clip);
2291 weston_output_update_matrix(output);
2293 r = output->repaint(output, &output_damage);
2295 pixman_region32_fini(&output_damage);
2297 output->repaint_needed = 0;
2299 weston_compositor_repick(ec);
2301 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
2302 wl_callback_send_done(cb->resource, output->frame_time);
2303 wl_resource_destroy(cb->resource);
2306 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
2307 animation->frame_counter++;
2308 animation->frame(animation, output, output->frame_time);
2311 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2317 weston_output_schedule_repaint_reset(struct weston_output *output)
2319 output->repaint_scheduled = 0;
2320 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2324 output_repaint_timer_handler(void *data)
2326 struct weston_output *output = data;
2327 struct weston_compositor *compositor = output->compositor;
2329 if (output->repaint_needed &&
2330 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2331 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2332 weston_output_repaint(output) == 0)
2335 weston_output_schedule_repaint_reset(output);
2341 weston_output_finish_frame(struct weston_output *output,
2342 const struct timespec *stamp,
2343 uint32_t presented_flags)
2345 struct weston_compositor *compositor = output->compositor;
2346 int32_t refresh_nsec;
2347 struct timespec now;
2348 struct timespec gone;
2351 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2352 TLP_VBLANK(stamp), TLP_END);
2354 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
2355 weston_presentation_feedback_present_list(&output->feedback_list,
2356 output, refresh_nsec, stamp,
2360 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
2362 weston_compositor_read_presentation_clock(compositor, &now);
2363 timespec_sub(&gone, &now, stamp);
2364 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2365 msec -= compositor->repaint_msec;
2367 if (msec < -1000 || msec > 1000) {
2371 weston_log("Warning: computed repaint delay is "
2372 "insane: %d msec\n", msec);
2378 /* Called from restart_repaint_loop and restart happens already after
2379 * the deadline given by repaint_msec? In that case we delay until
2380 * the deadline of the next frame, to give clients a more predictable
2381 * timing of the repaint cycle to lock on. */
2382 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
2383 msec += refresh_nsec / 1000000;
2386 output_repaint_timer_handler(output);
2388 wl_event_source_timer_update(output->repaint_timer, msec);
2392 idle_repaint(void *data)
2394 struct weston_output *output = data;
2396 output->start_repaint_loop(output);
2400 weston_layer_entry_insert(struct weston_layer_entry *list,
2401 struct weston_layer_entry *entry)
2403 wl_list_insert(&list->link, &entry->link);
2404 entry->layer = list->layer;
2408 weston_layer_entry_remove(struct weston_layer_entry *entry)
2410 wl_list_remove(&entry->link);
2411 wl_list_init(&entry->link);
2412 entry->layer = NULL;
2416 weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2418 wl_list_init(&layer->view_list.link);
2419 layer->view_list.layer = layer;
2420 weston_layer_set_mask_infinite(layer);
2422 wl_list_insert(below, &layer->link);
2426 weston_layer_set_mask(struct weston_layer *layer,
2427 int x, int y, int width, int height)
2429 struct weston_view *view;
2432 layer->mask.x2 = x + width;
2434 layer->mask.y2 = y + height;
2436 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2437 weston_view_geometry_dirty(view);
2442 weston_layer_set_mask_infinite(struct weston_layer *layer)
2444 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2445 UINT32_MAX, UINT32_MAX);
2449 weston_output_schedule_repaint(struct weston_output *output)
2451 struct weston_compositor *compositor = output->compositor;
2452 struct wl_event_loop *loop;
2454 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2455 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2458 if (!output->repaint_needed)
2459 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2461 loop = wl_display_get_event_loop(compositor->wl_display);
2462 output->repaint_needed = 1;
2463 if (output->repaint_scheduled)
2466 wl_event_loop_add_idle(loop, idle_repaint, output);
2467 output->repaint_scheduled = 1;
2468 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2472 weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2474 struct weston_output *output;
2476 wl_list_for_each(output, &compositor->output_list, link)
2477 weston_output_schedule_repaint(output);
2481 surface_destroy(struct wl_client *client, struct wl_resource *resource)
2483 wl_resource_destroy(resource);
2487 surface_attach(struct wl_client *client,
2488 struct wl_resource *resource,
2489 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2491 struct weston_surface *surface = wl_resource_get_user_data(resource);
2492 struct weston_buffer *buffer = NULL;
2494 if (buffer_resource) {
2495 buffer = weston_buffer_from_resource(buffer_resource);
2496 if (buffer == NULL) {
2497 wl_client_post_no_memory(client);
2502 /* Attach, attach, without commit in between does not send
2503 * wl_buffer.release. */
2504 weston_surface_state_set_buffer(&surface->pending, buffer);
2506 surface->pending.sx = sx;
2507 surface->pending.sy = sy;
2508 surface->pending.newly_attached = 1;
2512 surface_damage(struct wl_client *client,
2513 struct wl_resource *resource,
2514 int32_t x, int32_t y, int32_t width, int32_t height)
2516 struct weston_surface *surface = wl_resource_get_user_data(resource);
2518 if (width <= 0 || height <= 0)
2521 pixman_region32_union_rect(&surface->pending.damage_surface,
2522 &surface->pending.damage_surface,
2523 x, y, width, height);
2527 surface_damage_buffer(struct wl_client *client,
2528 struct wl_resource *resource,
2529 int32_t x, int32_t y, int32_t width, int32_t height)
2531 struct weston_surface *surface = wl_resource_get_user_data(resource);
2533 if (width <= 0 || height <= 0)
2536 pixman_region32_union_rect(&surface->pending.damage_buffer,
2537 &surface->pending.damage_buffer,
2538 x, y, width, height);
2542 destroy_frame_callback(struct wl_resource *resource)
2544 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
2546 wl_list_remove(&cb->link);
2551 surface_frame(struct wl_client *client,
2552 struct wl_resource *resource, uint32_t callback)
2554 struct weston_frame_callback *cb;
2555 struct weston_surface *surface = wl_resource_get_user_data(resource);
2557 cb = malloc(sizeof *cb);
2559 wl_resource_post_no_memory(resource);
2563 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2565 if (cb->resource == NULL) {
2567 wl_resource_post_no_memory(resource);
2571 wl_resource_set_implementation(cb->resource, NULL, cb,
2572 destroy_frame_callback);
2574 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
2578 surface_set_opaque_region(struct wl_client *client,
2579 struct wl_resource *resource,
2580 struct wl_resource *region_resource)
2582 struct weston_surface *surface = wl_resource_get_user_data(resource);
2583 struct weston_region *region;
2585 if (region_resource) {
2586 region = wl_resource_get_user_data(region_resource);
2587 pixman_region32_copy(&surface->pending.opaque,
2590 pixman_region32_clear(&surface->pending.opaque);
2595 surface_set_input_region(struct wl_client *client,
2596 struct wl_resource *resource,
2597 struct wl_resource *region_resource)
2599 struct weston_surface *surface = wl_resource_get_user_data(resource);
2600 struct weston_region *region;
2602 if (region_resource) {
2603 region = wl_resource_get_user_data(region_resource);
2604 pixman_region32_copy(&surface->pending.input,
2607 pixman_region32_fini(&surface->pending.input);
2608 region_init_infinite(&surface->pending.input);
2613 weston_surface_commit_subsurface_order(struct weston_surface *surface)
2615 struct weston_subsurface *sub;
2617 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2618 parent_link_pending) {
2619 wl_list_remove(&sub->parent_link);
2620 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2625 weston_surface_build_buffer_matrix(const struct weston_surface *surface,
2626 struct weston_matrix *matrix)
2628 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2629 double src_width, src_height, dest_width, dest_height;
2631 weston_matrix_init(matrix);
2633 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2634 src_width = surface->width_from_buffer;
2635 src_height = surface->height_from_buffer;
2637 src_width = wl_fixed_to_double(vp->buffer.src_width);
2638 src_height = wl_fixed_to_double(vp->buffer.src_height);
2641 if (vp->surface.width == -1) {
2642 dest_width = src_width;
2643 dest_height = src_height;
2645 dest_width = vp->surface.width;
2646 dest_height = vp->surface.height;
2649 if (src_width != dest_width || src_height != dest_height)
2650 weston_matrix_scale(matrix,
2651 src_width / dest_width,
2652 src_height / dest_height, 1);
2654 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2655 weston_matrix_translate(matrix,
2656 wl_fixed_to_double(vp->buffer.src_x),
2657 wl_fixed_to_double(vp->buffer.src_y),
2660 switch (vp->buffer.transform) {
2661 case WL_OUTPUT_TRANSFORM_FLIPPED:
2662 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2663 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2664 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2665 weston_matrix_scale(matrix, -1, 1, 1);
2666 weston_matrix_translate(matrix,
2667 surface->width_from_buffer, 0, 0);
2671 switch (vp->buffer.transform) {
2673 case WL_OUTPUT_TRANSFORM_NORMAL:
2674 case WL_OUTPUT_TRANSFORM_FLIPPED:
2676 case WL_OUTPUT_TRANSFORM_90:
2677 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2678 weston_matrix_rotate_xy(matrix, 0, 1);
2679 weston_matrix_translate(matrix,
2680 surface->height_from_buffer, 0, 0);
2682 case WL_OUTPUT_TRANSFORM_180:
2683 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2684 weston_matrix_rotate_xy(matrix, -1, 0);
2685 weston_matrix_translate(matrix,
2686 surface->width_from_buffer,
2687 surface->height_from_buffer, 0);
2689 case WL_OUTPUT_TRANSFORM_270:
2690 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2691 weston_matrix_rotate_xy(matrix, 0, -1);
2692 weston_matrix_translate(matrix,
2693 0, surface->width_from_buffer, 0);
2697 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2701 * Compute a + b > c while being safe to overflows.
2704 fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2706 return (int64_t)a + (int64_t)b > (int64_t)c;
2710 weston_surface_is_pending_viewport_source_valid(
2711 const struct weston_surface *surface)
2713 const struct weston_surface_state *pend = &surface->pending;
2714 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2715 int width_from_buffer = 0;
2716 int height_from_buffer = 0;
2720 /* If viewport source rect is not set, it is always ok. */
2721 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2724 if (pend->newly_attached) {
2726 convert_size_by_transform_scale(&width_from_buffer,
2727 &height_from_buffer,
2728 pend->buffer->width,
2729 pend->buffer->height,
2730 vp->buffer.transform,
2733 /* No buffer: viewport is irrelevant. */
2737 width_from_buffer = surface->width_from_buffer;
2738 height_from_buffer = surface->height_from_buffer;
2741 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2742 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2744 /* No buffer: viewport is irrelevant. */
2745 if (width_from_buffer == 0 || height_from_buffer == 0)
2748 /* overflow checks for wl_fixed_from_int() */
2749 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2751 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2754 w = wl_fixed_from_int(width_from_buffer);
2755 h = wl_fixed_from_int(height_from_buffer);
2757 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2759 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2766 fixed_is_integer(wl_fixed_t v)
2768 return (v & 0xff) == 0;
2772 weston_surface_is_pending_viewport_dst_size_int(
2773 const struct weston_surface *surface)
2775 const struct weston_buffer_viewport *vp =
2776 &surface->pending.buffer_viewport;
2778 if (vp->surface.width != -1) {
2779 assert(vp->surface.width > 0 && vp->surface.height > 0);
2783 return fixed_is_integer(vp->buffer.src_width) &&
2784 fixed_is_integer(vp->buffer.src_height);
2787 /* Translate pending damage in buffer co-ordinates to surface
2788 * co-ordinates and union it with a pixman_region32_t.
2789 * This should only be called after the buffer is attached.
2792 apply_damage_buffer(pixman_region32_t *dest,
2793 struct weston_surface *surface,
2794 struct weston_surface_state *state)
2796 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2798 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2799 * translated into surface co-ordinates and unioned with
2800 * any other surface damage.
2801 * None of this makes sense if there is no buffer though.
2803 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2804 pixman_region32_t buffer_damage;
2806 pixman_region32_intersect_rect(&state->damage_buffer,
2807 &state->damage_buffer,
2808 0, 0, buffer->width,
2810 pixman_region32_init(&buffer_damage);
2811 weston_matrix_transform_region(&buffer_damage,
2812 &surface->buffer_to_surface_matrix,
2813 &state->damage_buffer);
2814 pixman_region32_union(dest, dest, &buffer_damage);
2815 pixman_region32_fini(&buffer_damage);
2817 /* We should clear this on commit even if there was no buffer */
2818 pixman_region32_clear(&state->damage_buffer);
2822 weston_surface_commit_state(struct weston_surface *surface,
2823 struct weston_surface_state *state)
2825 struct weston_view *view;
2826 pixman_region32_t opaque;
2828 /* wl_surface.set_buffer_transform */
2829 /* wl_surface.set_buffer_scale */
2830 /* wp_viewport.set_source */
2831 /* wp_viewport.set_destination */
2832 surface->buffer_viewport = state->buffer_viewport;
2834 /* wl_surface.attach */
2835 if (state->newly_attached)
2836 weston_surface_attach(surface, state->buffer);
2837 weston_surface_state_set_buffer(state, NULL);
2839 weston_surface_build_buffer_matrix(surface,
2840 &surface->surface_to_buffer_matrix);
2841 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2842 &surface->surface_to_buffer_matrix);
2844 if (state->newly_attached || state->buffer_viewport.changed) {
2845 weston_surface_update_size(surface);
2846 if (surface->committed)
2847 surface->committed(surface, state->sx, state->sy);
2852 state->newly_attached = 0;
2853 state->buffer_viewport.changed = 0;
2855 /* wl_surface.damage and wl_surface.damage_buffer */
2856 if (weston_timeline_enabled_ &&
2857 (pixman_region32_not_empty(&state->damage_surface) ||
2858 pixman_region32_not_empty(&state->damage_buffer)))
2859 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
2861 pixman_region32_union(&surface->damage, &surface->damage,
2862 &state->damage_surface);
2864 apply_damage_buffer(&surface->damage, surface, state);
2866 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2867 0, 0, surface->width, surface->height);
2868 pixman_region32_clear(&state->damage_surface);
2870 /* wl_surface.set_opaque_region */
2871 pixman_region32_init(&opaque);
2872 pixman_region32_intersect_rect(&opaque, &state->opaque,
2873 0, 0, surface->width, surface->height);
2875 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2876 pixman_region32_copy(&surface->opaque, &opaque);
2877 wl_list_for_each(view, &surface->views, surface_link)
2878 weston_view_geometry_dirty(view);
2881 pixman_region32_fini(&opaque);
2883 /* wl_surface.set_input_region */
2884 pixman_region32_intersect_rect(&surface->input, &state->input,
2885 0, 0, surface->width, surface->height);
2887 /* wl_surface.frame */
2888 wl_list_insert_list(&surface->frame_callback_list,
2889 &state->frame_callback_list);
2890 wl_list_init(&state->frame_callback_list);
2893 * What should happen with a feedback request, if there
2894 * is no wl_buffer attached for this commit?
2897 /* presentation.feedback */
2898 wl_list_insert_list(&surface->feedback_list,
2899 &state->feedback_list);
2900 wl_list_init(&state->feedback_list);
2902 wl_signal_emit(&surface->commit_signal, surface);
2906 weston_surface_commit(struct weston_surface *surface)
2908 weston_surface_commit_state(surface, &surface->pending);
2910 weston_surface_commit_subsurface_order(surface);
2912 weston_surface_schedule_repaint(surface);
2916 weston_subsurface_commit(struct weston_subsurface *sub);
2919 weston_subsurface_parent_commit(struct weston_subsurface *sub,
2920 int parent_is_synchronized);
2923 surface_commit(struct wl_client *client, struct wl_resource *resource)
2925 struct weston_surface *surface = wl_resource_get_user_data(resource);
2926 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2928 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
2929 assert(surface->viewport_resource);
2931 wl_resource_post_error(surface->viewport_resource,
2932 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
2933 "wl_surface@%d has viewport source outside buffer",
2934 wl_resource_get_id(resource));
2938 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
2939 assert(surface->viewport_resource);
2941 wl_resource_post_error(surface->viewport_resource,
2942 WP_VIEWPORT_ERROR_BAD_SIZE,
2943 "wl_surface@%d viewport dst size not integer",
2944 wl_resource_get_id(resource));
2949 weston_subsurface_commit(sub);
2953 weston_surface_commit(surface);
2955 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2956 if (sub->surface != surface)
2957 weston_subsurface_parent_commit(sub, 0);
2962 surface_set_buffer_transform(struct wl_client *client,
2963 struct wl_resource *resource, int transform)
2965 struct weston_surface *surface = wl_resource_get_user_data(resource);
2967 /* if wl_output.transform grows more members this will need to be updated. */
2968 if (transform < 0 ||
2969 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2970 wl_resource_post_error(resource,
2971 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2972 "buffer transform must be a valid transform "
2973 "('%d' specified)", transform);
2977 surface->pending.buffer_viewport.buffer.transform = transform;
2978 surface->pending.buffer_viewport.changed = 1;
2982 surface_set_buffer_scale(struct wl_client *client,
2983 struct wl_resource *resource,
2986 struct weston_surface *surface = wl_resource_get_user_data(resource);
2989 wl_resource_post_error(resource,
2990 WL_SURFACE_ERROR_INVALID_SCALE,
2991 "buffer scale must be at least one "
2992 "('%d' specified)", scale);
2996 surface->pending.buffer_viewport.buffer.scale = scale;
2997 surface->pending.buffer_viewport.changed = 1;
3000 static const struct wl_surface_interface surface_interface = {
3005 surface_set_opaque_region,
3006 surface_set_input_region,
3008 surface_set_buffer_transform,
3009 surface_set_buffer_scale,
3010 surface_damage_buffer
3014 compositor_create_surface(struct wl_client *client,
3015 struct wl_resource *resource, uint32_t id)
3017 struct weston_compositor *ec = wl_resource_get_user_data(resource);
3018 struct weston_surface *surface;
3020 surface = weston_surface_create(ec);
3021 if (surface == NULL) {
3022 wl_resource_post_no_memory(resource);
3027 wl_resource_create(client, &wl_surface_interface,
3028 wl_resource_get_version(resource), id);
3029 if (surface->resource == NULL) {
3030 weston_surface_destroy(surface);
3031 wl_resource_post_no_memory(resource);
3034 wl_resource_set_implementation(surface->resource, &surface_interface,
3035 surface, destroy_surface);
3037 wl_signal_emit(&ec->create_surface_signal, surface);
3041 destroy_region(struct wl_resource *resource)
3043 struct weston_region *region = wl_resource_get_user_data(resource);
3045 pixman_region32_fini(®ion->region);
3050 region_destroy(struct wl_client *client, struct wl_resource *resource)
3052 wl_resource_destroy(resource);
3056 region_add(struct wl_client *client, struct wl_resource *resource,
3057 int32_t x, int32_t y, int32_t width, int32_t height)
3059 struct weston_region *region = wl_resource_get_user_data(resource);
3061 pixman_region32_union_rect(®ion->region, ®ion->region,
3062 x, y, width, height);
3066 region_subtract(struct wl_client *client, struct wl_resource *resource,
3067 int32_t x, int32_t y, int32_t width, int32_t height)
3069 struct weston_region *region = wl_resource_get_user_data(resource);
3070 pixman_region32_t rect;
3072 pixman_region32_init_rect(&rect, x, y, width, height);
3073 pixman_region32_subtract(®ion->region, ®ion->region, &rect);
3074 pixman_region32_fini(&rect);
3077 static const struct wl_region_interface region_interface = {
3084 compositor_create_region(struct wl_client *client,
3085 struct wl_resource *resource, uint32_t id)
3087 struct weston_region *region;
3089 region = malloc(sizeof *region);
3090 if (region == NULL) {
3091 wl_resource_post_no_memory(resource);
3095 pixman_region32_init(®ion->region);
3098 wl_resource_create(client, &wl_region_interface, 1, id);
3099 if (region->resource == NULL) {
3101 wl_resource_post_no_memory(resource);
3104 wl_resource_set_implementation(region->resource, ®ion_interface,
3105 region, destroy_region);
3108 static const struct wl_compositor_interface compositor_interface = {
3109 compositor_create_surface,
3110 compositor_create_region
3114 weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3116 struct weston_surface *surface = sub->surface;
3118 weston_surface_commit_state(surface, &sub->cached);
3119 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
3121 weston_surface_commit_subsurface_order(surface);
3123 weston_surface_schedule_repaint(surface);
3125 sub->has_cached_data = 0;
3129 weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3131 struct weston_surface *surface = sub->surface;
3134 * If this commit would cause the surface to move by the
3135 * attach(dx, dy) parameters, the old damage region must be
3136 * translated to correspond to the new surface coordinate system
3139 pixman_region32_translate(&sub->cached.damage_surface,
3140 -surface->pending.sx, -surface->pending.sy);
3141 pixman_region32_union(&sub->cached.damage_surface,
3142 &sub->cached.damage_surface,
3143 &surface->pending.damage_surface);
3144 pixman_region32_clear(&surface->pending.damage_surface);
3146 if (surface->pending.newly_attached) {
3147 sub->cached.newly_attached = 1;
3148 weston_surface_state_set_buffer(&sub->cached,
3149 surface->pending.buffer);
3150 weston_buffer_reference(&sub->cached_buffer_ref,
3151 surface->pending.buffer);
3152 weston_presentation_feedback_discard_list(
3153 &sub->cached.feedback_list);
3155 sub->cached.sx += surface->pending.sx;
3156 sub->cached.sy += surface->pending.sy;
3158 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3160 sub->cached.buffer_viewport.changed |=
3161 surface->pending.buffer_viewport.changed;
3162 sub->cached.buffer_viewport.buffer =
3163 surface->pending.buffer_viewport.buffer;
3164 sub->cached.buffer_viewport.surface =
3165 surface->pending.buffer_viewport.surface;
3167 weston_surface_reset_pending_buffer(surface);
3169 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3171 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3173 wl_list_insert_list(&sub->cached.frame_callback_list,
3174 &surface->pending.frame_callback_list);
3175 wl_list_init(&surface->pending.frame_callback_list);
3177 wl_list_insert_list(&sub->cached.feedback_list,
3178 &surface->pending.feedback_list);
3179 wl_list_init(&surface->pending.feedback_list);
3181 sub->has_cached_data = 1;
3185 weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3188 if (sub->synchronized)
3194 sub = weston_surface_to_subsurface(sub->parent);
3201 weston_subsurface_commit(struct weston_subsurface *sub)
3203 struct weston_surface *surface = sub->surface;
3204 struct weston_subsurface *tmp;
3206 /* Recursive check for effectively synchronized. */
3207 if (weston_subsurface_is_synchronized(sub)) {
3208 weston_subsurface_commit_to_cache(sub);
3210 if (sub->has_cached_data) {
3211 /* flush accumulated state from cache */
3212 weston_subsurface_commit_to_cache(sub);
3213 weston_subsurface_commit_from_cache(sub);
3215 weston_surface_commit(surface);
3218 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3219 if (tmp->surface != surface)
3220 weston_subsurface_parent_commit(tmp, 0);
3226 weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
3228 struct weston_surface *surface = sub->surface;
3229 struct weston_subsurface *tmp;
3231 /* From now on, commit_from_cache the whole sub-tree, regardless of
3232 * the synchronized mode of each child. This sub-surface or some
3233 * of its ancestors were synchronized, so we are synchronized
3237 if (sub->has_cached_data)
3238 weston_subsurface_commit_from_cache(sub);
3240 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3241 if (tmp->surface != surface)
3242 weston_subsurface_parent_commit(tmp, 1);
3247 weston_subsurface_parent_commit(struct weston_subsurface *sub,
3248 int parent_is_synchronized)
3250 struct weston_view *view;
3251 if (sub->position.set) {
3252 wl_list_for_each(view, &sub->surface->views, surface_link)
3253 weston_view_set_position(view,
3257 sub->position.set = 0;
3260 if (parent_is_synchronized || sub->synchronized)
3261 weston_subsurface_synchronized_commit(sub);
3265 subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3267 return snprintf(buf, len, "sub-surface");
3271 subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
3273 struct weston_view *view;
3275 wl_list_for_each(view, &surface->views, surface_link)
3276 weston_view_set_position(view,
3277 view->geometry.x + dx,
3278 view->geometry.y + dy);
3280 /* No need to check parent mappedness, because if parent is not
3281 * mapped, parent is not in a visible layer, so this sub-surface
3282 * will not be drawn either.
3285 if (!weston_surface_is_mapped(surface)) {
3286 surface->is_mapped = true;
3288 /* Cannot call weston_view_update_transform(),
3289 * because that would call it also for the parent surface,
3290 * which might not be mapped yet. That would lead to
3291 * inconsistent state, where the window could never be
3294 * Instead just force the is_mapped flag on, to make
3295 * weston_surface_is_mapped() return true, so that when the
3296 * parent surface does get mapped, this one will get
3297 * included, too. See view_list_add().
3302 static struct weston_subsurface *
3303 weston_surface_to_subsurface(struct weston_surface *surface)
3305 if (surface->committed == subsurface_committed)
3306 return surface->committed_private;
3311 WL_EXPORT struct weston_surface *
3312 weston_surface_get_main_surface(struct weston_surface *surface)
3314 struct weston_subsurface *sub;
3316 while (surface && (sub = weston_surface_to_subsurface(surface)))
3317 surface = sub->parent;
3323 weston_surface_set_role(struct weston_surface *surface,
3324 const char *role_name,
3325 struct wl_resource *error_resource,
3326 uint32_t error_code)
3330 if (surface->role_name == NULL ||
3331 surface->role_name == role_name ||
3332 strcmp(surface->role_name, role_name) == 0) {
3333 surface->role_name = role_name;
3338 wl_resource_post_error(error_resource, error_code,
3339 "Cannot assign role %s to wl_surface@%d,"
3340 " already has role %s\n",
3342 wl_resource_get_id(surface->resource),
3343 surface->role_name);
3348 weston_surface_set_label_func(struct weston_surface *surface,
3349 int (*desc)(struct weston_surface *,
3352 surface->get_label = desc;
3353 surface->timeline.force_refresh = 1;
3356 /** Get the size of surface contents
3358 * \param surface The surface to query.
3359 * \param width Returns the width of raw contents.
3360 * \param height Returns the height of raw contents.
3362 * Retrieves the raw surface content size in pixels for the given surface.
3363 * This is the whole content size in buffer pixels. If the surface
3364 * has no content or the renderer does not implement this feature,
3365 * zeroes are returned.
3367 * This function is used to determine the buffer size needed for
3368 * a weston_surface_copy_content() call.
3371 weston_surface_get_content_size(struct weston_surface *surface,
3372 int *width, int *height)
3374 struct weston_renderer *rer = surface->compositor->renderer;
3376 if (!rer->surface_get_content_size) {
3382 rer->surface_get_content_size(surface, width, height);
3385 /** Copy surface contents to system memory.
3387 * \param surface The surface to copy from.
3388 * \param target Pointer to the target memory buffer.
3389 * \param size Size of the target buffer in bytes.
3390 * \param src_x X location on contents to copy from.
3391 * \param src_y Y location on contents to copy from.
3392 * \param width Width in pixels of the area to copy.
3393 * \param height Height in pixels of the area to copy.
3394 * \return 0 for success, -1 for failure.
3396 * Surface contents are maintained by the renderer. They can be in a
3397 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3400 * Surface contents are copied into memory pointed to by target,
3401 * which has size bytes of space available. The target memory
3402 * may be larger than needed, but being smaller returns an error.
3403 * The extra bytes in target may or may not be written; their content is
3404 * unspecified. Size must be large enough to hold the image.
3406 * The image in the target memory will be arranged in rows from
3407 * top to bottom, and pixels on a row from left to right. The pixel
3408 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3411 * Parameters src_x and src_y define the upper-left corner in buffer
3412 * coordinates (pixels) to copy from. Parameters width and height
3413 * define the size of the area to copy in pixels.
3415 * The rectangle defined by src_x, src_y, width, height must fit in
3416 * the surface contents. Otherwise an error is returned.
3418 * Use surface_get_data_size to determine the content size; the
3419 * needed target buffer size and rectangle limits.
3421 * CURRENT IMPLEMENTATION RESTRICTIONS:
3422 * - the machine must be little-endian due to Pixman formats.
3424 * NOTE: Pixman formats are premultiplied.
3427 weston_surface_copy_content(struct weston_surface *surface,
3428 void *target, size_t size,
3429 int src_x, int src_y,
3430 int width, int height)
3432 struct weston_renderer *rer = surface->compositor->renderer;
3434 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3436 if (!rer->surface_copy_content)
3439 weston_surface_get_content_size(surface, &cw, &ch);
3441 if (src_x < 0 || src_y < 0)
3444 if (width <= 0 || height <= 0)
3447 if (src_x + width > cw || src_y + height > ch)
3450 if (width * bytespp * height > size)
3453 return rer->surface_copy_content(surface, target, size,
3454 src_x, src_y, width, height);
3458 subsurface_set_position(struct wl_client *client,
3459 struct wl_resource *resource, int32_t x, int32_t y)
3461 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3466 sub->position.x = x;
3467 sub->position.y = y;
3468 sub->position.set = 1;
3471 static struct weston_subsurface *
3472 subsurface_from_surface(struct weston_surface *surface)
3474 struct weston_subsurface *sub;
3476 sub = weston_surface_to_subsurface(surface);
3480 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3481 if (sub->surface == surface)
3487 static struct weston_subsurface *
3488 subsurface_sibling_check(struct weston_subsurface *sub,
3489 struct weston_surface *surface,
3490 const char *request)
3492 struct weston_subsurface *sibling;
3494 sibling = subsurface_from_surface(surface);
3497 wl_resource_post_error(sub->resource,
3498 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3499 "%s: wl_surface@%d is not a parent or sibling",
3500 request, wl_resource_get_id(surface->resource));
3504 if (sibling->parent != sub->parent) {
3505 wl_resource_post_error(sub->resource,
3506 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3507 "%s: wl_surface@%d has a different parent",
3508 request, wl_resource_get_id(surface->resource));
3516 subsurface_place_above(struct wl_client *client,
3517 struct wl_resource *resource,
3518 struct wl_resource *sibling_resource)
3520 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3521 struct weston_surface *surface =
3522 wl_resource_get_user_data(sibling_resource);
3523 struct weston_subsurface *sibling;
3528 sibling = subsurface_sibling_check(sub, surface, "place_above");
3532 wl_list_remove(&sub->parent_link_pending);
3533 wl_list_insert(sibling->parent_link_pending.prev,
3534 &sub->parent_link_pending);
3538 subsurface_place_below(struct wl_client *client,
3539 struct wl_resource *resource,
3540 struct wl_resource *sibling_resource)
3542 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3543 struct weston_surface *surface =
3544 wl_resource_get_user_data(sibling_resource);
3545 struct weston_subsurface *sibling;
3550 sibling = subsurface_sibling_check(sub, surface, "place_below");
3554 wl_list_remove(&sub->parent_link_pending);
3555 wl_list_insert(&sibling->parent_link_pending,
3556 &sub->parent_link_pending);
3560 subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3562 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3565 sub->synchronized = 1;
3569 subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3571 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3573 if (sub && sub->synchronized) {
3574 sub->synchronized = 0;
3576 /* If sub became effectively desynchronized, flush. */
3577 if (!weston_subsurface_is_synchronized(sub))
3578 weston_subsurface_synchronized_commit(sub);
3583 weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3585 wl_list_remove(&sub->parent_link);
3586 wl_list_remove(&sub->parent_link_pending);
3587 wl_list_remove(&sub->parent_destroy_listener.link);
3592 weston_subsurface_destroy(struct weston_subsurface *sub);
3595 subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3597 struct weston_subsurface *sub =
3598 container_of(listener, struct weston_subsurface,
3599 surface_destroy_listener);
3600 assert(data == sub->surface);
3602 /* The protocol object (wl_resource) is left inert. */
3604 wl_resource_set_user_data(sub->resource, NULL);
3606 weston_subsurface_destroy(sub);
3610 subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3612 struct weston_subsurface *sub =
3613 container_of(listener, struct weston_subsurface,
3614 parent_destroy_listener);
3615 assert(data == sub->parent);
3616 assert(sub->surface != sub->parent);
3618 if (weston_surface_is_mapped(sub->surface))
3619 weston_surface_unmap(sub->surface);
3621 weston_subsurface_unlink_parent(sub);
3625 subsurface_resource_destroy(struct wl_resource *resource)
3627 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
3630 weston_subsurface_destroy(sub);
3634 subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3636 wl_resource_destroy(resource);
3640 weston_subsurface_link_parent(struct weston_subsurface *sub,
3641 struct weston_surface *parent)
3643 sub->parent = parent;
3644 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
3645 wl_signal_add(&parent->destroy_signal,
3646 &sub->parent_destroy_listener);
3648 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3649 wl_list_insert(&parent->subsurface_list_pending,
3650 &sub->parent_link_pending);
3654 weston_subsurface_link_surface(struct weston_subsurface *sub,
3655 struct weston_surface *surface)
3657 sub->surface = surface;
3658 sub->surface_destroy_listener.notify =
3659 subsurface_handle_surface_destroy;
3660 wl_signal_add(&surface->destroy_signal,
3661 &sub->surface_destroy_listener);
3665 weston_subsurface_destroy(struct weston_subsurface *sub)
3667 struct weston_view *view, *next;
3669 assert(sub->surface);
3671 if (sub->resource) {
3672 assert(weston_surface_to_subsurface(sub->surface) == sub);
3673 assert(sub->parent_destroy_listener.notify ==
3674 subsurface_handle_parent_destroy);
3676 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3677 weston_view_unmap(view);
3678 weston_view_destroy(view);
3682 weston_subsurface_unlink_parent(sub);
3684 weston_surface_state_fini(&sub->cached);
3685 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
3687 sub->surface->committed = NULL;
3688 sub->surface->committed_private = NULL;
3689 weston_surface_set_label_func(sub->surface, NULL);
3691 /* the dummy weston_subsurface for the parent itself */
3692 assert(sub->parent_destroy_listener.notify == NULL);
3693 wl_list_remove(&sub->parent_link);
3694 wl_list_remove(&sub->parent_link_pending);
3697 wl_list_remove(&sub->surface_destroy_listener.link);
3701 static const struct wl_subsurface_interface subsurface_implementation = {
3703 subsurface_set_position,
3704 subsurface_place_above,
3705 subsurface_place_below,
3706 subsurface_set_sync,
3707 subsurface_set_desync
3710 static struct weston_subsurface *
3711 weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3712 struct weston_surface *parent)
3714 struct weston_subsurface *sub;
3715 struct wl_client *client = wl_resource_get_client(surface->resource);
3717 sub = zalloc(sizeof *sub);
3721 wl_list_init(&sub->unused_views);
3724 wl_resource_create(client, &wl_subsurface_interface, 1, id);
3725 if (!sub->resource) {
3730 wl_resource_set_implementation(sub->resource,
3731 &subsurface_implementation,
3732 sub, subsurface_resource_destroy);
3733 weston_subsurface_link_surface(sub, surface);
3734 weston_subsurface_link_parent(sub, parent);
3735 weston_surface_state_init(&sub->cached);
3736 sub->cached_buffer_ref.buffer = NULL;
3737 sub->synchronized = 1;
3742 /* Create a dummy subsurface for having the parent itself in its
3743 * sub-surface lists. Makes stacking order manipulation easy.
3745 static struct weston_subsurface *
3746 weston_subsurface_create_for_parent(struct weston_surface *parent)
3748 struct weston_subsurface *sub;
3750 sub = zalloc(sizeof *sub);
3754 weston_subsurface_link_surface(sub, parent);
3755 sub->parent = parent;
3756 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3757 wl_list_insert(&parent->subsurface_list_pending,
3758 &sub->parent_link_pending);
3764 subcompositor_get_subsurface(struct wl_client *client,
3765 struct wl_resource *resource,
3767 struct wl_resource *surface_resource,
3768 struct wl_resource *parent_resource)
3770 struct weston_surface *surface =
3771 wl_resource_get_user_data(surface_resource);
3772 struct weston_surface *parent =
3773 wl_resource_get_user_data(parent_resource);
3774 struct weston_subsurface *sub;
3775 static const char where[] = "get_subsurface: wl_subsurface@";
3777 if (surface == parent) {
3778 wl_resource_post_error(resource,
3779 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3780 "%s%d: wl_surface@%d cannot be its own parent",
3781 where, id, wl_resource_get_id(surface_resource));
3785 if (weston_surface_to_subsurface(surface)) {
3786 wl_resource_post_error(resource,
3787 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3788 "%s%d: wl_surface@%d is already a sub-surface",
3789 where, id, wl_resource_get_id(surface_resource));
3793 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3794 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
3797 if (weston_surface_get_main_surface(parent) == surface) {
3798 wl_resource_post_error(resource,
3799 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3800 "%s%d: wl_surface@%d is an ancestor of parent",
3801 where, id, wl_resource_get_id(surface_resource));
3805 /* make sure the parent is in its own list */
3806 if (wl_list_empty(&parent->subsurface_list)) {
3807 if (!weston_subsurface_create_for_parent(parent)) {
3808 wl_resource_post_no_memory(resource);
3813 sub = weston_subsurface_create(id, surface, parent);
3815 wl_resource_post_no_memory(resource);
3819 surface->committed = subsurface_committed;
3820 surface->committed_private = sub;
3821 weston_surface_set_label_func(surface, subsurface_get_label);
3825 subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3827 wl_resource_destroy(resource);
3830 static const struct wl_subcompositor_interface subcompositor_interface = {
3831 subcompositor_destroy,
3832 subcompositor_get_subsurface
3836 bind_subcompositor(struct wl_client *client,
3837 void *data, uint32_t version, uint32_t id)
3839 struct weston_compositor *compositor = data;
3840 struct wl_resource *resource;
3843 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
3844 if (resource == NULL) {
3845 wl_client_post_no_memory(client);
3848 wl_resource_set_implementation(resource, &subcompositor_interface,
3852 /** Set a DPMS mode on all of the compositor's outputs
3854 * \param compositor The compositor instance
3855 * \param state The DPMS state the outputs will be set to
3858 weston_compositor_dpms(struct weston_compositor *compositor,
3859 enum dpms_enum state)
3861 struct weston_output *output;
3863 wl_list_for_each(output, &compositor->output_list, link)
3864 if (output->set_dpms)
3865 output->set_dpms(output, state);
3868 /** Restores the compositor to active status
3870 * \param compositor The compositor instance
3872 * If the compositor was in a sleeping mode, all outputs are powered
3873 * back on via DPMS. Otherwise if the compositor was inactive
3874 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3877 * Restarts the idle timer.
3880 weston_compositor_wake(struct weston_compositor *compositor)
3882 uint32_t old_state = compositor->state;
3884 /* The state needs to be changed before emitting the wake
3885 * signal because that may try to schedule a repaint which
3886 * will not work if the compositor is still sleeping */
3887 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3889 switch (old_state) {
3890 case WESTON_COMPOSITOR_SLEEPING:
3891 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3893 case WESTON_COMPOSITOR_IDLE:
3894 case WESTON_COMPOSITOR_OFFSCREEN:
3895 wl_signal_emit(&compositor->wake_signal, compositor);
3898 wl_event_source_timer_update(compositor->idle_source,
3899 compositor->idle_time * 1000);
3903 /** Turns off rendering and frame events for the compositor.
3905 * \param compositor The compositor instance
3907 * This is used for example to prevent further rendering while the
3908 * compositor is shutting down.
3910 * \note When offscreen state is entered, outputs will be powered
3911 * back on if they were sleeping (in DPMS off mode), even though
3912 * no rendering will be performed.
3914 * Stops the idle timer.
3917 weston_compositor_offscreen(struct weston_compositor *compositor)
3919 switch (compositor->state) {
3920 case WESTON_COMPOSITOR_OFFSCREEN:
3922 case WESTON_COMPOSITOR_SLEEPING:
3923 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3926 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3927 wl_event_source_timer_update(compositor->idle_source, 0);
3931 /** Powers down all attached output devices
3933 * \param compositor The compositor instance
3935 * Causes rendering to the outputs to cease, and no frame events to be
3936 * sent. Only powers down the outputs if the compositor is not already
3939 * Stops the idle timer.
3942 weston_compositor_sleep(struct weston_compositor *compositor)
3944 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3947 wl_event_source_timer_update(compositor->idle_source, 0);
3948 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3949 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3952 /** Sets compositor to idle mode
3954 * \param data The compositor instance
3956 * This is called when the idle timer fires. Once the compositor is in
3957 * idle mode it requires a wake action (e.g. via
3958 * weston_compositor_wake()) to restore it. The compositor's
3959 * idle_signal will be triggered when the idle event occurs.
3961 * Idleness can be inhibited by setting the compositor's idle_inhibit
3965 idle_handler(void *data)
3967 struct weston_compositor *compositor = data;
3969 if (compositor->idle_inhibit)
3972 compositor->state = WESTON_COMPOSITOR_IDLE;
3973 wl_signal_emit(&compositor->idle_signal, compositor);
3979 weston_plane_init(struct weston_plane *plane,
3980 struct weston_compositor *ec,
3981 int32_t x, int32_t y)
3983 pixman_region32_init(&plane->damage);
3984 pixman_region32_init(&plane->clip);
3987 plane->compositor = ec;
3989 /* Init the link so that the call to wl_list_remove() when releasing
3990 * the plane without ever stacking doesn't lead to a crash */
3991 wl_list_init(&plane->link);
3995 weston_plane_release(struct weston_plane *plane)
3997 struct weston_view *view;
3999 pixman_region32_fini(&plane->damage);
4000 pixman_region32_fini(&plane->clip);
4002 wl_list_for_each(view, &plane->compositor->view_list, link) {
4003 if (view->plane == plane)
4007 wl_list_remove(&plane->link);
4011 weston_compositor_stack_plane(struct weston_compositor *ec,
4012 struct weston_plane *plane,
4013 struct weston_plane *above)
4016 wl_list_insert(above->link.prev, &plane->link);
4018 wl_list_insert(&ec->plane_list, &plane->link);
4022 output_release(struct wl_client *client, struct wl_resource *resource)
4024 wl_resource_destroy(resource);
4027 static const struct wl_output_interface output_interface = {
4032 static void unbind_resource(struct wl_resource *resource)
4034 wl_list_remove(wl_resource_get_link(resource));
4038 bind_output(struct wl_client *client,
4039 void *data, uint32_t version, uint32_t id)
4041 struct weston_output *output = data;
4042 struct weston_mode *mode;
4043 struct wl_resource *resource;
4045 resource = wl_resource_create(client, &wl_output_interface,
4047 if (resource == NULL) {
4048 wl_client_post_no_memory(client);
4052 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
4053 wl_resource_set_implementation(resource, &output_interface, data, unbind_resource);
4055 wl_output_send_geometry(resource,
4061 output->make, output->model,
4063 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
4064 wl_output_send_scale(resource,
4065 output->current_scale);
4067 wl_list_for_each (mode, &output->mode_list, link) {
4068 wl_output_send_mode(resource,
4075 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
4076 wl_output_send_done(resource);
4079 /* Move other outputs when one is resized so the space remains contiguous. */
4081 weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4082 struct weston_output *resized_output, int delta_width)
4084 struct weston_output *output;
4085 bool start_resizing = false;
4090 wl_list_for_each(output, &compositor->output_list, link) {
4091 if (output == resized_output) {
4092 start_resizing = true;
4096 if (start_resizing) {
4097 weston_output_move(output, output->x + delta_width, output->y);
4104 weston_output_destroy(struct weston_output *output)
4106 struct wl_resource *resource;
4107 struct weston_view *view;
4109 output->destroying = 1;
4111 wl_list_for_each(view, &output->compositor->view_list, link) {
4112 if (view->output_mask & (1u << output->id))
4113 weston_view_assign_output(view);
4116 wl_event_source_remove(output->repaint_timer);
4118 weston_presentation_feedback_discard_list(&output->feedback_list);
4120 weston_compositor_reflow_outputs(output->compositor, output, output->width);
4121 wl_list_remove(&output->link);
4123 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
4124 wl_signal_emit(&output->destroy_signal, output);
4127 pixman_region32_fini(&output->region);
4128 pixman_region32_fini(&output->previous_damage);
4129 output->compositor->output_id_pool &= ~(1u << output->id);
4131 wl_resource_for_each(resource, &output->resource_list) {
4132 wl_resource_set_destructor(resource, NULL);
4135 wl_global_destroy(output->global);
4139 weston_output_update_matrix(struct weston_output *output)
4141 float magnification;
4143 weston_matrix_init(&output->matrix);
4144 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
4146 if (output->zoom.active) {
4147 magnification = 1 / (1 - output->zoom.spring_z.current);
4148 weston_output_update_zoom(output);
4149 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
4150 -output->zoom.trans_y, 0);
4151 weston_matrix_scale(&output->matrix, magnification,
4152 magnification, 1.0);
4155 switch (output->transform) {
4156 case WL_OUTPUT_TRANSFORM_FLIPPED:
4157 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4158 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4159 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4160 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4161 weston_matrix_scale(&output->matrix, -1, 1, 1);
4165 switch (output->transform) {
4167 case WL_OUTPUT_TRANSFORM_NORMAL:
4168 case WL_OUTPUT_TRANSFORM_FLIPPED:
4170 case WL_OUTPUT_TRANSFORM_90:
4171 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4172 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4173 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4175 case WL_OUTPUT_TRANSFORM_180:
4176 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4177 weston_matrix_translate(&output->matrix,
4178 -output->width, -output->height, 0);
4179 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4181 case WL_OUTPUT_TRANSFORM_270:
4182 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4183 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4184 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4188 if (output->current_scale != 1)
4189 weston_matrix_scale(&output->matrix,
4190 output->current_scale,
4191 output->current_scale, 1);
4195 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
4199 weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
4201 output->transform = transform;
4202 output->native_scale = scale;
4203 output->current_scale = scale;
4205 convert_size_by_transform_scale(&output->width, &output->height,
4206 output->current_mode->width,
4207 output->current_mode->height,
4212 weston_output_init_geometry(struct weston_output *output, int x, int y)
4217 pixman_region32_init(&output->previous_damage);
4218 pixman_region32_init_rect(&output->region, x, y,
4224 weston_output_move(struct weston_output *output, int x, int y)
4226 struct wl_resource *resource;
4228 output->move_x = x - output->x;
4229 output->move_y = y - output->y;
4231 if (output->move_x == 0 && output->move_y == 0)
4234 weston_output_init_geometry(output, x, y);
4238 /* Move views on this output. */
4239 wl_signal_emit(&output->compositor->output_moved_signal, output);
4241 /* Notify clients of the change for output position. */
4242 wl_resource_for_each(resource, &output->resource_list) {
4243 wl_output_send_geometry(resource,
4253 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
4254 wl_output_send_done(resource);
4258 /** Initialize a weston_output object's parameters
4260 * \param output The weston_output object to initialize
4261 * \param c The output's compositor
4262 * \param x x coordinate for the output in global coordinate space
4263 * \param y y coordinate for the output in global coordinate space
4264 * \param mm_width Physical width of the output as reported by the backend
4265 * \param mm_height Physical height of the output as reported by the backend
4266 * \param transform Rotation of the output
4267 * \param scale Native scaling factor for the output
4269 * Sets up the transformation, zoom, and geometry of the output using
4270 * the input properties.
4272 * Establishes a repaint timer for the output with the relevant display
4273 * object's event loop. See output_repaint_timer_handler().
4275 * The output is assigned an ID. Weston can support up to 32 distinct
4276 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4277 * is referred to and used to find the first available ID number, and
4278 * then this ID is marked as used in output_id_pool.
4280 * The output is also assigned a Wayland global with the wl_output
4281 * external interface.
4284 weston_output_init(struct weston_output *output, struct weston_compositor *c,
4285 int x, int y, int mm_width, int mm_height, uint32_t transform,
4288 struct wl_event_loop *loop;
4290 /* Verify we haven't reached the limit of 32 available output IDs */
4291 assert(ffs(~c->output_id_pool) > 0);
4293 output->compositor = c;
4296 output->mm_width = mm_width;
4297 output->mm_height = mm_height;
4299 output->original_scale = scale;
4301 weston_output_transform_scale_init(output, transform, scale);
4302 weston_output_init_zoom(output);
4304 weston_output_init_geometry(output, x, y);
4305 weston_output_damage(output);
4307 wl_signal_init(&output->frame_signal);
4308 wl_signal_init(&output->destroy_signal);
4309 wl_list_init(&output->animation_list);
4310 wl_list_init(&output->resource_list);
4311 wl_list_init(&output->feedback_list);
4312 wl_list_init(&output->link);
4314 loop = wl_display_get_event_loop(c->wl_display);
4315 output->repaint_timer = wl_event_loop_add_timer(loop,
4316 output_repaint_timer_handler, output);
4318 /* Invert the output id pool and look for the lowest numbered
4319 * switch (the least significant bit). Take that bit's position
4320 * as our ID, and mark it used in the compositor's output_id_pool.
4322 output->id = ffs(~output->compositor->output_id_pool) - 1;
4323 output->compositor->output_id_pool |= 1u << output->id;
4326 wl_global_create(c->wl_display, &wl_output_interface, 3,
4327 output, bind_output);
4330 /** Adds an output to the compositor's output list and
4331 * send the compositor's output_created signal.
4333 * \param compositor The compositor instance.
4334 * \param output The output to be added.
4337 weston_compositor_add_output(struct weston_compositor *compositor,
4338 struct weston_output *output)
4340 struct weston_view *view, *next;
4342 wl_list_insert(compositor->output_list.prev, &output->link);
4343 wl_signal_emit(&compositor->output_created_signal, output);
4345 wl_list_for_each_safe(view, next, &compositor->view_list, link)
4346 weston_view_geometry_dirty(view);
4350 weston_output_transform_coordinate(struct weston_output *output,
4351 double device_x, double device_y,
4352 double *x, double *y)
4354 struct weston_vector p = { {
4360 weston_matrix_transform(&output->inverse_matrix, &p);
4362 *x = p.f[0] / p.f[3];
4363 *y = p.f[1] / p.f[3];
4367 destroy_viewport(struct wl_resource *resource)
4369 struct weston_surface *surface =
4370 wl_resource_get_user_data(resource);
4375 surface->viewport_resource = NULL;
4376 surface->pending.buffer_viewport.buffer.src_width =
4377 wl_fixed_from_int(-1);
4378 surface->pending.buffer_viewport.surface.width = -1;
4379 surface->pending.buffer_viewport.changed = 1;
4383 viewport_destroy(struct wl_client *client,
4384 struct wl_resource *resource)
4386 wl_resource_destroy(resource);
4390 viewport_set_source(struct wl_client *client,
4391 struct wl_resource *resource,
4394 wl_fixed_t src_width,
4395 wl_fixed_t src_height)
4397 struct weston_surface *surface =
4398 wl_resource_get_user_data(resource);
4401 wl_resource_post_error(resource,
4402 WP_VIEWPORT_ERROR_NO_SURFACE,
4403 "wl_surface for this viewport is no longer exists");
4407 assert(surface->viewport_resource == resource);
4408 assert(surface->resource);
4410 if (src_width == wl_fixed_from_int(-1) &&
4411 src_height == wl_fixed_from_int(-1) &&
4412 src_x == wl_fixed_from_int(-1) &&
4413 src_y == wl_fixed_from_int(-1)) {
4414 /* unset source rect */
4415 surface->pending.buffer_viewport.buffer.src_width =
4416 wl_fixed_from_int(-1);
4417 surface->pending.buffer_viewport.changed = 1;
4421 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
4422 wl_resource_post_error(resource,
4423 WP_VIEWPORT_ERROR_BAD_VALUE,
4424 "wl_surface@%d viewport source "
4425 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4426 wl_resource_get_id(surface->resource),
4427 wl_fixed_to_double(src_width),
4428 wl_fixed_to_double(src_height),
4429 wl_fixed_to_double(src_x),
4430 wl_fixed_to_double(src_y));
4434 surface->pending.buffer_viewport.buffer.src_x = src_x;
4435 surface->pending.buffer_viewport.buffer.src_y = src_y;
4436 surface->pending.buffer_viewport.buffer.src_width = src_width;
4437 surface->pending.buffer_viewport.buffer.src_height = src_height;
4438 surface->pending.buffer_viewport.changed = 1;
4442 viewport_set_destination(struct wl_client *client,
4443 struct wl_resource *resource,
4447 struct weston_surface *surface =
4448 wl_resource_get_user_data(resource);
4451 wl_resource_post_error(resource,
4452 WP_VIEWPORT_ERROR_NO_SURFACE,
4453 "wl_surface for this viewport no longer exists");
4457 assert(surface->viewport_resource == resource);
4459 if (dst_width == -1 && dst_height == -1) {
4460 /* unset destination size */
4461 surface->pending.buffer_viewport.surface.width = -1;
4462 surface->pending.buffer_viewport.changed = 1;
4466 if (dst_width <= 0 || dst_height <= 0) {
4467 wl_resource_post_error(resource,
4468 WP_VIEWPORT_ERROR_BAD_VALUE,
4469 "destination size must be positive (%dx%d)",
4470 dst_width, dst_height);
4474 surface->pending.buffer_viewport.surface.width = dst_width;
4475 surface->pending.buffer_viewport.surface.height = dst_height;
4476 surface->pending.buffer_viewport.changed = 1;
4479 static const struct wp_viewport_interface viewport_interface = {
4481 viewport_set_source,
4482 viewport_set_destination
4486 viewporter_destroy(struct wl_client *client,
4487 struct wl_resource *resource)
4489 wl_resource_destroy(resource);
4493 viewporter_get_viewport(struct wl_client *client,
4494 struct wl_resource *viewporter,
4496 struct wl_resource *surface_resource)
4498 int version = wl_resource_get_version(viewporter);
4499 struct weston_surface *surface =
4500 wl_resource_get_user_data(surface_resource);
4501 struct wl_resource *resource;
4503 if (surface->viewport_resource) {
4504 wl_resource_post_error(viewporter,
4505 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
4506 "a viewport for that surface already exists");
4510 resource = wl_resource_create(client, &wp_viewport_interface,
4512 if (resource == NULL) {
4513 wl_client_post_no_memory(client);
4517 wl_resource_set_implementation(resource, &viewport_interface,
4518 surface, destroy_viewport);
4520 surface->viewport_resource = resource;
4523 static const struct wp_viewporter_interface viewporter_interface = {
4525 viewporter_get_viewport
4529 bind_viewporter(struct wl_client *client,
4530 void *data, uint32_t version, uint32_t id)
4532 struct wl_resource *resource;
4534 resource = wl_resource_create(client, &wp_viewporter_interface,
4536 if (resource == NULL) {
4537 wl_client_post_no_memory(client);
4541 wl_resource_set_implementation(resource, &viewporter_interface,
4546 destroy_presentation_feedback(struct wl_resource *feedback_resource)
4548 struct weston_presentation_feedback *feedback;
4550 feedback = wl_resource_get_user_data(feedback_resource);
4552 wl_list_remove(&feedback->link);
4557 presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4559 wl_resource_destroy(resource);
4563 presentation_feedback(struct wl_client *client,
4564 struct wl_resource *presentation_resource,
4565 struct wl_resource *surface_resource,
4568 struct weston_surface *surface;
4569 struct weston_presentation_feedback *feedback;
4571 surface = wl_resource_get_user_data(surface_resource);
4573 feedback = zalloc(sizeof *feedback);
4574 if (feedback == NULL)
4577 feedback->resource = wl_resource_create(client,
4578 &wp_presentation_feedback_interface,
4580 if (!feedback->resource)
4583 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4584 destroy_presentation_feedback);
4586 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4594 wl_client_post_no_memory(client);
4597 static const struct wp_presentation_interface presentation_implementation = {
4598 presentation_destroy,
4599 presentation_feedback
4603 bind_presentation(struct wl_client *client,
4604 void *data, uint32_t version, uint32_t id)
4606 struct weston_compositor *compositor = data;
4607 struct wl_resource *resource;
4609 resource = wl_resource_create(client, &wp_presentation_interface,
4611 if (resource == NULL) {
4612 wl_client_post_no_memory(client);
4616 wl_resource_set_implementation(resource, &presentation_implementation,
4618 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
4622 compositor_bind(struct wl_client *client,
4623 void *data, uint32_t version, uint32_t id)
4625 struct weston_compositor *compositor = data;
4626 struct wl_resource *resource;
4628 resource = wl_resource_create(client, &wl_compositor_interface,
4630 if (resource == NULL) {
4631 wl_client_post_no_memory(client);
4635 wl_resource_set_implementation(resource, &compositor_interface,
4640 weston_environment_get_fd(const char *env)
4646 if (!e || !safe_strtoint(e, &fd))
4649 flags = fcntl(fd, F_GETFD);
4653 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4660 timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
4661 uint32_t key, void *data)
4663 struct weston_compositor *compositor = data;
4665 if (weston_timeline_enabled_)
4666 weston_timeline_close();
4668 weston_timeline_open(compositor);
4671 /** Create the compositor.
4673 * This functions creates and initializes a compositor instance.
4675 * \param display The Wayland display to be used.
4676 * \param user_data A pointer to an object that can later be retrieved
4677 * using the \ref weston_compositor_get_user_data function.
4678 * \return The compositor instance on success or NULL on failure.
4680 WL_EXPORT struct weston_compositor *
4681 weston_compositor_create(struct wl_display *display, void *user_data)
4683 struct weston_compositor *ec;
4684 struct wl_event_loop *loop;
4686 ec = zalloc(sizeof *ec);
4690 ec->wl_display = display;
4691 ec->user_data = user_data;
4692 wl_signal_init(&ec->destroy_signal);
4693 wl_signal_init(&ec->create_surface_signal);
4694 wl_signal_init(&ec->activate_signal);
4695 wl_signal_init(&ec->transform_signal);
4696 wl_signal_init(&ec->kill_signal);
4697 wl_signal_init(&ec->idle_signal);
4698 wl_signal_init(&ec->wake_signal);
4699 wl_signal_init(&ec->show_input_panel_signal);
4700 wl_signal_init(&ec->hide_input_panel_signal);
4701 wl_signal_init(&ec->update_input_panel_signal);
4702 wl_signal_init(&ec->seat_created_signal);
4703 wl_signal_init(&ec->output_created_signal);
4704 wl_signal_init(&ec->output_destroyed_signal);
4705 wl_signal_init(&ec->output_moved_signal);
4706 wl_signal_init(&ec->output_resized_signal);
4707 wl_signal_init(&ec->session_signal);
4708 ec->session_active = 1;
4710 ec->output_id_pool = 0;
4711 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
4713 ec->activate_serial = 1;
4715 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
4716 ec, compositor_bind))
4719 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
4720 ec, bind_subcompositor))
4723 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
4724 ec, bind_viewporter))
4727 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
4728 ec, bind_presentation))
4731 if (weston_input_init(ec) != 0)
4734 wl_list_init(&ec->view_list);
4735 wl_list_init(&ec->plane_list);
4736 wl_list_init(&ec->layer_list);
4737 wl_list_init(&ec->seat_list);
4738 wl_list_init(&ec->output_list);
4739 wl_list_init(&ec->key_binding_list);
4740 wl_list_init(&ec->modifier_binding_list);
4741 wl_list_init(&ec->button_binding_list);
4742 wl_list_init(&ec->touch_binding_list);
4743 wl_list_init(&ec->axis_binding_list);
4744 wl_list_init(&ec->debug_binding_list);
4746 wl_list_init(&ec->plugin_api_list);
4748 weston_plane_init(&ec->primary_plane, ec, 0, 0);
4749 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
4751 wl_data_device_manager_init(ec->wl_display);
4753 wl_display_init_shm(ec->wl_display);
4755 loop = wl_display_get_event_loop(ec->wl_display);
4756 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4758 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4759 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4761 weston_compositor_add_debug_binding(ec, KEY_T,
4762 timeline_key_binding_handler, ec);
4772 weston_compositor_shutdown(struct weston_compositor *ec)
4774 struct weston_output *output, *next;
4776 wl_event_source_remove(ec->idle_source);
4778 /* Destroy all outputs associated with this compositor */
4779 wl_list_for_each_safe(output, next, &ec->output_list, link)
4780 output->destroy(output);
4783 ec->renderer->destroy(ec);
4785 weston_binding_list_destroy_all(&ec->key_binding_list);
4786 weston_binding_list_destroy_all(&ec->modifier_binding_list);
4787 weston_binding_list_destroy_all(&ec->button_binding_list);
4788 weston_binding_list_destroy_all(&ec->touch_binding_list);
4789 weston_binding_list_destroy_all(&ec->axis_binding_list);
4790 weston_binding_list_destroy_all(&ec->debug_binding_list);
4792 weston_plane_release(&ec->primary_plane);
4796 weston_compositor_exit_with_code(struct weston_compositor *compositor,
4799 if (compositor->exit_code == EXIT_SUCCESS)
4800 compositor->exit_code = exit_code;
4802 weston_compositor_exit(compositor);
4806 weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4807 const struct weston_pointer_grab_interface *interface)
4809 struct weston_seat *seat;
4811 ec->default_pointer_grab = interface;
4812 wl_list_for_each(seat, &ec->seat_list, link) {
4813 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4816 weston_pointer_set_default_grab(pointer, interface);
4821 weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4826 if (clock_gettime(clk_id, &ts) < 0)
4829 compositor->presentation_clock = clk_id;
4835 * For choosing the software clock, when the display hardware or API
4836 * does not expose a compatible presentation timestamp.
4839 weston_compositor_set_presentation_clock_software(
4840 struct weston_compositor *compositor)
4842 /* In order of preference */
4843 static const clockid_t clocks[] = {
4844 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4845 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4846 CLOCK_MONOTONIC, /* no jumps, may crawl */
4847 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4848 CLOCK_REALTIME /* may jump and crawl */
4852 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4853 if (weston_compositor_set_presentation_clock(compositor,
4857 weston_log("Error: no suitable presentation clock available.\n");
4862 /** Read the current time from the Presentation clock
4865 * \param ts[out] The current time.
4867 * \note Reading the current time in user space is always imprecise to some
4870 * This function is never meant to fail. If reading the clock does fail,
4871 * an error message is logged and a zero time is returned. Callers are not
4872 * supposed to detect or react to failures.
4875 weston_compositor_read_presentation_clock(
4876 const struct weston_compositor *compositor,
4877 struct timespec *ts)
4882 ret = clock_gettime(compositor->presentation_clock, ts);
4888 weston_log("Error: failure to read "
4889 "the presentation clock %#x: '%m' (%d)\n",
4890 compositor->presentation_clock, errno);
4895 /** Import dmabuf buffer into current renderer
4898 * \param buffer the dmabuf buffer to import
4899 * \return true on usable buffers, false otherwise
4901 * This function tests that the linux_dmabuf_buffer is usable
4902 * for the current renderer. Returns false on unusable buffers. Usually
4903 * usability is tested by importing the dmabufs for composition.
4905 * This hook is also used for detecting if the renderer supports
4906 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4910 weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4911 struct linux_dmabuf_buffer *buffer)
4913 struct weston_renderer *renderer;
4915 renderer = compositor->renderer;
4917 if (renderer->import_dmabuf == NULL)
4920 return renderer->import_dmabuf(compositor, buffer);
4924 weston_version(int *major, int *minor, int *micro)
4926 *major = WESTON_VERSION_MAJOR;
4927 *minor = WESTON_VERSION_MINOR;
4928 *micro = WESTON_VERSION_MICRO;
4932 weston_load_module(const char *name, const char *entrypoint)
4934 const char *builddir = getenv("WESTON_BUILD_DIR");
4935 char path[PATH_MAX];
4936 void *module, *init;
4941 if (name[0] != '/') {
4943 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4945 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
4947 snprintf(path, sizeof path, "%s", name);
4950 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4952 weston_log("Module '%s' already loaded\n", path);
4957 weston_log("Loading module '%s'\n", path);
4958 module = dlopen(path, RTLD_NOW);
4960 weston_log("Failed to load module: %s\n", dlerror());
4964 init = dlsym(module, entrypoint);
4966 weston_log("Failed to lookup init function: %s\n", dlerror());
4975 /** Destroys the compositor.
4977 * This function cleans up the compositor state and destroys it.
4979 * \param compositor The compositor to be destroyed.
4982 weston_compositor_destroy(struct weston_compositor *compositor)
4984 /* prevent further rendering while shutting down */
4985 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4987 wl_signal_emit(&compositor->destroy_signal, compositor);
4989 weston_compositor_xkb_destroy(compositor);
4991 if (compositor->backend)
4992 compositor->backend->destroy(compositor);
4994 weston_plugin_api_destroy_list(compositor);
4999 /** Instruct the compositor to exit.
5001 * This functions does not directly destroy the compositor object, it merely
5002 * command it to start the tear down process. It is not guaranteed that the
5003 * tear down will happen immediately.
5005 * \param compositor The compositor to tear down.
5008 weston_compositor_exit(struct weston_compositor *compositor)
5010 compositor->exit(compositor);
5013 /** Return the user data stored in the compositor.
5015 * This function returns the user data pointer set with user_data parameter
5016 * to the \ref weston_compositor_create function.
5019 weston_compositor_get_user_data(struct weston_compositor *compositor)
5021 return compositor->user_data;
5024 static const char * const backend_map[] = {
5025 [WESTON_BACKEND_DRM] = "drm-backend.so",
5026 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5027 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5028 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5029 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5030 [WESTON_BACKEND_X11] = "x11-backend.so",
5033 /** Load a backend into a weston_compositor
5035 * A backend must be loaded to make a weston_compositor work. A backend
5036 * provides input and output capabilities, and determines the renderer to use.
5038 * \param compositor A compositor that has not had a backend loaded yet.
5039 * \param backend Name of the backend file.
5040 * \param config_base A pointer to a backend-specific configuration
5041 * structure's 'base' member.
5043 * \return 0 on success, or -1 on error.
5046 weston_compositor_load_backend(struct weston_compositor *compositor,
5047 enum weston_compositor_backend backend,
5048 struct weston_backend_config *config_base)
5050 int (*backend_init)(struct weston_compositor *c,
5051 struct weston_backend_config *config_base);
5053 if (backend < 0 || backend >= ARRAY_LENGTH(backend_map))
5056 backend_init = weston_load_module(backend_map[backend], "backend_init");
5060 return backend_init(compositor, config_base);
5064 weston_compositor_load_xwayland(struct weston_compositor *compositor)
5066 int (*module_init)(struct weston_compositor *ec,
5067 int *argc, char *argv[]);
5070 module_init = weston_load_module("xwayland.so", "module_init");
5073 if (module_init(compositor, &argc, NULL) < 0)