blob: 2e72513f50e23efa0a9da2e749e951a8f1f19a8f [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanen925788f2018-04-19 14:20:01 +03004 * Copyright © 2012-2018 Collabora, Ltd.
5 * Copyright © 2017, 2018 General Electric Company
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05006 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07007 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050014 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070015 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050027 */
28
Kristian Høgsberga9410222011-01-14 17:22:35 -050029#include "config.h"
30
Daniel Stoneb7452fe2012-06-01 12:14:06 +010031#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040032#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010036#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050037#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020038#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040039#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010040#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040041#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020042#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020043#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020044#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040045#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050046#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040047#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040048#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040049#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050050#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040051#include <sys/time.h>
52#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030053#include <errno.h>
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030054#include <inttypes.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050055
Pekka Paalanenb5026542014-11-12 15:09:24 +020056#include "timeline.h"
57
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020058#include <libweston/libweston.h>
Marius Vladf4f4c2b2019-04-29 13:27:47 +030059#include <libweston/weston-debug.h>
Daniel Stonece62cb32018-07-20 09:46:24 +010060#include "linux-dmabuf.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030061#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020062#include "presentation-time-server-protocol.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030063#include "linux-explicit-synchronization-unstable-v1-server-protocol.h"
Alexandros Frantzis67629672018-10-19 12:14:11 +030064#include "linux-explicit-synchronization.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030065#include "shared/fd-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070066#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070067#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070068#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030069#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040070#include "git-version.h"
Pekka Paalanencda14882019-04-04 15:41:02 +030071#include <libweston/version.h>
Pekka Paalanen27b377f2019-03-29 17:07:34 +020072#include <libweston/plugin-registry.h>
Marius Vlad00a6e012018-11-20 17:52:31 +020073#include "pixel-formats.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050074
Pekka Paalanen0513a952014-05-21 16:17:27 +030075#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
76
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020077static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030078weston_output_update_matrix(struct weston_output *output);
79
80static void
Alexander Larsson0b135062013-05-28 16:23:36 +020081weston_output_transform_scale_init(struct weston_output *output,
82 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020083
Rob Bradford27b17932013-06-26 18:08:46 +010084static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050085weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010086
Pekka Paalanendcac3512017-12-08 14:13:34 +020087static char *
88weston_output_create_heads_string(struct weston_output *output);
89
Pekka Paalanen6528c032017-03-28 15:27:10 +030090/** Send wl_output events for mode and scale changes
91 *
92 * \param head Send on all resources bound to this head.
93 * \param mode_changed If true, send the current mode.
94 * \param scale_changed If true, send the current scale.
95 */
96static void
97weston_mode_switch_send_events(struct weston_head *head,
98 bool mode_changed, bool scale_changed)
99{
100 struct weston_output *output = head->output;
101 struct wl_resource *resource;
102 int version;
103
104 wl_resource_for_each(resource, &head->resource_list) {
105 if (mode_changed) {
106 wl_output_send_mode(resource,
107 output->current_mode->flags,
108 output->current_mode->width,
109 output->current_mode->height,
110 output->current_mode->refresh);
111 }
112
113 version = wl_resource_get_version(resource);
114 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
115 wl_output_send_scale(resource, output->current_scale);
116
117 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
118 wl_output_send_done(resource);
119 }
120}
121
122static void
123weston_mode_switch_finish(struct weston_output *output,
124 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800125{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200126 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300127 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200128 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200129
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200130 pixman_region32_init(&old_output_region);
131 pixman_region32_copy(&old_output_region, &output->region);
132
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200133 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200134 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200135
136 pixman_region32_init(&output->previous_damage);
137 pixman_region32_init_rect(&output->region, output->x, output->y,
138 output->width, output->height);
139
140 weston_output_update_matrix(output);
141
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200142 /* If a pointer falls outside the outputs new geometry, move it to its
143 * lower-right corner */
144 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500145 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200146 int32_t x, y;
147
148 if (!pointer)
149 continue;
150
151 x = wl_fixed_to_int(pointer->x);
152 y = wl_fixed_to_int(pointer->y);
153
154 if (!pixman_region32_contains_point(&old_output_region,
155 x, y, NULL) ||
156 pixman_region32_contains_point(&output->region,
157 x, y, NULL))
158 continue;
159
160 if (x >= output->x + output->width)
161 x = output->x + output->width - 1;
162 if (y >= output->y + output->height)
163 y = output->y + output->height - 1;
164
165 pointer->x = wl_fixed_from_int(x);
166 pointer->y = wl_fixed_from_int(y);
167 }
168
169 pixman_region32_fini(&old_output_region);
170
Derek Foremandd4cd332014-11-10 10:29:59 -0600171 if (!mode_changed && !scale_changed)
172 return;
173
Hardening57388e42013-09-18 23:56:36 +0200174 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300175 wl_list_for_each(head, &output->head_list, output_link)
176 weston_mode_switch_send_events(head,
177 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600178}
179
David Fort0de859e2016-05-27 23:22:57 +0200180static void
181weston_compositor_reflow_outputs(struct weston_compositor *compositor,
182 struct weston_output *resized_output, int delta_width);
183
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600184WL_EXPORT int
185weston_output_mode_set_native(struct weston_output *output,
186 struct weston_mode *mode,
187 int32_t scale)
188{
189 int ret;
190 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200191 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600192
193 if (!output->switch_mode)
194 return -1;
195
196 if (!output->original_mode) {
197 mode_changed = 1;
198 ret = output->switch_mode(output, mode);
199 if (ret < 0)
200 return ret;
201 if (output->current_scale != scale) {
202 scale_changed = 1;
203 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200204 }
205 }
206
David Fort0de859e2016-05-27 23:22:57 +0200207 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600208 output->native_mode = mode;
209 output->native_scale = scale;
210
211 weston_mode_switch_finish(output, mode_changed, scale_changed);
212
David Fort0de859e2016-05-27 23:22:57 +0200213 if (mode_changed || scale_changed) {
214 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
215
216 wl_signal_emit(&output->compositor->output_resized_signal, output);
217 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600218 return 0;
219}
220
221WL_EXPORT int
222weston_output_mode_switch_to_native(struct weston_output *output)
223{
224 int ret;
225 int mode_changed = 0, scale_changed = 0;
226
227 if (!output->switch_mode)
228 return -1;
229
230 if (!output->original_mode) {
231 weston_log("already in the native mode\n");
232 return -1;
233 }
234 /* the non fullscreen clients haven't seen a mode set since we
235 * switched into a temporary, so we need to notify them if the
236 * mode at that time is different from the native mode now.
237 */
238 mode_changed = (output->original_mode != output->native_mode);
239 scale_changed = (output->original_scale != output->native_scale);
240
241 ret = output->switch_mode(output, output->native_mode);
242 if (ret < 0)
243 return ret;
244
245 output->current_scale = output->native_scale;
246
247 output->original_mode = NULL;
248 output->original_scale = 0;
249
250 weston_mode_switch_finish(output, mode_changed, scale_changed);
251
252 return 0;
253}
254
255WL_EXPORT int
256weston_output_mode_switch_to_temporary(struct weston_output *output,
257 struct weston_mode *mode,
258 int32_t scale)
259{
260 int ret;
261
262 if (!output->switch_mode)
263 return -1;
264
265 /* original_mode is the last mode non full screen clients have seen,
266 * so we shouldn't change it if we already have one set.
267 */
268 if (!output->original_mode) {
269 output->original_mode = output->native_mode;
270 output->original_scale = output->native_scale;
271 }
272 ret = output->switch_mode(output, mode);
273 if (ret < 0)
274 return ret;
275
276 output->current_scale = scale;
277
278 weston_mode_switch_finish(output, 0, 0);
279
280 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800281}
282
Benjamin Franzke06286262011-05-06 19:12:33 +0200283static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300284region_init_infinite(pixman_region32_t *region)
285{
286 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
287 UINT32_MAX, UINT32_MAX);
288}
289
Pekka Paalanene67858b2013-04-25 13:57:42 +0300290static struct weston_subsurface *
291weston_surface_to_subsurface(struct weston_surface *surface);
292
Jason Ekstranda7af7042013-10-12 22:38:11 -0500293WL_EXPORT struct weston_view *
294weston_view_create(struct weston_surface *surface)
295{
296 struct weston_view *view;
297
Bryce Harringtonde16d892014-11-20 22:21:57 -0800298 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500299 if (view == NULL)
300 return NULL;
301
302 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000303 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500304
Jason Ekstranda7af7042013-10-12 22:38:11 -0500305 /* Assign to surface */
306 wl_list_insert(&surface->views, &view->surface_link);
307
308 wl_signal_init(&view->destroy_signal);
309 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300310 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500311
Jason Ekstranda7af7042013-10-12 22:38:11 -0500312 pixman_region32_init(&view->clip);
313
314 view->alpha = 1.0;
315 pixman_region32_init(&view->transform.opaque);
316
317 wl_list_init(&view->geometry.transformation_list);
318 wl_list_insert(&view->geometry.transformation_list,
319 &view->transform.position.link);
320 weston_matrix_init(&view->transform.position.matrix);
321 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200322 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500323 pixman_region32_init(&view->transform.boundingbox);
324 view->transform.dirty = 1;
325
Jason Ekstranda7af7042013-10-12 22:38:11 -0500326 return view;
327}
328
Jason Ekstrand108865d2014-06-26 10:04:49 -0700329struct weston_frame_callback {
330 struct wl_resource *resource;
331 struct wl_list link;
332};
333
Pekka Paalanen133e4392014-09-23 22:08:46 -0400334struct weston_presentation_feedback {
335 struct wl_resource *resource;
336
337 /* XXX: could use just wl_resource_get_link() instead */
338 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200339
340 /* The per-surface feedback flags */
341 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400342};
343
344static void
345weston_presentation_feedback_discard(
346 struct weston_presentation_feedback *feedback)
347{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200348 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400349 wl_resource_destroy(feedback->resource);
350}
351
352static void
353weston_presentation_feedback_discard_list(struct wl_list *list)
354{
355 struct weston_presentation_feedback *feedback, *tmp;
356
357 wl_list_for_each_safe(feedback, tmp, list, link)
358 weston_presentation_feedback_discard(feedback);
359}
360
361static void
362weston_presentation_feedback_present(
363 struct weston_presentation_feedback *feedback,
364 struct weston_output *output,
365 uint32_t refresh_nsec,
366 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200367 uint64_t seq,
368 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400369{
370 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300371 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400372 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200373 uint32_t tv_sec_hi;
374 uint32_t tv_sec_lo;
375 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300376 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400377
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300378 wl_list_for_each(head, &output->head_list, output_link) {
379 wl_resource_for_each(o, &head->resource_list) {
380 if (wl_resource_get_client(o) != client)
381 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400382
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300383 wp_presentation_feedback_send_sync_output(feedback->resource, o);
384 done = true;
385 }
386
387 /* For clone mode, send it for just one wl_output global,
388 * they are all equivalent anyway.
389 */
390 if (done)
391 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400392 }
393
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200394 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200395 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200396 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200397 refresh_nsec,
398 seq >> 32, seq & 0xffffffff,
399 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400400 wl_resource_destroy(feedback->resource);
401}
402
403static void
404weston_presentation_feedback_present_list(struct wl_list *list,
405 struct weston_output *output,
406 uint32_t refresh_nsec,
407 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200408 uint64_t seq,
409 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400410{
411 struct weston_presentation_feedback *feedback, *tmp;
412
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200413 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200414 wl_list_empty(list));
415
Pekka Paalanen133e4392014-09-23 22:08:46 -0400416 wl_list_for_each_safe(feedback, tmp, list, link)
417 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200418 refresh_nsec, ts, seq,
419 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400420}
421
Jason Ekstrand7b982072014-05-20 14:33:03 -0500422static void
423surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
424{
425 struct weston_surface_state *state =
426 container_of(listener, struct weston_surface_state,
427 buffer_destroy_listener);
428
429 state->buffer = NULL;
430}
431
432static void
433weston_surface_state_init(struct weston_surface_state *state)
434{
435 state->newly_attached = 0;
436 state->buffer = NULL;
437 state->buffer_destroy_listener.notify =
438 surface_state_handle_buffer_destroy;
439 state->sx = 0;
440 state->sy = 0;
441
Derek Foreman152254b2015-11-26 14:17:48 -0600442 pixman_region32_init(&state->damage_surface);
443 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500444 pixman_region32_init(&state->opaque);
445 region_init_infinite(&state->input);
446
447 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400448 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500449
450 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
451 state->buffer_viewport.buffer.scale = 1;
452 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
453 state->buffer_viewport.surface.width = -1;
454 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300455
456 state->acquire_fence_fd = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500457}
458
459static void
460weston_surface_state_fini(struct weston_surface_state *state)
461{
462 struct weston_frame_callback *cb, *next;
463
464 wl_list_for_each_safe(cb, next,
465 &state->frame_callback_list, link)
466 wl_resource_destroy(cb->resource);
467
Pekka Paalanen133e4392014-09-23 22:08:46 -0400468 weston_presentation_feedback_discard_list(&state->feedback_list);
469
Jason Ekstrand7b982072014-05-20 14:33:03 -0500470 pixman_region32_fini(&state->input);
471 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600472 pixman_region32_fini(&state->damage_surface);
473 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500474
475 if (state->buffer)
476 wl_list_remove(&state->buffer_destroy_listener.link);
477 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300478
479 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300480 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500481}
482
483static void
484weston_surface_state_set_buffer(struct weston_surface_state *state,
485 struct weston_buffer *buffer)
486{
487 if (state->buffer == buffer)
488 return;
489
490 if (state->buffer)
491 wl_list_remove(&state->buffer_destroy_listener.link);
492 state->buffer = buffer;
493 if (state->buffer)
494 wl_signal_add(&state->buffer->destroy_signal,
495 &state->buffer_destroy_listener);
496}
497
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500498WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500499weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500500{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500501 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400502
Bryce Harringtonde16d892014-11-20 22:21:57 -0800503 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400504 if (surface == NULL)
505 return NULL;
506
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500507 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800508 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500509
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500510 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200511 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400512
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200513 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
514 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200515 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
516 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500517
518 weston_surface_state_init(&surface->pending);
519
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400520 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500521 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300522 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400523
Jason Ekstranda7af7042013-10-12 22:38:11 -0500524 wl_list_init(&surface->views);
525
526 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400527 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500528
Pekka Paalanene67858b2013-04-25 13:57:42 +0300529 wl_list_init(&surface->subsurface_list);
530 wl_list_init(&surface->subsurface_list_pending);
531
Jason Ekstrand1e059042014-10-16 10:55:19 -0500532 weston_matrix_init(&surface->buffer_to_surface_matrix);
533 weston_matrix_init(&surface->surface_to_buffer_matrix);
534
Jonas Ådahld3414f22016-07-22 17:56:31 +0800535 wl_list_init(&surface->pointer_constraints);
536
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300537 surface->acquire_fence_fd = -1;
538
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400539 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500540}
541
Alex Wu8811bf92012-02-28 18:07:54 +0800542WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500543weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200544 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500545{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100546 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200547 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500548}
549
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400550WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500551weston_view_to_global_float(struct weston_view *view,
552 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200553{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500554 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200555 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
556
Jason Ekstranda7af7042013-10-12 22:38:11 -0500557 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200558
559 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200560 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700561 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200562 v.f[3]);
563 *x = 0;
564 *y = 0;
565 return;
566 }
567
568 *x = v.f[0] / v.f[3];
569 *y = v.f[1] / v.f[3];
570 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500571 *x = sx + view->geometry.x;
572 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200573 }
574}
575
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500576WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200577weston_transformed_coord(int width, int height,
578 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200579 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200580 float sx, float sy, float *bx, float *by)
581{
582 switch (transform) {
583 case WL_OUTPUT_TRANSFORM_NORMAL:
584 default:
585 *bx = sx;
586 *by = sy;
587 break;
588 case WL_OUTPUT_TRANSFORM_FLIPPED:
589 *bx = width - sx;
590 *by = sy;
591 break;
592 case WL_OUTPUT_TRANSFORM_90:
593 *bx = height - sy;
594 *by = sx;
595 break;
596 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
597 *bx = height - sy;
598 *by = width - sx;
599 break;
600 case WL_OUTPUT_TRANSFORM_180:
601 *bx = width - sx;
602 *by = height - sy;
603 break;
604 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
605 *bx = sx;
606 *by = height - sy;
607 break;
608 case WL_OUTPUT_TRANSFORM_270:
609 *bx = sy;
610 *by = width - sx;
611 break;
612 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
613 *bx = sy;
614 *by = sx;
615 break;
616 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200617
618 *bx *= scale;
619 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200620}
621
622WL_EXPORT pixman_box32_t
623weston_transformed_rect(int width, int height,
624 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200625 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200626 pixman_box32_t rect)
627{
628 float x1, x2, y1, y2;
629
630 pixman_box32_t ret;
631
Alexander Larsson4ea95522013-05-22 14:41:37 +0200632 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200633 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200634 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200635 rect.x2, rect.y2, &x2, &y2);
636
637 if (x1 <= x2) {
638 ret.x1 = x1;
639 ret.x2 = x2;
640 } else {
641 ret.x1 = x2;
642 ret.x2 = x1;
643 }
644
645 if (y1 <= y2) {
646 ret.y1 = y1;
647 ret.y2 = y2;
648 } else {
649 ret.y1 = y2;
650 ret.y2 = y1;
651 }
652
653 return ret;
654}
655
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600656/** Transform a region by a matrix, restricted to axis-aligned transformations
657 *
658 * Warning: This function does not work for projective, affine, or matrices
659 * that encode arbitrary rotations. Only 90-degree step rotations are
660 * supported.
661 */
662WL_EXPORT void
663weston_matrix_transform_region(pixman_region32_t *dest,
664 struct weston_matrix *matrix,
665 pixman_region32_t *src)
666{
667 pixman_box32_t *src_rects, *dest_rects;
668 int nrects, i;
669
670 src_rects = pixman_region32_rectangles(src, &nrects);
671 dest_rects = malloc(nrects * sizeof(*dest_rects));
672 if (!dest_rects)
673 return;
674
675 for (i = 0; i < nrects; i++) {
676 struct weston_vector vec1 = {{
677 src_rects[i].x1, src_rects[i].y1, 0, 1
678 }};
679 weston_matrix_transform(matrix, &vec1);
680 vec1.f[0] /= vec1.f[3];
681 vec1.f[1] /= vec1.f[3];
682
683 struct weston_vector vec2 = {{
684 src_rects[i].x2, src_rects[i].y2, 0, 1
685 }};
686 weston_matrix_transform(matrix, &vec2);
687 vec2.f[0] /= vec2.f[3];
688 vec2.f[1] /= vec2.f[3];
689
690 if (vec1.f[0] < vec2.f[0]) {
691 dest_rects[i].x1 = floor(vec1.f[0]);
692 dest_rects[i].x2 = ceil(vec2.f[0]);
693 } else {
694 dest_rects[i].x1 = floor(vec2.f[0]);
695 dest_rects[i].x2 = ceil(vec1.f[0]);
696 }
697
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600698 if (vec1.f[1] < vec2.f[1]) {
699 dest_rects[i].y1 = floor(vec1.f[1]);
700 dest_rects[i].y2 = ceil(vec2.f[1]);
701 } else {
702 dest_rects[i].y1 = floor(vec2.f[1]);
703 dest_rects[i].y2 = ceil(vec1.f[1]);
704 }
705 }
706
707 pixman_region32_clear(dest);
708 pixman_region32_init_rects(dest, dest_rects, nrects);
709 free(dest_rects);
710}
711
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200712WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500713weston_transformed_region(int width, int height,
714 enum wl_output_transform transform,
715 int32_t scale,
716 pixman_region32_t *src, pixman_region32_t *dest)
717{
718 pixman_box32_t *src_rects, *dest_rects;
719 int nrects, i;
720
721 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
722 if (src != dest)
723 pixman_region32_copy(dest, src);
724 return;
725 }
726
727 src_rects = pixman_region32_rectangles(src, &nrects);
728 dest_rects = malloc(nrects * sizeof(*dest_rects));
729 if (!dest_rects)
730 return;
731
732 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
733 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
734 } else {
735 for (i = 0; i < nrects; i++) {
736 switch (transform) {
737 default:
738 case WL_OUTPUT_TRANSFORM_NORMAL:
739 dest_rects[i].x1 = src_rects[i].x1;
740 dest_rects[i].y1 = src_rects[i].y1;
741 dest_rects[i].x2 = src_rects[i].x2;
742 dest_rects[i].y2 = src_rects[i].y2;
743 break;
744 case WL_OUTPUT_TRANSFORM_90:
745 dest_rects[i].x1 = height - src_rects[i].y2;
746 dest_rects[i].y1 = src_rects[i].x1;
747 dest_rects[i].x2 = height - src_rects[i].y1;
748 dest_rects[i].y2 = src_rects[i].x2;
749 break;
750 case WL_OUTPUT_TRANSFORM_180:
751 dest_rects[i].x1 = width - src_rects[i].x2;
752 dest_rects[i].y1 = height - src_rects[i].y2;
753 dest_rects[i].x2 = width - src_rects[i].x1;
754 dest_rects[i].y2 = height - src_rects[i].y1;
755 break;
756 case WL_OUTPUT_TRANSFORM_270:
757 dest_rects[i].x1 = src_rects[i].y1;
758 dest_rects[i].y1 = width - src_rects[i].x2;
759 dest_rects[i].x2 = src_rects[i].y2;
760 dest_rects[i].y2 = width - src_rects[i].x1;
761 break;
762 case WL_OUTPUT_TRANSFORM_FLIPPED:
763 dest_rects[i].x1 = width - src_rects[i].x2;
764 dest_rects[i].y1 = src_rects[i].y1;
765 dest_rects[i].x2 = width - src_rects[i].x1;
766 dest_rects[i].y2 = src_rects[i].y2;
767 break;
768 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
769 dest_rects[i].x1 = height - src_rects[i].y2;
770 dest_rects[i].y1 = width - src_rects[i].x2;
771 dest_rects[i].x2 = height - src_rects[i].y1;
772 dest_rects[i].y2 = width - src_rects[i].x1;
773 break;
774 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
775 dest_rects[i].x1 = src_rects[i].x1;
776 dest_rects[i].y1 = height - src_rects[i].y2;
777 dest_rects[i].x2 = src_rects[i].x2;
778 dest_rects[i].y2 = height - src_rects[i].y1;
779 break;
780 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
781 dest_rects[i].x1 = src_rects[i].y1;
782 dest_rects[i].y1 = src_rects[i].x1;
783 dest_rects[i].x2 = src_rects[i].y2;
784 dest_rects[i].y2 = src_rects[i].x2;
785 break;
786 }
787 }
788 }
789
790 if (scale != 1) {
791 for (i = 0; i < nrects; i++) {
792 dest_rects[i].x1 *= scale;
793 dest_rects[i].x2 *= scale;
794 dest_rects[i].y1 *= scale;
795 dest_rects[i].y2 *= scale;
796 }
797 }
798
799 pixman_region32_clear(dest);
800 pixman_region32_init_rects(dest, dest_rects, nrects);
801 free(dest_rects);
802}
803
Jonny Lamb74130762013-11-26 18:19:46 +0100804static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300805viewport_surface_to_buffer(struct weston_surface *surface,
806 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100807{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200808 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200809 double src_width, src_height;
810 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200811
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200812 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
813 if (vp->surface.width == -1) {
814 *bx = sx;
815 *by = sy;
816 return;
817 }
Jonny Lamb74130762013-11-26 18:19:46 +0100818
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200819 src_x = 0.0;
820 src_y = 0.0;
821 src_width = surface->width_from_buffer;
822 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100823 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200824 src_x = wl_fixed_to_double(vp->buffer.src_x);
825 src_y = wl_fixed_to_double(vp->buffer.src_y);
826 src_width = wl_fixed_to_double(vp->buffer.src_width);
827 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100828 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200829
830 *bx = sx * src_width / surface->width + src_x;
831 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100832}
833
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500834WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200835weston_surface_to_buffer_float(struct weston_surface *surface,
836 float sx, float sy, float *bx, float *by)
837{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200838 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
839
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300840 /* first transform coordinates if the viewport is set */
841 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100842
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500843 weston_transformed_coord(surface->width_from_buffer,
844 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200845 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100846 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200847}
848
Derek Foreman99739672015-12-03 16:38:11 -0600849/** Transform a rectangle from surface coordinates to buffer coordinates
850 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300851 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600852 * from.
853 * \param rect The rectangle to transform.
854 * \return The transformed rectangle.
855 *
856 * Viewport and buffer transformations can only do translation, scaling,
857 * and rotations in 90-degree steps. Therefore the only loss in the
858 * conversion is coordinate rounding.
859 *
860 * However, some coordinate rounding takes place as an intermediate
861 * step before the buffer scale factor is applied, so the rectangle
862 * boundary may not be exactly as expected.
863 *
864 * This is OK for damage tracking since a little extra coverage is
865 * not a problem.
866 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200867WL_EXPORT pixman_box32_t
868weston_surface_to_buffer_rect(struct weston_surface *surface,
869 pixman_box32_t rect)
870{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200871 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100872 float xf, yf;
873
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300874 /* first transform box coordinates if the viewport is set */
875 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100876 rect.x1 = floorf(xf);
877 rect.y1 = floorf(yf);
878
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300879 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600880 rect.x2 = ceilf(xf);
881 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100882
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500883 return weston_transformed_rect(surface->width_from_buffer,
884 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200885 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200886 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200887}
888
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200889/** Transform a region from surface coordinates to buffer coordinates
890 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300891 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200892 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300893 * \param[in] surface_region The region in surface coordinates.
894 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200895 *
896 * Buffer_region must be init'd, but will be completely overwritten.
897 *
898 * Viewport and buffer transformations can only do translation, scaling,
899 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600900 * conversion is from the coordinate rounding that takes place in
901 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300902 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200903 */
904WL_EXPORT void
905weston_surface_to_buffer_region(struct weston_surface *surface,
906 pixman_region32_t *surface_region,
907 pixman_region32_t *buffer_region)
908{
909 pixman_box32_t *src_rects, *dest_rects;
910 int nrects, i;
911
912 src_rects = pixman_region32_rectangles(surface_region, &nrects);
913 dest_rects = malloc(nrects * sizeof(*dest_rects));
914 if (!dest_rects)
915 return;
916
917 for (i = 0; i < nrects; i++) {
918 dest_rects[i] = weston_surface_to_buffer_rect(surface,
919 src_rects[i]);
920 }
921
922 pixman_region32_fini(buffer_region);
923 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
924 free(dest_rects);
925}
926
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200927WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500928weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400929 struct weston_plane *plane)
930{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500931 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400932 return;
933
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934 weston_view_damage_below(view);
935 view->plane = plane;
936 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400937}
938
Pekka Paalanen51723d52015-02-17 13:10:01 +0200939/** Inflict damage on the plane where the view is visible.
940 *
941 * \param view The view that causes the damage.
942 *
943 * If the view is currently on a plane (including the primary plane),
944 * take the view's boundingbox, subtract all the opaque views that cover it,
945 * and add the remaining region as damage to the plane. This corresponds
946 * to the damage inflicted to the plane if this view disappeared.
947 *
948 * A repaint is scheduled for this view.
949 *
950 * The region of all opaque views covering this view is stored in
951 * weston_view::clip and updated by view_accumulate_damage() during
952 * weston_output_repaint(). Specifically, that region matches the
953 * scenegraph as it was last painted.
954 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400955WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500956weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200957{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500958 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200959
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500960 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200961 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500962 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800963 if (view->plane)
964 pixman_region32_union(&view->plane->damage,
965 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500966 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800967 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200968}
969
Pekka Paalanen01e00682017-03-24 16:21:06 +0200970/** Send wl_surface.enter/leave events
971 *
972 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300973 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200974 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +0300975 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200976 *
977 * Send the enter/leave events for all protocol objects bound to the given
978 * output by the client owning the surface.
979 */
980static void
981weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300982 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +0200983 bool enter,
984 bool leave)
985{
986 struct wl_resource *wloutput;
987 struct wl_client *client;
988
989 assert(enter != leave);
990
991 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300992 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +0200993 if (wl_resource_get_client(wloutput) != client)
994 continue;
995
996 if (enter)
997 wl_surface_send_enter(surface->resource, wloutput);
998 if (leave)
999 wl_surface_send_leave(surface->resource, wloutput);
1000 }
1001}
1002
Bryce Harrington3f650b82015-12-23 11:01:58 -08001003/**
1004 * \param es The surface
1005 * \param mask The new set of outputs for the surface
1006 *
1007 * Sets the surface's set of outputs to the ones specified by
1008 * the new output mask provided. Identifies the outputs that
1009 * have changed, the posts enter and leave events for these
1010 * outputs as appropriate.
1011 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001012static void
1013weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1014{
1015 uint32_t different = es->output_mask ^ mask;
1016 uint32_t entered = mask & different;
1017 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001018 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001019 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001020 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001021
1022 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001023 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001024 return;
1025 if (different == 0)
1026 return;
1027
1028 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001029 output_bit = 1u << output->id;
1030 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001031 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001032
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001033 wl_list_for_each(head, &output->head_list, output_link) {
1034 weston_surface_send_enter_leave(es, head,
1035 output_bit & entered,
1036 output_bit & left);
1037 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001038 }
1039}
1040
Semi Malinene7a52fb2018-04-26 11:08:10 +02001041static void
1042notify_view_output_destroy(struct wl_listener *listener, void *data)
1043{
1044 struct weston_view *view =
1045 container_of(listener,
1046 struct weston_view, output_destroy_listener);
1047
1048 view->output = NULL;
1049 view->output_destroy_listener.notify = NULL;
1050}
1051
1052/** Set the primary output of the view
1053 *
1054 * \param view The view whose primary output to set
1055 * \param output The new primary output for the view
1056 *
1057 * Set \a output to be the primary output of the \a view.
1058 *
1059 * Notice that the assignment may be temporary; the primary output could be
1060 * automatically changed. Hence, one cannot rely on the value persisting.
1061 *
1062 * Passing NULL as /a output will set the primary output to NULL.
1063 */
1064WL_EXPORT void
1065weston_view_set_output(struct weston_view *view, struct weston_output *output)
1066{
1067 if (view->output_destroy_listener.notify) {
1068 wl_list_remove(&view->output_destroy_listener.link);
1069 view->output_destroy_listener.notify = NULL;
1070 }
1071 view->output = output;
1072 if (output) {
1073 view->output_destroy_listener.notify =
1074 notify_view_output_destroy;
1075 wl_signal_add(&output->destroy_signal,
1076 &view->output_destroy_listener);
1077 }
1078}
1079
Bryce Harrington3f650b82015-12-23 11:01:58 -08001080/** Recalculate which output(s) the surface has views displayed on
1081 *
1082 * \param es The surface to remap to outputs
1083 *
1084 * Finds the output that is showing the largest amount of one
1085 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001086 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001087 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001088 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001089 * in the output_mask for the surface.
1090 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001091static void
1092weston_surface_assign_output(struct weston_surface *es)
1093{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001094 struct weston_output *new_output;
1095 struct weston_view *view;
1096 pixman_region32_t region;
1097 uint32_t max, area, mask;
1098 pixman_box32_t *e;
1099
1100 new_output = NULL;
1101 max = 0;
1102 mask = 0;
1103 pixman_region32_init(&region);
1104 wl_list_for_each(view, &es->views, surface_link) {
1105 if (!view->output)
1106 continue;
1107
1108 pixman_region32_intersect(&region, &view->transform.boundingbox,
1109 &view->output->region);
1110
1111 e = pixman_region32_extents(&region);
1112 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1113
1114 mask |= view->output_mask;
1115
1116 if (area >= max) {
1117 new_output = view->output;
1118 max = area;
1119 }
1120 }
1121 pixman_region32_fini(&region);
1122
1123 es->output = new_output;
1124 weston_surface_update_output_mask(es, mask);
1125}
1126
Bryce Harrington3f650b82015-12-23 11:01:58 -08001127/** Recalculate which output(s) the view is displayed on
1128 *
1129 * \param ev The view to remap to outputs
1130 *
1131 * Identifies the set of outputs that the view is visible on,
1132 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001133 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001134 *
1135 * Also does the same for the view's surface. See
1136 * weston_surface_assign_output().
1137 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138static void
1139weston_view_assign_output(struct weston_view *ev)
1140{
1141 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001142 struct weston_output *output, *new_output;
1143 pixman_region32_t region;
1144 uint32_t max, area, mask;
1145 pixman_box32_t *e;
1146
1147 new_output = NULL;
1148 max = 0;
1149 mask = 0;
1150 pixman_region32_init(&region);
1151 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001152 if (output->destroying)
1153 continue;
1154
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001156 &output->region);
1157
1158 e = pixman_region32_extents(&region);
1159 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1160
1161 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001162 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001163
1164 if (area >= max) {
1165 new_output = output;
1166 max = area;
1167 }
1168 }
1169 pixman_region32_fini(&region);
1170
Semi Malinene7a52fb2018-04-26 11:08:10 +02001171 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 ev->output_mask = mask;
1173
1174 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001175}
1176
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001177static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001178weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1179 int from_x, int from_y, int *to_x, int *to_y)
1180{
1181 float x, y;
1182
1183 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1184 weston_view_from_global_float(to, x, y, &x, &y);
1185
1186 *to_x = round(x);
1187 *to_y = round(y);
1188}
1189
1190static void
1191weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1192{
1193 pixman_box32_t *a;
1194 pixman_box32_t b;
1195
1196 a = pixman_region32_extents(&from->geometry.scissor);
1197
1198 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1199 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1200
1201 pixman_region32_fini(&to->geometry.scissor);
1202 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1203}
1204
1205static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001206view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001208{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001209 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1210 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001211 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001212 { inbox->x1, inbox->y1 },
1213 { inbox->x1, inbox->y2 },
1214 { inbox->x2, inbox->y1 },
1215 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001216 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001217 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001218 int i;
1219
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001220 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001221 /* avoid rounding empty bbox to 1x1 */
1222 pixman_region32_init(bbox);
1223 return;
1224 }
1225
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001226 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001227 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001229 if (x < min_x)
1230 min_x = x;
1231 if (x > max_x)
1232 max_x = x;
1233 if (y < min_y)
1234 min_y = y;
1235 if (y > max_y)
1236 max_y = y;
1237 }
1238
Pekka Paalanen219b9822012-02-08 15:38:37 +02001239 int_x = floorf(min_x);
1240 int_y = floorf(min_y);
1241 pixman_region32_init_rect(bbox, int_x, int_y,
1242 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001243}
1244
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001245static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001247{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001248 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001249
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001250 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001251 view->geometry.x = roundf(view->geometry.x);
1252 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001253
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001254 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1256 view->transform.position.matrix.d[12] = view->geometry.x;
1257 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001258
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001260
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261 view->transform.inverse = view->transform.position.matrix;
1262 view->transform.inverse.d[12] = -view->geometry.x;
1263 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001264
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001266 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001267 view->surface->width,
1268 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001269 if (view->geometry.scissor_enabled)
1270 pixman_region32_intersect(&view->transform.boundingbox,
1271 &view->transform.boundingbox,
1272 &view->geometry.scissor);
1273
1274 pixman_region32_translate(&view->transform.boundingbox,
1275 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001276
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 if (view->alpha == 1.0) {
1278 pixman_region32_copy(&view->transform.opaque,
1279 &view->surface->opaque);
1280 pixman_region32_translate(&view->transform.opaque,
1281 view->geometry.x,
1282 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001283 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001284}
1285
1286static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001288{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289 struct weston_view *parent = view->geometry.parent;
1290 struct weston_matrix *matrix = &view->transform.matrix;
1291 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001292 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001293 pixman_region32_t surfregion;
1294 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001295
Jason Ekstranda7af7042013-10-12 22:38:11 -05001296 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001297
1298 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1300 view->transform.position.matrix.d[12] = view->geometry.x;
1301 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001302
1303 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001305 weston_matrix_multiply(matrix, &tform->matrix);
1306
Pekka Paalanen483243f2013-03-08 14:56:50 +02001307 if (parent)
1308 weston_matrix_multiply(matrix, &parent->transform.matrix);
1309
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001310 if (weston_matrix_invert(inverse, matrix) < 0) {
1311 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 weston_log("error: weston_view %p"
1313 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001314 return -1;
1315 }
1316
Giulio Camuffo148c1992016-09-04 18:50:46 +03001317 if (view->alpha == 1.0 &&
1318 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1319 pixman_region32_copy(&view->transform.opaque,
1320 &view->surface->opaque);
1321 pixman_region32_translate(&view->transform.opaque,
1322 matrix->d[12],
1323 matrix->d[13]);
1324 }
1325
Pekka Paalanen380adf52015-02-16 14:39:11 +02001326 pixman_region32_init_rect(&surfregion, 0, 0,
1327 view->surface->width, view->surface->height);
1328 if (view->geometry.scissor_enabled)
1329 pixman_region32_intersect(&surfregion, &surfregion,
1330 &view->geometry.scissor);
1331 surfbox = pixman_region32_extents(&surfregion);
1332
1333 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1334 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001335
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001336 return 0;
1337}
1338
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001339static struct weston_layer *
1340get_view_layer(struct weston_view *view)
1341{
1342 if (view->parent_view)
1343 return get_view_layer(view->parent_view);
1344 return view->layer_link.layer;
1345}
1346
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001347WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001349{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001351 struct weston_layer *layer;
1352 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001353
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001355 return;
1356
Pekka Paalanen483243f2013-03-08 14:56:50 +02001357 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001358 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001359
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001361
Jason Ekstranda7af7042013-10-12 22:38:11 -05001362 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001363
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364 pixman_region32_fini(&view->transform.boundingbox);
1365 pixman_region32_fini(&view->transform.opaque);
1366 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001367
Pekka Paalanencd403622012-01-25 13:37:39 +02001368 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 if (view->geometry.transformation_list.next ==
1370 &view->transform.position.link &&
1371 view->geometry.transformation_list.prev ==
1372 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001373 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001375 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001376 if (weston_view_update_transform_enable(view) < 0)
1377 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001378 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001379
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001380 layer = get_view_layer(view);
1381 if (layer) {
1382 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001383 pixman_region32_intersect(&view->transform.boundingbox,
1384 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001385 pixman_region32_intersect(&view->transform.opaque,
1386 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001387 pixman_region32_fini(&mask);
1388 }
1389
Pekka Paalanen380adf52015-02-16 14:39:11 +02001390 if (parent) {
1391 if (parent->geometry.scissor_enabled) {
1392 view->geometry.scissor_enabled = true;
1393 weston_view_transfer_scissor(parent, view);
1394 } else {
1395 view->geometry.scissor_enabled = false;
1396 }
1397 }
1398
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001400
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001402
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403 wl_signal_emit(&view->surface->compositor->transform_signal,
1404 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001405}
1406
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001407WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001409{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001411
1412 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 * The invariant: if view->geometry.dirty, then all views
1414 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001415 * Corollary: if not parent->geometry.dirty, then all ancestors
1416 * are not dirty.
1417 */
1418
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001420 return;
1421
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001423
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001425 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001426 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001427}
1428
1429WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001430weston_view_to_global_fixed(struct weston_view *view,
1431 wl_fixed_t vx, wl_fixed_t vy,
1432 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001433{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001434 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001435
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 weston_view_to_global_float(view,
1437 wl_fixed_to_double(vx),
1438 wl_fixed_to_double(vy),
1439 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001440 *x = wl_fixed_from_double(xf);
1441 *y = wl_fixed_from_double(yf);
1442}
1443
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001444WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445weston_view_from_global_float(struct weston_view *view,
1446 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001447{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001449 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1450
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001452
1453 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001454 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001456 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 *vx = 0;
1458 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001459 return;
1460 }
1461
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462 *vx = v.f[0] / v.f[3];
1463 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001464 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001465 *vx = x - view->geometry.x;
1466 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001467 }
1468}
1469
1470WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471weston_view_from_global_fixed(struct weston_view *view,
1472 wl_fixed_t x, wl_fixed_t y,
1473 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001474{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001475 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001476
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477 weston_view_from_global_float(view,
1478 wl_fixed_to_double(x),
1479 wl_fixed_to_double(y),
1480 &vxf, &vyf);
1481 *vx = wl_fixed_from_double(vxf);
1482 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001483}
1484
1485WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486weston_view_from_global(struct weston_view *view,
1487 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001488{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001490
Jason Ekstranda7af7042013-10-12 22:38:11 -05001491 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1492 *vx = floorf(vxf);
1493 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001494}
1495
Bryce Harrington3f650b82015-12-23 11:01:58 -08001496/**
1497 * \param surface The surface to be repainted
1498 *
1499 * Marks the output(s) that the surface is shown on as needing to be
1500 * repainted. See weston_output_schedule_repaint().
1501 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001502WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001503weston_surface_schedule_repaint(struct weston_surface *surface)
1504{
1505 struct weston_output *output;
1506
1507 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001508 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001509 weston_output_schedule_repaint(output);
1510}
1511
Bryce Harrington3f650b82015-12-23 11:01:58 -08001512/**
1513 * \param view The view to be repainted
1514 *
1515 * Marks the output(s) that the view is shown on as needing to be
1516 * repainted. See weston_output_schedule_repaint().
1517 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001518WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001519weston_view_schedule_repaint(struct weston_view *view)
1520{
1521 struct weston_output *output;
1522
1523 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001524 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001525 weston_output_schedule_repaint(output);
1526}
1527
Pekka Paalanene508ce62015-02-19 13:59:55 +02001528/**
1529 * XXX: This function does it the wrong way.
1530 * surface->damage is the damage from the client, and causes
1531 * surface_flush_damage() to copy pixels. No window management action can
1532 * cause damage to the client-provided content, warranting re-upload!
1533 *
1534 * Instead of surface->damage, this function should record the damage
1535 * with all the views for this surface to avoid extraneous texture
1536 * uploads.
1537 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001538WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001539weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001540{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001541 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001542 0, 0, surface->width,
1543 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001544
Kristian Høgsberg98238702012-08-03 16:29:12 -04001545 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001546}
1547
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001548WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001549weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001550{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001551 if (view->geometry.x == x && view->geometry.y == y)
1552 return;
1553
Jason Ekstranda7af7042013-10-12 22:38:11 -05001554 view->geometry.x = x;
1555 view->geometry.y = y;
1556 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001557}
1558
Pekka Paalanen483243f2013-03-08 14:56:50 +02001559static void
1560transform_parent_handle_parent_destroy(struct wl_listener *listener,
1561 void *data)
1562{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001563 struct weston_view *view =
1564 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001565 geometry.parent_destroy_listener);
1566
Jason Ekstranda7af7042013-10-12 22:38:11 -05001567 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001568}
1569
1570WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001571weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001572 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001573{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574 if (view->geometry.parent) {
1575 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1576 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001577
1578 if (!parent)
1579 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001580 }
1581
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001583
Jason Ekstranda7af7042013-10-12 22:38:11 -05001584 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001585 transform_parent_handle_parent_destroy;
1586 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001587 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001589 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001590 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001591 }
1592
Jason Ekstranda7af7042013-10-12 22:38:11 -05001593 weston_view_geometry_dirty(view);
1594}
1595
Pekka Paalanen380adf52015-02-16 14:39:11 +02001596/** Set a clip mask rectangle on a view
1597 *
1598 * \param view The view to set the clip mask on.
1599 * \param x Top-left corner X coordinate of the clip rectangle.
1600 * \param y Top-left corner Y coordinate of the clip rectangle.
1601 * \param width Width of the clip rectangle, non-negative.
1602 * \param height Height of the clip rectangle, non-negative.
1603 *
1604 * A shell may set a clip mask rectangle on a view. Everything outside
1605 * the rectangle is cut away for input and output purposes: it is
1606 * not drawn and cannot be hit by hit-test based input like pointer
1607 * motion or touch-downs. Everything inside the rectangle will behave
1608 * normally. Clients are unaware of clipping.
1609 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001610 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001611 * mask rectangle does not affect the view position, the view is positioned
1612 * as it would be without a clip. The clip also does not change
1613 * weston_surface::width,height.
1614 *
1615 * The clip mask rectangle is part of transformation inheritance
1616 * (weston_view_set_transform_parent()). A clip set in the root of the
1617 * transformation inheritance tree will affect all views in the tree.
1618 * A clip can be set only on the root view. Attempting to set a clip
1619 * on view that has a transformation parent will fail. Assigning a parent
1620 * to a view that has a clip set will cause the clip to be forgotten.
1621 *
1622 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1623 * on the additional transformations in the child views. These transformations
1624 * may not rotate the coordinate axes, i.e., only translation and scaling
1625 * are allowed. Violating this restriction causes the clipping to malfunction.
1626 * Furthermore, using scaling may cause rounding errors in child clipping.
1627 *
1628 * The clip mask rectangle is not automatically adjusted based on
1629 * wl_surface.attach dx and dy arguments.
1630 *
1631 * A clip mask rectangle can be set only if the compositor capability
1632 * WESTON_CAP_VIEW_CLIP_MASK is present.
1633 *
1634 * This function sets the clip mask rectangle and schedules a repaint for
1635 * the view.
1636 */
1637WL_EXPORT void
1638weston_view_set_mask(struct weston_view *view,
1639 int x, int y, int width, int height)
1640{
1641 struct weston_compositor *compositor = view->surface->compositor;
1642
1643 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1644 weston_log("%s not allowed without capability!\n", __func__);
1645 return;
1646 }
1647
1648 if (view->geometry.parent) {
1649 weston_log("view %p has a parent, clip forbidden!\n", view);
1650 return;
1651 }
1652
1653 if (width < 0 || height < 0) {
1654 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1655 x, y, width, height);
1656 return;
1657 }
1658
1659 pixman_region32_fini(&view->geometry.scissor);
1660 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1661 view->geometry.scissor_enabled = true;
1662 weston_view_geometry_dirty(view);
1663 weston_view_schedule_repaint(view);
1664}
1665
1666/** Remove the clip mask from a view
1667 *
1668 * \param view The view to remove the clip mask from.
1669 *
1670 * Removed the clip mask rectangle and schedules a repaint.
1671 *
1672 * \sa weston_view_set_mask
1673 */
1674WL_EXPORT void
1675weston_view_set_mask_infinite(struct weston_view *view)
1676{
1677 view->geometry.scissor_enabled = false;
1678 weston_view_geometry_dirty(view);
1679 weston_view_schedule_repaint(view);
1680}
1681
Armin Krezović0da12b82016-06-30 06:04:33 +02001682/* Check if view should be displayed
1683 *
1684 * The indicator is set manually when assigning
1685 * a view to a surface.
1686 *
1687 * This needs reworking. See the thread starting at:
1688 *
1689 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1690 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001691WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692weston_view_is_mapped(struct weston_view *view)
1693{
Armin Krezović0da12b82016-06-30 06:04:33 +02001694 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001695}
1696
Philipp Zabel70decd52018-09-03 20:11:15 +02001697/* Check if view is opaque in specified region
1698 *
1699 * \param view The view to check for opacity.
1700 * \param region The region to check for opacity, in view coordinates.
1701 *
1702 * Returns true if the view is opaque in the specified region, because view
1703 * alpha is 1.0 and either the opaque region set by the client contains the
1704 * specified region, or the buffer pixel format or solid color is opaque.
1705 */
1706WL_EXPORT bool
1707weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1708{
1709 pixman_region32_t r;
1710 bool ret = false;
1711
1712 if (ev->alpha < 1.0)
1713 return false;
1714
1715 if (ev->surface->is_opaque)
1716 return true;
1717
1718 if (ev->transform.dirty) {
1719 weston_log("%s: transform dirty", __func__);
1720 return false;
1721 }
1722
1723 pixman_region32_init(&r);
1724 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1725
1726 if (!pixman_region32_not_empty(&r))
1727 ret = true;
1728
1729 pixman_region32_fini(&r);
1730
1731 return ret;
1732}
1733
Armin Krezović0da12b82016-06-30 06:04:33 +02001734/* Check if a surface has a view assigned to it
1735 *
1736 * The indicator is set manually when mapping
1737 * a surface and creating a view for it.
1738 *
1739 * This needs to go. See the thread starting at:
1740 *
1741 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1742 *
1743 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001744WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001745weston_surface_is_mapped(struct weston_surface *surface)
1746{
Armin Krezović0da12b82016-06-30 06:04:33 +02001747 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001748}
1749
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001750static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001751surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001752{
1753 struct weston_view *view;
1754
1755 if (surface->width == width && surface->height == height)
1756 return;
1757
1758 surface->width = width;
1759 surface->height = height;
1760
1761 wl_list_for_each(view, &surface->views, surface_link)
1762 weston_view_geometry_dirty(view);
1763}
1764
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001765WL_EXPORT void
1766weston_surface_set_size(struct weston_surface *surface,
1767 int32_t width, int32_t height)
1768{
1769 assert(!surface->resource);
1770 surface_set_size(surface, width, height);
1771}
1772
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001773static int
1774fixed_round_up_to_int(wl_fixed_t f)
1775{
1776 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1777}
1778
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001779static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001780convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1781 int32_t width, int32_t height,
1782 uint32_t transform,
1783 int32_t scale)
1784{
1785 assert(scale > 0);
1786
1787 switch (transform) {
1788 case WL_OUTPUT_TRANSFORM_NORMAL:
1789 case WL_OUTPUT_TRANSFORM_180:
1790 case WL_OUTPUT_TRANSFORM_FLIPPED:
1791 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1792 *width_out = width / scale;
1793 *height_out = height / scale;
1794 break;
1795 case WL_OUTPUT_TRANSFORM_90:
1796 case WL_OUTPUT_TRANSFORM_270:
1797 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1798 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1799 *width_out = height / scale;
1800 *height_out = width / scale;
1801 break;
1802 default:
1803 assert(0 && "invalid transform");
1804 }
1805}
1806
1807static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001808weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001809{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001810 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001811
1812 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001813 surface->width_from_buffer = 0;
1814 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001815 return;
1816 }
1817
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001818 convert_size_by_transform_scale(&surface->width_from_buffer,
1819 &surface->height_from_buffer,
1820 surface->buffer_ref.buffer->width,
1821 surface->buffer_ref.buffer->height,
1822 vp->buffer.transform,
1823 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001824}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001825
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001826static void
1827weston_surface_update_size(struct weston_surface *surface)
1828{
1829 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1830 int32_t width, height;
1831
1832 width = surface->width_from_buffer;
1833 height = surface->height_from_buffer;
1834
1835 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001836 surface_set_size(surface,
1837 vp->surface.width, vp->surface.height);
1838 return;
1839 }
1840
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001841 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001842 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1843 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1844
1845 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001846 return;
1847 }
1848
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001849 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001850}
1851
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001852WL_EXPORT void
1853weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001854{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001855 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001856}
1857
Jason Ekstranda7af7042013-10-12 22:38:11 -05001858WL_EXPORT struct weston_view *
1859weston_compositor_pick_view(struct weston_compositor *compositor,
1860 wl_fixed_t x, wl_fixed_t y,
1861 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001862{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001863 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001864 wl_fixed_t view_x, view_y;
1865 int view_ix, view_iy;
1866 int ix = wl_fixed_to_int(x);
1867 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001868
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001870 if (!pixman_region32_contains_point(
1871 &view->transform.boundingbox, ix, iy, NULL))
1872 continue;
1873
1874 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1875 view_ix = wl_fixed_to_int(view_x);
1876 view_iy = wl_fixed_to_int(view_y);
1877
1878 if (!pixman_region32_contains_point(&view->surface->input,
1879 view_ix, view_iy, NULL))
1880 continue;
1881
Pekka Paalanen380adf52015-02-16 14:39:11 +02001882 if (view->geometry.scissor_enabled &&
1883 !pixman_region32_contains_point(&view->geometry.scissor,
1884 view_ix, view_iy, NULL))
1885 continue;
1886
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001887 *vx = view_x;
1888 *vy = view_y;
1889 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001890 }
1891
Derek Foremanf9318d12015-05-11 15:40:11 -05001892 *vx = wl_fixed_from_int(-1000000);
1893 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001894 return NULL;
1895}
1896
1897static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001898weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001899{
Daniel Stone37816df2012-05-16 18:45:18 +01001900 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001901
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001902 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001903 return;
1904
Daniel Stone37816df2012-05-16 18:45:18 +01001905 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001906 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001907}
1908
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001909WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001910weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001911{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001912 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001913
Jason Ekstranda7af7042013-10-12 22:38:11 -05001914 if (!weston_view_is_mapped(view))
1915 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001916
Jason Ekstranda7af7042013-10-12 22:38:11 -05001917 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02001918 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08001919 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001920 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001921 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001922 wl_list_remove(&view->link);
1923 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001924 view->output_mask = 0;
1925 weston_surface_assign_output(view->surface);
1926
1927 if (weston_surface_is_mapped(view->surface))
1928 return;
1929
1930 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001931 struct weston_touch *touch = weston_seat_get_touch(seat);
1932 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1933 struct weston_keyboard *keyboard =
1934 weston_seat_get_keyboard(seat);
1935
1936 if (keyboard && keyboard->focus == view->surface)
1937 weston_keyboard_set_focus(keyboard, NULL);
1938 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001939 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001940 if (touch && touch->focus == view)
1941 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001942 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001943}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001944
Jason Ekstranda7af7042013-10-12 22:38:11 -05001945WL_EXPORT void
1946weston_surface_unmap(struct weston_surface *surface)
1947{
1948 struct weston_view *view;
1949
Armin Krezovićf8486c32016-06-30 06:04:28 +02001950 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001951 wl_list_for_each(view, &surface->views, surface_link)
1952 weston_view_unmap(view);
1953 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001954}
1955
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001956static void
1957weston_surface_reset_pending_buffer(struct weston_surface *surface)
1958{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001959 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001960 surface->pending.sx = 0;
1961 surface->pending.sy = 0;
1962 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001963 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001964}
1965
Jason Ekstranda7af7042013-10-12 22:38:11 -05001966WL_EXPORT void
1967weston_view_destroy(struct weston_view *view)
1968{
1969 wl_signal_emit(&view->destroy_signal, view);
1970
1971 assert(wl_list_empty(&view->geometry.child_list));
1972
1973 if (weston_view_is_mapped(view)) {
1974 weston_view_unmap(view);
1975 weston_compositor_build_view_list(view->surface->compositor);
1976 }
1977
1978 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001979 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001980
1981 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001982 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001983 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001984 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001985
1986 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03001987 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001988
Jason Ekstranda7af7042013-10-12 22:38:11 -05001989 wl_list_remove(&view->surface_link);
1990
1991 free(view);
1992}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001993
1994WL_EXPORT void
1995weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001996{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001997 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001998 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001999 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002000
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002001 if (--surface->ref_count > 0)
2002 return;
2003
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002004 assert(surface->resource == NULL);
2005
Pekka Paalanenca790762015-04-17 14:23:38 +03002006 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002007
Pekka Paalanene67858b2013-04-25 13:57:42 +03002008 assert(wl_list_empty(&surface->subsurface_list_pending));
2009 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002010
Jason Ekstranda7af7042013-10-12 22:38:11 -05002011 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2012 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002013
Jason Ekstrand7b982072014-05-20 14:33:03 -05002014 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002015
Pekka Paalanende685b82012-12-04 15:58:12 +02002016 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002017 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002018
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002019 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002020 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002021 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002022
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002023 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002024 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002025
Pekka Paalanen133e4392014-09-23 22:08:46 -04002026 weston_presentation_feedback_discard_list(&surface->feedback_list);
2027
Jonas Ådahld3414f22016-07-22 17:56:31 +08002028 wl_list_for_each_safe(constraint, next_constraint,
2029 &surface->pointer_constraints,
2030 link)
2031 weston_pointer_constraint_destroy(constraint);
2032
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002033 fd_clear(&surface->acquire_fence_fd);
2034
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002035 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002036}
2037
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002038static void
2039destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002040{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002041 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002042
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002043 assert(surface);
2044
Giulio Camuffo0d379742013-11-15 22:06:15 +01002045 /* Set the resource to NULL, since we don't want to leave a
2046 * dangling pointer if the surface was refcounted and survives
2047 * the weston_surface_destroy() call. */
2048 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002049
2050 if (surface->viewport_resource)
2051 wl_resource_set_user_data(surface->viewport_resource, NULL);
2052
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002053 if (surface->synchronization_resource) {
2054 wl_resource_set_user_data(surface->synchronization_resource,
2055 NULL);
2056 }
2057
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002058 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002059}
2060
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002061static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002062weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2063{
2064 struct weston_buffer *buffer =
2065 container_of(listener, struct weston_buffer, destroy_listener);
2066
2067 wl_signal_emit(&buffer->destroy_signal, buffer);
2068 free(buffer);
2069}
2070
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002071WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002072weston_buffer_from_resource(struct wl_resource *resource)
2073{
2074 struct weston_buffer *buffer;
2075 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002076
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002077 listener = wl_resource_get_destroy_listener(resource,
2078 weston_buffer_destroy_handler);
2079
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002080 if (listener)
2081 return container_of(listener, struct weston_buffer,
2082 destroy_listener);
2083
2084 buffer = zalloc(sizeof *buffer);
2085 if (buffer == NULL)
2086 return NULL;
2087
2088 buffer->resource = resource;
2089 wl_signal_init(&buffer->destroy_signal);
2090 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002091 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002092 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002093
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002094 return buffer;
2095}
2096
2097static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002098weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2099 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002100{
Pekka Paalanende685b82012-12-04 15:58:12 +02002101 struct weston_buffer_reference *ref =
2102 container_of(listener, struct weston_buffer_reference,
2103 destroy_listener);
2104
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002105 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002106 ref->buffer = NULL;
2107}
2108
2109WL_EXPORT void
2110weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002111 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002112{
2113 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002114 ref->buffer->busy_count--;
2115 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002116 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002117 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002118 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002119 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002120 }
2121
Pekka Paalanende685b82012-12-04 15:58:12 +02002122 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002123 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002124 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002125 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002126 }
2127
Pekka Paalanende685b82012-12-04 15:58:12 +02002128 ref->buffer = buffer;
2129 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2130}
2131
2132static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002133weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2134 void *data)
2135{
2136 struct weston_buffer_release_reference *ref =
2137 container_of(listener, struct weston_buffer_release_reference,
2138 destroy_listener);
2139
2140 assert((struct wl_resource *)data == ref->buffer_release->resource);
2141 ref->buffer_release = NULL;
2142}
2143
2144static void
2145weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2146{
2147 struct wl_resource *resource = buffer_release->resource;
2148 int release_fence_fd = buffer_release->fence_fd;
2149
2150 if (release_fence_fd >= 0) {
2151 zwp_linux_buffer_release_v1_send_fenced_release(
2152 resource, release_fence_fd);
2153 } else {
2154 zwp_linux_buffer_release_v1_send_immediate_release(
2155 resource);
2156 }
2157
2158 wl_resource_destroy(resource);
2159}
2160
2161WL_EXPORT void
2162weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2163 struct weston_buffer_release *buffer_release)
2164{
2165 if (buffer_release == ref->buffer_release)
2166 return;
2167
2168 if (ref->buffer_release) {
2169 ref->buffer_release->ref_count--;
2170 wl_list_remove(&ref->destroy_listener.link);
2171 if (ref->buffer_release->ref_count == 0)
2172 weston_buffer_release_destroy(ref->buffer_release);
2173 }
2174
2175 if (buffer_release) {
2176 buffer_release->ref_count++;
2177 wl_resource_add_destroy_listener(buffer_release->resource,
2178 &ref->destroy_listener);
2179 }
2180
2181 ref->buffer_release = buffer_release;
2182 ref->destroy_listener.notify =
2183 weston_buffer_release_reference_handle_destroy;
2184}
2185
2186WL_EXPORT void
2187weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2188 struct weston_buffer_release_reference *src)
2189{
2190 weston_buffer_release_reference(dest, src->buffer_release);
2191 weston_buffer_release_reference(src, NULL);
2192}
2193
2194static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002195weston_surface_attach(struct weston_surface *surface,
2196 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002197{
2198 weston_buffer_reference(&surface->buffer_ref, buffer);
2199
Pekka Paalanena6421c42012-12-04 15:58:10 +02002200 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002201 if (weston_surface_is_mapped(surface))
2202 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002203 }
2204
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002205 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002206
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002207 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002208 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002209}
2210
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002211WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002212weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002213{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002214 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002215
2216 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002217 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002218}
2219
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002220WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002221weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002222{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002223 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002224
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002225 pixman_region32_union(&compositor->primary_plane.damage,
2226 &compositor->primary_plane.damage,
2227 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002228 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002229}
2230
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002231static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002232surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002233{
Pekka Paalanende685b82012-12-04 15:58:12 +02002234 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002235 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002236 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002237
Pekka Paalanenb5026542014-11-12 15:09:24 +02002238 if (weston_timeline_enabled_ &&
2239 pixman_region32_not_empty(&surface->damage))
2240 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2241 TLP_OUTPUT(surface->output), TLP_END);
2242
Jason Ekstrandef540082014-06-26 10:37:36 -07002243 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002244}
2245
2246static void
2247view_accumulate_damage(struct weston_view *view,
2248 pixman_region32_t *opaque)
2249{
2250 pixman_region32_t damage;
2251
2252 pixman_region32_init(&damage);
2253 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002254 pixman_box32_t *extents;
2255
Jason Ekstranda7af7042013-10-12 22:38:11 -05002256 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002257 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002258 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002259 pixman_region32_copy(&damage, &view->surface->damage);
2260 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002261 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002262 }
2263
Pekka Paalanen380adf52015-02-16 14:39:11 +02002264 pixman_region32_intersect(&damage, &damage,
2265 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002266 pixman_region32_subtract(&damage, &damage, opaque);
2267 pixman_region32_union(&view->plane->damage,
2268 &view->plane->damage, &damage);
2269 pixman_region32_fini(&damage);
2270 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002271 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002272}
2273
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002274static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002275compositor_accumulate_damage(struct weston_compositor *ec)
2276{
2277 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002278 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002279 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002280
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002281 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002282
2283 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002284 pixman_region32_copy(&plane->clip, &clip);
2285
2286 pixman_region32_init(&opaque);
2287
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288 wl_list_for_each(ev, &ec->view_list, link) {
2289 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002290 continue;
2291
Jason Ekstranda7af7042013-10-12 22:38:11 -05002292 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002293 }
2294
2295 pixman_region32_union(&clip, &clip, &opaque);
2296 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002297 }
2298
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002299 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002300
Jason Ekstranda7af7042013-10-12 22:38:11 -05002301 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002302 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002303
2304 wl_list_for_each(ev, &ec->view_list, link) {
2305 if (ev->surface->touched)
2306 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002307 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002308
2309 surface_flush_damage(ev->surface);
2310
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002311 /* Both the renderer and the backend have seen the buffer
2312 * by now. If renderer needs the buffer, it has its own
2313 * reference set. If the backend wants to keep the buffer
2314 * around for migrating the surface into a non-primary plane
2315 * later, keep_buffer is true. Otherwise, drop the core
2316 * reference now, and allow early buffer release. This enables
2317 * clients to use single-buffering.
2318 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002319 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002320 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002321 weston_buffer_release_reference(
2322 &ev->surface->buffer_release_ref, NULL);
2323 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002324 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002325}
2326
2327static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002328surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002329{
2330 struct weston_subsurface *sub;
2331
Pekka Paalanene67858b2013-04-25 13:57:42 +03002332 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002333 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002334 continue;
2335
Jason Ekstranda7af7042013-10-12 22:38:11 -05002336 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2337 wl_list_init(&sub->surface->views);
2338
2339 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002340 }
2341}
2342
2343static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002344surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002345{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002346 struct weston_subsurface *sub;
2347 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002348
Jason Ekstranda7af7042013-10-12 22:38:11 -05002349 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2350 if (sub->surface == surface)
2351 continue;
2352
George Kiagiadakised04d382014-06-13 18:10:26 +02002353 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2354 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002355 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002356 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002357
2358 surface_free_unused_subsurface_views(sub->surface);
2359 }
2360}
2361
2362static void
2363view_list_add_subsurface_view(struct weston_compositor *compositor,
2364 struct weston_subsurface *sub,
2365 struct weston_view *parent)
2366{
2367 struct weston_subsurface *child;
2368 struct weston_view *view = NULL, *iv;
2369
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002370 if (!weston_surface_is_mapped(sub->surface))
2371 return;
2372
Jason Ekstranda7af7042013-10-12 22:38:11 -05002373 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2374 if (iv->geometry.parent == parent) {
2375 view = iv;
2376 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002377 }
2378 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002379
2380 if (view) {
2381 /* Put it back in the surface's list of views */
2382 wl_list_remove(&view->surface_link);
2383 wl_list_insert(&sub->surface->views, &view->surface_link);
2384 } else {
2385 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002386 weston_view_set_position(view,
2387 sub->position.x,
2388 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002389 weston_view_set_transform_parent(view, parent);
2390 }
2391
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002392 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002393 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002394 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002395
Pekka Paalanenb188e912013-11-19 14:03:35 +02002396 if (wl_list_empty(&sub->surface->subsurface_list)) {
2397 wl_list_insert(compositor->view_list.prev, &view->link);
2398 return;
2399 }
2400
2401 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2402 if (child->surface == sub->surface)
2403 wl_list_insert(compositor->view_list.prev, &view->link);
2404 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002405 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002406 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002407}
2408
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002409/* This recursively adds the sub-surfaces for a view, relying on the
2410 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2411 * change first happens to the sub-surface list, and then automatically
2412 * propagates here. See weston_surface_damage_subsurfaces() for how the
2413 * sub-surfaces receive damage when the client changes the state.
2414 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002415static void
2416view_list_add(struct weston_compositor *compositor,
2417 struct weston_view *view)
2418{
2419 struct weston_subsurface *sub;
2420
2421 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002422
Pekka Paalanenb188e912013-11-19 14:03:35 +02002423 if (wl_list_empty(&view->surface->subsurface_list)) {
2424 wl_list_insert(compositor->view_list.prev, &view->link);
2425 return;
2426 }
2427
2428 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2429 if (sub->surface == view->surface)
2430 wl_list_insert(compositor->view_list.prev, &view->link);
2431 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002432 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002433 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002434}
2435
2436static void
2437weston_compositor_build_view_list(struct weston_compositor *compositor)
2438{
2439 struct weston_view *view;
2440 struct weston_layer *layer;
2441
2442 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002443 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002444 surface_stash_subsurface_views(view->surface);
2445
2446 wl_list_init(&compositor->view_list);
2447 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002448 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002449 view_list_add(compositor, view);
2450 }
2451 }
2452
2453 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002454 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002455 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002456}
2457
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002458static void
2459weston_output_take_feedback_list(struct weston_output *output,
2460 struct weston_surface *surface)
2461{
2462 struct weston_view *view;
2463 struct weston_presentation_feedback *feedback;
2464 uint32_t flags = 0xffffffff;
2465
2466 if (wl_list_empty(&surface->feedback_list))
2467 return;
2468
2469 /* All views must have the flag for the flag to survive. */
2470 wl_list_for_each(view, &surface->views, surface_link) {
2471 /* ignore views that are not on this output at all */
2472 if (view->output_mask & (1u << output->id))
2473 flags &= view->psf_flags;
2474 }
2475
2476 wl_list_for_each(feedback, &surface->feedback_list, link)
2477 feedback->psf_flags = flags;
2478
2479 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2480 wl_list_init(&surface->feedback_list);
2481}
2482
David Herrmann1edf44c2013-10-22 17:11:26 +02002483static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002484weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002485{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002486 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002487 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002488 struct weston_animation *animation, *next;
2489 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002490 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002491 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002492 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002493 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002494
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002495 if (output->destroying)
2496 return 0;
2497
Pekka Paalanenb5026542014-11-12 15:09:24 +02002498 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2499
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002500 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002501 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002502
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002503 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002504 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002505 } else {
2506 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002507 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002508 ev->psf_flags = 0;
2509 }
2510 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002511
Pekka Paalanene67858b2013-04-25 13:57:42 +03002512 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002513 wl_list_for_each(ev, &ec->view_list, link) {
2514 /* Note: This operation is safe to do multiple times on the
2515 * same surface.
2516 */
2517 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002518 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002519 &ev->surface->frame_callback_list);
2520 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002521
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002522 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002523 }
2524 }
2525
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002526 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002527
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002528 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002529 pixman_region32_intersect(&output_damage,
2530 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002531 pixman_region32_subtract(&output_damage,
2532 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002533
Scott Moreauccbf29d2012-02-22 14:21:41 -07002534 if (output->dirty)
2535 weston_output_update_matrix(output);
2536
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002537 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002538
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002539 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002540
Daniel Stone09a97e22017-03-01 11:34:06 +00002541 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002542 if (r == 0)
2543 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002544
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002545 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002546
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002547 frame_time_msec = timespec_to_msec(&output->frame_time);
2548
Jonas Ådahldb773762012-06-13 00:01:21 +02002549 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002550 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002551 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002552 }
2553
Scott Moreaud64cf212012-06-08 19:40:54 -06002554 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002555 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002556 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002557 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002558
Pekka Paalanenb5026542014-11-12 15:09:24 +02002559 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2560
David Herrmann1edf44c2013-10-22 17:11:26 +02002561 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002562}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002563
Pekka Paalanen82919792014-05-21 13:51:49 +03002564static void
2565weston_output_schedule_repaint_reset(struct weston_output *output)
2566{
Daniel Stone05df8c12017-03-03 16:59:42 +00002567 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002568 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002569}
2570
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002571static int
2572weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2573 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002574{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002575 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002576 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002577 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002578
Daniel Stone6847b852017-03-01 11:34:08 +00002579 /* We're not ready yet; come back to make a decision later. */
2580 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002581 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002582
2583 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2584 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002585 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002586
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002587 /* If we're sleeping, drop the repaint machinery entirely; we will
2588 * explicitly repaint all outputs when we come back. */
2589 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2590 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2591 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002592
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002593 /* We don't actually need to repaint this output; drop it from
2594 * repaint until something causes damage. */
2595 if (!output->repaint_needed)
2596 goto err;
2597
2598 /* If repaint fails, we aren't going to get weston_output_finish_frame
2599 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002600 * something schedules a successful repaint later. As repainting may
2601 * take some time, re-read our clock as a courtesy to the next
2602 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002603 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002604 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002605 if (ret != 0)
2606 goto err;
2607
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002608 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002609 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002610
2611err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002612 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002613 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002614}
2615
2616static void
2617output_repaint_timer_arm(struct weston_compositor *compositor)
2618{
2619 struct weston_output *output;
2620 bool any_should_repaint = false;
2621 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002622 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002623
2624 weston_compositor_read_presentation_clock(compositor, &now);
2625
2626 wl_list_for_each(output, &compositor->output_list, link) {
2627 int64_t msec_to_this;
2628
2629 if (output->repaint_status != REPAINT_SCHEDULED)
2630 continue;
2631
2632 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2633 &now);
2634 if (!any_should_repaint || msec_to_this < msec_to_next)
2635 msec_to_next = msec_to_this;
2636
2637 any_should_repaint = true;
2638 }
2639
2640 if (!any_should_repaint)
2641 return;
2642
2643 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2644 * This is a workaround to allow coalescing multiple output repaints
2645 * particularly from weston_output_finish_frame()
2646 * into the same call, which would not happen if we called
2647 * output_repaint_timer_handler() directly.
2648 */
2649 if (msec_to_next < 1)
2650 msec_to_next = 1;
2651
2652 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2653}
2654
2655static int
2656output_repaint_timer_handler(void *data)
2657{
2658 struct weston_compositor *compositor = data;
2659 struct weston_output *output;
2660 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002661 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002662 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002663
2664 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002665
2666 if (compositor->backend->repaint_begin)
2667 repaint_data = compositor->backend->repaint_begin(compositor);
2668
2669 wl_list_for_each(output, &compositor->output_list, link) {
2670 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2671 if (ret)
2672 break;
2673 }
2674
2675 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002676 if (compositor->backend->repaint_flush)
2677 compositor->backend->repaint_flush(compositor,
2678 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002679 } else {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002680 wl_list_for_each(output, &compositor->output_list, link) {
2681 if (output->repainted)
2682 weston_output_schedule_repaint_reset(output);
2683 }
2684
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002685 if (compositor->backend->repaint_cancel)
2686 compositor->backend->repaint_cancel(compositor,
2687 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002688 }
Daniel Stone6847b852017-03-01 11:34:08 +00002689
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002690 wl_list_for_each(output, &compositor->output_list, link)
2691 output->repainted = false;
2692
Daniel Stone6847b852017-03-01 11:34:08 +00002693 output_repaint_timer_arm(compositor);
2694
Pekka Paalanen0513a952014-05-21 16:17:27 +03002695 return 0;
2696}
2697
Kristian Høgsbergef044142011-06-21 15:02:12 -04002698WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002699weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002700 const struct timespec *stamp,
2701 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002702{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002703 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002704 int32_t refresh_nsec;
2705 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002706 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002707
Pekka Paalanenb5026542014-11-12 15:09:24 +02002708
Daniel Stone05df8c12017-03-03 16:59:42 +00002709 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002710 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2711
Daniel Stone6847b852017-03-01 11:34:08 +00002712 weston_compositor_read_presentation_clock(compositor, &now);
2713
Daniel Stone3615ce12017-03-01 11:34:05 +00002714 /* If we haven't been supplied any timestamp at all, we don't have a
2715 * timebase to work against, so any delay just wastes time. Push a
2716 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002717 if (!stamp) {
2718 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002719 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002720 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002721
Marius Vladdf9278a2018-03-06 18:56:23 +02002722 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2723 TLP_VBLANK(stamp), TLP_END);
2724
Pekka Paalanend7894d02015-07-03 15:08:53 +03002725 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002726 weston_presentation_feedback_present_list(&output->feedback_list,
2727 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002728 output->msc,
2729 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002730
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002731 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002732
Daniel Stone6847b852017-03-01 11:34:08 +00002733 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2734 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2735 -compositor->repaint_msec);
2736 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002737
2738 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002739 static bool warned;
2740
2741 if (!warned)
2742 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002743 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002744 warned = true;
2745
Daniel Stone6847b852017-03-01 11:34:08 +00002746 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002747 }
2748
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002749 /* Called from restart_repaint_loop and restart happens already after
2750 * the deadline given by repaint_msec? In that case we delay until
2751 * the deadline of the next frame, to give clients a more predictable
2752 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002753 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2754 msec_rel < 0) {
2755 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2756 timespec_add_nsec(&output->next_repaint,
2757 &output->next_repaint,
2758 refresh_nsec);
2759 }
2760 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002761
Daniel Stone3615ce12017-03-01 11:34:05 +00002762out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002763 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002764 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002765}
2766
2767static void
2768idle_repaint(void *data)
2769{
2770 struct weston_output *output = data;
2771
Daniel Stone05df8c12017-03-03 16:59:42 +00002772 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2773 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002774 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002775 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002776}
2777
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002778WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002779weston_layer_entry_insert(struct weston_layer_entry *list,
2780 struct weston_layer_entry *entry)
2781{
2782 wl_list_insert(&list->link, &entry->link);
2783 entry->layer = list->layer;
2784}
2785
2786WL_EXPORT void
2787weston_layer_entry_remove(struct weston_layer_entry *entry)
2788{
2789 wl_list_remove(&entry->link);
2790 wl_list_init(&entry->link);
2791 entry->layer = NULL;
2792}
2793
Quentin Glidic82681572016-12-17 13:40:51 +01002794
2795/** Initialize the weston_layer struct.
2796 *
2797 * \param compositor The compositor instance
2798 * \param layer The layer to initialize
2799 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002800WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002801weston_layer_init(struct weston_layer *layer,
2802 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002803{
Quentin Glidic82681572016-12-17 13:40:51 +01002804 layer->compositor = compositor;
2805 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002806 wl_list_init(&layer->view_list.link);
2807 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002808 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002809}
2810
2811/** Sets the position of the layer in the layer list. The layer will be placed
2812 * below any layer with the same position value, if any.
2813 * This function is safe to call if the layer is already on the list, but the
2814 * layer may be moved below other layers at the same position, if any.
2815 *
2816 * \param layer The layer to modify
2817 * \param position The position the layer will be placed at
2818 */
2819WL_EXPORT void
2820weston_layer_set_position(struct weston_layer *layer,
2821 enum weston_layer_position position)
2822{
2823 struct weston_layer *below;
2824
2825 wl_list_remove(&layer->link);
2826
2827 /* layer_list is ordered from top to bottom, the last layer being the
2828 * background with the smallest position value */
2829
2830 layer->position = position;
2831 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2832 if (below->position >= layer->position) {
2833 wl_list_insert(&below->link, &layer->link);
2834 return;
2835 }
2836 }
2837 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2838}
2839
2840/** Hide a layer by taking it off the layer list.
2841 * This function is safe to call if the layer is not on the list.
2842 *
2843 * \param layer The layer to hide
2844 */
2845WL_EXPORT void
2846weston_layer_unset_position(struct weston_layer *layer)
2847{
2848 wl_list_remove(&layer->link);
2849 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002850}
2851
2852WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002853weston_layer_set_mask(struct weston_layer *layer,
2854 int x, int y, int width, int height)
2855{
2856 struct weston_view *view;
2857
2858 layer->mask.x1 = x;
2859 layer->mask.x2 = x + width;
2860 layer->mask.y1 = y;
2861 layer->mask.y2 = y + height;
2862
2863 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2864 weston_view_geometry_dirty(view);
2865 }
2866}
2867
2868WL_EXPORT void
2869weston_layer_set_mask_infinite(struct weston_layer *layer)
2870{
2871 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2872 UINT32_MAX, UINT32_MAX);
2873}
2874
Daniel Stone3b775632018-07-20 08:38:25 +01002875WL_EXPORT bool
2876weston_layer_mask_is_infinite(struct weston_layer *layer)
2877{
2878 return layer->mask.x1 == INT32_MIN &&
2879 layer->mask.y1 == INT32_MIN &&
2880 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
2881 layer->mask.y2 == INT32_MIN + UINT32_MAX;
2882}
2883
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002884WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002885weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002886{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002887 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002888 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002889
Bryce Harrington08976ac2016-08-30 12:05:16 -07002890 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2891 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002892 return;
2893
Pekka Paalanenb5026542014-11-12 15:09:24 +02002894 if (!output->repaint_needed)
2895 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2896
Kristian Høgsbergef044142011-06-21 15:02:12 -04002897 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002898 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002899
2900 /* If we already have a repaint scheduled for our idle handler,
2901 * no need to set it again. If the repaint has been called but
2902 * not finished, then weston_output_finish_frame() will notice
2903 * that a repaint is needed and schedule one. */
2904 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002905 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002906
Daniel Stone05df8c12017-03-03 16:59:42 +00002907 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002908 assert(!output->idle_repaint_source);
2909 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2910 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002911 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002912}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002913
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002914WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002915weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2916{
2917 struct weston_output *output;
2918
2919 wl_list_for_each(output, &compositor->output_list, link)
2920 weston_output_schedule_repaint(output);
2921}
2922
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002923static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002924surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002925{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002926 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002927}
2928
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002929static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002930surface_attach(struct wl_client *client,
2931 struct wl_resource *resource,
2932 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2933{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002934 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002935 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002936
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002937 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002938 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002939 if (buffer == NULL) {
2940 wl_client_post_no_memory(client);
2941 return;
2942 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002943 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002944
Pekka Paalanende685b82012-12-04 15:58:12 +02002945 /* Attach, attach, without commit in between does not send
2946 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002947 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002948
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002949 surface->pending.sx = sx;
2950 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002951 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002952}
2953
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002954static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002955surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002956 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002957 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002958{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002959 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002960
Derek Foreman57e92ed2015-11-17 14:11:35 -06002961 if (width <= 0 || height <= 0)
2962 return;
2963
Derek Foreman152254b2015-11-26 14:17:48 -06002964 pixman_region32_union_rect(&surface->pending.damage_surface,
2965 &surface->pending.damage_surface,
2966 x, y, width, height);
2967}
2968
2969static void
2970surface_damage_buffer(struct wl_client *client,
2971 struct wl_resource *resource,
2972 int32_t x, int32_t y, int32_t width, int32_t height)
2973{
2974 struct weston_surface *surface = wl_resource_get_user_data(resource);
2975
2976 if (width <= 0 || height <= 0)
2977 return;
2978
2979 pixman_region32_union_rect(&surface->pending.damage_buffer,
2980 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002981 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002982}
2983
Kristian Høgsberg33418202011-08-16 23:01:28 -04002984static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002985destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002986{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002987 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002988
2989 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002990 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002991}
2992
2993static void
2994surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002995 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002996{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002997 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002998 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002999
3000 cb = malloc(sizeof *cb);
3001 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003002 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003003 return;
3004 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003005
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003006 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3007 callback);
3008 if (cb->resource == NULL) {
3009 free(cb);
3010 wl_resource_post_no_memory(resource);
3011 return;
3012 }
3013
Jason Ekstranda85118c2013-06-27 20:17:02 -05003014 wl_resource_set_implementation(cb->resource, NULL, cb,
3015 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003016
Pekka Paalanenbc106382012-10-10 12:49:31 +03003017 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003018}
3019
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003020static void
3021surface_set_opaque_region(struct wl_client *client,
3022 struct wl_resource *resource,
3023 struct wl_resource *region_resource)
3024{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003025 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003026 struct weston_region *region;
3027
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003028 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003029 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003030 pixman_region32_copy(&surface->pending.opaque,
3031 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003032 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003033 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003034 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003035}
3036
3037static void
3038surface_set_input_region(struct wl_client *client,
3039 struct wl_resource *resource,
3040 struct wl_resource *region_resource)
3041{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003042 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003043 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003044
3045 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003046 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003047 pixman_region32_copy(&surface->pending.input,
3048 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003049 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003050 pixman_region32_fini(&surface->pending.input);
3051 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003052 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003053}
3054
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003055/* Cause damage to this sub-surface and all its children.
3056 *
3057 * This is useful when there are state changes that need an implicit
3058 * damage, e.g. a z-order change.
3059 */
3060static void
3061weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3062{
3063 struct weston_subsurface *child;
3064
3065 weston_surface_damage(sub->surface);
3066 sub->reordered = false;
3067
3068 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3069 if (child != sub)
3070 weston_surface_damage_subsurfaces(child);
3071}
3072
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003073static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003074weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003075{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003076 struct weston_subsurface *sub;
3077
3078 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3079 parent_link_pending) {
3080 wl_list_remove(&sub->parent_link);
3081 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003082
3083 if (sub->reordered)
3084 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003085 }
3086}
3087
3088static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003089weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003090 struct weston_matrix *matrix)
3091{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003092 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003093 double src_width, src_height, dest_width, dest_height;
3094
3095 weston_matrix_init(matrix);
3096
3097 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3098 src_width = surface->width_from_buffer;
3099 src_height = surface->height_from_buffer;
3100 } else {
3101 src_width = wl_fixed_to_double(vp->buffer.src_width);
3102 src_height = wl_fixed_to_double(vp->buffer.src_height);
3103 }
3104
3105 if (vp->surface.width == -1) {
3106 dest_width = src_width;
3107 dest_height = src_height;
3108 } else {
3109 dest_width = vp->surface.width;
3110 dest_height = vp->surface.height;
3111 }
3112
3113 if (src_width != dest_width || src_height != dest_height)
3114 weston_matrix_scale(matrix,
3115 src_width / dest_width,
3116 src_height / dest_height, 1);
3117
3118 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3119 weston_matrix_translate(matrix,
3120 wl_fixed_to_double(vp->buffer.src_x),
3121 wl_fixed_to_double(vp->buffer.src_y),
3122 0);
3123
3124 switch (vp->buffer.transform) {
3125 case WL_OUTPUT_TRANSFORM_FLIPPED:
3126 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3127 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3128 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3129 weston_matrix_scale(matrix, -1, 1, 1);
3130 weston_matrix_translate(matrix,
3131 surface->width_from_buffer, 0, 0);
3132 break;
3133 }
3134
3135 switch (vp->buffer.transform) {
3136 default:
3137 case WL_OUTPUT_TRANSFORM_NORMAL:
3138 case WL_OUTPUT_TRANSFORM_FLIPPED:
3139 break;
3140 case WL_OUTPUT_TRANSFORM_90:
3141 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3142 weston_matrix_rotate_xy(matrix, 0, 1);
3143 weston_matrix_translate(matrix,
3144 surface->height_from_buffer, 0, 0);
3145 break;
3146 case WL_OUTPUT_TRANSFORM_180:
3147 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3148 weston_matrix_rotate_xy(matrix, -1, 0);
3149 weston_matrix_translate(matrix,
3150 surface->width_from_buffer,
3151 surface->height_from_buffer, 0);
3152 break;
3153 case WL_OUTPUT_TRANSFORM_270:
3154 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3155 weston_matrix_rotate_xy(matrix, 0, -1);
3156 weston_matrix_translate(matrix,
3157 0, surface->width_from_buffer, 0);
3158 break;
3159 }
3160
3161 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3162}
3163
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003164/**
3165 * Compute a + b > c while being safe to overflows.
3166 */
3167static bool
3168fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3169{
3170 return (int64_t)a + (int64_t)b > (int64_t)c;
3171}
3172
3173static bool
3174weston_surface_is_pending_viewport_source_valid(
3175 const struct weston_surface *surface)
3176{
3177 const struct weston_surface_state *pend = &surface->pending;
3178 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3179 int width_from_buffer = 0;
3180 int height_from_buffer = 0;
3181 wl_fixed_t w;
3182 wl_fixed_t h;
3183
3184 /* If viewport source rect is not set, it is always ok. */
3185 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3186 return true;
3187
3188 if (pend->newly_attached) {
3189 if (pend->buffer) {
3190 convert_size_by_transform_scale(&width_from_buffer,
3191 &height_from_buffer,
3192 pend->buffer->width,
3193 pend->buffer->height,
3194 vp->buffer.transform,
3195 vp->buffer.scale);
3196 } else {
3197 /* No buffer: viewport is irrelevant. */
3198 return true;
3199 }
3200 } else {
3201 width_from_buffer = surface->width_from_buffer;
3202 height_from_buffer = surface->height_from_buffer;
3203 }
3204
3205 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3206 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3207
3208 /* No buffer: viewport is irrelevant. */
3209 if (width_from_buffer == 0 || height_from_buffer == 0)
3210 return true;
3211
3212 /* overflow checks for wl_fixed_from_int() */
3213 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3214 return false;
3215 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3216 return false;
3217
3218 w = wl_fixed_from_int(width_from_buffer);
3219 h = wl_fixed_from_int(height_from_buffer);
3220
3221 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3222 return false;
3223 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3224 return false;
3225
3226 return true;
3227}
3228
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003229static bool
3230fixed_is_integer(wl_fixed_t v)
3231{
3232 return (v & 0xff) == 0;
3233}
3234
3235static bool
3236weston_surface_is_pending_viewport_dst_size_int(
3237 const struct weston_surface *surface)
3238{
3239 const struct weston_buffer_viewport *vp =
3240 &surface->pending.buffer_viewport;
3241
3242 if (vp->surface.width != -1) {
3243 assert(vp->surface.width > 0 && vp->surface.height > 0);
3244 return true;
3245 }
3246
3247 return fixed_is_integer(vp->buffer.src_width) &&
3248 fixed_is_integer(vp->buffer.src_height);
3249}
3250
Derek Foreman152254b2015-11-26 14:17:48 -06003251/* Translate pending damage in buffer co-ordinates to surface
3252 * co-ordinates and union it with a pixman_region32_t.
3253 * This should only be called after the buffer is attached.
3254 */
3255static void
3256apply_damage_buffer(pixman_region32_t *dest,
3257 struct weston_surface *surface,
3258 struct weston_surface_state *state)
3259{
3260 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3261
3262 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3263 * translated into surface co-ordinates and unioned with
3264 * any other surface damage.
3265 * None of this makes sense if there is no buffer though.
3266 */
3267 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3268 pixman_region32_t buffer_damage;
3269
3270 pixman_region32_intersect_rect(&state->damage_buffer,
3271 &state->damage_buffer,
3272 0, 0, buffer->width,
3273 buffer->height);
3274 pixman_region32_init(&buffer_damage);
3275 weston_matrix_transform_region(&buffer_damage,
3276 &surface->buffer_to_surface_matrix,
3277 &state->damage_buffer);
3278 pixman_region32_union(dest, dest, &buffer_damage);
3279 pixman_region32_fini(&buffer_damage);
3280 }
3281 /* We should clear this on commit even if there was no buffer */
3282 pixman_region32_clear(&state->damage_buffer);
3283}
3284
Jason Ekstrand1e059042014-10-16 10:55:19 -05003285static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003286weston_surface_commit_state(struct weston_surface *surface,
3287 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003288{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003289 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003290 pixman_region32_t opaque;
3291
Alexander Larsson4ea95522013-05-22 14:41:37 +02003292 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003293 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003294 /* wp_viewport.set_source */
3295 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003296 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003297
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003298 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003299 if (state->newly_attached) {
3300 /* zwp_surface_synchronization_v1.set_acquire_fence */
3301 fd_move(&surface->acquire_fence_fd,
3302 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003303 /* zwp_surface_synchronization_v1.get_release */
3304 weston_buffer_release_move(&surface->buffer_release_ref,
3305 &state->buffer_release_ref);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003306
Jason Ekstrand7b982072014-05-20 14:33:03 -05003307 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003308 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003309 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003310 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003311 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003312
Jason Ekstrand1e059042014-10-16 10:55:19 -05003313 weston_surface_build_buffer_matrix(surface,
3314 &surface->surface_to_buffer_matrix);
3315 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3316 &surface->surface_to_buffer_matrix);
3317
Jason Ekstrand7b982072014-05-20 14:33:03 -05003318 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003319 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003320 if (surface->committed)
3321 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003322 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003323
Jason Ekstrand7b982072014-05-20 14:33:03 -05003324 state->sx = 0;
3325 state->sy = 0;
3326 state->newly_attached = 0;
3327 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003328
Derek Foreman152254b2015-11-26 14:17:48 -06003329 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003330 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003331 (pixman_region32_not_empty(&state->damage_surface) ||
3332 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003333 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003334
Pekka Paalanen8e159182012-10-10 12:49:25 +03003335 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003336 &state->damage_surface);
3337
3338 apply_damage_buffer(&surface->damage, surface, state);
3339
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003340 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003341 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003342 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003343
3344 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003345 pixman_region32_init(&opaque);
3346 pixman_region32_intersect_rect(&opaque, &state->opaque,
3347 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003348
3349 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3350 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003351 wl_list_for_each(view, &surface->views, surface_link)
3352 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003353 }
3354
3355 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003356
3357 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003358 pixman_region32_intersect_rect(&surface->input, &state->input,
3359 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003360
Pekka Paalanenbc106382012-10-10 12:49:31 +03003361 /* wl_surface.frame */
3362 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003363 &state->frame_callback_list);
3364 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003365
3366 /* XXX:
3367 * What should happen with a feedback request, if there
3368 * is no wl_buffer attached for this commit?
3369 */
3370
3371 /* presentation.feedback */
3372 wl_list_insert_list(&surface->feedback_list,
3373 &state->feedback_list);
3374 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003375
3376 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003377}
3378
3379static void
3380weston_surface_commit(struct weston_surface *surface)
3381{
3382 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003383
Pekka Paalanene67858b2013-04-25 13:57:42 +03003384 weston_surface_commit_subsurface_order(surface);
3385
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003386 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003387}
3388
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003389static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003390weston_subsurface_commit(struct weston_subsurface *sub);
3391
3392static void
3393weston_subsurface_parent_commit(struct weston_subsurface *sub,
3394 int parent_is_synchronized);
3395
3396static void
3397surface_commit(struct wl_client *client, struct wl_resource *resource)
3398{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003399 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003400 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3401
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003402 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3403 assert(surface->viewport_resource);
3404
3405 wl_resource_post_error(surface->viewport_resource,
3406 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3407 "wl_surface@%d has viewport source outside buffer",
3408 wl_resource_get_id(resource));
3409 return;
3410 }
3411
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003412 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3413 assert(surface->viewport_resource);
3414
3415 wl_resource_post_error(surface->viewport_resource,
3416 WP_VIEWPORT_ERROR_BAD_SIZE,
3417 "wl_surface@%d viewport dst size not integer",
3418 wl_resource_get_id(resource));
3419 return;
3420 }
3421
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003422 if (surface->pending.acquire_fence_fd >= 0) {
3423 assert(surface->synchronization_resource);
3424
3425 if (!surface->pending.buffer) {
3426 fd_clear(&surface->pending.acquire_fence_fd);
3427 wl_resource_post_error(surface->synchronization_resource,
3428 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3429 "wl_surface@%"PRIu32" no buffer for synchronization",
3430 wl_resource_get_id(resource));
3431 return;
3432 }
3433
3434 /* We support fences for both wp_linux_dmabuf and opaque EGL
3435 * buffers, as mandated by minor version 2 of the
3436 * zwp_linux_explicit_synchronization_v1 protocol. Since
3437 * renderers that support fences currently only support these
3438 * two buffer types plus SHM buffers, we can just check for the
3439 * SHM buffer case here.
3440 */
3441 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3442 fd_clear(&surface->pending.acquire_fence_fd);
3443 wl_resource_post_error(surface->synchronization_resource,
3444 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3445 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3446 wl_resource_get_id(resource));
3447 return;
3448 }
3449 }
3450
Alexandros Frantzis67629672018-10-19 12:14:11 +03003451 if (surface->pending.buffer_release_ref.buffer_release &&
3452 !surface->pending.buffer) {
3453 assert(surface->synchronization_resource);
3454
3455 wl_resource_post_error(surface->synchronization_resource,
3456 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3457 "wl_surface@%"PRIu32" no buffer for synchronization",
3458 wl_resource_get_id(resource));
3459 return;
3460 }
3461
Pekka Paalanene67858b2013-04-25 13:57:42 +03003462 if (sub) {
3463 weston_subsurface_commit(sub);
3464 return;
3465 }
3466
3467 weston_surface_commit(surface);
3468
3469 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3470 if (sub->surface != surface)
3471 weston_subsurface_parent_commit(sub, 0);
3472 }
3473}
3474
3475static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003476surface_set_buffer_transform(struct wl_client *client,
3477 struct wl_resource *resource, int transform)
3478{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003479 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003480
Jonny Lamba55f1392014-05-30 12:07:15 +02003481 /* if wl_output.transform grows more members this will need to be updated. */
3482 if (transform < 0 ||
3483 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3484 wl_resource_post_error(resource,
3485 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3486 "buffer transform must be a valid transform "
3487 "('%d' specified)", transform);
3488 return;
3489 }
3490
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003491 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003492 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003493}
3494
Alexander Larsson4ea95522013-05-22 14:41:37 +02003495static void
3496surface_set_buffer_scale(struct wl_client *client,
3497 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003498 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003499{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003500 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003501
Jonny Lamba55f1392014-05-30 12:07:15 +02003502 if (scale < 1) {
3503 wl_resource_post_error(resource,
3504 WL_SURFACE_ERROR_INVALID_SCALE,
3505 "buffer scale must be at least one "
3506 "('%d' specified)", scale);
3507 return;
3508 }
3509
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003510 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003511 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003512}
3513
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003514static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003515 surface_destroy,
3516 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003517 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003518 surface_frame,
3519 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003520 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003521 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003522 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003523 surface_set_buffer_scale,
3524 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003525};
3526
3527static void
3528compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003529 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003530{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003531 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003532 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003533
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003534 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003535 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003536 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003537 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003538 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003539
Jason Ekstranda85118c2013-06-27 20:17:02 -05003540 surface->resource =
3541 wl_resource_create(client, &wl_surface_interface,
3542 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003543 if (surface->resource == NULL) {
3544 weston_surface_destroy(surface);
3545 wl_resource_post_no_memory(resource);
3546 return;
3547 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003548 wl_resource_set_implementation(surface->resource, &surface_interface,
3549 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003550
3551 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003552}
3553
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003554static void
3555destroy_region(struct wl_resource *resource)
3556{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003557 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003558
3559 pixman_region32_fini(&region->region);
3560 free(region);
3561}
3562
3563static void
3564region_destroy(struct wl_client *client, struct wl_resource *resource)
3565{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003566 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003567}
3568
3569static void
3570region_add(struct wl_client *client, struct wl_resource *resource,
3571 int32_t x, int32_t y, int32_t width, int32_t height)
3572{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003573 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003574
3575 pixman_region32_union_rect(&region->region, &region->region,
3576 x, y, width, height);
3577}
3578
3579static void
3580region_subtract(struct wl_client *client, struct wl_resource *resource,
3581 int32_t x, int32_t y, int32_t width, int32_t height)
3582{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003583 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003584 pixman_region32_t rect;
3585
3586 pixman_region32_init_rect(&rect, x, y, width, height);
3587 pixman_region32_subtract(&region->region, &region->region, &rect);
3588 pixman_region32_fini(&rect);
3589}
3590
3591static const struct wl_region_interface region_interface = {
3592 region_destroy,
3593 region_add,
3594 region_subtract
3595};
3596
3597static void
3598compositor_create_region(struct wl_client *client,
3599 struct wl_resource *resource, uint32_t id)
3600{
3601 struct weston_region *region;
3602
3603 region = malloc(sizeof *region);
3604 if (region == NULL) {
3605 wl_resource_post_no_memory(resource);
3606 return;
3607 }
3608
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003609 pixman_region32_init(&region->region);
3610
Jason Ekstranda85118c2013-06-27 20:17:02 -05003611 region->resource =
3612 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003613 if (region->resource == NULL) {
3614 free(region);
3615 wl_resource_post_no_memory(resource);
3616 return;
3617 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003618 wl_resource_set_implementation(region->resource, &region_interface,
3619 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003620}
3621
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003622static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003623 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003624 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003625};
3626
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003627static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003628weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3629{
3630 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003631
Jason Ekstrand7b982072014-05-20 14:33:03 -05003632 weston_surface_commit_state(surface, &sub->cached);
3633 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003634
3635 weston_surface_commit_subsurface_order(surface);
3636
3637 weston_surface_schedule_repaint(surface);
3638
Jason Ekstrand7b982072014-05-20 14:33:03 -05003639 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003640}
3641
3642static void
3643weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3644{
3645 struct weston_surface *surface = sub->surface;
3646
3647 /*
3648 * If this commit would cause the surface to move by the
3649 * attach(dx, dy) parameters, the old damage region must be
3650 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003651 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003652 */
Derek Foreman152254b2015-11-26 14:17:48 -06003653 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003654 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003655 pixman_region32_union(&sub->cached.damage_surface,
3656 &sub->cached.damage_surface,
3657 &surface->pending.damage_surface);
3658 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003659
3660 if (surface->pending.newly_attached) {
3661 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003662 weston_surface_state_set_buffer(&sub->cached,
3663 surface->pending.buffer);
3664 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003665 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003666 weston_presentation_feedback_discard_list(
3667 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003668 /* zwp_surface_synchronization_v1.set_acquire_fence */
3669 fd_move(&sub->cached.acquire_fence_fd,
3670 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003671 /* zwp_surface_synchronization_v1.get_release */
3672 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3673 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003674 }
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003675 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003676 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003677 sub->cached.sx += surface->pending.sx;
3678 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003679
Derek Foreman152254b2015-11-26 14:17:48 -06003680 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3681
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003682 sub->cached.buffer_viewport.changed |=
3683 surface->pending.buffer_viewport.changed;
3684 sub->cached.buffer_viewport.buffer =
3685 surface->pending.buffer_viewport.buffer;
3686 sub->cached.buffer_viewport.surface =
3687 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003688
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003689 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003690
3691 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3692
3693 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3694
3695 wl_list_insert_list(&sub->cached.frame_callback_list,
3696 &surface->pending.frame_callback_list);
3697 wl_list_init(&surface->pending.frame_callback_list);
3698
Pekka Paalanen133e4392014-09-23 22:08:46 -04003699 wl_list_insert_list(&sub->cached.feedback_list,
3700 &surface->pending.feedback_list);
3701 wl_list_init(&surface->pending.feedback_list);
3702
Jason Ekstrand7b982072014-05-20 14:33:03 -05003703 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003704}
3705
Derek Foreman280e7dd2014-10-03 13:13:42 -05003706static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003707weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3708{
3709 while (sub) {
3710 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003711 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003712
3713 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003714 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003715
3716 sub = weston_surface_to_subsurface(sub->parent);
3717 }
3718
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003719 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003720}
3721
3722static void
3723weston_subsurface_commit(struct weston_subsurface *sub)
3724{
3725 struct weston_surface *surface = sub->surface;
3726 struct weston_subsurface *tmp;
3727
3728 /* Recursive check for effectively synchronized. */
3729 if (weston_subsurface_is_synchronized(sub)) {
3730 weston_subsurface_commit_to_cache(sub);
3731 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003732 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003733 /* flush accumulated state from cache */
3734 weston_subsurface_commit_to_cache(sub);
3735 weston_subsurface_commit_from_cache(sub);
3736 } else {
3737 weston_surface_commit(surface);
3738 }
3739
3740 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3741 if (tmp->surface != surface)
3742 weston_subsurface_parent_commit(tmp, 0);
3743 }
3744 }
3745}
3746
3747static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003748weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003749{
3750 struct weston_surface *surface = sub->surface;
3751 struct weston_subsurface *tmp;
3752
Pekka Paalanene67858b2013-04-25 13:57:42 +03003753 /* From now on, commit_from_cache the whole sub-tree, regardless of
3754 * the synchronized mode of each child. This sub-surface or some
3755 * of its ancestors were synchronized, so we are synchronized
3756 * all the way down.
3757 */
3758
Jason Ekstrand7b982072014-05-20 14:33:03 -05003759 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003760 weston_subsurface_commit_from_cache(sub);
3761
3762 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3763 if (tmp->surface != surface)
3764 weston_subsurface_parent_commit(tmp, 1);
3765 }
3766}
3767
3768static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003769weston_subsurface_parent_commit(struct weston_subsurface *sub,
3770 int parent_is_synchronized)
3771{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003772 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003773 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003774 wl_list_for_each(view, &sub->surface->views, surface_link)
3775 weston_view_set_position(view,
3776 sub->position.x,
3777 sub->position.y);
3778
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003779 sub->position.set = 0;
3780 }
3781
3782 if (parent_is_synchronized || sub->synchronized)
3783 weston_subsurface_synchronized_commit(sub);
3784}
3785
Pekka Paalanen8274d902014-08-06 19:36:51 +03003786static int
3787subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3788{
3789 return snprintf(buf, len, "sub-surface");
3790}
3791
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003792static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003793subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003794{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003795 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003796
Jason Ekstranda7af7042013-10-12 22:38:11 -05003797 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003798 weston_view_set_position(view,
3799 view->geometry.x + dx,
3800 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003801
3802 /* No need to check parent mappedness, because if parent is not
3803 * mapped, parent is not in a visible layer, so this sub-surface
3804 * will not be drawn either.
3805 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003806
Pekka Paalanene67858b2013-04-25 13:57:42 +03003807 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003808 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003809
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003810 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003811 * because that would call it also for the parent surface,
3812 * which might not be mapped yet. That would lead to
3813 * inconsistent state, where the window could never be
3814 * mapped.
3815 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003816 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003817 * weston_surface_is_mapped() return true, so that when the
3818 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003819 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003820 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003821 }
3822}
3823
3824static struct weston_subsurface *
3825weston_surface_to_subsurface(struct weston_surface *surface)
3826{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003827 if (surface->committed == subsurface_committed)
3828 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003829
3830 return NULL;
3831}
3832
Pekka Paalanen01388e22013-04-25 13:57:44 +03003833WL_EXPORT struct weston_surface *
3834weston_surface_get_main_surface(struct weston_surface *surface)
3835{
3836 struct weston_subsurface *sub;
3837
3838 while (surface && (sub = weston_surface_to_subsurface(surface)))
3839 surface = sub->parent;
3840
3841 return surface;
3842}
3843
Pekka Paalanen50b67472014-10-01 15:02:41 +03003844WL_EXPORT int
3845weston_surface_set_role(struct weston_surface *surface,
3846 const char *role_name,
3847 struct wl_resource *error_resource,
3848 uint32_t error_code)
3849{
3850 assert(role_name);
3851
3852 if (surface->role_name == NULL ||
3853 surface->role_name == role_name ||
3854 strcmp(surface->role_name, role_name) == 0) {
3855 surface->role_name = role_name;
3856
3857 return 0;
3858 }
3859
3860 wl_resource_post_error(error_resource, error_code,
3861 "Cannot assign role %s to wl_surface@%d,"
3862 " already has role %s\n",
3863 role_name,
3864 wl_resource_get_id(surface->resource),
3865 surface->role_name);
3866 return -1;
3867}
3868
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003869WL_EXPORT const char *
3870weston_surface_get_role(struct weston_surface *surface)
3871{
3872 return surface->role_name;
3873}
3874
Pekka Paalanen8274d902014-08-06 19:36:51 +03003875WL_EXPORT void
3876weston_surface_set_label_func(struct weston_surface *surface,
3877 int (*desc)(struct weston_surface *,
3878 char *, size_t))
3879{
3880 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003881 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003882}
3883
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003884/** Get the size of surface contents
3885 *
3886 * \param surface The surface to query.
3887 * \param width Returns the width of raw contents.
3888 * \param height Returns the height of raw contents.
3889 *
3890 * Retrieves the raw surface content size in pixels for the given surface.
3891 * This is the whole content size in buffer pixels. If the surface
3892 * has no content or the renderer does not implement this feature,
3893 * zeroes are returned.
3894 *
3895 * This function is used to determine the buffer size needed for
3896 * a weston_surface_copy_content() call.
3897 */
3898WL_EXPORT void
3899weston_surface_get_content_size(struct weston_surface *surface,
3900 int *width, int *height)
3901{
3902 struct weston_renderer *rer = surface->compositor->renderer;
3903
3904 if (!rer->surface_get_content_size) {
3905 *width = 0;
3906 *height = 0;
3907 return;
3908 }
3909
3910 rer->surface_get_content_size(surface, width, height);
3911}
3912
Quentin Glidic248dd102016-08-12 10:41:34 +02003913/** Get the bounding box of a surface and its subsurfaces
3914 *
3915 * \param surface The surface to query.
3916 * \return The bounding box relative to the surface origin.
3917 *
3918 */
3919WL_EXPORT struct weston_geometry
3920weston_surface_get_bounding_box(struct weston_surface *surface)
3921{
3922 pixman_region32_t region;
3923 pixman_box32_t *box;
3924 struct weston_subsurface *subsurface;
3925
3926 pixman_region32_init_rect(&region,
3927 0, 0,
3928 surface->width, surface->height);
3929
3930 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3931 pixman_region32_union_rect(&region, &region,
3932 subsurface->position.x,
3933 subsurface->position.y,
3934 subsurface->surface->width,
3935 subsurface->surface->height);
3936
3937 box = pixman_region32_extents(&region);
3938 struct weston_geometry geometry = {
3939 .x = box->x1,
3940 .y = box->y1,
3941 .width = box->x2 - box->x1,
3942 .height = box->y2 - box->y1,
3943 };
3944
3945 pixman_region32_fini(&region);
3946
3947 return geometry;
3948}
3949
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003950/** Copy surface contents to system memory.
3951 *
3952 * \param surface The surface to copy from.
3953 * \param target Pointer to the target memory buffer.
3954 * \param size Size of the target buffer in bytes.
3955 * \param src_x X location on contents to copy from.
3956 * \param src_y Y location on contents to copy from.
3957 * \param width Width in pixels of the area to copy.
3958 * \param height Height in pixels of the area to copy.
3959 * \return 0 for success, -1 for failure.
3960 *
3961 * Surface contents are maintained by the renderer. They can be in a
3962 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3963 * else.
3964 *
3965 * Surface contents are copied into memory pointed to by target,
3966 * which has size bytes of space available. The target memory
3967 * may be larger than needed, but being smaller returns an error.
3968 * The extra bytes in target may or may not be written; their content is
3969 * unspecified. Size must be large enough to hold the image.
3970 *
3971 * The image in the target memory will be arranged in rows from
3972 * top to bottom, and pixels on a row from left to right. The pixel
3973 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3974 * width * 4.
3975 *
3976 * Parameters src_x and src_y define the upper-left corner in buffer
3977 * coordinates (pixels) to copy from. Parameters width and height
3978 * define the size of the area to copy in pixels.
3979 *
3980 * The rectangle defined by src_x, src_y, width, height must fit in
3981 * the surface contents. Otherwise an error is returned.
3982 *
Changwoo Chof97d2502017-08-05 00:30:47 +09003983 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003984 * needed target buffer size and rectangle limits.
3985 *
3986 * CURRENT IMPLEMENTATION RESTRICTIONS:
3987 * - the machine must be little-endian due to Pixman formats.
3988 *
3989 * NOTE: Pixman formats are premultiplied.
3990 */
3991WL_EXPORT int
3992weston_surface_copy_content(struct weston_surface *surface,
3993 void *target, size_t size,
3994 int src_x, int src_y,
3995 int width, int height)
3996{
3997 struct weston_renderer *rer = surface->compositor->renderer;
3998 int cw, ch;
3999 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4000
4001 if (!rer->surface_copy_content)
4002 return -1;
4003
4004 weston_surface_get_content_size(surface, &cw, &ch);
4005
4006 if (src_x < 0 || src_y < 0)
4007 return -1;
4008
4009 if (width <= 0 || height <= 0)
4010 return -1;
4011
4012 if (src_x + width > cw || src_y + height > ch)
4013 return -1;
4014
4015 if (width * bytespp * height > size)
4016 return -1;
4017
4018 return rer->surface_copy_content(surface, target, size,
4019 src_x, src_y, width, height);
4020}
4021
Pekka Paalanene67858b2013-04-25 13:57:42 +03004022static void
4023subsurface_set_position(struct wl_client *client,
4024 struct wl_resource *resource, int32_t x, int32_t y)
4025{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004026 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004027
4028 if (!sub)
4029 return;
4030
4031 sub->position.x = x;
4032 sub->position.y = y;
4033 sub->position.set = 1;
4034}
4035
4036static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004037subsurface_find_sibling(struct weston_subsurface *sub,
4038 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004039{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004040 struct weston_surface *parent = sub->parent;
4041 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004042
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004043 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4044 if (sibling->surface == surface && sibling != sub)
4045 return sibling;
4046 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004047
4048 return NULL;
4049}
4050
4051static struct weston_subsurface *
4052subsurface_sibling_check(struct weston_subsurface *sub,
4053 struct weston_surface *surface,
4054 const char *request)
4055{
4056 struct weston_subsurface *sibling;
4057
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004058 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004059 if (!sibling) {
4060 wl_resource_post_error(sub->resource,
4061 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4062 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004063 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004064 return NULL;
4065 }
4066
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004067 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004068
4069 return sibling;
4070}
4071
4072static void
4073subsurface_place_above(struct wl_client *client,
4074 struct wl_resource *resource,
4075 struct wl_resource *sibling_resource)
4076{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004077 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004078 struct weston_surface *surface =
4079 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004080 struct weston_subsurface *sibling;
4081
4082 if (!sub)
4083 return;
4084
4085 sibling = subsurface_sibling_check(sub, surface, "place_above");
4086 if (!sibling)
4087 return;
4088
4089 wl_list_remove(&sub->parent_link_pending);
4090 wl_list_insert(sibling->parent_link_pending.prev,
4091 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004092
4093 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004094}
4095
4096static void
4097subsurface_place_below(struct wl_client *client,
4098 struct wl_resource *resource,
4099 struct wl_resource *sibling_resource)
4100{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004101 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004102 struct weston_surface *surface =
4103 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004104 struct weston_subsurface *sibling;
4105
4106 if (!sub)
4107 return;
4108
4109 sibling = subsurface_sibling_check(sub, surface, "place_below");
4110 if (!sibling)
4111 return;
4112
4113 wl_list_remove(&sub->parent_link_pending);
4114 wl_list_insert(&sibling->parent_link_pending,
4115 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004116
4117 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004118}
4119
4120static void
4121subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4122{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004123 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004124
4125 if (sub)
4126 sub->synchronized = 1;
4127}
4128
4129static void
4130subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4131{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004132 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004133
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004134 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004135 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004136
4137 /* If sub became effectively desynchronized, flush. */
4138 if (!weston_subsurface_is_synchronized(sub))
4139 weston_subsurface_synchronized_commit(sub);
4140 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004141}
4142
4143static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004144weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4145{
4146 wl_list_remove(&sub->parent_link);
4147 wl_list_remove(&sub->parent_link_pending);
4148 wl_list_remove(&sub->parent_destroy_listener.link);
4149 sub->parent = NULL;
4150}
4151
4152static void
4153weston_subsurface_destroy(struct weston_subsurface *sub);
4154
4155static void
4156subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4157{
4158 struct weston_subsurface *sub =
4159 container_of(listener, struct weston_subsurface,
4160 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004161 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004162
4163 /* The protocol object (wl_resource) is left inert. */
4164 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004165 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004166
4167 weston_subsurface_destroy(sub);
4168}
4169
4170static void
4171subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4172{
4173 struct weston_subsurface *sub =
4174 container_of(listener, struct weston_subsurface,
4175 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004176 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004177 assert(sub->surface != sub->parent);
4178
4179 if (weston_surface_is_mapped(sub->surface))
4180 weston_surface_unmap(sub->surface);
4181
4182 weston_subsurface_unlink_parent(sub);
4183}
4184
4185static void
4186subsurface_resource_destroy(struct wl_resource *resource)
4187{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004188 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004189
4190 if (sub)
4191 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004192}
4193
4194static void
4195subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4196{
4197 wl_resource_destroy(resource);
4198}
4199
4200static void
4201weston_subsurface_link_parent(struct weston_subsurface *sub,
4202 struct weston_surface *parent)
4203{
4204 sub->parent = parent;
4205 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004206 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004207 &sub->parent_destroy_listener);
4208
4209 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4210 wl_list_insert(&parent->subsurface_list_pending,
4211 &sub->parent_link_pending);
4212}
4213
4214static void
4215weston_subsurface_link_surface(struct weston_subsurface *sub,
4216 struct weston_surface *surface)
4217{
4218 sub->surface = surface;
4219 sub->surface_destroy_listener.notify =
4220 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004221 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004222 &sub->surface_destroy_listener);
4223}
4224
4225static void
4226weston_subsurface_destroy(struct weston_subsurface *sub)
4227{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004228 struct weston_view *view, *next;
4229
Pekka Paalanene67858b2013-04-25 13:57:42 +03004230 assert(sub->surface);
4231
4232 if (sub->resource) {
4233 assert(weston_surface_to_subsurface(sub->surface) == sub);
4234 assert(sub->parent_destroy_listener.notify ==
4235 subsurface_handle_parent_destroy);
4236
George Kiagiadakised04d382014-06-13 18:10:26 +02004237 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4238 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004239 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004240 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004241
Pekka Paalanene67858b2013-04-25 13:57:42 +03004242 if (sub->parent)
4243 weston_subsurface_unlink_parent(sub);
4244
Jason Ekstrand7b982072014-05-20 14:33:03 -05004245 weston_surface_state_fini(&sub->cached);
4246 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004247
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004248 sub->surface->committed = NULL;
4249 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004250 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004251 } else {
4252 /* the dummy weston_subsurface for the parent itself */
4253 assert(sub->parent_destroy_listener.notify == NULL);
4254 wl_list_remove(&sub->parent_link);
4255 wl_list_remove(&sub->parent_link_pending);
4256 }
4257
4258 wl_list_remove(&sub->surface_destroy_listener.link);
4259 free(sub);
4260}
4261
4262static const struct wl_subsurface_interface subsurface_implementation = {
4263 subsurface_destroy,
4264 subsurface_set_position,
4265 subsurface_place_above,
4266 subsurface_place_below,
4267 subsurface_set_sync,
4268 subsurface_set_desync
4269};
4270
4271static struct weston_subsurface *
4272weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4273 struct weston_surface *parent)
4274{
4275 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004276 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004277
Bryce Harringtonde16d892014-11-20 22:21:57 -08004278 sub = zalloc(sizeof *sub);
4279 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004280 return NULL;
4281
Jason Ekstranda7af7042013-10-12 22:38:11 -05004282 wl_list_init(&sub->unused_views);
4283
Jason Ekstranda85118c2013-06-27 20:17:02 -05004284 sub->resource =
4285 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004286 if (!sub->resource) {
4287 free(sub);
4288 return NULL;
4289 }
4290
Jason Ekstranda85118c2013-06-27 20:17:02 -05004291 wl_resource_set_implementation(sub->resource,
4292 &subsurface_implementation,
4293 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004294 weston_subsurface_link_surface(sub, surface);
4295 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004296 weston_surface_state_init(&sub->cached);
4297 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004298 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004299
4300 return sub;
4301}
4302
4303/* Create a dummy subsurface for having the parent itself in its
4304 * sub-surface lists. Makes stacking order manipulation easy.
4305 */
4306static struct weston_subsurface *
4307weston_subsurface_create_for_parent(struct weston_surface *parent)
4308{
4309 struct weston_subsurface *sub;
4310
Bryce Harringtonde16d892014-11-20 22:21:57 -08004311 sub = zalloc(sizeof *sub);
4312 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004313 return NULL;
4314
4315 weston_subsurface_link_surface(sub, parent);
4316 sub->parent = parent;
4317 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4318 wl_list_insert(&parent->subsurface_list_pending,
4319 &sub->parent_link_pending);
4320
4321 return sub;
4322}
4323
4324static void
4325subcompositor_get_subsurface(struct wl_client *client,
4326 struct wl_resource *resource,
4327 uint32_t id,
4328 struct wl_resource *surface_resource,
4329 struct wl_resource *parent_resource)
4330{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004331 struct weston_surface *surface =
4332 wl_resource_get_user_data(surface_resource);
4333 struct weston_surface *parent =
4334 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004335 struct weston_subsurface *sub;
4336 static const char where[] = "get_subsurface: wl_subsurface@";
4337
4338 if (surface == parent) {
4339 wl_resource_post_error(resource,
4340 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4341 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004342 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004343 return;
4344 }
4345
4346 if (weston_surface_to_subsurface(surface)) {
4347 wl_resource_post_error(resource,
4348 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4349 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004350 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004351 return;
4352 }
4353
Pekka Paalanen50b67472014-10-01 15:02:41 +03004354 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4355 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004356 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004357
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004358 if (weston_surface_get_main_surface(parent) == surface) {
4359 wl_resource_post_error(resource,
4360 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4361 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004362 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004363 return;
4364 }
4365
Pekka Paalanene67858b2013-04-25 13:57:42 +03004366 /* make sure the parent is in its own list */
4367 if (wl_list_empty(&parent->subsurface_list)) {
4368 if (!weston_subsurface_create_for_parent(parent)) {
4369 wl_resource_post_no_memory(resource);
4370 return;
4371 }
4372 }
4373
4374 sub = weston_subsurface_create(id, surface, parent);
4375 if (!sub) {
4376 wl_resource_post_no_memory(resource);
4377 return;
4378 }
4379
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004380 surface->committed = subsurface_committed;
4381 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004382 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004383}
4384
4385static void
4386subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4387{
4388 wl_resource_destroy(resource);
4389}
4390
4391static const struct wl_subcompositor_interface subcompositor_interface = {
4392 subcompositor_destroy,
4393 subcompositor_get_subsurface
4394};
4395
4396static void
4397bind_subcompositor(struct wl_client *client,
4398 void *data, uint32_t version, uint32_t id)
4399{
4400 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004401 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004402
Jason Ekstranda85118c2013-06-27 20:17:02 -05004403 resource =
4404 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004405 if (resource == NULL) {
4406 wl_client_post_no_memory(client);
4407 return;
4408 }
4409 wl_resource_set_implementation(resource, &subcompositor_interface,
4410 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004411}
4412
Bryce Harrington0795ece2016-08-30 12:04:26 -07004413/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004414 *
4415 * \param compositor The compositor instance
4416 * \param state The DPMS state the outputs will be set to
4417 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004418static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004419weston_compositor_dpms(struct weston_compositor *compositor,
4420 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004421{
4422 struct weston_output *output;
4423
Bryce Harrington08976ac2016-08-30 12:05:16 -07004424 wl_list_for_each(output, &compositor->output_list, link)
4425 if (output->set_dpms)
4426 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004427}
4428
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004429/** Restores the compositor to active status
4430 *
4431 * \param compositor The compositor instance
4432 *
4433 * If the compositor was in a sleeping mode, all outputs are powered
4434 * back on via DPMS. Otherwise if the compositor was inactive
4435 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4436 * signal will fire.
4437 *
4438 * Restarts the idle timer.
4439 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004440WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004441weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004442{
Neil Roberts8b62e202013-09-30 13:14:47 +01004443 uint32_t old_state = compositor->state;
4444
4445 /* The state needs to be changed before emitting the wake
4446 * signal because that may try to schedule a repaint which
4447 * will not work if the compositor is still sleeping */
4448 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4449
4450 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004451 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004452 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004453 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004454 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004455 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004456 /* fall through */
4457 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004458 wl_event_source_timer_update(compositor->idle_source,
4459 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004460 }
4461}
4462
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004463/** Turns off rendering and frame events for the compositor.
4464 *
4465 * \param compositor The compositor instance
4466 *
4467 * This is used for example to prevent further rendering while the
4468 * compositor is shutting down.
4469 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004470 * Stops the idle timer.
4471 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004472WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004473weston_compositor_offscreen(struct weston_compositor *compositor)
4474{
4475 switch (compositor->state) {
4476 case WESTON_COMPOSITOR_OFFSCREEN:
4477 return;
4478 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004479 default:
4480 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4481 wl_event_source_timer_update(compositor->idle_source, 0);
4482 }
4483}
4484
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004485/** Powers down all attached output devices
4486 *
4487 * \param compositor The compositor instance
4488 *
4489 * Causes rendering to the outputs to cease, and no frame events to be
4490 * sent. Only powers down the outputs if the compositor is not already
4491 * in sleep mode.
4492 *
4493 * Stops the idle timer.
4494 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004495WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004496weston_compositor_sleep(struct weston_compositor *compositor)
4497{
4498 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4499 return;
4500
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004501 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004502 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4503 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4504}
4505
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004506/** Sets compositor to idle mode
4507 *
4508 * \param data The compositor instance
4509 *
4510 * This is called when the idle timer fires. Once the compositor is in
4511 * idle mode it requires a wake action (e.g. via
4512 * weston_compositor_wake()) to restore it. The compositor's
4513 * idle_signal will be triggered when the idle event occurs.
4514 *
4515 * Idleness can be inhibited by setting the compositor's idle_inhibit
4516 * property.
4517 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004518static int
4519idle_handler(void *data)
4520{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004521 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004522
4523 if (compositor->idle_inhibit)
4524 return 1;
4525
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004526 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004527 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004528
4529 return 1;
4530}
4531
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004532WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004533weston_plane_init(struct weston_plane *plane,
4534 struct weston_compositor *ec,
4535 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004536{
4537 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004538 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004539 plane->x = x;
4540 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004541 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004542
4543 /* Init the link so that the call to wl_list_remove() when releasing
4544 * the plane without ever stacking doesn't lead to a crash */
4545 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004546}
4547
4548WL_EXPORT void
4549weston_plane_release(struct weston_plane *plane)
4550{
Xiong Zhang97116532013-10-23 13:58:31 +08004551 struct weston_view *view;
4552
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004553 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004554 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004555
Xiong Zhang97116532013-10-23 13:58:31 +08004556 wl_list_for_each(view, &plane->compositor->view_list, link) {
4557 if (view->plane == plane)
4558 view->plane = NULL;
4559 }
4560
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004561 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004562}
4563
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004564WL_EXPORT void
4565weston_compositor_stack_plane(struct weston_compositor *ec,
4566 struct weston_plane *plane,
4567 struct weston_plane *above)
4568{
4569 if (above)
4570 wl_list_insert(above->link.prev, &plane->link);
4571 else
4572 wl_list_insert(&ec->plane_list, &plane->link);
4573}
4574
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004575static void
4576output_release(struct wl_client *client, struct wl_resource *resource)
4577{
4578 wl_resource_destroy(resource);
4579}
4580
4581static const struct wl_output_interface output_interface = {
4582 output_release,
4583};
4584
4585
Casey Dahlin9074db52012-04-19 22:50:09 -04004586static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004587{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004588 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004589}
4590
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004591static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004592bind_output(struct wl_client *client,
4593 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004594{
Pekka Paalanen05347622017-03-27 12:24:34 +03004595 struct weston_head *head = data;
4596 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004597 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004598 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004599
Jason Ekstranda85118c2013-06-27 20:17:02 -05004600 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004601 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004602 if (resource == NULL) {
4603 wl_client_post_no_memory(client);
4604 return;
4605 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004606
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004607 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004608 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004609 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004610
Pekka Paalanen05347622017-03-27 12:24:34 +03004611 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004612 wl_output_send_geometry(resource,
4613 output->x,
4614 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004615 head->mm_width,
4616 head->mm_height,
4617 head->subpixel,
4618 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004619 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004620 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004621 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004622 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004623
4624 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004625 wl_output_send_mode(resource,
4626 mode->flags,
4627 mode->width,
4628 mode->height,
4629 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004630 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004631
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004632 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004633 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004634}
4635
Pekka Paalanendcac3512017-12-08 14:13:34 +02004636static void
4637weston_head_add_global(struct weston_head *head)
4638{
4639 head->global = wl_global_create(head->compositor->wl_display,
4640 &wl_output_interface, 3,
4641 head, bind_output);
4642}
4643
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004644/** Remove the global wl_output protocol object
4645 *
4646 * \param head The head whose global to remove.
4647 *
4648 * Also orphans the wl_resources for this head (wl_output).
4649 */
4650static void
4651weston_head_remove_global(struct weston_head *head)
4652{
4653 struct wl_resource *resource, *tmp;
4654
4655 if (head->global)
4656 wl_global_destroy(head->global);
4657 head->global = NULL;
4658
4659 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4660 unbind_resource(resource);
4661 wl_resource_set_destructor(resource, NULL);
4662 wl_resource_set_user_data(resource, NULL);
4663 }
4664}
4665
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004666/** Get the backing object of wl_output
4667 *
4668 * \param resource A wl_output protocol object.
4669 * \return The backing object (user data) of a wl_resource representing a
4670 * wl_output protocol object.
4671 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004672WL_EXPORT struct weston_head *
4673weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004674{
4675 assert(wl_resource_instance_of(resource, &wl_output_interface,
4676 &output_interface));
4677
4678 return wl_resource_get_user_data(resource);
4679}
4680
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004681/** Initialize a pre-allocated weston_head
4682 *
4683 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004684 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004685 *
4686 * The head will be safe to attach, detach and release.
4687 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004688 * The name is used in logs, and can be used by compositors as a configuration
4689 * identifier.
4690 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004691 * \memberof weston_head
4692 * \internal
4693 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004694WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004695weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004696{
4697 /* Add some (in)sane defaults which can be used
4698 * for checking if an output was properly configured
4699 */
4700 memset(head, 0, sizeof *head);
4701
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004702 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004703 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004704 wl_list_init(&head->output_link);
4705 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004706 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004707}
4708
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004709/** Send output heads changed signal
4710 *
4711 * \param output The output that changed.
4712 *
4713 * Notify that the enabled output gained and/or lost heads, or that the
4714 * associated heads may have changed their connection status. This does not
4715 * include cases where the output becomes enabled or disabled. The registered
4716 * callbacks are called after the change has successfully happened.
4717 *
4718 * If connection status change causes the compositor to attach or detach a head
4719 * to an enabled output, the registered callbacks may be called multiple times.
4720 */
4721static void
4722weston_output_emit_heads_changed(struct weston_output *output)
4723{
4724 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4725 output);
4726}
4727
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004728/** Idle task for emitting heads_changed_signal */
4729static void
4730weston_compositor_call_heads_changed(void *data)
4731{
4732 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004733 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004734
4735 compositor->heads_changed_source = NULL;
4736
4737 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004738
4739 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4740 if (head->output && head->output->enabled)
4741 weston_output_emit_heads_changed(head->output);
4742 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004743}
4744
4745/** Schedule a call on idle to heads_changed callback
4746 *
4747 * \param compositor The Compositor.
4748 *
4749 * \memberof weston_compositor
4750 * \internal
4751 */
4752static void
4753weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4754{
4755 struct wl_event_loop *loop;
4756
4757 if (compositor->heads_changed_source)
4758 return;
4759
4760 loop = wl_display_get_event_loop(compositor->wl_display);
4761 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4762 weston_compositor_call_heads_changed, compositor);
4763}
4764
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004765/** Register a new head
4766 *
4767 * \param compositor The compositor.
4768 * \param head The head to register, must not be already registered.
4769 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004770 * This signals the core that a new head has become available, leading to
4771 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004772 *
4773 * \memberof weston_compositor
4774 * \internal
4775 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004776WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004777weston_compositor_add_head(struct weston_compositor *compositor,
4778 struct weston_head *head)
4779{
4780 assert(wl_list_empty(&head->compositor_link));
4781 assert(head->name);
4782
4783 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4784 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004785 weston_compositor_schedule_heads_changed(compositor);
4786}
4787
4788/** Adds a listener to be called when heads change
4789 *
4790 * \param compositor The compositor.
4791 * \param listener The listener to add.
4792 *
Marius Vlada2dace22019-06-12 16:05:44 +03004793 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004794 *
4795 * The listener function will be called after heads are added or their
4796 * connection status has changed. Several changes may be accumulated into a
4797 * single call. The user is expected to iterate over the existing heads and
4798 * check their statuses to find out what changed.
4799 *
4800 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4801 * weston_head_is_enabled
4802 * \memberof weston_compositor
4803 */
4804WL_EXPORT void
4805weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4806 struct wl_listener *listener)
4807{
4808 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004809}
4810
4811/** Iterate over available heads
4812 *
4813 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03004814 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004815 * \return The next available head in the list.
4816 *
4817 * Returns all available heads, regardless of being connected or enabled.
4818 *
4819 * You can iterate over all heads as follows:
4820 * \code
4821 * struct weston_head *head = NULL;
4822 *
4823 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4824 * ...
4825 * }
4826 * \endcode
4827 *
4828 * If you cause \c iter to be removed from the list, you cannot use it to
4829 * continue iterating. Removing any other item is safe.
4830 *
4831 * \memberof weston_compositor
4832 */
4833WL_EXPORT struct weston_head *
4834weston_compositor_iterate_heads(struct weston_compositor *compositor,
4835 struct weston_head *iter)
4836{
4837 struct wl_list *list = &compositor->head_list;
4838 struct wl_list *node;
4839
4840 assert(compositor);
4841 assert(!iter || iter->compositor == compositor);
4842
4843 if (iter)
4844 node = iter->compositor_link.next;
4845 else
4846 node = list->next;
4847
4848 assert(node);
4849 assert(!iter || node != &iter->compositor_link);
4850
4851 if (node == list)
4852 return NULL;
4853
4854 return container_of(node, struct weston_head, compositor_link);
4855}
4856
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004857/** Iterate over attached heads
4858 *
4859 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03004860 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004861 * \return The next attached head in the list.
4862 *
4863 * Returns all heads currently attached to the output.
4864 *
4865 * You can iterate over all heads as follows:
4866 * \code
4867 * struct weston_head *head = NULL;
4868 *
4869 * while ((head = weston_output_iterate_heads(output, head))) {
4870 * ...
4871 * }
4872 * \endcode
4873 *
4874 * If you cause \c iter to be removed from the list, you cannot use it to
4875 * continue iterating. Removing any other item is safe.
4876 *
4877 * \memberof weston_compositor
4878 */
4879WL_EXPORT struct weston_head *
4880weston_output_iterate_heads(struct weston_output *output,
4881 struct weston_head *iter)
4882{
4883 struct wl_list *list = &output->head_list;
4884 struct wl_list *node;
4885
4886 assert(output);
4887 assert(!iter || iter->output == output);
4888
4889 if (iter)
4890 node = iter->output_link.next;
4891 else
4892 node = list->next;
4893
4894 assert(node);
4895 assert(!iter || node != &iter->output_link);
4896
4897 if (node == list)
4898 return NULL;
4899
4900 return container_of(node, struct weston_head, output_link);
4901}
4902
Pekka Paalanendcac3512017-12-08 14:13:34 +02004903/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004904 *
4905 * \param output The output to attach to.
4906 * \param head The head that is not yet attached.
4907 * \return 0 on success, -1 on failure.
4908 *
4909 * Attaches the given head to the output. All heads of an output are clones
4910 * and share the resolution and timings.
4911 *
4912 * Cloning heads this way uses less resources than creating an output for
4913 * each head, but is not always possible due to environment, driver and hardware
4914 * limitations.
4915 *
4916 * On failure, the head remains unattached. Success of this function does not
4917 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004918 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004919 *
4920 * \memberof weston_output
4921 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004922WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004923weston_output_attach_head(struct weston_output *output,
4924 struct weston_head *head)
4925{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004926 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004927
4928 if (!wl_list_empty(&head->output_link))
4929 return -1;
4930
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004931 if (output->attach_head) {
4932 if (output->attach_head(output, head) < 0)
4933 return -1;
4934 } else if (!wl_list_empty(&output->head_list)) {
4935 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004936 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004937 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004938
4939 head->output = output;
4940 wl_list_insert(output->head_list.prev, &head->output_link);
4941
Pekka Paalanendcac3512017-12-08 14:13:34 +02004942 if (output->enabled) {
4943 weston_head_add_global(head);
4944
4945 head_names = weston_output_create_heads_string(output);
4946 weston_log("Output '%s' updated to have head(s) %s\n",
4947 output->name, head_names);
4948 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004949
4950 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02004951 }
4952
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004953 return 0;
4954}
4955
4956/** Detach a head from its output
4957 *
4958 * \param head The head to detach.
4959 *
4960 * It is safe to detach a non-attached head.
4961 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004962 * If the head is attached to an enabled output and the output will be left
4963 * with no heads, the output will be disabled.
4964 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004965 * \memberof weston_head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004966 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004967 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004968WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004969weston_head_detach(struct weston_head *head)
4970{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004971 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02004972 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004973
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004974 wl_list_remove(&head->output_link);
4975 wl_list_init(&head->output_link);
4976 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004977
4978 if (!output)
4979 return;
4980
4981 if (output->detach_head)
4982 output->detach_head(output, head);
4983
4984 if (output->enabled) {
4985 weston_head_remove_global(head);
4986
Pekka Paalanena0106992017-12-08 16:11:17 +02004987 if (wl_list_empty(&output->head_list)) {
4988 weston_log("Output '%s' no heads left, disabling.\n",
4989 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004990 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004991 } else {
4992 head_names = weston_output_create_heads_string(output);
4993 weston_log("Output '%s' updated to have head(s) %s\n",
4994 output->name, head_names);
4995 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004996
4997 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004998 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004999 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005000}
5001
5002/** Destroy a head
5003 *
5004 * \param head The head to be released.
5005 *
5006 * Destroys the head. The caller is responsible for freeing the memory pointed
5007 * to by \c head.
5008 *
5009 * \memberof weston_head
5010 * \internal
5011 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005012WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005013weston_head_release(struct weston_head *head)
5014{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005015 wl_signal_emit(&head->destroy_signal, head);
5016
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005017 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005018
5019 free(head->make);
5020 free(head->model);
5021 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005022 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005023
5024 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005025}
5026
Pekka Paalanene19970f2017-08-28 14:11:02 +03005027static void
5028weston_head_set_device_changed(struct weston_head *head)
5029{
5030 head->device_changed = true;
5031
5032 if (head->compositor)
5033 weston_compositor_schedule_heads_changed(head->compositor);
5034}
5035
5036/** String equal comparison with NULLs being equal */
5037static bool
5038str_null_eq(const char *a, const char *b)
5039{
5040 if (!a && !b)
5041 return true;
5042
5043 if (!!a != !!b)
5044 return false;
5045
5046 return strcmp(a, b) == 0;
5047}
5048
Pekka Paalanen01f60212017-03-24 15:39:24 +02005049/** Store monitor make, model and serial number
5050 *
5051 * \param head The head to modify.
5052 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5053 * any string, or NULL for none.
5054 * \param model The monitor model or name, or a made-up string, or NULL for
5055 * none.
5056 * \param serialno The monitor serial number, a made-up string, or NULL for
5057 * none.
5058 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005059 * This may set the device_changed flag.
5060 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02005061 * \memberof weston_head
5062 * \internal
5063 */
5064WL_EXPORT void
5065weston_head_set_monitor_strings(struct weston_head *head,
5066 const char *make,
5067 const char *model,
5068 const char *serialno)
5069{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005070 if (str_null_eq(head->make, make) &&
5071 str_null_eq(head->model, model) &&
5072 str_null_eq(head->serial_number, serialno))
5073 return;
5074
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005075 free(head->make);
5076 free(head->model);
5077 free(head->serial_number);
5078
5079 head->make = make ? strdup(make) : NULL;
5080 head->model = model ? strdup(model) : NULL;
5081 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005082
5083 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005084}
5085
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005086/** Store display non-desktop status
5087 *
5088 * \param head The head to modify.
5089 * \param non_desktop Whether the head connects to a non-desktop display.
5090 *
5091 * \memberof weston_head
5092 * \internal
5093 */
5094WL_EXPORT void
5095weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5096{
5097 if (head->non_desktop == non_desktop)
5098 return;
5099
5100 head->non_desktop = non_desktop;
5101
5102 weston_head_set_device_changed(head);
5103}
5104
Pekka Paalanen01f60212017-03-24 15:39:24 +02005105/** Store physical image size
5106 *
5107 * \param head The head to modify.
5108 * \param mm_width Image area width in millimeters.
5109 * \param mm_height Image area height in millimeters.
5110 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005111 * This may set the device_changed flag.
5112 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02005113 * \memberof weston_head
5114 * \internal
5115 */
5116WL_EXPORT void
5117weston_head_set_physical_size(struct weston_head *head,
5118 int32_t mm_width, int32_t mm_height)
5119{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005120 if (head->mm_width == mm_width &&
5121 head->mm_height == mm_height)
5122 return;
5123
Pekka Paalanen01f60212017-03-24 15:39:24 +02005124 head->mm_width = mm_width;
5125 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005126
5127 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005128}
5129
5130/** Store monitor sub-pixel layout
5131 *
5132 * \param head The head to modify.
5133 * \param sp Sub-pixel layout. The possible values are:
5134 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5135 * - WL_OUTPUT_SUBPIXEL_NONE,
5136 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5137 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5138 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5139 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5140 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005141 * This may set the device_changed flag.
5142 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02005143 * \memberof weston_head
5144 * \internal
5145 */
5146WL_EXPORT void
5147weston_head_set_subpixel(struct weston_head *head,
5148 enum wl_output_subpixel sp)
5149{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005150 if (head->subpixel == sp)
5151 return;
5152
Pekka Paalanen01f60212017-03-24 15:39:24 +02005153 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005154
5155 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005156}
5157
5158/** Mark the monitor as internal
5159 *
5160 * This is used for embedded screens, like laptop panels.
5161 *
5162 * \param head The head to mark as internal.
5163 *
5164 * By default a head is external. The type is often inferred from the physical
5165 * connector type.
5166 *
5167 * \memberof weston_head
5168 * \internal
5169 */
5170WL_EXPORT void
5171weston_head_set_internal(struct weston_head *head)
5172{
5173 head->connection_internal = true;
5174}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005175
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005176/** Store connector status
5177 *
5178 * \param head The head to modify.
5179 * \param connected Whether the head is connected.
5180 *
5181 * Connectors are created as disconnected. This function can be used to
5182 * set the connector status.
5183 *
5184 * The status should be set to true when a physical connector is connected to
5185 * a video sink device like a monitor and to false when the connector is
5186 * disconnected. For nested backends, the connection status should reflect the
5187 * connection to the parent display server.
5188 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005189 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005190 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005191 *
5192 * \sa weston_compositor_set_heads_changed_cb
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005193 * \memberof weston_head
5194 * \internal
5195 */
5196WL_EXPORT void
5197weston_head_set_connection_status(struct weston_head *head, bool connected)
5198{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005199 if (head->connected == connected)
5200 return;
5201
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005202 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005203
Pekka Paalanene19970f2017-08-28 14:11:02 +03005204 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005205}
5206
5207/** Is the head currently connected?
5208 *
5209 * \param head The head to query.
5210 * \return Connection status.
5211 *
5212 * Returns true if the head is physically connected to a monitor, or in
5213 * case of a nested backend returns true when there is a connection to the
5214 * parent display server.
5215 *
5216 * This is independent from the head being enabled.
5217 *
5218 * \sa weston_head_is_enabled
5219 * \memberof weston_head
5220 */
5221WL_EXPORT bool
5222weston_head_is_connected(struct weston_head *head)
5223{
5224 return head->connected;
5225}
5226
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005227/** Is the head currently enabled?
5228 *
5229 * \param head The head to query.
5230 * \return Video status.
5231 *
5232 * Returns true if the head is currently transmitting a video stream.
5233 *
5234 * This is independent of the head being connected.
5235 *
5236 * \sa weston_head_is_connected
5237 * \memberof weston_head
5238 */
5239WL_EXPORT bool
5240weston_head_is_enabled(struct weston_head *head)
5241{
5242 if (!head->output)
5243 return false;
5244
5245 return head->output->enabled;
5246}
5247
Pekka Paalanene19970f2017-08-28 14:11:02 +03005248/** Has the device information changed?
5249 *
5250 * \param head The head to query.
5251 * \return True if the device information has changed since last reset.
5252 *
5253 * The information about the connected display device, e.g. a monitor, may
5254 * change without being disconnected in between. Changing information
5255 * causes a call to the heads_changed hook.
5256 *
5257 * The information includes make, model, serial number, physical size,
5258 * and sub-pixel type. The connection status is also included.
5259 *
5260 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
5261 * \memberof weston_head
5262 */
5263WL_EXPORT bool
5264weston_head_is_device_changed(struct weston_head *head)
5265{
5266 return head->device_changed;
5267}
5268
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005269/** Does the head represent a non-desktop display?
5270 *
5271 * \param head The head to query.
5272 * \return True if the device is a non-desktop display.
5273 *
5274 * Non-desktop heads are not attached to outputs by default.
5275 * This stops weston from extending the desktop onto head mounted displays.
5276 *
5277 * \memberof weston_head
5278 */
5279WL_EXPORT bool
5280weston_head_is_non_desktop(struct weston_head *head)
5281{
5282 return head->non_desktop;
5283}
5284
Pekka Paalanene19970f2017-08-28 14:11:02 +03005285/** Acknowledge device information change
5286 *
5287 * \param head The head to acknowledge.
5288 *
5289 * Clears the device changed flag on this head. When a compositor has processed
5290 * device information, it should call this to be able to notice further
5291 * changes.
5292 *
5293 * \sa weston_head_is_device_changed
5294 * \memberof weston_head
5295 */
5296WL_EXPORT void
5297weston_head_reset_device_changed(struct weston_head *head)
5298{
5299 head->device_changed = false;
5300}
5301
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005302/** Get the name of a head
5303 *
5304 * \param head The head to query.
5305 * \return The head's name, not NULL.
5306 *
5307 * The name depends on the backend. The DRM backend uses connector names,
5308 * other backends may use hardcoded names or user-given names.
5309 */
5310WL_EXPORT const char *
5311weston_head_get_name(struct weston_head *head)
5312{
5313 return head->name;
5314}
5315
5316/** Get the output the head is attached to
5317 *
5318 * \param head The head to query.
5319 * \return The output the head is attached to, or NULL if detached.
5320 */
5321WL_EXPORT struct weston_output *
5322weston_head_get_output(struct weston_head *head)
5323{
5324 return head->output;
5325}
5326
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005327/** Add destroy callback for a head
5328 *
5329 * \param head The head to watch for.
5330 * \param listener The listener to add. The \c notify member must be set.
5331 *
5332 * Heads may get destroyed for various reasons by the backends. If a head is
5333 * attached to an output, the compositor should listen for head destruction
5334 * and reconfigure or destroy the output if necessary.
5335 *
5336 * The destroy callbacks will be called on weston_head destruction before any
5337 * automatic detaching from an associated weston_output and before any
5338 * weston_head information is lost.
5339 *
5340 * The \c data argument to the notify callback is the weston_head being
5341 * destroyed.
5342 */
5343WL_EXPORT void
5344weston_head_add_destroy_listener(struct weston_head *head,
5345 struct wl_listener *listener)
5346{
5347 wl_signal_add(&head->destroy_signal, listener);
5348}
5349
5350/** Look up destroy listener for a head
5351 *
5352 * \param head The head to query.
5353 * \param notify The notify function used used for the added destroy listener.
5354 * \return The listener, or NULL if not found.
5355 *
5356 * This looks up the previously added destroy listener struct based on the
5357 * notify function it has. The listener can be used to access user data
5358 * through \c container_of().
5359 *
5360 * \sa wl_signal_get()
5361 */
5362WL_EXPORT struct wl_listener *
5363weston_head_get_destroy_listener(struct weston_head *head,
5364 wl_notify_func_t notify)
5365{
5366 return wl_signal_get(&head->destroy_signal, notify);
5367}
5368
David Fort0de859e2016-05-27 23:22:57 +02005369/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005370static void
David Fort0de859e2016-05-27 23:22:57 +02005371weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5372 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005373{
5374 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005375 bool start_resizing = false;
5376
5377 if (!delta_width)
5378 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005379
5380 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005381 if (output == resized_output) {
5382 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005383 continue;
5384 }
5385
David Fort0de859e2016-05-27 23:22:57 +02005386 if (start_resizing) {
5387 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005388 output->dirty = 1;
5389 }
5390 }
5391}
5392
Pekka Paalanend72bad22017-03-29 17:01:41 +03005393static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005394weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005395{
Scott Moreau850ca422012-05-21 15:21:25 -06005396 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005397
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005398 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005399 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005400
Scott Moreauccbf29d2012-02-22 14:21:41 -07005401 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005402 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005403 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005404 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005405 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005406 weston_matrix_scale(&output->matrix, magnification,
5407 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005408 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005409
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005410 switch (output->transform) {
5411 case WL_OUTPUT_TRANSFORM_FLIPPED:
5412 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5413 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5414 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5415 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5416 weston_matrix_scale(&output->matrix, -1, 1, 1);
5417 break;
5418 }
5419
5420 switch (output->transform) {
5421 default:
5422 case WL_OUTPUT_TRANSFORM_NORMAL:
5423 case WL_OUTPUT_TRANSFORM_FLIPPED:
5424 break;
5425 case WL_OUTPUT_TRANSFORM_90:
5426 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5427 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5428 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5429 break;
5430 case WL_OUTPUT_TRANSFORM_180:
5431 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5432 weston_matrix_translate(&output->matrix,
5433 -output->width, -output->height, 0);
5434 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5435 break;
5436 case WL_OUTPUT_TRANSFORM_270:
5437 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5438 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5439 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5440 break;
5441 }
5442
5443 if (output->current_scale != 1)
5444 weston_matrix_scale(&output->matrix,
5445 output->current_scale,
5446 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005447
Scott Moreauccbf29d2012-02-22 14:21:41 -07005448 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005449
5450 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005451}
5452
Scott Moreau1bad5db2012-08-18 01:04:05 -06005453static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005454weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005455{
5456 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005457 output->native_scale = scale;
5458 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005459
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005460 convert_size_by_transform_scale(&output->width, &output->height,
5461 output->current_mode->width,
5462 output->current_mode->height,
5463 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005464}
5465
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005466static void
5467weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005468{
5469 output->x = x;
5470 output->y = y;
5471
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005472 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005473 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005474
5475 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005476 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005477 output->width,
5478 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005479}
5480
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005481WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005482weston_output_move(struct weston_output *output, int x, int y)
5483{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005484 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005485 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005486 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005487
5488 output->move_x = x - output->x;
5489 output->move_y = y - output->y;
5490
5491 if (output->move_x == 0 && output->move_y == 0)
5492 return;
5493
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005494 weston_output_init_geometry(output, x, y);
5495
5496 output->dirty = 1;
5497
5498 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005499 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005500
5501 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005502 wl_list_for_each(head, &output->head_list, output_link) {
5503 wl_resource_for_each(resource, &head->resource_list) {
5504 wl_output_send_geometry(resource,
5505 output->x,
5506 output->y,
5507 head->mm_width,
5508 head->mm_height,
5509 head->subpixel,
5510 head->make,
5511 head->model,
5512 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005513
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005514 ver = wl_resource_get_version(resource);
5515 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5516 wl_output_send_done(resource);
5517 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005518 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005519}
5520
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005521/** Signal that a pending output is taken into use.
5522 *
5523 * Removes the output from the pending list and adds it to the compositor's
5524 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005525 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005526 * The output gets an internal ID assigned, and the wl_output global is
5527 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005528 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005529 * \param compositor The compositor instance.
5530 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005531 *
5532 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005533 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005534static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005535weston_compositor_add_output(struct weston_compositor *compositor,
5536 struct weston_output *output)
5537{
Armin Krezoviće5403842016-08-05 15:28:29 +02005538 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005539 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005540
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005541 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005542
5543 /* Verify we haven't reached the limit of 32 available output IDs */
5544 assert(ffs(~compositor->output_id_pool) > 0);
5545
5546 /* Invert the output id pool and look for the lowest numbered
5547 * switch (the least significant bit). Take that bit's position
5548 * as our ID, and mark it used in the compositor's output_id_pool.
5549 */
5550 output->id = ffs(~compositor->output_id_pool) - 1;
5551 compositor->output_id_pool |= 1u << output->id;
5552
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005553 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005554 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005555 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005556
Pekka Paalanendcac3512017-12-08 14:13:34 +02005557 wl_list_for_each(head, &output->head_list, output_link)
5558 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005559
Giulio Camuffob1147152015-05-06 21:41:57 +03005560 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005561
5562 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5563 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005564}
5565
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005566/** Transform device coordinates into global coordinates
5567 *
Marius Vlada2dace22019-06-12 16:05:44 +03005568 * \param output the weston_output object
5569 * \param[in] device_x X coordinate in device units.
5570 * \param[in] device_y Y coordinate in device units.
5571 * \param[out] x X coordinate in the global space.
5572 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005573 *
Marius Vlada2dace22019-06-12 16:05:44 +03005574 * Transforms coordinates from the device coordinate space (physical pixel
5575 * units) to the global coordinate space (logical pixel units). This takes
5576 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005577 *
5578 * \memberof weston_output
5579 * \internal
5580 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005581WL_EXPORT void
5582weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005583 double device_x, double device_y,
5584 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005585{
Derek Foreman0f679412014-10-02 13:41:17 -05005586 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005587 device_x,
5588 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005589 0.0,
5590 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005591
Derek Foreman67a18b92015-03-24 11:36:14 -05005592 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005593
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005594 *x = p.f[0] / p.f[3];
5595 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005596}
5597
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005598/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005599 *
5600 * \param output The weston_output object that is being removed.
5601 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005602 * The following happens:
5603 *
5604 * - The output assignments of all views in the current scenegraph are
5605 * recomputed.
5606 *
5607 * - Presentation feedback is discarded.
5608 *
5609 * - Compositor is notified that outputs were changed and
5610 * applies the necessary changes to re-layout outputs.
5611 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005612 * - The output is put back in the pending outputs list.
5613 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005614 * - Signal is emitted to notify all users of the weston_output
5615 * object that the output is being destroyed.
5616 *
5617 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005618 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005619 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005620 * - The output's internal ID is released.
5621 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005622 * \memberof weston_output
5623 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005624 */
5625static void
5626weston_compositor_remove_output(struct weston_output *output)
5627{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005628 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005629 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005630 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005631
5632 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005633 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005634
Pekka Paalanenbccda712017-03-29 16:16:04 +03005635 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005636 if (view->output_mask & (1u << output->id))
5637 weston_view_assign_output(view);
5638 }
5639
5640 weston_presentation_feedback_discard_list(&output->feedback_list);
5641
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005642 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005643
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005644 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005645 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005646 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005647
Pekka Paalanenbccda712017-03-29 16:16:04 +03005648 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005649 wl_signal_emit(&output->destroy_signal, output);
5650
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005651 wl_list_for_each(head, &output->head_list, output_link)
5652 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005653
5654 compositor->output_id_pool &= ~(1u << output->id);
5655 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005656}
5657
5658/** Sets the output scale for a given output.
5659 *
5660 * \param output The weston_output object that the scale is set for.
5661 * \param scale Scale factor for the given output.
5662 *
5663 * It only supports setting scale for an output that
5664 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005665 *
5666 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005667 */
5668WL_EXPORT void
5669weston_output_set_scale(struct weston_output *output,
5670 int32_t scale)
5671{
5672 /* We can only set scale on a disabled output */
5673 assert(!output->enabled);
5674
5675 /* We only want to set scale once */
5676 assert(!output->scale);
5677
5678 output->scale = scale;
5679}
5680
5681/** Sets the output transform for a given output.
5682 *
5683 * \param output The weston_output object that the transform is set for.
5684 * \param transform Transform value for the given output.
5685 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005686 * Refer to wl_output::transform section located at
5687 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5688 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005689 *
5690 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005691 */
5692WL_EXPORT void
5693weston_output_set_transform(struct weston_output *output,
5694 uint32_t transform)
5695{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005696 struct weston_pointer_motion_event ev;
5697 struct wl_resource *resource;
5698 struct weston_seat *seat;
5699 pixman_region32_t old_region;
5700 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005701 struct weston_head *head;
5702 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005703
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005704 if (!output->enabled && output->transform == UINT32_MAX) {
5705 output->transform = transform;
5706 return;
5707 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005708
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005709 weston_output_transform_scale_init(output, transform, output->scale);
5710
5711 pixman_region32_init(&old_region);
5712 pixman_region32_copy(&old_region, &output->region);
5713
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005714 weston_output_init_geometry(output, output->x, output->y);
5715
5716 output->dirty = 1;
5717
5718 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005719 wl_list_for_each(head, &output->head_list, output_link) {
5720 wl_resource_for_each(resource, &head->resource_list) {
5721 wl_output_send_geometry(resource,
5722 output->x,
5723 output->y,
5724 head->mm_width,
5725 head->mm_height,
5726 head->subpixel,
5727 head->make,
5728 head->model,
5729 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005730
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005731 ver = wl_resource_get_version(resource);
5732 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5733 wl_output_send_done(resource);
5734 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005735 }
5736
5737 /* we must ensure that pointers are inside output, otherwise they disappear */
5738 mid_x = output->x + output->width / 2;
5739 mid_y = output->y + output->height / 2;
5740
5741 ev.mask = WESTON_POINTER_MOTION_ABS;
5742 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5743 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5744
5745 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5746 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5747
5748 if (pointer && pixman_region32_contains_point(&old_region,
5749 wl_fixed_to_int(pointer->x),
5750 wl_fixed_to_int(pointer->y),
5751 NULL))
5752 weston_pointer_move(pointer, &ev);
5753 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005754}
5755
5756/** Initializes a weston_output object with enough data so
5757 ** an output can be configured.
5758 *
5759 * \param output The weston_output object to initialize
5760 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005761 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005762 *
5763 * Sets initial values for fields that are expected to be
5764 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005765 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005766 * The name is used in logs, and can be used by compositors as a configuration
5767 * identifier.
5768 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005769 * \memberof weston_output
5770 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005771 */
5772WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005773weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005774 struct weston_compositor *compositor,
5775 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005776{
5777 output->compositor = compositor;
5778 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005779 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005780 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005781 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005782 output->enabled = false;
5783
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005784 wl_list_init(&output->head_list);
5785
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005786 /* Add some (in)sane defaults which can be used
5787 * for checking if an output was properly configured
5788 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005789 output->scale = 0;
5790 /* Can't use -1 on uint32_t and 0 is valid enum value */
5791 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005792
5793 pixman_region32_init(&output->previous_damage);
5794 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005795 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005796}
5797
5798/** Adds weston_output object to pending output list.
5799 *
5800 * \param output The weston_output object to add
5801 * \param compositor The compositor instance.
5802 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005803 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005804 *
5805 * \memberof weston_output
5806 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005807 */
5808WL_EXPORT void
5809weston_compositor_add_pending_output(struct weston_output *output,
5810 struct weston_compositor *compositor)
5811{
Pekka Paalanene952a012017-03-29 17:14:00 +03005812 assert(output->disable);
5813 assert(output->enable);
5814
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005815 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005816 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005817}
5818
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005819/** Create a string with the attached heads' names.
5820 *
5821 * The string must be free()'d.
5822 */
5823static char *
5824weston_output_create_heads_string(struct weston_output *output)
5825{
5826 FILE *fp;
5827 char *str = NULL;
5828 size_t size = 0;
5829 struct weston_head *head;
5830 const char *sep = "";
5831
5832 fp = open_memstream(&str, &size);
5833 if (!fp)
5834 return NULL;
5835
5836 wl_list_for_each(head, &output->head_list, output_link) {
5837 fprintf(fp, "%s%s", sep, head->name);
5838 sep = ", ";
5839 }
5840 fclose(fp);
5841
5842 return str;
5843}
5844
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005845/** Constructs a weston_output object that can be used by the compositor.
5846 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005847 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005848 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005849 *
5850 * Output coordinates are calculated and each new output is by default
5851 * assigned to the right of previous one.
5852 *
5853 * Sets up the transformation, zoom, and geometry of the output using
5854 * the properties that need to be configured by the compositor.
5855 *
5856 * Establishes a repaint timer for the output with the relevant display
5857 * object's event loop. See output_repaint_timer_handler().
5858 *
5859 * The output is assigned an ID. Weston can support up to 32 distinct
5860 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5861 * is referred to and used to find the first available ID number, and
5862 * then this ID is marked as used in output_id_pool.
5863 *
5864 * The output is also assigned a Wayland global with the wl_output
5865 * external interface.
5866 *
5867 * Backend specific function is called to set up the output output.
5868 *
5869 * Output is added to the compositor's output list
5870 *
5871 * If the backend specific function fails, the weston_output object
5872 * is returned to a state it was before calling this function and
5873 * is added to the compositor's pending_output_list in case it needs
5874 * to be reconfigured or just so it can be destroyed at shutdown.
5875 *
5876 * 0 is returned on success, -1 on failure.
5877 */
5878WL_EXPORT int
5879weston_output_enable(struct weston_output *output)
5880{
Armin Krezović782f5df2016-09-30 14:11:11 +02005881 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005882 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005883 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005884 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005885 int x = 0, y = 0;
5886
Pekka Paalanencc201e42017-03-30 15:11:25 +03005887 if (output->enabled) {
5888 weston_log("Error: attempt to enable an enabled output '%s'\n",
5889 output->name);
5890 return -1;
5891 }
5892
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005893 if (wl_list_empty(&output->head_list)) {
5894 weston_log("Error: cannot enable output '%s' without heads.\n",
5895 output->name);
5896 return -1;
5897 }
5898
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005899 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5900 weston_log("Error: no video mode for output '%s'.\n",
5901 output->name);
5902 return -1;
5903 }
5904
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005905 wl_list_for_each(head, &output->head_list, output_link) {
5906 assert(head->make);
5907 assert(head->model);
5908 }
5909
Armin Krezović782f5df2016-09-30 14:11:11 +02005910 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005911 struct weston_output, link);
5912
Armin Krezović782f5df2016-09-30 14:11:11 +02005913 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005914 x = iterator->x + iterator->width;
5915
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005916 /* Make sure the scale is set up */
5917 assert(output->scale);
5918
5919 /* Make sure we have a transform set */
5920 assert(output->transform != UINT32_MAX);
5921
Armin Krezović782f5df2016-09-30 14:11:11 +02005922 output->x = x;
5923 output->y = y;
5924 output->dirty = 1;
5925 output->original_scale = output->scale;
5926
5927 weston_output_transform_scale_init(output, output->transform, output->scale);
5928 weston_output_init_zoom(output);
5929
5930 weston_output_init_geometry(output, x, y);
5931 weston_output_damage(output);
5932
5933 wl_signal_init(&output->frame_signal);
5934 wl_signal_init(&output->destroy_signal);
5935 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005936 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005937
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005938 /* Enable the output (set up the crtc or create a
5939 * window representing the output, set up the
5940 * renderer, etc)
5941 */
5942 if (output->enable(output) < 0) {
5943 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005944 return -1;
5945 }
5946
5947 weston_compositor_add_output(output->compositor, output);
5948
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005949 head_names = weston_output_create_heads_string(output);
5950 weston_log("Output '%s' enabled with head(s) %s\n",
5951 output->name, head_names);
5952 free(head_names);
5953
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005954 return 0;
5955}
5956
5957/** Converts a weston_output object to a pending output state, so it
5958 ** can be configured again or destroyed.
5959 *
5960 * \param output The weston_output object that needs to be disabled.
5961 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005962 * Calls a backend specific function to disable an output, in case
5963 * such function exists.
5964 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005965 * The backend specific disable function may choose to postpone the disabling
5966 * by returning a negative value, in which case this function returns early.
5967 * In that case the backend will guarantee the output will be disabled soon
5968 * by the backend calling this function again. One must not attempt to re-enable
5969 * the output until that happens.
5970 *
5971 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005972 * from weston's output_list (see weston_compositor_remove_output())
5973 * and is returned to a state it was before weston_output_enable()
5974 * was ran (see weston_output_enable_undo()).
5975 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005976 * See weston_output_init() for more information on the
5977 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005978 *
5979 * If the output has never been enabled yet, this function can still be
5980 * called to ensure that the output is actually turned off rather than left
5981 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005982 */
5983WL_EXPORT void
5984weston_output_disable(struct weston_output *output)
5985{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005986 /* Should we rename this? */
5987 output->destroying = 1;
5988
Pekka Paalanenc65df642017-03-29 15:45:46 +03005989 /* Disable is called unconditionally also for not-enabled outputs,
5990 * because at compositor start-up, if there is an output that is
5991 * already on but the compositor wants to turn it off, we have to
5992 * forward the turn-off to the backend so it knows to do it.
5993 * The backend cannot initially turn off everything, because it
5994 * would cause unnecessary mode-sets for all outputs the compositor
5995 * wants to be on.
5996 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005997 if (output->disable(output) < 0)
5998 return;
5999
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006000 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006001 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006002
6003 output->destroying = 0;
6004}
6005
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006006/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006007 *
6008 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006009 *
6010 * If there are new or changed heads, calls the heads_changed hook and
6011 * returns after the hook returns.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006012 */
6013WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006014weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006015{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006016 if (compositor->heads_changed_source) {
6017 wl_event_source_remove(compositor->heads_changed_source);
6018 weston_compositor_call_heads_changed(compositor);
6019 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006020}
6021
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006022/** Add destroy callback for an output
6023 *
6024 * \param output The output to watch.
6025 * \param listener The listener to add. The \c notify member must be set.
6026 *
6027 * The listener callback will be called when user destroys an output. This
6028 * may be delayed by a backend in some cases. The main purpose of the
6029 * listener is to allow hooking up custom data to the output. The custom data
6030 * can be fetched via weston_output_get_destroy_listener() followed by
6031 * container_of().
6032 *
6033 * The \c data argument to the notify callback is the weston_output being
6034 * destroyed.
6035 *
6036 * @note This is for the final destruction of an output, not when it gets
6037 * disabled. If you want to keep track of enabled outputs, this is not it.
6038 */
6039WL_EXPORT void
6040weston_output_add_destroy_listener(struct weston_output *output,
6041 struct wl_listener *listener)
6042{
6043 wl_signal_add(&output->user_destroy_signal, listener);
6044}
6045
6046/** Look up destroy listener for an output
6047 *
6048 * \param output The output to query.
6049 * \param notify The notify function used used for the added destroy listener.
6050 * \return The listener, or NULL if not found.
6051 *
6052 * This looks up the previously added destroy listener struct based on the
6053 * notify function it has. The listener can be used to access user data
6054 * through \c container_of().
6055 *
6056 * \sa wl_signal_get() weston_output_add_destroy_listener()
6057 */
6058WL_EXPORT struct wl_listener *
6059weston_output_get_destroy_listener(struct weston_output *output,
6060 wl_notify_func_t notify)
6061{
6062 return wl_signal_get(&output->user_destroy_signal, notify);
6063}
6064
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006065/** Uninitialize an output
6066 *
6067 * Removes the output from the list of enabled outputs if necessary, but
6068 * does not call the backend's output disable function. The output will no
6069 * longer be in the list of pending outputs either.
6070 *
6071 * All fields of weston_output become uninitialized, i.e. should not be used
6072 * anymore. The caller can free the memory after this.
6073 *
6074 * \memberof weston_output
6075 * \internal
6076 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006077WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006078weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006079{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006080 struct weston_head *head, *tmp;
6081
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006082 output->destroying = 1;
6083
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006084 wl_signal_emit(&output->user_destroy_signal, output);
6085
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006086 if (output->idle_repaint_source)
6087 wl_event_source_remove(output->idle_repaint_source);
6088
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006089 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006090 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006091
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006092 pixman_region32_fini(&output->region);
6093 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006094 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006095
6096 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6097 weston_head_detach(head);
6098
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006099 free(output->name);
6100}
6101
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006102/** Find an output by its given name
6103 *
6104 * \param compositor The compositor to search in.
6105 * \param name The output name to search for.
6106 * \return An existing output with the given name, or NULL if not found.
6107 *
6108 * \memberof weston_compositor
6109 */
6110WL_EXPORT struct weston_output *
6111weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6112 const char *name)
6113{
6114 struct weston_output *output;
6115
6116 wl_list_for_each(output, &compositor->output_list, link)
6117 if (strcmp(output->name, name) == 0)
6118 return output;
6119
6120 wl_list_for_each(output, &compositor->pending_output_list, link)
6121 if (strcmp(output->name, name) == 0)
6122 return output;
6123
6124 return NULL;
6125}
6126
6127/** Create a named output
6128 *
6129 * \param compositor The compositor.
6130 * \param name The name for the output.
6131 * \return A new \c weston_output, or NULL on failure.
6132 *
6133 * This creates a new weston_output that starts with no heads attached.
6134 *
6135 * An output must be configured and it must have at least one head before
6136 * it can be enabled.
6137 *
6138 * \memberof weston_compositor
6139 */
6140WL_EXPORT struct weston_output *
6141weston_compositor_create_output(struct weston_compositor *compositor,
6142 const char *name)
6143{
6144 assert(compositor->backend->create_output);
6145
6146 if (weston_compositor_find_output_by_name(compositor, name)) {
6147 weston_log("Warning: attempted to create an output with a "
6148 "duplicate name '%s'.\n", name);
6149 return NULL;
6150 }
6151
6152 return compositor->backend->create_output(compositor, name);
6153}
6154
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006155/** Create an output for an unused head
6156 *
6157 * \param compositor The compositor.
6158 * \param head The head to attach to the output.
6159 * \return A new \c weston_output, or NULL on failure.
6160 *
6161 * This creates a new weston_output that starts with the given head attached.
6162 * The output inherits the name of the head. The head must not be already
6163 * attached to another output.
6164 *
6165 * An output must be configured before it can be enabled.
6166 *
6167 * \memberof weston_compositor
6168 */
6169WL_EXPORT struct weston_output *
6170weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6171 struct weston_head *head)
6172{
6173 struct weston_output *output;
6174
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006175 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006176 if (!output)
6177 return NULL;
6178
6179 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006180 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006181 return NULL;
6182 }
6183
6184 return output;
6185}
6186
6187/** Destroy an output
6188 *
6189 * \param output The output to destroy.
6190 *
6191 * The heads attached to the given output are detached and become unused again.
6192 *
6193 * It is not necessary to explicitly destroy all outputs at compositor exit.
6194 * weston_compositor_destroy() will automatically destroy any remaining
6195 * outputs.
6196 *
6197 * \memberof weston_output
6198 */
6199WL_EXPORT void
6200weston_output_destroy(struct weston_output *output)
6201{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006202 output->destroy(output);
6203}
6204
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006205/** When you need a head...
6206 *
6207 * This function is a hack, used until all code has been converted to become
6208 * multi-head aware.
6209 *
6210 * \param output The weston_output whose head to get.
6211 * \return The first head in the output's list.
6212 */
6213WL_EXPORT struct weston_head *
6214weston_output_get_first_head(struct weston_output *output)
6215{
6216 if (wl_list_empty(&output->head_list))
6217 return NULL;
6218
6219 return container_of(output->head_list.next,
6220 struct weston_head, output_link);
6221}
6222
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006223static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006224destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006225{
Jonny Lamb74130762013-11-26 18:19:46 +01006226 struct weston_surface *surface =
6227 wl_resource_get_user_data(resource);
6228
Pekka Paalanen4826f872016-04-22 14:14:38 +03006229 if (!surface)
6230 return;
6231
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006232 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006233 surface->pending.buffer_viewport.buffer.src_width =
6234 wl_fixed_from_int(-1);
6235 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006236 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006237}
6238
6239static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006240viewport_destroy(struct wl_client *client,
6241 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006242{
6243 wl_resource_destroy(resource);
6244}
6245
6246static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006247viewport_set_source(struct wl_client *client,
6248 struct wl_resource *resource,
6249 wl_fixed_t src_x,
6250 wl_fixed_t src_y,
6251 wl_fixed_t src_width,
6252 wl_fixed_t src_height)
6253{
6254 struct weston_surface *surface =
6255 wl_resource_get_user_data(resource);
6256
Pekka Paalanen4826f872016-04-22 14:14:38 +03006257 if (!surface) {
6258 wl_resource_post_error(resource,
6259 WP_VIEWPORT_ERROR_NO_SURFACE,
6260 "wl_surface for this viewport is no longer exists");
6261 return;
6262 }
6263
6264 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006265 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006266
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006267 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006268 src_height == wl_fixed_from_int(-1) &&
6269 src_x == wl_fixed_from_int(-1) &&
6270 src_y == wl_fixed_from_int(-1)) {
6271 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006272 surface->pending.buffer_viewport.buffer.src_width =
6273 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006274 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006275 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006276 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006277
Pekka Paalanen201769a2016-04-26 14:42:11 +03006278 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006279 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006280 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006281 "wl_surface@%d viewport source "
6282 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6283 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006284 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006285 wl_fixed_to_double(src_height),
6286 wl_fixed_to_double(src_x),
6287 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006288 return;
6289 }
6290
6291 surface->pending.buffer_viewport.buffer.src_x = src_x;
6292 surface->pending.buffer_viewport.buffer.src_y = src_y;
6293 surface->pending.buffer_viewport.buffer.src_width = src_width;
6294 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006295 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006296}
6297
6298static void
6299viewport_set_destination(struct wl_client *client,
6300 struct wl_resource *resource,
6301 int32_t dst_width,
6302 int32_t dst_height)
6303{
6304 struct weston_surface *surface =
6305 wl_resource_get_user_data(resource);
6306
Pekka Paalanen4826f872016-04-22 14:14:38 +03006307 if (!surface) {
6308 wl_resource_post_error(resource,
6309 WP_VIEWPORT_ERROR_NO_SURFACE,
6310 "wl_surface for this viewport no longer exists");
6311 return;
6312 }
6313
6314 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006315
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006316 if (dst_width == -1 && dst_height == -1) {
6317 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006318 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006319 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006320 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006321 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006322
6323 if (dst_width <= 0 || dst_height <= 0) {
6324 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006325 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006326 "destination size must be positive (%dx%d)",
6327 dst_width, dst_height);
6328 return;
6329 }
6330
6331 surface->pending.buffer_viewport.surface.width = dst_width;
6332 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006333 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006334}
6335
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006336static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006337 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006338 viewport_set_source,
6339 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006340};
6341
6342static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006343viewporter_destroy(struct wl_client *client,
6344 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006345{
6346 wl_resource_destroy(resource);
6347}
6348
6349static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006350viewporter_get_viewport(struct wl_client *client,
6351 struct wl_resource *viewporter,
6352 uint32_t id,
6353 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006354{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006355 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006356 struct weston_surface *surface =
6357 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006358 struct wl_resource *resource;
6359
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006360 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006361 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006362 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006363 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006364 return;
6365 }
6366
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006367 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006368 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006369 if (resource == NULL) {
6370 wl_client_post_no_memory(client);
6371 return;
6372 }
6373
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006374 wl_resource_set_implementation(resource, &viewport_interface,
6375 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006376
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006377 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006378}
6379
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006380static const struct wp_viewporter_interface viewporter_interface = {
6381 viewporter_destroy,
6382 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006383};
6384
6385static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006386bind_viewporter(struct wl_client *client,
6387 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006388{
6389 struct wl_resource *resource;
6390
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006391 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006392 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006393 if (resource == NULL) {
6394 wl_client_post_no_memory(client);
6395 return;
6396 }
6397
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006398 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006399 NULL, NULL);
6400}
6401
6402static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006403destroy_presentation_feedback(struct wl_resource *feedback_resource)
6404{
6405 struct weston_presentation_feedback *feedback;
6406
6407 feedback = wl_resource_get_user_data(feedback_resource);
6408
6409 wl_list_remove(&feedback->link);
6410 free(feedback);
6411}
6412
6413static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006414presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6415{
6416 wl_resource_destroy(resource);
6417}
6418
6419static void
6420presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006421 struct wl_resource *presentation_resource,
6422 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006423 uint32_t callback)
6424{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006425 struct weston_surface *surface;
6426 struct weston_presentation_feedback *feedback;
6427
6428 surface = wl_resource_get_user_data(surface_resource);
6429
Bryce Harringtonde16d892014-11-20 22:21:57 -08006430 feedback = zalloc(sizeof *feedback);
6431 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006432 goto err_calloc;
6433
6434 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006435 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006436 1, callback);
6437 if (!feedback->resource)
6438 goto err_create;
6439
6440 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6441 destroy_presentation_feedback);
6442
6443 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6444
6445 return;
6446
6447err_create:
6448 free(feedback);
6449
6450err_calloc:
6451 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006452}
6453
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006454static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006455 presentation_destroy,
6456 presentation_feedback
6457};
6458
6459static void
6460bind_presentation(struct wl_client *client,
6461 void *data, uint32_t version, uint32_t id)
6462{
6463 struct weston_compositor *compositor = data;
6464 struct wl_resource *resource;
6465
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006466 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006467 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006468 if (resource == NULL) {
6469 wl_client_post_no_memory(client);
6470 return;
6471 }
6472
6473 wl_resource_set_implementation(resource, &presentation_implementation,
6474 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006475 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006476}
6477
6478static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006479compositor_bind(struct wl_client *client,
6480 void *data, uint32_t version, uint32_t id)
6481{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006482 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006483 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006484
Jason Ekstranda85118c2013-06-27 20:17:02 -05006485 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006486 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006487 if (resource == NULL) {
6488 wl_client_post_no_memory(client);
6489 return;
6490 }
6491
6492 wl_resource_set_implementation(resource, &compositor_interface,
6493 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006494}
6495
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006496WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006497weston_environment_get_fd(const char *env)
6498{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006499 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006500 int fd, flags;
6501
6502 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006503 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006504 return -1;
6505
6506 flags = fcntl(fd, F_GETFD);
6507 if (flags == -1)
6508 return -1;
6509
6510 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6511 unsetenv(env);
6512
6513 return fd;
6514}
6515
Pekka Paalanenb5026542014-11-12 15:09:24 +02006516static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006517timeline_key_binding_handler(struct weston_keyboard *keyboard,
6518 const struct timespec *time, uint32_t key,
6519 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006520{
6521 struct weston_compositor *compositor = data;
6522
6523 if (weston_timeline_enabled_)
6524 weston_timeline_close();
6525 else
6526 weston_timeline_open(compositor);
6527}
6528
Daniel Stonece62cb32018-07-20 09:46:24 +01006529static const char *
6530output_repaint_status_text(struct weston_output *output)
6531{
6532 switch (output->repaint_status) {
6533 case REPAINT_NOT_SCHEDULED:
6534 return "no repaint";
6535 case REPAINT_BEGIN_FROM_IDLE:
6536 return "start_repaint_loop scheduled";
6537 case REPAINT_SCHEDULED:
6538 return "repaint scheduled";
6539 case REPAINT_AWAITING_COMPLETION:
6540 return "awaiting completion";
6541 }
6542
6543 assert(!"output_repaint_status_text missing enum");
6544 return NULL;
6545}
6546
6547static void
6548debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6549{
6550 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6551 struct wl_shm_buffer *shm;
6552 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006553 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006554
6555 if (!buffer) {
6556 fprintf(fp, "\t\t[buffer not available]\n");
6557 return;
6558 }
6559
6560 shm = wl_shm_buffer_get(buffer->resource);
6561 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006562 uint32_t _format = wl_shm_buffer_get_format(shm);
6563 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006564 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006565 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6566 (unsigned long) _format,
6567 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006568 return;
6569 }
6570
6571 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6572 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006573 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006574 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006575 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6576 (unsigned long) dmabuf->attributes.format,
6577 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006578 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6579 (unsigned long long) dmabuf->attributes.modifier[0]);
6580 return;
6581 }
6582
Marius Vlad253ba9a2019-03-17 18:22:21 +02006583 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006584}
6585
6586static void
6587debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6588{
6589 struct weston_compositor *ec = view->surface->compositor;
6590 struct weston_output *output;
6591 char desc[512];
6592 pixman_box32_t *box;
6593 uint32_t surface_id = 0;
6594 pid_t pid = 0;
6595
6596 if (view->surface->resource) {
6597 struct wl_resource *resource = view->surface->resource;
6598 wl_client_get_credentials(wl_resource_get_client(resource),
6599 &pid, NULL, NULL);
6600 surface_id = wl_resource_get_id(view->surface->resource);
6601 }
6602
6603 if (!view->surface->get_label ||
6604 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6605 strcpy(desc, "[no description available]");
6606 }
6607 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6608 view_idx, view->surface->role_name, pid, surface_id,
6609 desc, view);
6610
6611 box = pixman_region32_extents(&view->transform.boundingbox);
6612 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6613 box->x1, box->y1, box->x2, box->y2);
6614 box = pixman_region32_extents(&view->transform.opaque);
6615
6616 if (pixman_region32_equal(&view->transform.opaque,
6617 &view->transform.boundingbox)) {
6618 fprintf(fp, "\t\t[fully opaque]\n");
6619 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6620 fprintf(fp, "\t\t[not opaque]\n");
6621 } else {
6622 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6623 box->x1, box->y1, box->x2, box->y2);
6624 }
6625
6626 if (view->alpha < 1.0)
6627 fprintf(fp, "\t\talpha: %f\n", view->alpha);
6628
6629 if (view->output_mask != 0) {
6630 bool first_output = true;
6631 fprintf(fp, "\t\toutputs: ");
6632 wl_list_for_each(output, &ec->output_list, link) {
6633 if (!(view->output_mask & (1 << output->id)))
6634 continue;
6635 fprintf(fp, "%s%d (%s)%s",
6636 (first_output) ? "" : ", ",
6637 output->id, output->name,
6638 (view->output == output) ? " (primary)" : "");
6639 first_output = false;
6640 }
6641 } else {
6642 fprintf(fp, "\t\t[no outputs]");
6643 }
6644
6645 fprintf(fp, "\n");
6646
6647 debug_scene_view_print_buffer(fp, view);
6648}
6649
Marius Vlad433f4e72019-02-17 22:14:23 +02006650static void
6651debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02006652 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02006653{
6654 struct weston_subsurface *sub;
6655 struct weston_view *ev;
6656
6657 /*
6658 * print the view first, then we recursively go on printing
6659 * sub-surfaces. We bail out once no more sub-surfaces are available.
6660 */
Marius Vlada6acfa82019-03-17 18:10:09 +02006661 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02006662
6663 /* no more sub-surfaces */
6664 if (wl_list_empty(&view->surface->subsurface_list))
6665 return;
6666
6667 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
6668 wl_list_for_each(ev, &sub->surface->views, surface_link) {
6669 /* do not print again the parent view */
6670 if (view == ev)
6671 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02006672
6673 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02006674 debug_scene_view_print_tree(ev, fp, view_idx);
6675 }
6676 }
6677}
6678
Daniel Stonece62cb32018-07-20 09:46:24 +01006679/**
6680 * Output information on how libweston is currently composing the scene
6681 * graph.
6682 */
6683WL_EXPORT char *
6684weston_compositor_print_scene_graph(struct weston_compositor *ec)
6685{
6686 struct weston_output *output;
6687 struct weston_layer *layer;
6688 struct timespec now;
6689 int layer_idx = 0;
6690 FILE *fp;
6691 char *ret;
6692 size_t len;
6693 int err;
6694
6695 fp = open_memstream(&ret, &len);
6696 assert(fp);
6697
6698 weston_compositor_read_presentation_clock(ec, &now);
6699 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
6700 now.tv_sec, now.tv_nsec);
6701
6702 wl_list_for_each(output, &ec->output_list, link) {
6703 struct weston_head *head;
6704 int head_idx = 0;
6705
6706 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
6707 assert(output->enabled);
6708
6709 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
6710 output->x, output->y,
6711 output->x + output->width,
6712 output->y + output->height);
6713 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
6714 output->current_mode->width,
6715 output->current_mode->height,
6716 output->current_mode->refresh / 1000.0);
6717 fprintf(fp, "\tscale: %d\n", output->scale);
6718
6719 fprintf(fp, "\trepaint status: %s\n",
6720 output_repaint_status_text(output));
6721 if (output->repaint_status == REPAINT_SCHEDULED)
6722 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
6723 output->next_repaint.tv_sec,
6724 output->next_repaint.tv_nsec);
6725
6726 wl_list_for_each(head, &output->head_list, output_link) {
6727 fprintf(fp, "\tHead %d (%s): %sconnected\n",
6728 head_idx++, head->name,
6729 (head->connected) ? "" : "not ");
6730 }
6731 }
6732
6733 fprintf(fp, "\n");
6734
6735 wl_list_for_each(layer, &ec->layer_list, link) {
6736 struct weston_view *view;
6737 int view_idx = 0;
6738
6739 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
6740 (unsigned long) layer->position);
6741
6742 if (!weston_layer_mask_is_infinite(layer)) {
6743 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
6744 layer->mask.x1, layer->mask.y1,
6745 layer->mask.x2, layer->mask.y2);
6746 }
6747
Marius Vlada6acfa82019-03-17 18:10:09 +02006748 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
6749 debug_scene_view_print_tree(view, fp, &view_idx);
6750 view_idx++;
6751 }
Daniel Stonece62cb32018-07-20 09:46:24 +01006752
6753 if (wl_list_empty(&layer->view_list.link))
6754 fprintf(fp, "\t[no views]\n");
6755
6756 fprintf(fp, "\n");
6757 }
6758
6759 err = fclose(fp);
6760 assert(err == 0);
6761
6762 return ret;
6763}
6764
6765/**
6766 * Called when the 'scene-graph' debug scope is bound by a client. This
6767 * one-shot weston-debug scope prints the current scene graph when bound,
6768 * and then terminates the stream.
6769 */
6770static void
6771debug_scene_graph_cb(struct weston_debug_stream *stream, void *data)
6772{
6773 struct weston_compositor *ec = data;
6774 char *str = weston_compositor_print_scene_graph(ec);
6775
6776 weston_debug_stream_printf(stream, "%s", str);
6777 free(str);
6778 weston_debug_stream_complete(stream);
6779}
6780
Giulio Camuffo459137b2014-10-11 23:56:24 +03006781/** Create the compositor.
6782 *
6783 * This functions creates and initializes a compositor instance.
6784 *
6785 * \param display The Wayland display to be used.
6786 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03006787 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03006788 * using the \ref weston_compositor_get_user_data function.
6789 * \return The compositor instance on success or NULL on failure.
6790 */
6791WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03006792weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03006793 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03006794 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006795{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006796 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006797 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006798
Giulio Camuffo459137b2014-10-11 23:56:24 +03006799 ec = zalloc(sizeof *ec);
6800 if (!ec)
6801 return NULL;
6802
6803 ec->wl_display = display;
6804 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006805 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006806 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006807 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006808 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006809 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006810 wl_signal_init(&ec->idle_signal);
6811 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006812 wl_signal_init(&ec->show_input_panel_signal);
6813 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006814 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006815 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006816 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006817 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006818 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006819 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006820 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02006821 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006822 wl_signal_init(&ec->session_signal);
6823 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006824
Casey Dahlin58ba1372012-04-19 22:50:08 -04006825 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006826 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006827
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006828 ec->activate_serial = 1;
6829
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05006830 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
6831
Derek Foreman152254b2015-11-26 14:17:48 -06006832 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006833 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006834 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006835
Giulio Camuffo954f1832014-10-11 18:27:30 +03006836 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006837 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006838 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006839
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006840 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006841 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006842 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006843
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006844 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006845 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006846 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006847
Marius Vlad3d7d9782019-04-17 12:35:38 +03006848 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02006849 goto fail;
6850
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006851 if (weston_input_init(ec) != 0)
6852 goto fail;
6853
Jason Ekstranda7af7042013-10-12 22:38:11 -05006854 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006855 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006856 wl_list_init(&ec->layer_list);
6857 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006858 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006859 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006860 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006861 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006862 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006863 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006864 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006865 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006866 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006867
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006868 wl_list_init(&ec->plugin_api_list);
6869
Xiong Zhang97116532013-10-23 13:58:31 +08006870 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006871 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006872
Giulio Camuffo459137b2014-10-11 23:56:24 +03006873 wl_data_device_manager_init(ec->wl_display);
6874
6875 wl_display_init_shm(ec->wl_display);
6876
6877 loop = wl_display_get_event_loop(ec->wl_display);
6878 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006879 ec->repaint_timer =
6880 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6881 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006882
Quentin Glidic82681572016-12-17 13:40:51 +01006883 weston_layer_init(&ec->fade_layer, ec);
6884 weston_layer_init(&ec->cursor_layer, ec);
6885
6886 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6887 weston_layer_set_position(&ec->cursor_layer,
6888 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006889
6890 weston_compositor_add_debug_binding(ec, KEY_T,
6891 timeline_key_binding_handler, ec);
6892
Daniel Stonece62cb32018-07-20 09:46:24 +01006893 ec->debug_scene =
Marius Vlad7e4db952019-04-17 13:47:06 +03006894 weston_compositor_add_log_scope(ec->weston_log_ctx, "scene-graph",
6895 "Scene graph details\n",
6896 debug_scene_graph_cb,
6897 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01006898
Giulio Camuffo459137b2014-10-11 23:56:24 +03006899 return ec;
6900
6901fail:
6902 free(ec);
6903 return NULL;
6904}
6905
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006906WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006907weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006908{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006909 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006910
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006911 wl_event_source_remove(ec->idle_source);
6912
Matt Roper361d2ad2011-08-29 13:52:23 -07006913 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006914 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006915 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006916
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006917 /* Destroy all pending outputs associated with this compositor */
6918 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6919 output->destroy(output);
6920
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006921 if (ec->renderer)
6922 ec->renderer->destroy(ec);
6923
Daniel Stone325fc2d2012-05-30 16:31:58 +01006924 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006925 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006926 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006927 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006928 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006929 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006930
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006931 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006932}
6933
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006934WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006935weston_compositor_exit_with_code(struct weston_compositor *compositor,
6936 int exit_code)
6937{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006938 if (compositor->exit_code == EXIT_SUCCESS)
6939 compositor->exit_code = exit_code;
6940
Giulio Camuffo459137b2014-10-11 23:56:24 +03006941 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006942}
6943
6944WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006945weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6946 const struct weston_pointer_grab_interface *interface)
6947{
6948 struct weston_seat *seat;
6949
6950 ec->default_pointer_grab = interface;
6951 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05006952 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6953
6954 if (pointer)
6955 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006956 }
6957}
6958
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04006959WL_EXPORT int
6960weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
6961 clockid_t clk_id)
6962{
6963 struct timespec ts;
6964
6965 if (clock_gettime(clk_id, &ts) < 0)
6966 return -1;
6967
6968 compositor->presentation_clock = clk_id;
6969
6970 return 0;
6971}
6972
6973/*
6974 * For choosing the software clock, when the display hardware or API
6975 * does not expose a compatible presentation timestamp.
6976 */
6977WL_EXPORT int
6978weston_compositor_set_presentation_clock_software(
6979 struct weston_compositor *compositor)
6980{
6981 /* In order of preference */
6982 static const clockid_t clocks[] = {
6983 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
6984 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
6985 CLOCK_MONOTONIC, /* no jumps, may crawl */
6986 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
6987 CLOCK_REALTIME /* may jump and crawl */
6988 };
6989 unsigned i;
6990
6991 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
6992 if (weston_compositor_set_presentation_clock(compositor,
6993 clocks[i]) == 0)
6994 return 0;
6995
6996 weston_log("Error: no suitable presentation clock available.\n");
6997
6998 return -1;
6999}
7000
Pekka Paalanen662f3842015-03-18 12:17:26 +02007001/** Read the current time from the Presentation clock
7002 *
7003 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007004 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007005 *
7006 * \note Reading the current time in user space is always imprecise to some
7007 * degree.
7008 *
7009 * This function is never meant to fail. If reading the clock does fail,
7010 * an error message is logged and a zero time is returned. Callers are not
7011 * supposed to detect or react to failures.
7012 */
7013WL_EXPORT void
7014weston_compositor_read_presentation_clock(
7015 const struct weston_compositor *compositor,
7016 struct timespec *ts)
7017{
7018 static bool warned;
7019 int ret;
7020
7021 ret = clock_gettime(compositor->presentation_clock, ts);
7022 if (ret < 0) {
7023 ts->tv_sec = 0;
7024 ts->tv_nsec = 0;
7025
7026 if (!warned)
7027 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007028 "the presentation clock %#x: '%s' (%d)\n",
7029 compositor->presentation_clock,
7030 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007031 warned = true;
7032 }
7033}
7034
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007035/** Import dmabuf buffer into current renderer
7036 *
7037 * \param compositor
7038 * \param buffer the dmabuf buffer to import
7039 * \return true on usable buffers, false otherwise
7040 *
7041 * This function tests that the linux_dmabuf_buffer is usable
7042 * for the current renderer. Returns false on unusable buffers. Usually
7043 * usability is tested by importing the dmabufs for composition.
7044 *
7045 * This hook is also used for detecting if the renderer supports
7046 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7047 * supported.
7048 * */
7049WL_EXPORT bool
7050weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7051 struct linux_dmabuf_buffer *buffer)
7052{
7053 struct weston_renderer *renderer;
7054
7055 renderer = compositor->renderer;
7056
7057 if (renderer->import_dmabuf == NULL)
7058 return false;
7059
7060 return renderer->import_dmabuf(compositor, buffer);
7061}
7062
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007063WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007064weston_version(int *major, int *minor, int *micro)
7065{
7066 *major = WESTON_VERSION_MAJOR;
7067 *minor = WESTON_VERSION_MINOR;
7068 *micro = WESTON_VERSION_MICRO;
7069}
7070
Daniel Stonee03c1112016-11-24 20:45:45 +00007071/**
7072 * Attempts to find a module path from the module map specified in the
7073 * environment. If found, writes the full path into the path variable.
7074 *
7075 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7076 * each entry is of the form "name=path" and entries are separated by
7077 * semicolons. Whitespace is significant.
7078 *
7079 * \param name The name to search for.
7080 * \param path Where the path is written to if found.
7081 * \param path_len Allocated bytes at \c path .
7082 * \returns The length of the string written to path on success, or 0 if the
7083 * module was not specified in the environment map or path_len was too small.
7084 */
7085WL_EXPORT size_t
7086weston_module_path_from_env(const char *name, char *path, size_t path_len)
7087{
7088 const char *mapping = getenv("WESTON_MODULE_MAP");
7089 const char *end;
7090 const int name_len = strlen(name);
7091
7092 if (!mapping)
7093 return 0;
7094
7095 end = mapping + strlen(mapping);
7096 while (mapping < end && *mapping) {
7097 const char *filename, *next;
7098
7099 /* early out: impossibly short string */
7100 if (end - mapping < name_len + 1)
7101 return 0;
7102
7103 filename = &mapping[name_len + 1];
7104 next = strchrnul(mapping, ';');
7105
7106 if (strncmp(mapping, name, name_len) == 0 &&
7107 mapping[name_len] == '=') {
7108 size_t file_len = next - filename; /* no trailing NUL */
7109 if (file_len >= path_len)
7110 return 0;
7111 strncpy(path, filename, file_len);
7112 path[file_len] = '\0';
7113 return file_len;
7114 }
7115
7116 mapping = next + 1;
7117 }
7118
7119 return 0;
7120}
7121
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007122WL_EXPORT void *
7123weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007124{
7125 char path[PATH_MAX];
7126 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007127 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007128
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007129 if (name == NULL)
7130 return NULL;
7131
Derek Foreman3f86e502015-06-08 11:46:54 -05007132 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007133 len = weston_module_path_from_env(name, path, sizeof path);
7134 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007135 len = snprintf(path, sizeof path, "%s/%s",
7136 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007137 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007138 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007139 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007140
Daniel Stonebeb97e52016-11-28 12:13:54 +00007141 /* snprintf returns the length of the string it would've written,
7142 * _excluding_ the NUL byte. So even being equal to the size of
7143 * our buffer is an error here. */
7144 if (len >= sizeof path)
7145 return NULL;
7146
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007147 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7148 if (module) {
7149 weston_log("Module '%s' already loaded\n", path);
7150 dlclose(module);
7151 return NULL;
7152 }
7153
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007154 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007155 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007156 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007157 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007158 return NULL;
7159 }
7160
7161 init = dlsym(module, entrypoint);
7162 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007163 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007164 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007165 return NULL;
7166 }
7167
7168 return init;
7169}
7170
Giulio Camuffo459137b2014-10-11 23:56:24 +03007171
7172/** Destroys the compositor.
7173 *
7174 * This function cleans up the compositor state and destroys it.
7175 *
7176 * \param compositor The compositor to be destroyed.
7177 */
7178WL_EXPORT void
7179weston_compositor_destroy(struct weston_compositor *compositor)
7180{
7181 /* prevent further rendering while shutting down */
7182 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7183
7184 wl_signal_emit(&compositor->destroy_signal, compositor);
7185
7186 weston_compositor_xkb_destroy(compositor);
7187
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007188 if (compositor->backend)
7189 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007190
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007191 /* The backend is responsible for destroying the heads. */
7192 assert(wl_list_empty(&compositor->head_list));
7193
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007194 weston_plugin_api_destroy_list(compositor);
7195
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007196 if (compositor->heads_changed_source)
7197 wl_event_source_remove(compositor->heads_changed_source);
7198
Marius Vlad7e4db952019-04-17 13:47:06 +03007199 weston_compositor_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007200 compositor->debug_scene = NULL;
Marius Vlad3d7d9782019-04-17 12:35:38 +03007201 weston_log_ctx_compositor_destroy(compositor);
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007202
Giulio Camuffo459137b2014-10-11 23:56:24 +03007203 free(compositor);
7204}
7205
7206/** Instruct the compositor to exit.
7207 *
7208 * This functions does not directly destroy the compositor object, it merely
7209 * command it to start the tear down process. It is not guaranteed that the
7210 * tear down will happen immediately.
7211 *
7212 * \param compositor The compositor to tear down.
7213 */
7214WL_EXPORT void
7215weston_compositor_exit(struct weston_compositor *compositor)
7216{
7217 compositor->exit(compositor);
7218}
7219
7220/** Return the user data stored in the compositor.
7221 *
7222 * This function returns the user data pointer set with user_data parameter
7223 * to the \ref weston_compositor_create function.
7224 */
7225WL_EXPORT void *
7226weston_compositor_get_user_data(struct weston_compositor *compositor)
7227{
7228 return compositor->user_data;
7229}
Pekka Paalanendd186732016-06-03 14:49:54 +03007230
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007231static const char * const backend_map[] = {
7232 [WESTON_BACKEND_DRM] = "drm-backend.so",
7233 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7234 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7235 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7236 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7237 [WESTON_BACKEND_X11] = "x11-backend.so",
7238};
7239
Pekka Paalanendd186732016-06-03 14:49:54 +03007240/** Load a backend into a weston_compositor
7241 *
7242 * A backend must be loaded to make a weston_compositor work. A backend
7243 * provides input and output capabilities, and determines the renderer to use.
7244 *
7245 * \param compositor A compositor that has not had a backend loaded yet.
7246 * \param backend Name of the backend file.
7247 * \param config_base A pointer to a backend-specific configuration
7248 * structure's 'base' member.
7249 *
7250 * \return 0 on success, or -1 on error.
7251 */
7252WL_EXPORT int
7253weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007254 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007255 struct weston_backend_config *config_base)
7256{
7257 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007258 struct weston_backend_config *config_base);
7259
Pekka Paalanend7e35112017-08-29 17:04:12 +03007260 if (compositor->backend) {
7261 weston_log("Error: attempt to load a backend when one is already loaded\n");
7262 return -1;
7263 }
7264
Quentin Glidic887c0182016-07-10 11:00:53 +02007265 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007266 return -1;
7267
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007268 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007269 if (!backend_init)
7270 return -1;
7271
Pekka Paalanend7e35112017-08-29 17:04:12 +03007272 if (backend_init(compositor, config_base) < 0) {
7273 compositor->backend = NULL;
7274 return -1;
7275 }
7276
7277 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007278}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007279
7280WL_EXPORT int
7281weston_compositor_load_xwayland(struct weston_compositor *compositor)
7282{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007283 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007284
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007285 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007286 if (!module_init)
7287 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007288 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007289 return -1;
7290 return 0;
7291}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007292
7293/** Resolve an internal compositor error by disconnecting the client.
7294 *
7295 * This function is used in cases when the wl_buffer turns out
7296 * unusable and there is no fallback path.
7297 *
7298 * It is possible the fault is caused by a compositor bug, the underlying
7299 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7300 * In any case, the options are to either composite garbage or nothing,
7301 * or disconnect the client. This is a helper function for the latter.
7302 *
7303 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7304 *
7305 * \param buffer The weston buffer that is unusable.
7306 * \param msg A custom error message attached to the protocol error.
7307 */
7308WL_EXPORT void
7309weston_buffer_send_server_error(struct weston_buffer *buffer,
7310 const char *msg)
7311{
7312 struct wl_client *client;
7313 struct wl_resource *display_resource;
7314 uint32_t id;
7315
7316 assert(buffer->resource);
7317 id = wl_resource_get_id(buffer->resource);
7318 client = wl_resource_get_client(buffer->resource);
7319 display_resource = wl_client_get_object(client, 1);
7320
7321 assert(display_resource);
7322 wl_resource_post_error(display_resource,
7323 WL_DISPLAY_ERROR_INVALID_OBJECT,
7324 "server error with "
7325 "wl_buffer@%u: %s", id, msg);
7326}