blob: 2e84e4595cc6034eea1328ad2cfd8e996df5c33b [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
Marius Vlad78984ee2019-06-11 00:05:08 +030075/**
76 * \defgroup head Head
77 * \defgroup output Output
78 * \defgroup compositor Compositor
79 */
80
Pekka Paalanen0513a952014-05-21 16:17:27 +030081#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
82
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020083static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030084weston_output_update_matrix(struct weston_output *output);
85
86static void
Alexander Larsson0b135062013-05-28 16:23:36 +020087weston_output_transform_scale_init(struct weston_output *output,
88 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020089
Rob Bradford27b17932013-06-26 18:08:46 +010090static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050091weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010092
Pekka Paalanendcac3512017-12-08 14:13:34 +020093static char *
94weston_output_create_heads_string(struct weston_output *output);
95
Pekka Paalanen6528c032017-03-28 15:27:10 +030096/** Send wl_output events for mode and scale changes
97 *
98 * \param head Send on all resources bound to this head.
99 * \param mode_changed If true, send the current mode.
100 * \param scale_changed If true, send the current scale.
101 */
102static void
103weston_mode_switch_send_events(struct weston_head *head,
104 bool mode_changed, bool scale_changed)
105{
106 struct weston_output *output = head->output;
107 struct wl_resource *resource;
108 int version;
109
110 wl_resource_for_each(resource, &head->resource_list) {
111 if (mode_changed) {
112 wl_output_send_mode(resource,
113 output->current_mode->flags,
114 output->current_mode->width,
115 output->current_mode->height,
116 output->current_mode->refresh);
117 }
118
119 version = wl_resource_get_version(resource);
120 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
121 wl_output_send_scale(resource, output->current_scale);
122
123 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
124 wl_output_send_done(resource);
125 }
126}
127
128static void
129weston_mode_switch_finish(struct weston_output *output,
130 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800131{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200132 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300133 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200134 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200135
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200136 pixman_region32_init(&old_output_region);
137 pixman_region32_copy(&old_output_region, &output->region);
138
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200139 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200140 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200141
142 pixman_region32_init(&output->previous_damage);
143 pixman_region32_init_rect(&output->region, output->x, output->y,
144 output->width, output->height);
145
146 weston_output_update_matrix(output);
147
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200148 /* If a pointer falls outside the outputs new geometry, move it to its
149 * lower-right corner */
150 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500151 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200152 int32_t x, y;
153
154 if (!pointer)
155 continue;
156
157 x = wl_fixed_to_int(pointer->x);
158 y = wl_fixed_to_int(pointer->y);
159
160 if (!pixman_region32_contains_point(&old_output_region,
161 x, y, NULL) ||
162 pixman_region32_contains_point(&output->region,
163 x, y, NULL))
164 continue;
165
166 if (x >= output->x + output->width)
167 x = output->x + output->width - 1;
168 if (y >= output->y + output->height)
169 y = output->y + output->height - 1;
170
171 pointer->x = wl_fixed_from_int(x);
172 pointer->y = wl_fixed_from_int(y);
173 }
174
175 pixman_region32_fini(&old_output_region);
176
Derek Foremandd4cd332014-11-10 10:29:59 -0600177 if (!mode_changed && !scale_changed)
178 return;
179
Hardening57388e42013-09-18 23:56:36 +0200180 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300181 wl_list_for_each(head, &output->head_list, output_link)
182 weston_mode_switch_send_events(head,
183 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600184}
185
David Fort0de859e2016-05-27 23:22:57 +0200186static void
187weston_compositor_reflow_outputs(struct weston_compositor *compositor,
188 struct weston_output *resized_output, int delta_width);
189
Marius Vlad55d87362019-06-11 01:15:35 +0300190/**
191 * \ingroup output
192 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600193WL_EXPORT int
194weston_output_mode_set_native(struct weston_output *output,
195 struct weston_mode *mode,
196 int32_t scale)
197{
198 int ret;
199 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200200 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600201
202 if (!output->switch_mode)
203 return -1;
204
205 if (!output->original_mode) {
206 mode_changed = 1;
207 ret = output->switch_mode(output, mode);
208 if (ret < 0)
209 return ret;
210 if (output->current_scale != scale) {
211 scale_changed = 1;
212 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200213 }
214 }
215
David Fort0de859e2016-05-27 23:22:57 +0200216 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600217 output->native_mode = mode;
218 output->native_scale = scale;
219
220 weston_mode_switch_finish(output, mode_changed, scale_changed);
221
David Fort0de859e2016-05-27 23:22:57 +0200222 if (mode_changed || scale_changed) {
223 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
224
225 wl_signal_emit(&output->compositor->output_resized_signal, output);
226 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600227 return 0;
228}
229
Marius Vlad55d87362019-06-11 01:15:35 +0300230/**
231 * \ingroup output
232 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600233WL_EXPORT int
234weston_output_mode_switch_to_native(struct weston_output *output)
235{
236 int ret;
237 int mode_changed = 0, scale_changed = 0;
238
239 if (!output->switch_mode)
240 return -1;
241
242 if (!output->original_mode) {
243 weston_log("already in the native mode\n");
244 return -1;
245 }
246 /* the non fullscreen clients haven't seen a mode set since we
247 * switched into a temporary, so we need to notify them if the
248 * mode at that time is different from the native mode now.
249 */
250 mode_changed = (output->original_mode != output->native_mode);
251 scale_changed = (output->original_scale != output->native_scale);
252
253 ret = output->switch_mode(output, output->native_mode);
254 if (ret < 0)
255 return ret;
256
257 output->current_scale = output->native_scale;
258
259 output->original_mode = NULL;
260 output->original_scale = 0;
261
262 weston_mode_switch_finish(output, mode_changed, scale_changed);
263
264 return 0;
265}
266
Marius Vlad55d87362019-06-11 01:15:35 +0300267/**
268 * \ingroup output
269 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600270WL_EXPORT int
271weston_output_mode_switch_to_temporary(struct weston_output *output,
272 struct weston_mode *mode,
273 int32_t scale)
274{
275 int ret;
276
277 if (!output->switch_mode)
278 return -1;
279
280 /* original_mode is the last mode non full screen clients have seen,
281 * so we shouldn't change it if we already have one set.
282 */
283 if (!output->original_mode) {
284 output->original_mode = output->native_mode;
285 output->original_scale = output->native_scale;
286 }
287 ret = output->switch_mode(output, mode);
288 if (ret < 0)
289 return ret;
290
291 output->current_scale = scale;
292
293 weston_mode_switch_finish(output, 0, 0);
294
295 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800296}
297
Benjamin Franzke06286262011-05-06 19:12:33 +0200298static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300299region_init_infinite(pixman_region32_t *region)
300{
301 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
302 UINT32_MAX, UINT32_MAX);
303}
304
Pekka Paalanene67858b2013-04-25 13:57:42 +0300305static struct weston_subsurface *
306weston_surface_to_subsurface(struct weston_surface *surface);
307
Jason Ekstranda7af7042013-10-12 22:38:11 -0500308WL_EXPORT struct weston_view *
309weston_view_create(struct weston_surface *surface)
310{
311 struct weston_view *view;
312
Bryce Harringtonde16d892014-11-20 22:21:57 -0800313 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500314 if (view == NULL)
315 return NULL;
316
317 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000318 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500319
Jason Ekstranda7af7042013-10-12 22:38:11 -0500320 /* Assign to surface */
321 wl_list_insert(&surface->views, &view->surface_link);
322
323 wl_signal_init(&view->destroy_signal);
324 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300325 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500326
Jason Ekstranda7af7042013-10-12 22:38:11 -0500327 pixman_region32_init(&view->clip);
328
329 view->alpha = 1.0;
330 pixman_region32_init(&view->transform.opaque);
331
332 wl_list_init(&view->geometry.transformation_list);
333 wl_list_insert(&view->geometry.transformation_list,
334 &view->transform.position.link);
335 weston_matrix_init(&view->transform.position.matrix);
336 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200337 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500338 pixman_region32_init(&view->transform.boundingbox);
339 view->transform.dirty = 1;
340
Jason Ekstranda7af7042013-10-12 22:38:11 -0500341 return view;
342}
343
Jason Ekstrand108865d2014-06-26 10:04:49 -0700344struct weston_frame_callback {
345 struct wl_resource *resource;
346 struct wl_list link;
347};
348
Pekka Paalanen133e4392014-09-23 22:08:46 -0400349struct weston_presentation_feedback {
350 struct wl_resource *resource;
351
352 /* XXX: could use just wl_resource_get_link() instead */
353 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200354
355 /* The per-surface feedback flags */
356 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400357};
358
359static void
360weston_presentation_feedback_discard(
361 struct weston_presentation_feedback *feedback)
362{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200363 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400364 wl_resource_destroy(feedback->resource);
365}
366
367static void
368weston_presentation_feedback_discard_list(struct wl_list *list)
369{
370 struct weston_presentation_feedback *feedback, *tmp;
371
372 wl_list_for_each_safe(feedback, tmp, list, link)
373 weston_presentation_feedback_discard(feedback);
374}
375
376static void
377weston_presentation_feedback_present(
378 struct weston_presentation_feedback *feedback,
379 struct weston_output *output,
380 uint32_t refresh_nsec,
381 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200382 uint64_t seq,
383 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400384{
385 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300386 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400387 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200388 uint32_t tv_sec_hi;
389 uint32_t tv_sec_lo;
390 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300391 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400392
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300393 wl_list_for_each(head, &output->head_list, output_link) {
394 wl_resource_for_each(o, &head->resource_list) {
395 if (wl_resource_get_client(o) != client)
396 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400397
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300398 wp_presentation_feedback_send_sync_output(feedback->resource, o);
399 done = true;
400 }
401
402 /* For clone mode, send it for just one wl_output global,
403 * they are all equivalent anyway.
404 */
405 if (done)
406 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400407 }
408
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200409 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200410 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200411 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200412 refresh_nsec,
413 seq >> 32, seq & 0xffffffff,
414 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400415 wl_resource_destroy(feedback->resource);
416}
417
418static void
419weston_presentation_feedback_present_list(struct wl_list *list,
420 struct weston_output *output,
421 uint32_t refresh_nsec,
422 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200423 uint64_t seq,
424 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400425{
426 struct weston_presentation_feedback *feedback, *tmp;
427
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200428 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200429 wl_list_empty(list));
430
Pekka Paalanen133e4392014-09-23 22:08:46 -0400431 wl_list_for_each_safe(feedback, tmp, list, link)
432 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200433 refresh_nsec, ts, seq,
434 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400435}
436
Jason Ekstrand7b982072014-05-20 14:33:03 -0500437static void
438surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
439{
440 struct weston_surface_state *state =
441 container_of(listener, struct weston_surface_state,
442 buffer_destroy_listener);
443
444 state->buffer = NULL;
445}
446
447static void
448weston_surface_state_init(struct weston_surface_state *state)
449{
450 state->newly_attached = 0;
451 state->buffer = NULL;
452 state->buffer_destroy_listener.notify =
453 surface_state_handle_buffer_destroy;
454 state->sx = 0;
455 state->sy = 0;
456
Derek Foreman152254b2015-11-26 14:17:48 -0600457 pixman_region32_init(&state->damage_surface);
458 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500459 pixman_region32_init(&state->opaque);
460 region_init_infinite(&state->input);
461
462 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400463 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500464
465 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
466 state->buffer_viewport.buffer.scale = 1;
467 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
468 state->buffer_viewport.surface.width = -1;
469 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300470
471 state->acquire_fence_fd = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500472}
473
474static void
475weston_surface_state_fini(struct weston_surface_state *state)
476{
477 struct weston_frame_callback *cb, *next;
478
479 wl_list_for_each_safe(cb, next,
480 &state->frame_callback_list, link)
481 wl_resource_destroy(cb->resource);
482
Pekka Paalanen133e4392014-09-23 22:08:46 -0400483 weston_presentation_feedback_discard_list(&state->feedback_list);
484
Jason Ekstrand7b982072014-05-20 14:33:03 -0500485 pixman_region32_fini(&state->input);
486 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600487 pixman_region32_fini(&state->damage_surface);
488 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500489
490 if (state->buffer)
491 wl_list_remove(&state->buffer_destroy_listener.link);
492 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300493
494 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300495 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500496}
497
498static void
499weston_surface_state_set_buffer(struct weston_surface_state *state,
500 struct weston_buffer *buffer)
501{
502 if (state->buffer == buffer)
503 return;
504
505 if (state->buffer)
506 wl_list_remove(&state->buffer_destroy_listener.link);
507 state->buffer = buffer;
508 if (state->buffer)
509 wl_signal_add(&state->buffer->destroy_signal,
510 &state->buffer_destroy_listener);
511}
512
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500513WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500514weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500515{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500516 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400517
Bryce Harringtonde16d892014-11-20 22:21:57 -0800518 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400519 if (surface == NULL)
520 return NULL;
521
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500522 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800523 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500524
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500525 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200526 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400527
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200528 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
529 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200530 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
531 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500532
533 weston_surface_state_init(&surface->pending);
534
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400535 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500536 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300537 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400538
Jason Ekstranda7af7042013-10-12 22:38:11 -0500539 wl_list_init(&surface->views);
540
541 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400542 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500543
Pekka Paalanene67858b2013-04-25 13:57:42 +0300544 wl_list_init(&surface->subsurface_list);
545 wl_list_init(&surface->subsurface_list_pending);
546
Jason Ekstrand1e059042014-10-16 10:55:19 -0500547 weston_matrix_init(&surface->buffer_to_surface_matrix);
548 weston_matrix_init(&surface->surface_to_buffer_matrix);
549
Jonas Ådahld3414f22016-07-22 17:56:31 +0800550 wl_list_init(&surface->pointer_constraints);
551
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300552 surface->acquire_fence_fd = -1;
553
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400554 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500555}
556
Alex Wu8811bf92012-02-28 18:07:54 +0800557WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500558weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200559 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500560{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100561 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200562 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500563}
564
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400565WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500566weston_view_to_global_float(struct weston_view *view,
567 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200568{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500569 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200570 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
571
Jason Ekstranda7af7042013-10-12 22:38:11 -0500572 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200573
574 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200575 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700576 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200577 v.f[3]);
578 *x = 0;
579 *y = 0;
580 return;
581 }
582
583 *x = v.f[0] / v.f[3];
584 *y = v.f[1] / v.f[3];
585 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586 *x = sx + view->geometry.x;
587 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200588 }
589}
590
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500591WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200592weston_transformed_coord(int width, int height,
593 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200594 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200595 float sx, float sy, float *bx, float *by)
596{
597 switch (transform) {
598 case WL_OUTPUT_TRANSFORM_NORMAL:
599 default:
600 *bx = sx;
601 *by = sy;
602 break;
603 case WL_OUTPUT_TRANSFORM_FLIPPED:
604 *bx = width - sx;
605 *by = sy;
606 break;
607 case WL_OUTPUT_TRANSFORM_90:
608 *bx = height - sy;
609 *by = sx;
610 break;
611 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
612 *bx = height - sy;
613 *by = width - sx;
614 break;
615 case WL_OUTPUT_TRANSFORM_180:
616 *bx = width - sx;
617 *by = height - sy;
618 break;
619 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
620 *bx = sx;
621 *by = height - sy;
622 break;
623 case WL_OUTPUT_TRANSFORM_270:
624 *bx = sy;
625 *by = width - sx;
626 break;
627 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
628 *bx = sy;
629 *by = sx;
630 break;
631 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200632
633 *bx *= scale;
634 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200635}
636
637WL_EXPORT pixman_box32_t
638weston_transformed_rect(int width, int height,
639 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200640 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200641 pixman_box32_t rect)
642{
643 float x1, x2, y1, y2;
644
645 pixman_box32_t ret;
646
Alexander Larsson4ea95522013-05-22 14:41:37 +0200647 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200648 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200649 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200650 rect.x2, rect.y2, &x2, &y2);
651
652 if (x1 <= x2) {
653 ret.x1 = x1;
654 ret.x2 = x2;
655 } else {
656 ret.x1 = x2;
657 ret.x2 = x1;
658 }
659
660 if (y1 <= y2) {
661 ret.y1 = y1;
662 ret.y2 = y2;
663 } else {
664 ret.y1 = y2;
665 ret.y2 = y1;
666 }
667
668 return ret;
669}
670
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600671/** Transform a region by a matrix, restricted to axis-aligned transformations
672 *
673 * Warning: This function does not work for projective, affine, or matrices
674 * that encode arbitrary rotations. Only 90-degree step rotations are
675 * supported.
676 */
677WL_EXPORT void
678weston_matrix_transform_region(pixman_region32_t *dest,
679 struct weston_matrix *matrix,
680 pixman_region32_t *src)
681{
682 pixman_box32_t *src_rects, *dest_rects;
683 int nrects, i;
684
685 src_rects = pixman_region32_rectangles(src, &nrects);
686 dest_rects = malloc(nrects * sizeof(*dest_rects));
687 if (!dest_rects)
688 return;
689
690 for (i = 0; i < nrects; i++) {
691 struct weston_vector vec1 = {{
692 src_rects[i].x1, src_rects[i].y1, 0, 1
693 }};
694 weston_matrix_transform(matrix, &vec1);
695 vec1.f[0] /= vec1.f[3];
696 vec1.f[1] /= vec1.f[3];
697
698 struct weston_vector vec2 = {{
699 src_rects[i].x2, src_rects[i].y2, 0, 1
700 }};
701 weston_matrix_transform(matrix, &vec2);
702 vec2.f[0] /= vec2.f[3];
703 vec2.f[1] /= vec2.f[3];
704
705 if (vec1.f[0] < vec2.f[0]) {
706 dest_rects[i].x1 = floor(vec1.f[0]);
707 dest_rects[i].x2 = ceil(vec2.f[0]);
708 } else {
709 dest_rects[i].x1 = floor(vec2.f[0]);
710 dest_rects[i].x2 = ceil(vec1.f[0]);
711 }
712
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600713 if (vec1.f[1] < vec2.f[1]) {
714 dest_rects[i].y1 = floor(vec1.f[1]);
715 dest_rects[i].y2 = ceil(vec2.f[1]);
716 } else {
717 dest_rects[i].y1 = floor(vec2.f[1]);
718 dest_rects[i].y2 = ceil(vec1.f[1]);
719 }
720 }
721
722 pixman_region32_clear(dest);
723 pixman_region32_init_rects(dest, dest_rects, nrects);
724 free(dest_rects);
725}
726
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200727WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500728weston_transformed_region(int width, int height,
729 enum wl_output_transform transform,
730 int32_t scale,
731 pixman_region32_t *src, pixman_region32_t *dest)
732{
733 pixman_box32_t *src_rects, *dest_rects;
734 int nrects, i;
735
736 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
737 if (src != dest)
738 pixman_region32_copy(dest, src);
739 return;
740 }
741
742 src_rects = pixman_region32_rectangles(src, &nrects);
743 dest_rects = malloc(nrects * sizeof(*dest_rects));
744 if (!dest_rects)
745 return;
746
747 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
748 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
749 } else {
750 for (i = 0; i < nrects; i++) {
751 switch (transform) {
752 default:
753 case WL_OUTPUT_TRANSFORM_NORMAL:
754 dest_rects[i].x1 = src_rects[i].x1;
755 dest_rects[i].y1 = src_rects[i].y1;
756 dest_rects[i].x2 = src_rects[i].x2;
757 dest_rects[i].y2 = src_rects[i].y2;
758 break;
759 case WL_OUTPUT_TRANSFORM_90:
760 dest_rects[i].x1 = height - src_rects[i].y2;
761 dest_rects[i].y1 = src_rects[i].x1;
762 dest_rects[i].x2 = height - src_rects[i].y1;
763 dest_rects[i].y2 = src_rects[i].x2;
764 break;
765 case WL_OUTPUT_TRANSFORM_180:
766 dest_rects[i].x1 = width - src_rects[i].x2;
767 dest_rects[i].y1 = height - src_rects[i].y2;
768 dest_rects[i].x2 = width - src_rects[i].x1;
769 dest_rects[i].y2 = height - src_rects[i].y1;
770 break;
771 case WL_OUTPUT_TRANSFORM_270:
772 dest_rects[i].x1 = src_rects[i].y1;
773 dest_rects[i].y1 = width - src_rects[i].x2;
774 dest_rects[i].x2 = src_rects[i].y2;
775 dest_rects[i].y2 = width - src_rects[i].x1;
776 break;
777 case WL_OUTPUT_TRANSFORM_FLIPPED:
778 dest_rects[i].x1 = width - src_rects[i].x2;
779 dest_rects[i].y1 = src_rects[i].y1;
780 dest_rects[i].x2 = width - src_rects[i].x1;
781 dest_rects[i].y2 = src_rects[i].y2;
782 break;
783 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
784 dest_rects[i].x1 = height - src_rects[i].y2;
785 dest_rects[i].y1 = width - src_rects[i].x2;
786 dest_rects[i].x2 = height - src_rects[i].y1;
787 dest_rects[i].y2 = width - src_rects[i].x1;
788 break;
789 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
790 dest_rects[i].x1 = src_rects[i].x1;
791 dest_rects[i].y1 = height - src_rects[i].y2;
792 dest_rects[i].x2 = src_rects[i].x2;
793 dest_rects[i].y2 = height - src_rects[i].y1;
794 break;
795 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
796 dest_rects[i].x1 = src_rects[i].y1;
797 dest_rects[i].y1 = src_rects[i].x1;
798 dest_rects[i].x2 = src_rects[i].y2;
799 dest_rects[i].y2 = src_rects[i].x2;
800 break;
801 }
802 }
803 }
804
805 if (scale != 1) {
806 for (i = 0; i < nrects; i++) {
807 dest_rects[i].x1 *= scale;
808 dest_rects[i].x2 *= scale;
809 dest_rects[i].y1 *= scale;
810 dest_rects[i].y2 *= scale;
811 }
812 }
813
814 pixman_region32_clear(dest);
815 pixman_region32_init_rects(dest, dest_rects, nrects);
816 free(dest_rects);
817}
818
Jonny Lamb74130762013-11-26 18:19:46 +0100819static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300820viewport_surface_to_buffer(struct weston_surface *surface,
821 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100822{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200823 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200824 double src_width, src_height;
825 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200826
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200827 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
828 if (vp->surface.width == -1) {
829 *bx = sx;
830 *by = sy;
831 return;
832 }
Jonny Lamb74130762013-11-26 18:19:46 +0100833
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200834 src_x = 0.0;
835 src_y = 0.0;
836 src_width = surface->width_from_buffer;
837 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100838 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200839 src_x = wl_fixed_to_double(vp->buffer.src_x);
840 src_y = wl_fixed_to_double(vp->buffer.src_y);
841 src_width = wl_fixed_to_double(vp->buffer.src_width);
842 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100843 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200844
845 *bx = sx * src_width / surface->width + src_x;
846 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100847}
848
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500849WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200850weston_surface_to_buffer_float(struct weston_surface *surface,
851 float sx, float sy, float *bx, float *by)
852{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200853 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
854
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300855 /* first transform coordinates if the viewport is set */
856 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100857
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500858 weston_transformed_coord(surface->width_from_buffer,
859 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200860 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100861 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200862}
863
Derek Foreman99739672015-12-03 16:38:11 -0600864/** Transform a rectangle from surface coordinates to buffer coordinates
865 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300866 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600867 * from.
868 * \param rect The rectangle to transform.
869 * \return The transformed rectangle.
870 *
871 * Viewport and buffer transformations can only do translation, scaling,
872 * and rotations in 90-degree steps. Therefore the only loss in the
873 * conversion is coordinate rounding.
874 *
875 * However, some coordinate rounding takes place as an intermediate
876 * step before the buffer scale factor is applied, so the rectangle
877 * boundary may not be exactly as expected.
878 *
879 * This is OK for damage tracking since a little extra coverage is
880 * not a problem.
881 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200882WL_EXPORT pixman_box32_t
883weston_surface_to_buffer_rect(struct weston_surface *surface,
884 pixman_box32_t rect)
885{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200886 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100887 float xf, yf;
888
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300889 /* first transform box coordinates if the viewport is set */
890 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100891 rect.x1 = floorf(xf);
892 rect.y1 = floorf(yf);
893
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300894 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600895 rect.x2 = ceilf(xf);
896 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100897
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500898 return weston_transformed_rect(surface->width_from_buffer,
899 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200900 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200901 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200902}
903
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200904/** Transform a region from surface coordinates to buffer coordinates
905 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300906 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200907 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300908 * \param[in] surface_region The region in surface coordinates.
909 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200910 *
911 * Buffer_region must be init'd, but will be completely overwritten.
912 *
913 * Viewport and buffer transformations can only do translation, scaling,
914 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600915 * conversion is from the coordinate rounding that takes place in
916 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300917 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200918 */
919WL_EXPORT void
920weston_surface_to_buffer_region(struct weston_surface *surface,
921 pixman_region32_t *surface_region,
922 pixman_region32_t *buffer_region)
923{
924 pixman_box32_t *src_rects, *dest_rects;
925 int nrects, i;
926
927 src_rects = pixman_region32_rectangles(surface_region, &nrects);
928 dest_rects = malloc(nrects * sizeof(*dest_rects));
929 if (!dest_rects)
930 return;
931
932 for (i = 0; i < nrects; i++) {
933 dest_rects[i] = weston_surface_to_buffer_rect(surface,
934 src_rects[i]);
935 }
936
937 pixman_region32_fini(buffer_region);
938 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
939 free(dest_rects);
940}
941
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200942WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500943weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400944 struct weston_plane *plane)
945{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500946 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400947 return;
948
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949 weston_view_damage_below(view);
950 view->plane = plane;
951 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400952}
953
Pekka Paalanen51723d52015-02-17 13:10:01 +0200954/** Inflict damage on the plane where the view is visible.
955 *
956 * \param view The view that causes the damage.
957 *
958 * If the view is currently on a plane (including the primary plane),
959 * take the view's boundingbox, subtract all the opaque views that cover it,
960 * and add the remaining region as damage to the plane. This corresponds
961 * to the damage inflicted to the plane if this view disappeared.
962 *
963 * A repaint is scheduled for this view.
964 *
965 * The region of all opaque views covering this view is stored in
966 * weston_view::clip and updated by view_accumulate_damage() during
967 * weston_output_repaint(). Specifically, that region matches the
968 * scenegraph as it was last painted.
969 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400970WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500971weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200972{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500973 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200974
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500975 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200976 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800978 if (view->plane)
979 pixman_region32_union(&view->plane->damage,
980 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500981 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800982 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200983}
984
Pekka Paalanen01e00682017-03-24 16:21:06 +0200985/** Send wl_surface.enter/leave events
986 *
987 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300988 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200989 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +0300990 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200991 *
992 * Send the enter/leave events for all protocol objects bound to the given
993 * output by the client owning the surface.
994 */
995static void
996weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300997 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +0200998 bool enter,
999 bool leave)
1000{
1001 struct wl_resource *wloutput;
1002 struct wl_client *client;
1003
1004 assert(enter != leave);
1005
1006 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001007 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001008 if (wl_resource_get_client(wloutput) != client)
1009 continue;
1010
1011 if (enter)
1012 wl_surface_send_enter(surface->resource, wloutput);
1013 if (leave)
1014 wl_surface_send_leave(surface->resource, wloutput);
1015 }
1016}
1017
Bryce Harrington3f650b82015-12-23 11:01:58 -08001018/**
1019 * \param es The surface
1020 * \param mask The new set of outputs for the surface
1021 *
1022 * Sets the surface's set of outputs to the ones specified by
1023 * the new output mask provided. Identifies the outputs that
1024 * have changed, the posts enter and leave events for these
1025 * outputs as appropriate.
1026 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001027static void
1028weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1029{
1030 uint32_t different = es->output_mask ^ mask;
1031 uint32_t entered = mask & different;
1032 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001033 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001034 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001035 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001036
1037 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001038 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001039 return;
1040 if (different == 0)
1041 return;
1042
1043 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001044 output_bit = 1u << output->id;
1045 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001046 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001047
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001048 wl_list_for_each(head, &output->head_list, output_link) {
1049 weston_surface_send_enter_leave(es, head,
1050 output_bit & entered,
1051 output_bit & left);
1052 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001053 }
1054}
1055
Semi Malinene7a52fb2018-04-26 11:08:10 +02001056static void
1057notify_view_output_destroy(struct wl_listener *listener, void *data)
1058{
1059 struct weston_view *view =
1060 container_of(listener,
1061 struct weston_view, output_destroy_listener);
1062
1063 view->output = NULL;
1064 view->output_destroy_listener.notify = NULL;
1065}
1066
1067/** Set the primary output of the view
1068 *
1069 * \param view The view whose primary output to set
1070 * \param output The new primary output for the view
1071 *
1072 * Set \a output to be the primary output of the \a view.
1073 *
1074 * Notice that the assignment may be temporary; the primary output could be
1075 * automatically changed. Hence, one cannot rely on the value persisting.
1076 *
1077 * Passing NULL as /a output will set the primary output to NULL.
1078 */
1079WL_EXPORT void
1080weston_view_set_output(struct weston_view *view, struct weston_output *output)
1081{
1082 if (view->output_destroy_listener.notify) {
1083 wl_list_remove(&view->output_destroy_listener.link);
1084 view->output_destroy_listener.notify = NULL;
1085 }
1086 view->output = output;
1087 if (output) {
1088 view->output_destroy_listener.notify =
1089 notify_view_output_destroy;
1090 wl_signal_add(&output->destroy_signal,
1091 &view->output_destroy_listener);
1092 }
1093}
1094
Bryce Harrington3f650b82015-12-23 11:01:58 -08001095/** Recalculate which output(s) the surface has views displayed on
1096 *
1097 * \param es The surface to remap to outputs
1098 *
1099 * Finds the output that is showing the largest amount of one
1100 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001101 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001102 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001103 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001104 * in the output_mask for the surface.
1105 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001106static void
1107weston_surface_assign_output(struct weston_surface *es)
1108{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001109 struct weston_output *new_output;
1110 struct weston_view *view;
1111 pixman_region32_t region;
1112 uint32_t max, area, mask;
1113 pixman_box32_t *e;
1114
1115 new_output = NULL;
1116 max = 0;
1117 mask = 0;
1118 pixman_region32_init(&region);
1119 wl_list_for_each(view, &es->views, surface_link) {
1120 if (!view->output)
1121 continue;
1122
1123 pixman_region32_intersect(&region, &view->transform.boundingbox,
1124 &view->output->region);
1125
1126 e = pixman_region32_extents(&region);
1127 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1128
1129 mask |= view->output_mask;
1130
1131 if (area >= max) {
1132 new_output = view->output;
1133 max = area;
1134 }
1135 }
1136 pixman_region32_fini(&region);
1137
1138 es->output = new_output;
1139 weston_surface_update_output_mask(es, mask);
1140}
1141
Bryce Harrington3f650b82015-12-23 11:01:58 -08001142/** Recalculate which output(s) the view is displayed on
1143 *
1144 * \param ev The view to remap to outputs
1145 *
1146 * Identifies the set of outputs that the view is visible on,
1147 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001148 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001149 *
1150 * Also does the same for the view's surface. See
1151 * weston_surface_assign_output().
1152 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001153static void
1154weston_view_assign_output(struct weston_view *ev)
1155{
1156 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001157 struct weston_output *output, *new_output;
1158 pixman_region32_t region;
1159 uint32_t max, area, mask;
1160 pixman_box32_t *e;
1161
1162 new_output = NULL;
1163 max = 0;
1164 mask = 0;
1165 pixman_region32_init(&region);
1166 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001167 if (output->destroying)
1168 continue;
1169
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001171 &output->region);
1172
1173 e = pixman_region32_extents(&region);
1174 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1175
1176 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001177 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001178
1179 if (area >= max) {
1180 new_output = output;
1181 max = area;
1182 }
1183 }
1184 pixman_region32_fini(&region);
1185
Semi Malinene7a52fb2018-04-26 11:08:10 +02001186 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 ev->output_mask = mask;
1188
1189 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001190}
1191
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001192static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001193weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1194 int from_x, int from_y, int *to_x, int *to_y)
1195{
1196 float x, y;
1197
1198 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1199 weston_view_from_global_float(to, x, y, &x, &y);
1200
1201 *to_x = round(x);
1202 *to_y = round(y);
1203}
1204
1205static void
1206weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1207{
1208 pixman_box32_t *a;
1209 pixman_box32_t b;
1210
1211 a = pixman_region32_extents(&from->geometry.scissor);
1212
1213 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1214 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1215
1216 pixman_region32_fini(&to->geometry.scissor);
1217 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1218}
1219
1220static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001221view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001223{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001224 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1225 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001226 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001227 { inbox->x1, inbox->y1 },
1228 { inbox->x1, inbox->y2 },
1229 { inbox->x2, inbox->y1 },
1230 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001231 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001232 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001233 int i;
1234
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001235 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001236 /* avoid rounding empty bbox to 1x1 */
1237 pixman_region32_init(bbox);
1238 return;
1239 }
1240
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001241 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001242 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001244 if (x < min_x)
1245 min_x = x;
1246 if (x > max_x)
1247 max_x = x;
1248 if (y < min_y)
1249 min_y = y;
1250 if (y > max_y)
1251 max_y = y;
1252 }
1253
Pekka Paalanen219b9822012-02-08 15:38:37 +02001254 int_x = floorf(min_x);
1255 int_y = floorf(min_y);
1256 pixman_region32_init_rect(bbox, int_x, int_y,
1257 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001258}
1259
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001260static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001262{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001264
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001265 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 view->geometry.x = roundf(view->geometry.x);
1267 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001268
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001269 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001270 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1271 view->transform.position.matrix.d[12] = view->geometry.x;
1272 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001273
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001275
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 view->transform.inverse = view->transform.position.matrix;
1277 view->transform.inverse.d[12] = -view->geometry.x;
1278 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001279
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001281 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001282 view->surface->width,
1283 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001284 if (view->geometry.scissor_enabled)
1285 pixman_region32_intersect(&view->transform.boundingbox,
1286 &view->transform.boundingbox,
1287 &view->geometry.scissor);
1288
1289 pixman_region32_translate(&view->transform.boundingbox,
1290 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001291
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 if (view->alpha == 1.0) {
1293 pixman_region32_copy(&view->transform.opaque,
1294 &view->surface->opaque);
1295 pixman_region32_translate(&view->transform.opaque,
1296 view->geometry.x,
1297 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001298 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001299}
1300
1301static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001303{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 struct weston_view *parent = view->geometry.parent;
1305 struct weston_matrix *matrix = &view->transform.matrix;
1306 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001307 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001308 pixman_region32_t surfregion;
1309 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001310
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001312
1313 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1315 view->transform.position.matrix.d[12] = view->geometry.x;
1316 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001317
1318 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001320 weston_matrix_multiply(matrix, &tform->matrix);
1321
Pekka Paalanen483243f2013-03-08 14:56:50 +02001322 if (parent)
1323 weston_matrix_multiply(matrix, &parent->transform.matrix);
1324
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001325 if (weston_matrix_invert(inverse, matrix) < 0) {
1326 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327 weston_log("error: weston_view %p"
1328 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001329 return -1;
1330 }
1331
Giulio Camuffo148c1992016-09-04 18:50:46 +03001332 if (view->alpha == 1.0 &&
1333 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1334 pixman_region32_copy(&view->transform.opaque,
1335 &view->surface->opaque);
1336 pixman_region32_translate(&view->transform.opaque,
1337 matrix->d[12],
1338 matrix->d[13]);
1339 }
1340
Pekka Paalanen380adf52015-02-16 14:39:11 +02001341 pixman_region32_init_rect(&surfregion, 0, 0,
1342 view->surface->width, view->surface->height);
1343 if (view->geometry.scissor_enabled)
1344 pixman_region32_intersect(&surfregion, &surfregion,
1345 &view->geometry.scissor);
1346 surfbox = pixman_region32_extents(&surfregion);
1347
1348 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1349 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001350
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001351 return 0;
1352}
1353
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001354static struct weston_layer *
1355get_view_layer(struct weston_view *view)
1356{
1357 if (view->parent_view)
1358 return get_view_layer(view->parent_view);
1359 return view->layer_link.layer;
1360}
1361
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001362WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001364{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001366 struct weston_layer *layer;
1367 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001368
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001370 return;
1371
Pekka Paalanen483243f2013-03-08 14:56:50 +02001372 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001374
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001376
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001378
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 pixman_region32_fini(&view->transform.boundingbox);
1380 pixman_region32_fini(&view->transform.opaque);
1381 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001382
Pekka Paalanencd403622012-01-25 13:37:39 +02001383 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384 if (view->geometry.transformation_list.next ==
1385 &view->transform.position.link &&
1386 view->geometry.transformation_list.prev ==
1387 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001388 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001390 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 if (weston_view_update_transform_enable(view) < 0)
1392 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001393 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001394
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001395 layer = get_view_layer(view);
1396 if (layer) {
1397 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001398 pixman_region32_intersect(&view->transform.boundingbox,
1399 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001400 pixman_region32_intersect(&view->transform.opaque,
1401 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001402 pixman_region32_fini(&mask);
1403 }
1404
Pekka Paalanen380adf52015-02-16 14:39:11 +02001405 if (parent) {
1406 if (parent->geometry.scissor_enabled) {
1407 view->geometry.scissor_enabled = true;
1408 weston_view_transfer_scissor(parent, view);
1409 } else {
1410 view->geometry.scissor_enabled = false;
1411 }
1412 }
1413
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001415
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001417
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 wl_signal_emit(&view->surface->compositor->transform_signal,
1419 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001420}
1421
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001422WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001424{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001425 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001426
1427 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 * The invariant: if view->geometry.dirty, then all views
1429 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001430 * Corollary: if not parent->geometry.dirty, then all ancestors
1431 * are not dirty.
1432 */
1433
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001435 return;
1436
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001438
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001440 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001442}
1443
1444WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445weston_view_to_global_fixed(struct weston_view *view,
1446 wl_fixed_t vx, wl_fixed_t vy,
1447 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001448{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001449 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001450
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 weston_view_to_global_float(view,
1452 wl_fixed_to_double(vx),
1453 wl_fixed_to_double(vy),
1454 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001455 *x = wl_fixed_from_double(xf);
1456 *y = wl_fixed_from_double(yf);
1457}
1458
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001459WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460weston_view_from_global_float(struct weston_view *view,
1461 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001462{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001464 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1465
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001467
1468 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001469 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001470 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001471 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 *vx = 0;
1473 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001474 return;
1475 }
1476
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477 *vx = v.f[0] / v.f[3];
1478 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001479 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480 *vx = x - view->geometry.x;
1481 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001482 }
1483}
1484
1485WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486weston_view_from_global_fixed(struct weston_view *view,
1487 wl_fixed_t x, wl_fixed_t y,
1488 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001489{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001491
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492 weston_view_from_global_float(view,
1493 wl_fixed_to_double(x),
1494 wl_fixed_to_double(y),
1495 &vxf, &vyf);
1496 *vx = wl_fixed_from_double(vxf);
1497 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001498}
1499
1500WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501weston_view_from_global(struct weston_view *view,
1502 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001503{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001504 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001505
Jason Ekstranda7af7042013-10-12 22:38:11 -05001506 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1507 *vx = floorf(vxf);
1508 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001509}
1510
Bryce Harrington3f650b82015-12-23 11:01:58 -08001511/**
1512 * \param surface The surface to be repainted
1513 *
1514 * Marks the output(s) that the surface is shown on as needing to be
1515 * repainted. See weston_output_schedule_repaint().
1516 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001517WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001518weston_surface_schedule_repaint(struct weston_surface *surface)
1519{
1520 struct weston_output *output;
1521
1522 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001523 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001524 weston_output_schedule_repaint(output);
1525}
1526
Bryce Harrington3f650b82015-12-23 11:01:58 -08001527/**
1528 * \param view The view to be repainted
1529 *
1530 * Marks the output(s) that the view is shown on as needing to be
1531 * repainted. See weston_output_schedule_repaint().
1532 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001533WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534weston_view_schedule_repaint(struct weston_view *view)
1535{
1536 struct weston_output *output;
1537
1538 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001539 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001540 weston_output_schedule_repaint(output);
1541}
1542
Pekka Paalanene508ce62015-02-19 13:59:55 +02001543/**
1544 * XXX: This function does it the wrong way.
1545 * surface->damage is the damage from the client, and causes
1546 * surface_flush_damage() to copy pixels. No window management action can
1547 * cause damage to the client-provided content, warranting re-upload!
1548 *
1549 * Instead of surface->damage, this function should record the damage
1550 * with all the views for this surface to avoid extraneous texture
1551 * uploads.
1552 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001554weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001555{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001556 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557 0, 0, surface->width,
1558 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001559
Kristian Høgsberg98238702012-08-03 16:29:12 -04001560 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001561}
1562
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001563WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001565{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001566 if (view->geometry.x == x && view->geometry.y == y)
1567 return;
1568
Jason Ekstranda7af7042013-10-12 22:38:11 -05001569 view->geometry.x = x;
1570 view->geometry.y = y;
1571 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001572}
1573
Pekka Paalanen483243f2013-03-08 14:56:50 +02001574static void
1575transform_parent_handle_parent_destroy(struct wl_listener *listener,
1576 void *data)
1577{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 struct weston_view *view =
1579 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001580 geometry.parent_destroy_listener);
1581
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001583}
1584
1585WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001587 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001588{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589 if (view->geometry.parent) {
1590 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1591 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001592
1593 if (!parent)
1594 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001595 }
1596
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001598
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001600 transform_parent_handle_parent_destroy;
1601 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001602 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001604 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001605 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001606 }
1607
Jason Ekstranda7af7042013-10-12 22:38:11 -05001608 weston_view_geometry_dirty(view);
1609}
1610
Pekka Paalanen380adf52015-02-16 14:39:11 +02001611/** Set a clip mask rectangle on a view
1612 *
1613 * \param view The view to set the clip mask on.
1614 * \param x Top-left corner X coordinate of the clip rectangle.
1615 * \param y Top-left corner Y coordinate of the clip rectangle.
1616 * \param width Width of the clip rectangle, non-negative.
1617 * \param height Height of the clip rectangle, non-negative.
1618 *
1619 * A shell may set a clip mask rectangle on a view. Everything outside
1620 * the rectangle is cut away for input and output purposes: it is
1621 * not drawn and cannot be hit by hit-test based input like pointer
1622 * motion or touch-downs. Everything inside the rectangle will behave
1623 * normally. Clients are unaware of clipping.
1624 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001625 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001626 * mask rectangle does not affect the view position, the view is positioned
1627 * as it would be without a clip. The clip also does not change
1628 * weston_surface::width,height.
1629 *
1630 * The clip mask rectangle is part of transformation inheritance
1631 * (weston_view_set_transform_parent()). A clip set in the root of the
1632 * transformation inheritance tree will affect all views in the tree.
1633 * A clip can be set only on the root view. Attempting to set a clip
1634 * on view that has a transformation parent will fail. Assigning a parent
1635 * to a view that has a clip set will cause the clip to be forgotten.
1636 *
1637 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1638 * on the additional transformations in the child views. These transformations
1639 * may not rotate the coordinate axes, i.e., only translation and scaling
1640 * are allowed. Violating this restriction causes the clipping to malfunction.
1641 * Furthermore, using scaling may cause rounding errors in child clipping.
1642 *
1643 * The clip mask rectangle is not automatically adjusted based on
1644 * wl_surface.attach dx and dy arguments.
1645 *
1646 * A clip mask rectangle can be set only if the compositor capability
1647 * WESTON_CAP_VIEW_CLIP_MASK is present.
1648 *
1649 * This function sets the clip mask rectangle and schedules a repaint for
1650 * the view.
1651 */
1652WL_EXPORT void
1653weston_view_set_mask(struct weston_view *view,
1654 int x, int y, int width, int height)
1655{
1656 struct weston_compositor *compositor = view->surface->compositor;
1657
1658 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1659 weston_log("%s not allowed without capability!\n", __func__);
1660 return;
1661 }
1662
1663 if (view->geometry.parent) {
1664 weston_log("view %p has a parent, clip forbidden!\n", view);
1665 return;
1666 }
1667
1668 if (width < 0 || height < 0) {
1669 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1670 x, y, width, height);
1671 return;
1672 }
1673
1674 pixman_region32_fini(&view->geometry.scissor);
1675 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1676 view->geometry.scissor_enabled = true;
1677 weston_view_geometry_dirty(view);
1678 weston_view_schedule_repaint(view);
1679}
1680
1681/** Remove the clip mask from a view
1682 *
1683 * \param view The view to remove the clip mask from.
1684 *
1685 * Removed the clip mask rectangle and schedules a repaint.
1686 *
1687 * \sa weston_view_set_mask
1688 */
1689WL_EXPORT void
1690weston_view_set_mask_infinite(struct weston_view *view)
1691{
1692 view->geometry.scissor_enabled = false;
1693 weston_view_geometry_dirty(view);
1694 weston_view_schedule_repaint(view);
1695}
1696
Armin Krezović0da12b82016-06-30 06:04:33 +02001697/* Check if view should be displayed
1698 *
1699 * The indicator is set manually when assigning
1700 * a view to a surface.
1701 *
1702 * This needs reworking. See the thread starting at:
1703 *
1704 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1705 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001706WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001707weston_view_is_mapped(struct weston_view *view)
1708{
Armin Krezović0da12b82016-06-30 06:04:33 +02001709 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001710}
1711
Philipp Zabel70decd52018-09-03 20:11:15 +02001712/* Check if view is opaque in specified region
1713 *
1714 * \param view The view to check for opacity.
1715 * \param region The region to check for opacity, in view coordinates.
1716 *
1717 * Returns true if the view is opaque in the specified region, because view
1718 * alpha is 1.0 and either the opaque region set by the client contains the
1719 * specified region, or the buffer pixel format or solid color is opaque.
1720 */
1721WL_EXPORT bool
1722weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1723{
1724 pixman_region32_t r;
1725 bool ret = false;
1726
1727 if (ev->alpha < 1.0)
1728 return false;
1729
1730 if (ev->surface->is_opaque)
1731 return true;
1732
1733 if (ev->transform.dirty) {
1734 weston_log("%s: transform dirty", __func__);
1735 return false;
1736 }
1737
1738 pixman_region32_init(&r);
1739 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1740
1741 if (!pixman_region32_not_empty(&r))
1742 ret = true;
1743
1744 pixman_region32_fini(&r);
1745
1746 return ret;
1747}
1748
Armin Krezović0da12b82016-06-30 06:04:33 +02001749/* Check if a surface has a view assigned to it
1750 *
1751 * The indicator is set manually when mapping
1752 * a surface and creating a view for it.
1753 *
1754 * This needs to go. See the thread starting at:
1755 *
1756 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1757 *
1758 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001759WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001760weston_surface_is_mapped(struct weston_surface *surface)
1761{
Armin Krezović0da12b82016-06-30 06:04:33 +02001762 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001763}
1764
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001765static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001766surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001767{
1768 struct weston_view *view;
1769
1770 if (surface->width == width && surface->height == height)
1771 return;
1772
1773 surface->width = width;
1774 surface->height = height;
1775
1776 wl_list_for_each(view, &surface->views, surface_link)
1777 weston_view_geometry_dirty(view);
1778}
1779
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001780WL_EXPORT void
1781weston_surface_set_size(struct weston_surface *surface,
1782 int32_t width, int32_t height)
1783{
1784 assert(!surface->resource);
1785 surface_set_size(surface, width, height);
1786}
1787
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001788static int
1789fixed_round_up_to_int(wl_fixed_t f)
1790{
1791 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1792}
1793
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001794static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001795convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1796 int32_t width, int32_t height,
1797 uint32_t transform,
1798 int32_t scale)
1799{
1800 assert(scale > 0);
1801
1802 switch (transform) {
1803 case WL_OUTPUT_TRANSFORM_NORMAL:
1804 case WL_OUTPUT_TRANSFORM_180:
1805 case WL_OUTPUT_TRANSFORM_FLIPPED:
1806 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1807 *width_out = width / scale;
1808 *height_out = height / scale;
1809 break;
1810 case WL_OUTPUT_TRANSFORM_90:
1811 case WL_OUTPUT_TRANSFORM_270:
1812 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1813 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1814 *width_out = height / scale;
1815 *height_out = width / scale;
1816 break;
1817 default:
1818 assert(0 && "invalid transform");
1819 }
1820}
1821
1822static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001823weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001824{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001825 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001826
1827 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001828 surface->width_from_buffer = 0;
1829 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001830 return;
1831 }
1832
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001833 convert_size_by_transform_scale(&surface->width_from_buffer,
1834 &surface->height_from_buffer,
1835 surface->buffer_ref.buffer->width,
1836 surface->buffer_ref.buffer->height,
1837 vp->buffer.transform,
1838 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001839}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001840
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001841static void
1842weston_surface_update_size(struct weston_surface *surface)
1843{
1844 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1845 int32_t width, height;
1846
1847 width = surface->width_from_buffer;
1848 height = surface->height_from_buffer;
1849
1850 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001851 surface_set_size(surface,
1852 vp->surface.width, vp->surface.height);
1853 return;
1854 }
1855
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001856 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001857 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1858 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1859
1860 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001861 return;
1862 }
1863
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001864 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001865}
1866
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001867WL_EXPORT void
1868weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001869{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001870 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001871}
1872
Jason Ekstranda7af7042013-10-12 22:38:11 -05001873WL_EXPORT struct weston_view *
1874weston_compositor_pick_view(struct weston_compositor *compositor,
1875 wl_fixed_t x, wl_fixed_t y,
1876 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001877{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001878 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001879 wl_fixed_t view_x, view_y;
1880 int view_ix, view_iy;
1881 int ix = wl_fixed_to_int(x);
1882 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001883
Jason Ekstranda7af7042013-10-12 22:38:11 -05001884 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001885 if (!pixman_region32_contains_point(
1886 &view->transform.boundingbox, ix, iy, NULL))
1887 continue;
1888
1889 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1890 view_ix = wl_fixed_to_int(view_x);
1891 view_iy = wl_fixed_to_int(view_y);
1892
1893 if (!pixman_region32_contains_point(&view->surface->input,
1894 view_ix, view_iy, NULL))
1895 continue;
1896
Pekka Paalanen380adf52015-02-16 14:39:11 +02001897 if (view->geometry.scissor_enabled &&
1898 !pixman_region32_contains_point(&view->geometry.scissor,
1899 view_ix, view_iy, NULL))
1900 continue;
1901
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001902 *vx = view_x;
1903 *vy = view_y;
1904 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001905 }
1906
Derek Foremanf9318d12015-05-11 15:40:11 -05001907 *vx = wl_fixed_from_int(-1000000);
1908 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001909 return NULL;
1910}
1911
1912static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001913weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001914{
Daniel Stone37816df2012-05-16 18:45:18 +01001915 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001916
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001917 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001918 return;
1919
Daniel Stone37816df2012-05-16 18:45:18 +01001920 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001921 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001922}
1923
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001924WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001925weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001926{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001927 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001928
Jason Ekstranda7af7042013-10-12 22:38:11 -05001929 if (!weston_view_is_mapped(view))
1930 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001931
Jason Ekstranda7af7042013-10-12 22:38:11 -05001932 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02001933 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08001934 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001935 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001936 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001937 wl_list_remove(&view->link);
1938 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001939 view->output_mask = 0;
1940 weston_surface_assign_output(view->surface);
1941
1942 if (weston_surface_is_mapped(view->surface))
1943 return;
1944
1945 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001946 struct weston_touch *touch = weston_seat_get_touch(seat);
1947 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1948 struct weston_keyboard *keyboard =
1949 weston_seat_get_keyboard(seat);
1950
1951 if (keyboard && keyboard->focus == view->surface)
1952 weston_keyboard_set_focus(keyboard, NULL);
1953 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001954 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001955 if (touch && touch->focus == view)
1956 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001957 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001958}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001959
Jason Ekstranda7af7042013-10-12 22:38:11 -05001960WL_EXPORT void
1961weston_surface_unmap(struct weston_surface *surface)
1962{
1963 struct weston_view *view;
1964
Armin Krezovićf8486c32016-06-30 06:04:28 +02001965 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001966 wl_list_for_each(view, &surface->views, surface_link)
1967 weston_view_unmap(view);
1968 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001969}
1970
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001971static void
1972weston_surface_reset_pending_buffer(struct weston_surface *surface)
1973{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001974 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001975 surface->pending.sx = 0;
1976 surface->pending.sy = 0;
1977 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001978 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001979}
1980
Jason Ekstranda7af7042013-10-12 22:38:11 -05001981WL_EXPORT void
1982weston_view_destroy(struct weston_view *view)
1983{
1984 wl_signal_emit(&view->destroy_signal, view);
1985
1986 assert(wl_list_empty(&view->geometry.child_list));
1987
1988 if (weston_view_is_mapped(view)) {
1989 weston_view_unmap(view);
1990 weston_compositor_build_view_list(view->surface->compositor);
1991 }
1992
1993 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001994 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001995
1996 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001997 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001998 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001999 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002000
2001 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002002 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002003
Jason Ekstranda7af7042013-10-12 22:38:11 -05002004 wl_list_remove(&view->surface_link);
2005
2006 free(view);
2007}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002008
2009WL_EXPORT void
2010weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002011{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002012 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002013 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002014 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002015
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002016 if (--surface->ref_count > 0)
2017 return;
2018
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002019 assert(surface->resource == NULL);
2020
Pekka Paalanenca790762015-04-17 14:23:38 +03002021 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002022
Pekka Paalanene67858b2013-04-25 13:57:42 +03002023 assert(wl_list_empty(&surface->subsurface_list_pending));
2024 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002025
Jason Ekstranda7af7042013-10-12 22:38:11 -05002026 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2027 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002028
Jason Ekstrand7b982072014-05-20 14:33:03 -05002029 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002030
Pekka Paalanende685b82012-12-04 15:58:12 +02002031 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002032 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002033
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002034 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002035 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002036 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002037
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002038 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002039 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002040
Pekka Paalanen133e4392014-09-23 22:08:46 -04002041 weston_presentation_feedback_discard_list(&surface->feedback_list);
2042
Jonas Ådahld3414f22016-07-22 17:56:31 +08002043 wl_list_for_each_safe(constraint, next_constraint,
2044 &surface->pointer_constraints,
2045 link)
2046 weston_pointer_constraint_destroy(constraint);
2047
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002048 fd_clear(&surface->acquire_fence_fd);
2049
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002050 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002051}
2052
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002053static void
2054destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002055{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002056 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002057
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002058 assert(surface);
2059
Giulio Camuffo0d379742013-11-15 22:06:15 +01002060 /* Set the resource to NULL, since we don't want to leave a
2061 * dangling pointer if the surface was refcounted and survives
2062 * the weston_surface_destroy() call. */
2063 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002064
2065 if (surface->viewport_resource)
2066 wl_resource_set_user_data(surface->viewport_resource, NULL);
2067
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002068 if (surface->synchronization_resource) {
2069 wl_resource_set_user_data(surface->synchronization_resource,
2070 NULL);
2071 }
2072
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002073 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002074}
2075
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002076static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002077weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2078{
2079 struct weston_buffer *buffer =
2080 container_of(listener, struct weston_buffer, destroy_listener);
2081
2082 wl_signal_emit(&buffer->destroy_signal, buffer);
2083 free(buffer);
2084}
2085
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002086WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002087weston_buffer_from_resource(struct wl_resource *resource)
2088{
2089 struct weston_buffer *buffer;
2090 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002091
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002092 listener = wl_resource_get_destroy_listener(resource,
2093 weston_buffer_destroy_handler);
2094
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002095 if (listener)
2096 return container_of(listener, struct weston_buffer,
2097 destroy_listener);
2098
2099 buffer = zalloc(sizeof *buffer);
2100 if (buffer == NULL)
2101 return NULL;
2102
2103 buffer->resource = resource;
2104 wl_signal_init(&buffer->destroy_signal);
2105 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002106 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002107 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002108
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002109 return buffer;
2110}
2111
2112static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002113weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2114 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002115{
Pekka Paalanende685b82012-12-04 15:58:12 +02002116 struct weston_buffer_reference *ref =
2117 container_of(listener, struct weston_buffer_reference,
2118 destroy_listener);
2119
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002120 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002121 ref->buffer = NULL;
2122}
2123
2124WL_EXPORT void
2125weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002126 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002127{
2128 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002129 ref->buffer->busy_count--;
2130 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002131 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002132 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002133 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002134 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002135 }
2136
Pekka Paalanende685b82012-12-04 15:58:12 +02002137 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002138 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002139 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002140 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002141 }
2142
Pekka Paalanende685b82012-12-04 15:58:12 +02002143 ref->buffer = buffer;
2144 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2145}
2146
2147static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002148weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2149 void *data)
2150{
2151 struct weston_buffer_release_reference *ref =
2152 container_of(listener, struct weston_buffer_release_reference,
2153 destroy_listener);
2154
2155 assert((struct wl_resource *)data == ref->buffer_release->resource);
2156 ref->buffer_release = NULL;
2157}
2158
2159static void
2160weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2161{
2162 struct wl_resource *resource = buffer_release->resource;
2163 int release_fence_fd = buffer_release->fence_fd;
2164
2165 if (release_fence_fd >= 0) {
2166 zwp_linux_buffer_release_v1_send_fenced_release(
2167 resource, release_fence_fd);
2168 } else {
2169 zwp_linux_buffer_release_v1_send_immediate_release(
2170 resource);
2171 }
2172
2173 wl_resource_destroy(resource);
2174}
2175
2176WL_EXPORT void
2177weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2178 struct weston_buffer_release *buffer_release)
2179{
2180 if (buffer_release == ref->buffer_release)
2181 return;
2182
2183 if (ref->buffer_release) {
2184 ref->buffer_release->ref_count--;
2185 wl_list_remove(&ref->destroy_listener.link);
2186 if (ref->buffer_release->ref_count == 0)
2187 weston_buffer_release_destroy(ref->buffer_release);
2188 }
2189
2190 if (buffer_release) {
2191 buffer_release->ref_count++;
2192 wl_resource_add_destroy_listener(buffer_release->resource,
2193 &ref->destroy_listener);
2194 }
2195
2196 ref->buffer_release = buffer_release;
2197 ref->destroy_listener.notify =
2198 weston_buffer_release_reference_handle_destroy;
2199}
2200
2201WL_EXPORT void
2202weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2203 struct weston_buffer_release_reference *src)
2204{
2205 weston_buffer_release_reference(dest, src->buffer_release);
2206 weston_buffer_release_reference(src, NULL);
2207}
2208
2209static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002210weston_surface_attach(struct weston_surface *surface,
2211 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002212{
2213 weston_buffer_reference(&surface->buffer_ref, buffer);
2214
Pekka Paalanena6421c42012-12-04 15:58:10 +02002215 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002216 if (weston_surface_is_mapped(surface))
2217 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002218 }
2219
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002220 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002221
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002222 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002223 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002224}
2225
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002226WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002227weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002228{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002229 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002230
2231 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002232 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002233}
2234
Marius Vlad55d87362019-06-11 01:15:35 +03002235/**
2236 * \ingroup output
2237 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002238WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002239weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002240{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002241 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002242
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002243 pixman_region32_union(&compositor->primary_plane.damage,
2244 &compositor->primary_plane.damage,
2245 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002246 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002247}
2248
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002249static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002250surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002251{
Pekka Paalanende685b82012-12-04 15:58:12 +02002252 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002253 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002254 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002255
Pekka Paalanenb5026542014-11-12 15:09:24 +02002256 if (weston_timeline_enabled_ &&
2257 pixman_region32_not_empty(&surface->damage))
2258 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2259 TLP_OUTPUT(surface->output), TLP_END);
2260
Jason Ekstrandef540082014-06-26 10:37:36 -07002261 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002262}
2263
2264static void
2265view_accumulate_damage(struct weston_view *view,
2266 pixman_region32_t *opaque)
2267{
2268 pixman_region32_t damage;
2269
2270 pixman_region32_init(&damage);
2271 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002272 pixman_box32_t *extents;
2273
Jason Ekstranda7af7042013-10-12 22:38:11 -05002274 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002275 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002276 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002277 pixman_region32_copy(&damage, &view->surface->damage);
2278 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002279 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002280 }
2281
Pekka Paalanen380adf52015-02-16 14:39:11 +02002282 pixman_region32_intersect(&damage, &damage,
2283 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002284 pixman_region32_subtract(&damage, &damage, opaque);
2285 pixman_region32_union(&view->plane->damage,
2286 &view->plane->damage, &damage);
2287 pixman_region32_fini(&damage);
2288 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002289 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002290}
2291
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002292static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002293compositor_accumulate_damage(struct weston_compositor *ec)
2294{
2295 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002296 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002297 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002298
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002299 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002300
2301 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002302 pixman_region32_copy(&plane->clip, &clip);
2303
2304 pixman_region32_init(&opaque);
2305
Jason Ekstranda7af7042013-10-12 22:38:11 -05002306 wl_list_for_each(ev, &ec->view_list, link) {
2307 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002308 continue;
2309
Jason Ekstranda7af7042013-10-12 22:38:11 -05002310 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002311 }
2312
2313 pixman_region32_union(&clip, &clip, &opaque);
2314 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002315 }
2316
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002317 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002318
Jason Ekstranda7af7042013-10-12 22:38:11 -05002319 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002320 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002321
2322 wl_list_for_each(ev, &ec->view_list, link) {
2323 if (ev->surface->touched)
2324 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002325 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002326
2327 surface_flush_damage(ev->surface);
2328
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002329 /* Both the renderer and the backend have seen the buffer
2330 * by now. If renderer needs the buffer, it has its own
2331 * reference set. If the backend wants to keep the buffer
2332 * around for migrating the surface into a non-primary plane
2333 * later, keep_buffer is true. Otherwise, drop the core
2334 * reference now, and allow early buffer release. This enables
2335 * clients to use single-buffering.
2336 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002337 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002338 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002339 weston_buffer_release_reference(
2340 &ev->surface->buffer_release_ref, NULL);
2341 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002342 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002343}
2344
2345static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002346surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002347{
2348 struct weston_subsurface *sub;
2349
Pekka Paalanene67858b2013-04-25 13:57:42 +03002350 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002351 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002352 continue;
2353
Jason Ekstranda7af7042013-10-12 22:38:11 -05002354 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2355 wl_list_init(&sub->surface->views);
2356
2357 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002358 }
2359}
2360
2361static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002362surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002363{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002364 struct weston_subsurface *sub;
2365 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002366
Jason Ekstranda7af7042013-10-12 22:38:11 -05002367 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2368 if (sub->surface == surface)
2369 continue;
2370
George Kiagiadakised04d382014-06-13 18:10:26 +02002371 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2372 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002373 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002374 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002375
2376 surface_free_unused_subsurface_views(sub->surface);
2377 }
2378}
2379
2380static void
2381view_list_add_subsurface_view(struct weston_compositor *compositor,
2382 struct weston_subsurface *sub,
2383 struct weston_view *parent)
2384{
2385 struct weston_subsurface *child;
2386 struct weston_view *view = NULL, *iv;
2387
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002388 if (!weston_surface_is_mapped(sub->surface))
2389 return;
2390
Jason Ekstranda7af7042013-10-12 22:38:11 -05002391 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2392 if (iv->geometry.parent == parent) {
2393 view = iv;
2394 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002395 }
2396 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002397
2398 if (view) {
2399 /* Put it back in the surface's list of views */
2400 wl_list_remove(&view->surface_link);
2401 wl_list_insert(&sub->surface->views, &view->surface_link);
2402 } else {
2403 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002404 weston_view_set_position(view,
2405 sub->position.x,
2406 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002407 weston_view_set_transform_parent(view, parent);
2408 }
2409
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002410 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002411 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002412 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002413
Pekka Paalanenb188e912013-11-19 14:03:35 +02002414 if (wl_list_empty(&sub->surface->subsurface_list)) {
2415 wl_list_insert(compositor->view_list.prev, &view->link);
2416 return;
2417 }
2418
2419 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2420 if (child->surface == sub->surface)
2421 wl_list_insert(compositor->view_list.prev, &view->link);
2422 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002423 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002424 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002425}
2426
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002427/* This recursively adds the sub-surfaces for a view, relying on the
2428 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2429 * change first happens to the sub-surface list, and then automatically
2430 * propagates here. See weston_surface_damage_subsurfaces() for how the
2431 * sub-surfaces receive damage when the client changes the state.
2432 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002433static void
2434view_list_add(struct weston_compositor *compositor,
2435 struct weston_view *view)
2436{
2437 struct weston_subsurface *sub;
2438
2439 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002440
Pekka Paalanenb188e912013-11-19 14:03:35 +02002441 if (wl_list_empty(&view->surface->subsurface_list)) {
2442 wl_list_insert(compositor->view_list.prev, &view->link);
2443 return;
2444 }
2445
2446 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2447 if (sub->surface == view->surface)
2448 wl_list_insert(compositor->view_list.prev, &view->link);
2449 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002450 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002451 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002452}
2453
2454static void
2455weston_compositor_build_view_list(struct weston_compositor *compositor)
2456{
2457 struct weston_view *view;
2458 struct weston_layer *layer;
2459
2460 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002461 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002462 surface_stash_subsurface_views(view->surface);
2463
2464 wl_list_init(&compositor->view_list);
2465 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002466 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002467 view_list_add(compositor, view);
2468 }
2469 }
2470
2471 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002472 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002473 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002474}
2475
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002476static void
2477weston_output_take_feedback_list(struct weston_output *output,
2478 struct weston_surface *surface)
2479{
2480 struct weston_view *view;
2481 struct weston_presentation_feedback *feedback;
2482 uint32_t flags = 0xffffffff;
2483
2484 if (wl_list_empty(&surface->feedback_list))
2485 return;
2486
2487 /* All views must have the flag for the flag to survive. */
2488 wl_list_for_each(view, &surface->views, surface_link) {
2489 /* ignore views that are not on this output at all */
2490 if (view->output_mask & (1u << output->id))
2491 flags &= view->psf_flags;
2492 }
2493
2494 wl_list_for_each(feedback, &surface->feedback_list, link)
2495 feedback->psf_flags = flags;
2496
2497 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2498 wl_list_init(&surface->feedback_list);
2499}
2500
David Herrmann1edf44c2013-10-22 17:11:26 +02002501static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002502weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002503{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002504 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002505 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002506 struct weston_animation *animation, *next;
2507 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002508 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002509 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002510 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002511 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002512
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002513 if (output->destroying)
2514 return 0;
2515
Pekka Paalanenb5026542014-11-12 15:09:24 +02002516 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2517
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002518 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002519 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002520
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002521 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002522 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002523 } else {
2524 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002525 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002526 ev->psf_flags = 0;
2527 }
2528 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002529
Pekka Paalanene67858b2013-04-25 13:57:42 +03002530 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002531 wl_list_for_each(ev, &ec->view_list, link) {
2532 /* Note: This operation is safe to do multiple times on the
2533 * same surface.
2534 */
2535 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002536 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002537 &ev->surface->frame_callback_list);
2538 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002539
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002540 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002541 }
2542 }
2543
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002544 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002545
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002546 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002547 pixman_region32_intersect(&output_damage,
2548 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002549 pixman_region32_subtract(&output_damage,
2550 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002551
Scott Moreauccbf29d2012-02-22 14:21:41 -07002552 if (output->dirty)
2553 weston_output_update_matrix(output);
2554
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002555 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002556
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002557 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002558
Daniel Stone09a97e22017-03-01 11:34:06 +00002559 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002560 if (r == 0)
2561 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002562
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002563 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002564
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002565 frame_time_msec = timespec_to_msec(&output->frame_time);
2566
Jonas Ådahldb773762012-06-13 00:01:21 +02002567 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002568 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002569 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002570 }
2571
Scott Moreaud64cf212012-06-08 19:40:54 -06002572 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002573 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002574 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002575 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002576
Pekka Paalanenb5026542014-11-12 15:09:24 +02002577 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2578
David Herrmann1edf44c2013-10-22 17:11:26 +02002579 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002580}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002581
Pekka Paalanen82919792014-05-21 13:51:49 +03002582static void
2583weston_output_schedule_repaint_reset(struct weston_output *output)
2584{
Daniel Stone05df8c12017-03-03 16:59:42 +00002585 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002586 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002587}
2588
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002589static int
2590weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2591 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002592{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002593 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002594 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002595 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002596
Daniel Stone6847b852017-03-01 11:34:08 +00002597 /* We're not ready yet; come back to make a decision later. */
2598 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002599 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002600
2601 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2602 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002603 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002604
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002605 /* If we're sleeping, drop the repaint machinery entirely; we will
2606 * explicitly repaint all outputs when we come back. */
2607 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2608 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2609 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002610
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002611 /* We don't actually need to repaint this output; drop it from
2612 * repaint until something causes damage. */
2613 if (!output->repaint_needed)
2614 goto err;
2615
2616 /* If repaint fails, we aren't going to get weston_output_finish_frame
2617 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002618 * something schedules a successful repaint later. As repainting may
2619 * take some time, re-read our clock as a courtesy to the next
2620 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002621 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002622 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002623 if (ret != 0)
2624 goto err;
2625
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002626 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002627 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002628
2629err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002630 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002631 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002632}
2633
2634static void
2635output_repaint_timer_arm(struct weston_compositor *compositor)
2636{
2637 struct weston_output *output;
2638 bool any_should_repaint = false;
2639 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002640 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002641
2642 weston_compositor_read_presentation_clock(compositor, &now);
2643
2644 wl_list_for_each(output, &compositor->output_list, link) {
2645 int64_t msec_to_this;
2646
2647 if (output->repaint_status != REPAINT_SCHEDULED)
2648 continue;
2649
2650 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2651 &now);
2652 if (!any_should_repaint || msec_to_this < msec_to_next)
2653 msec_to_next = msec_to_this;
2654
2655 any_should_repaint = true;
2656 }
2657
2658 if (!any_should_repaint)
2659 return;
2660
2661 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2662 * This is a workaround to allow coalescing multiple output repaints
2663 * particularly from weston_output_finish_frame()
2664 * into the same call, which would not happen if we called
2665 * output_repaint_timer_handler() directly.
2666 */
2667 if (msec_to_next < 1)
2668 msec_to_next = 1;
2669
2670 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2671}
2672
2673static int
2674output_repaint_timer_handler(void *data)
2675{
2676 struct weston_compositor *compositor = data;
2677 struct weston_output *output;
2678 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002679 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002680 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002681
2682 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002683
2684 if (compositor->backend->repaint_begin)
2685 repaint_data = compositor->backend->repaint_begin(compositor);
2686
2687 wl_list_for_each(output, &compositor->output_list, link) {
2688 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2689 if (ret)
2690 break;
2691 }
2692
2693 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002694 if (compositor->backend->repaint_flush)
2695 compositor->backend->repaint_flush(compositor,
2696 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002697 } else {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002698 wl_list_for_each(output, &compositor->output_list, link) {
2699 if (output->repainted)
2700 weston_output_schedule_repaint_reset(output);
2701 }
2702
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002703 if (compositor->backend->repaint_cancel)
2704 compositor->backend->repaint_cancel(compositor,
2705 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002706 }
Daniel Stone6847b852017-03-01 11:34:08 +00002707
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002708 wl_list_for_each(output, &compositor->output_list, link)
2709 output->repainted = false;
2710
Daniel Stone6847b852017-03-01 11:34:08 +00002711 output_repaint_timer_arm(compositor);
2712
Pekka Paalanen0513a952014-05-21 16:17:27 +03002713 return 0;
2714}
2715
Marius Vlad55d87362019-06-11 01:15:35 +03002716/**
2717 * \ingroup output
2718 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002719WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002720weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002721 const struct timespec *stamp,
2722 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002723{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002724 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002725 int32_t refresh_nsec;
2726 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002727 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002728
Pekka Paalanenb5026542014-11-12 15:09:24 +02002729
Daniel Stone05df8c12017-03-03 16:59:42 +00002730 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002731 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2732
Daniel Stone6847b852017-03-01 11:34:08 +00002733 weston_compositor_read_presentation_clock(compositor, &now);
2734
Daniel Stone3615ce12017-03-01 11:34:05 +00002735 /* If we haven't been supplied any timestamp at all, we don't have a
2736 * timebase to work against, so any delay just wastes time. Push a
2737 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002738 if (!stamp) {
2739 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002740 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002741 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002742
Marius Vladdf9278a2018-03-06 18:56:23 +02002743 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2744 TLP_VBLANK(stamp), TLP_END);
2745
Pekka Paalanend7894d02015-07-03 15:08:53 +03002746 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002747 weston_presentation_feedback_present_list(&output->feedback_list,
2748 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002749 output->msc,
2750 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002751
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002752 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002753
Daniel Stone6847b852017-03-01 11:34:08 +00002754 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2755 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2756 -compositor->repaint_msec);
2757 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002758
2759 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002760 static bool warned;
2761
2762 if (!warned)
2763 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002764 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002765 warned = true;
2766
Daniel Stone6847b852017-03-01 11:34:08 +00002767 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002768 }
2769
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002770 /* Called from restart_repaint_loop and restart happens already after
2771 * the deadline given by repaint_msec? In that case we delay until
2772 * the deadline of the next frame, to give clients a more predictable
2773 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002774 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2775 msec_rel < 0) {
2776 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2777 timespec_add_nsec(&output->next_repaint,
2778 &output->next_repaint,
2779 refresh_nsec);
2780 }
2781 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002782
Daniel Stone3615ce12017-03-01 11:34:05 +00002783out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002784 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002785 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002786}
2787
2788static void
2789idle_repaint(void *data)
2790{
2791 struct weston_output *output = data;
2792
Daniel Stone05df8c12017-03-03 16:59:42 +00002793 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2794 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002795 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002796 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002797}
2798
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002799WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002800weston_layer_entry_insert(struct weston_layer_entry *list,
2801 struct weston_layer_entry *entry)
2802{
2803 wl_list_insert(&list->link, &entry->link);
2804 entry->layer = list->layer;
2805}
2806
2807WL_EXPORT void
2808weston_layer_entry_remove(struct weston_layer_entry *entry)
2809{
2810 wl_list_remove(&entry->link);
2811 wl_list_init(&entry->link);
2812 entry->layer = NULL;
2813}
2814
Quentin Glidic82681572016-12-17 13:40:51 +01002815
2816/** Initialize the weston_layer struct.
2817 *
2818 * \param compositor The compositor instance
2819 * \param layer The layer to initialize
2820 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002821WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002822weston_layer_init(struct weston_layer *layer,
2823 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002824{
Quentin Glidic82681572016-12-17 13:40:51 +01002825 layer->compositor = compositor;
2826 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002827 wl_list_init(&layer->view_list.link);
2828 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002829 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002830}
2831
2832/** Sets the position of the layer in the layer list. The layer will be placed
2833 * below any layer with the same position value, if any.
2834 * This function is safe to call if the layer is already on the list, but the
2835 * layer may be moved below other layers at the same position, if any.
2836 *
2837 * \param layer The layer to modify
2838 * \param position The position the layer will be placed at
2839 */
2840WL_EXPORT void
2841weston_layer_set_position(struct weston_layer *layer,
2842 enum weston_layer_position position)
2843{
2844 struct weston_layer *below;
2845
2846 wl_list_remove(&layer->link);
2847
2848 /* layer_list is ordered from top to bottom, the last layer being the
2849 * background with the smallest position value */
2850
2851 layer->position = position;
2852 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2853 if (below->position >= layer->position) {
2854 wl_list_insert(&below->link, &layer->link);
2855 return;
2856 }
2857 }
2858 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2859}
2860
2861/** Hide a layer by taking it off the layer list.
2862 * This function is safe to call if the layer is not on the list.
2863 *
2864 * \param layer The layer to hide
2865 */
2866WL_EXPORT void
2867weston_layer_unset_position(struct weston_layer *layer)
2868{
2869 wl_list_remove(&layer->link);
2870 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002871}
2872
2873WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002874weston_layer_set_mask(struct weston_layer *layer,
2875 int x, int y, int width, int height)
2876{
2877 struct weston_view *view;
2878
2879 layer->mask.x1 = x;
2880 layer->mask.x2 = x + width;
2881 layer->mask.y1 = y;
2882 layer->mask.y2 = y + height;
2883
2884 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2885 weston_view_geometry_dirty(view);
2886 }
2887}
2888
2889WL_EXPORT void
2890weston_layer_set_mask_infinite(struct weston_layer *layer)
2891{
2892 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2893 UINT32_MAX, UINT32_MAX);
2894}
2895
Daniel Stone3b775632018-07-20 08:38:25 +01002896WL_EXPORT bool
2897weston_layer_mask_is_infinite(struct weston_layer *layer)
2898{
2899 return layer->mask.x1 == INT32_MIN &&
2900 layer->mask.y1 == INT32_MIN &&
2901 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
2902 layer->mask.y2 == INT32_MIN + UINT32_MAX;
2903}
2904
Marius Vlad55d87362019-06-11 01:15:35 +03002905/**
2906 * \ingroup output
2907 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002908WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002909weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002910{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002911 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002912 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002913
Bryce Harrington08976ac2016-08-30 12:05:16 -07002914 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2915 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002916 return;
2917
Pekka Paalanenb5026542014-11-12 15:09:24 +02002918 if (!output->repaint_needed)
2919 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2920
Kristian Høgsbergef044142011-06-21 15:02:12 -04002921 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002922 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002923
2924 /* If we already have a repaint scheduled for our idle handler,
2925 * no need to set it again. If the repaint has been called but
2926 * not finished, then weston_output_finish_frame() will notice
2927 * that a repaint is needed and schedule one. */
2928 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002929 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002930
Daniel Stone05df8c12017-03-03 16:59:42 +00002931 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002932 assert(!output->idle_repaint_source);
2933 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2934 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002935 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002936}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002937
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002938WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002939weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2940{
2941 struct weston_output *output;
2942
2943 wl_list_for_each(output, &compositor->output_list, link)
2944 weston_output_schedule_repaint(output);
2945}
2946
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002947static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002948surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002949{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002950 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002951}
2952
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002953static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002954surface_attach(struct wl_client *client,
2955 struct wl_resource *resource,
2956 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2957{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002958 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002959 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002960
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002961 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002962 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002963 if (buffer == NULL) {
2964 wl_client_post_no_memory(client);
2965 return;
2966 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002967 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002968
Pekka Paalanende685b82012-12-04 15:58:12 +02002969 /* Attach, attach, without commit in between does not send
2970 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002971 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002972
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002973 surface->pending.sx = sx;
2974 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002975 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002976}
2977
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002978static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002979surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002980 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002981 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002982{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002983 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002984
Derek Foreman57e92ed2015-11-17 14:11:35 -06002985 if (width <= 0 || height <= 0)
2986 return;
2987
Derek Foreman152254b2015-11-26 14:17:48 -06002988 pixman_region32_union_rect(&surface->pending.damage_surface,
2989 &surface->pending.damage_surface,
2990 x, y, width, height);
2991}
2992
2993static void
2994surface_damage_buffer(struct wl_client *client,
2995 struct wl_resource *resource,
2996 int32_t x, int32_t y, int32_t width, int32_t height)
2997{
2998 struct weston_surface *surface = wl_resource_get_user_data(resource);
2999
3000 if (width <= 0 || height <= 0)
3001 return;
3002
3003 pixman_region32_union_rect(&surface->pending.damage_buffer,
3004 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003005 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003006}
3007
Kristian Høgsberg33418202011-08-16 23:01:28 -04003008static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003009destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003010{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003011 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003012
3013 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003014 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003015}
3016
3017static void
3018surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003019 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003020{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003021 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003022 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003023
3024 cb = malloc(sizeof *cb);
3025 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003026 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003027 return;
3028 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003029
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003030 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3031 callback);
3032 if (cb->resource == NULL) {
3033 free(cb);
3034 wl_resource_post_no_memory(resource);
3035 return;
3036 }
3037
Jason Ekstranda85118c2013-06-27 20:17:02 -05003038 wl_resource_set_implementation(cb->resource, NULL, cb,
3039 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003040
Pekka Paalanenbc106382012-10-10 12:49:31 +03003041 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003042}
3043
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003044static void
3045surface_set_opaque_region(struct wl_client *client,
3046 struct wl_resource *resource,
3047 struct wl_resource *region_resource)
3048{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003049 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003050 struct weston_region *region;
3051
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003052 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003053 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003054 pixman_region32_copy(&surface->pending.opaque,
3055 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003056 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003057 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003058 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003059}
3060
3061static void
3062surface_set_input_region(struct wl_client *client,
3063 struct wl_resource *resource,
3064 struct wl_resource *region_resource)
3065{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003066 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003067 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003068
3069 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003070 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003071 pixman_region32_copy(&surface->pending.input,
3072 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003073 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003074 pixman_region32_fini(&surface->pending.input);
3075 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003076 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003077}
3078
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003079/* Cause damage to this sub-surface and all its children.
3080 *
3081 * This is useful when there are state changes that need an implicit
3082 * damage, e.g. a z-order change.
3083 */
3084static void
3085weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3086{
3087 struct weston_subsurface *child;
3088
3089 weston_surface_damage(sub->surface);
3090 sub->reordered = false;
3091
3092 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3093 if (child != sub)
3094 weston_surface_damage_subsurfaces(child);
3095}
3096
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003097static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003098weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003099{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003100 struct weston_subsurface *sub;
3101
3102 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3103 parent_link_pending) {
3104 wl_list_remove(&sub->parent_link);
3105 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003106
3107 if (sub->reordered)
3108 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003109 }
3110}
3111
3112static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003113weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003114 struct weston_matrix *matrix)
3115{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003116 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003117 double src_width, src_height, dest_width, dest_height;
3118
3119 weston_matrix_init(matrix);
3120
3121 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3122 src_width = surface->width_from_buffer;
3123 src_height = surface->height_from_buffer;
3124 } else {
3125 src_width = wl_fixed_to_double(vp->buffer.src_width);
3126 src_height = wl_fixed_to_double(vp->buffer.src_height);
3127 }
3128
3129 if (vp->surface.width == -1) {
3130 dest_width = src_width;
3131 dest_height = src_height;
3132 } else {
3133 dest_width = vp->surface.width;
3134 dest_height = vp->surface.height;
3135 }
3136
3137 if (src_width != dest_width || src_height != dest_height)
3138 weston_matrix_scale(matrix,
3139 src_width / dest_width,
3140 src_height / dest_height, 1);
3141
3142 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3143 weston_matrix_translate(matrix,
3144 wl_fixed_to_double(vp->buffer.src_x),
3145 wl_fixed_to_double(vp->buffer.src_y),
3146 0);
3147
3148 switch (vp->buffer.transform) {
3149 case WL_OUTPUT_TRANSFORM_FLIPPED:
3150 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3151 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3152 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3153 weston_matrix_scale(matrix, -1, 1, 1);
3154 weston_matrix_translate(matrix,
3155 surface->width_from_buffer, 0, 0);
3156 break;
3157 }
3158
3159 switch (vp->buffer.transform) {
3160 default:
3161 case WL_OUTPUT_TRANSFORM_NORMAL:
3162 case WL_OUTPUT_TRANSFORM_FLIPPED:
3163 break;
3164 case WL_OUTPUT_TRANSFORM_90:
3165 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3166 weston_matrix_rotate_xy(matrix, 0, 1);
3167 weston_matrix_translate(matrix,
3168 surface->height_from_buffer, 0, 0);
3169 break;
3170 case WL_OUTPUT_TRANSFORM_180:
3171 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3172 weston_matrix_rotate_xy(matrix, -1, 0);
3173 weston_matrix_translate(matrix,
3174 surface->width_from_buffer,
3175 surface->height_from_buffer, 0);
3176 break;
3177 case WL_OUTPUT_TRANSFORM_270:
3178 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3179 weston_matrix_rotate_xy(matrix, 0, -1);
3180 weston_matrix_translate(matrix,
3181 0, surface->width_from_buffer, 0);
3182 break;
3183 }
3184
3185 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3186}
3187
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003188/**
3189 * Compute a + b > c while being safe to overflows.
3190 */
3191static bool
3192fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3193{
3194 return (int64_t)a + (int64_t)b > (int64_t)c;
3195}
3196
3197static bool
3198weston_surface_is_pending_viewport_source_valid(
3199 const struct weston_surface *surface)
3200{
3201 const struct weston_surface_state *pend = &surface->pending;
3202 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3203 int width_from_buffer = 0;
3204 int height_from_buffer = 0;
3205 wl_fixed_t w;
3206 wl_fixed_t h;
3207
3208 /* If viewport source rect is not set, it is always ok. */
3209 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3210 return true;
3211
3212 if (pend->newly_attached) {
3213 if (pend->buffer) {
3214 convert_size_by_transform_scale(&width_from_buffer,
3215 &height_from_buffer,
3216 pend->buffer->width,
3217 pend->buffer->height,
3218 vp->buffer.transform,
3219 vp->buffer.scale);
3220 } else {
3221 /* No buffer: viewport is irrelevant. */
3222 return true;
3223 }
3224 } else {
3225 width_from_buffer = surface->width_from_buffer;
3226 height_from_buffer = surface->height_from_buffer;
3227 }
3228
3229 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3230 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3231
3232 /* No buffer: viewport is irrelevant. */
3233 if (width_from_buffer == 0 || height_from_buffer == 0)
3234 return true;
3235
3236 /* overflow checks for wl_fixed_from_int() */
3237 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3238 return false;
3239 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3240 return false;
3241
3242 w = wl_fixed_from_int(width_from_buffer);
3243 h = wl_fixed_from_int(height_from_buffer);
3244
3245 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3246 return false;
3247 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3248 return false;
3249
3250 return true;
3251}
3252
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003253static bool
3254fixed_is_integer(wl_fixed_t v)
3255{
3256 return (v & 0xff) == 0;
3257}
3258
3259static bool
3260weston_surface_is_pending_viewport_dst_size_int(
3261 const struct weston_surface *surface)
3262{
3263 const struct weston_buffer_viewport *vp =
3264 &surface->pending.buffer_viewport;
3265
3266 if (vp->surface.width != -1) {
3267 assert(vp->surface.width > 0 && vp->surface.height > 0);
3268 return true;
3269 }
3270
3271 return fixed_is_integer(vp->buffer.src_width) &&
3272 fixed_is_integer(vp->buffer.src_height);
3273}
3274
Derek Foreman152254b2015-11-26 14:17:48 -06003275/* Translate pending damage in buffer co-ordinates to surface
3276 * co-ordinates and union it with a pixman_region32_t.
3277 * This should only be called after the buffer is attached.
3278 */
3279static void
3280apply_damage_buffer(pixman_region32_t *dest,
3281 struct weston_surface *surface,
3282 struct weston_surface_state *state)
3283{
3284 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3285
3286 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3287 * translated into surface co-ordinates and unioned with
3288 * any other surface damage.
3289 * None of this makes sense if there is no buffer though.
3290 */
3291 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3292 pixman_region32_t buffer_damage;
3293
3294 pixman_region32_intersect_rect(&state->damage_buffer,
3295 &state->damage_buffer,
3296 0, 0, buffer->width,
3297 buffer->height);
3298 pixman_region32_init(&buffer_damage);
3299 weston_matrix_transform_region(&buffer_damage,
3300 &surface->buffer_to_surface_matrix,
3301 &state->damage_buffer);
3302 pixman_region32_union(dest, dest, &buffer_damage);
3303 pixman_region32_fini(&buffer_damage);
3304 }
3305 /* We should clear this on commit even if there was no buffer */
3306 pixman_region32_clear(&state->damage_buffer);
3307}
3308
Jason Ekstrand1e059042014-10-16 10:55:19 -05003309static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003310weston_surface_commit_state(struct weston_surface *surface,
3311 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003312{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003313 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003314 pixman_region32_t opaque;
3315
Alexander Larsson4ea95522013-05-22 14:41:37 +02003316 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003317 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003318 /* wp_viewport.set_source */
3319 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003320 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003321
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003322 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003323 if (state->newly_attached) {
3324 /* zwp_surface_synchronization_v1.set_acquire_fence */
3325 fd_move(&surface->acquire_fence_fd,
3326 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003327 /* zwp_surface_synchronization_v1.get_release */
3328 weston_buffer_release_move(&surface->buffer_release_ref,
3329 &state->buffer_release_ref);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003330
Jason Ekstrand7b982072014-05-20 14:33:03 -05003331 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003332 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003333 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003334 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003335 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003336
Jason Ekstrand1e059042014-10-16 10:55:19 -05003337 weston_surface_build_buffer_matrix(surface,
3338 &surface->surface_to_buffer_matrix);
3339 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3340 &surface->surface_to_buffer_matrix);
3341
Jason Ekstrand7b982072014-05-20 14:33:03 -05003342 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003343 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003344 if (surface->committed)
3345 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003346 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003347
Jason Ekstrand7b982072014-05-20 14:33:03 -05003348 state->sx = 0;
3349 state->sy = 0;
3350 state->newly_attached = 0;
3351 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003352
Derek Foreman152254b2015-11-26 14:17:48 -06003353 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003354 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003355 (pixman_region32_not_empty(&state->damage_surface) ||
3356 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003357 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003358
Pekka Paalanen8e159182012-10-10 12:49:25 +03003359 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003360 &state->damage_surface);
3361
3362 apply_damage_buffer(&surface->damage, surface, state);
3363
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003364 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003365 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003366 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003367
3368 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003369 pixman_region32_init(&opaque);
3370 pixman_region32_intersect_rect(&opaque, &state->opaque,
3371 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003372
3373 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3374 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003375 wl_list_for_each(view, &surface->views, surface_link)
3376 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003377 }
3378
3379 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003380
3381 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003382 pixman_region32_intersect_rect(&surface->input, &state->input,
3383 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003384
Pekka Paalanenbc106382012-10-10 12:49:31 +03003385 /* wl_surface.frame */
3386 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003387 &state->frame_callback_list);
3388 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003389
3390 /* XXX:
3391 * What should happen with a feedback request, if there
3392 * is no wl_buffer attached for this commit?
3393 */
3394
3395 /* presentation.feedback */
3396 wl_list_insert_list(&surface->feedback_list,
3397 &state->feedback_list);
3398 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003399
3400 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003401}
3402
3403static void
3404weston_surface_commit(struct weston_surface *surface)
3405{
3406 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003407
Pekka Paalanene67858b2013-04-25 13:57:42 +03003408 weston_surface_commit_subsurface_order(surface);
3409
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003410 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003411}
3412
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003413static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003414weston_subsurface_commit(struct weston_subsurface *sub);
3415
3416static void
3417weston_subsurface_parent_commit(struct weston_subsurface *sub,
3418 int parent_is_synchronized);
3419
3420static void
3421surface_commit(struct wl_client *client, struct wl_resource *resource)
3422{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003423 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003424 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3425
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003426 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3427 assert(surface->viewport_resource);
3428
3429 wl_resource_post_error(surface->viewport_resource,
3430 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3431 "wl_surface@%d has viewport source outside buffer",
3432 wl_resource_get_id(resource));
3433 return;
3434 }
3435
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003436 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3437 assert(surface->viewport_resource);
3438
3439 wl_resource_post_error(surface->viewport_resource,
3440 WP_VIEWPORT_ERROR_BAD_SIZE,
3441 "wl_surface@%d viewport dst size not integer",
3442 wl_resource_get_id(resource));
3443 return;
3444 }
3445
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003446 if (surface->pending.acquire_fence_fd >= 0) {
3447 assert(surface->synchronization_resource);
3448
3449 if (!surface->pending.buffer) {
3450 fd_clear(&surface->pending.acquire_fence_fd);
3451 wl_resource_post_error(surface->synchronization_resource,
3452 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3453 "wl_surface@%"PRIu32" no buffer for synchronization",
3454 wl_resource_get_id(resource));
3455 return;
3456 }
3457
3458 /* We support fences for both wp_linux_dmabuf and opaque EGL
3459 * buffers, as mandated by minor version 2 of the
3460 * zwp_linux_explicit_synchronization_v1 protocol. Since
3461 * renderers that support fences currently only support these
3462 * two buffer types plus SHM buffers, we can just check for the
3463 * SHM buffer case here.
3464 */
3465 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3466 fd_clear(&surface->pending.acquire_fence_fd);
3467 wl_resource_post_error(surface->synchronization_resource,
3468 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3469 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3470 wl_resource_get_id(resource));
3471 return;
3472 }
3473 }
3474
Alexandros Frantzis67629672018-10-19 12:14:11 +03003475 if (surface->pending.buffer_release_ref.buffer_release &&
3476 !surface->pending.buffer) {
3477 assert(surface->synchronization_resource);
3478
3479 wl_resource_post_error(surface->synchronization_resource,
3480 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3481 "wl_surface@%"PRIu32" no buffer for synchronization",
3482 wl_resource_get_id(resource));
3483 return;
3484 }
3485
Pekka Paalanene67858b2013-04-25 13:57:42 +03003486 if (sub) {
3487 weston_subsurface_commit(sub);
3488 return;
3489 }
3490
3491 weston_surface_commit(surface);
3492
3493 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3494 if (sub->surface != surface)
3495 weston_subsurface_parent_commit(sub, 0);
3496 }
3497}
3498
3499static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003500surface_set_buffer_transform(struct wl_client *client,
3501 struct wl_resource *resource, int transform)
3502{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003503 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003504
Jonny Lamba55f1392014-05-30 12:07:15 +02003505 /* if wl_output.transform grows more members this will need to be updated. */
3506 if (transform < 0 ||
3507 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3508 wl_resource_post_error(resource,
3509 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3510 "buffer transform must be a valid transform "
3511 "('%d' specified)", transform);
3512 return;
3513 }
3514
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003515 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003516 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003517}
3518
Alexander Larsson4ea95522013-05-22 14:41:37 +02003519static void
3520surface_set_buffer_scale(struct wl_client *client,
3521 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003522 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003523{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003524 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003525
Jonny Lamba55f1392014-05-30 12:07:15 +02003526 if (scale < 1) {
3527 wl_resource_post_error(resource,
3528 WL_SURFACE_ERROR_INVALID_SCALE,
3529 "buffer scale must be at least one "
3530 "('%d' specified)", scale);
3531 return;
3532 }
3533
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003534 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003535 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003536}
3537
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003538static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003539 surface_destroy,
3540 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003541 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003542 surface_frame,
3543 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003544 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003545 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003546 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003547 surface_set_buffer_scale,
3548 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003549};
3550
3551static void
3552compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003553 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003554{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003555 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003556 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003557
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003558 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003559 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003560 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003561 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003562 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003563
Jason Ekstranda85118c2013-06-27 20:17:02 -05003564 surface->resource =
3565 wl_resource_create(client, &wl_surface_interface,
3566 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003567 if (surface->resource == NULL) {
3568 weston_surface_destroy(surface);
3569 wl_resource_post_no_memory(resource);
3570 return;
3571 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003572 wl_resource_set_implementation(surface->resource, &surface_interface,
3573 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003574
3575 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003576}
3577
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003578static void
3579destroy_region(struct wl_resource *resource)
3580{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003581 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003582
3583 pixman_region32_fini(&region->region);
3584 free(region);
3585}
3586
3587static void
3588region_destroy(struct wl_client *client, struct wl_resource *resource)
3589{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003590 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003591}
3592
3593static void
3594region_add(struct wl_client *client, struct wl_resource *resource,
3595 int32_t x, int32_t y, int32_t width, int32_t height)
3596{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003597 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003598
3599 pixman_region32_union_rect(&region->region, &region->region,
3600 x, y, width, height);
3601}
3602
3603static void
3604region_subtract(struct wl_client *client, struct wl_resource *resource,
3605 int32_t x, int32_t y, int32_t width, int32_t height)
3606{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003607 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003608 pixman_region32_t rect;
3609
3610 pixman_region32_init_rect(&rect, x, y, width, height);
3611 pixman_region32_subtract(&region->region, &region->region, &rect);
3612 pixman_region32_fini(&rect);
3613}
3614
3615static const struct wl_region_interface region_interface = {
3616 region_destroy,
3617 region_add,
3618 region_subtract
3619};
3620
3621static void
3622compositor_create_region(struct wl_client *client,
3623 struct wl_resource *resource, uint32_t id)
3624{
3625 struct weston_region *region;
3626
3627 region = malloc(sizeof *region);
3628 if (region == NULL) {
3629 wl_resource_post_no_memory(resource);
3630 return;
3631 }
3632
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003633 pixman_region32_init(&region->region);
3634
Jason Ekstranda85118c2013-06-27 20:17:02 -05003635 region->resource =
3636 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003637 if (region->resource == NULL) {
3638 free(region);
3639 wl_resource_post_no_memory(resource);
3640 return;
3641 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003642 wl_resource_set_implementation(region->resource, &region_interface,
3643 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003644}
3645
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003646static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003647 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003648 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003649};
3650
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003651static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003652weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3653{
3654 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003655
Jason Ekstrand7b982072014-05-20 14:33:03 -05003656 weston_surface_commit_state(surface, &sub->cached);
3657 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003658
3659 weston_surface_commit_subsurface_order(surface);
3660
3661 weston_surface_schedule_repaint(surface);
3662
Jason Ekstrand7b982072014-05-20 14:33:03 -05003663 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003664}
3665
3666static void
3667weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3668{
3669 struct weston_surface *surface = sub->surface;
3670
3671 /*
3672 * If this commit would cause the surface to move by the
3673 * attach(dx, dy) parameters, the old damage region must be
3674 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003675 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003676 */
Derek Foreman152254b2015-11-26 14:17:48 -06003677 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003678 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003679 pixman_region32_union(&sub->cached.damage_surface,
3680 &sub->cached.damage_surface,
3681 &surface->pending.damage_surface);
3682 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003683
3684 if (surface->pending.newly_attached) {
3685 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003686 weston_surface_state_set_buffer(&sub->cached,
3687 surface->pending.buffer);
3688 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003689 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003690 weston_presentation_feedback_discard_list(
3691 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003692 /* zwp_surface_synchronization_v1.set_acquire_fence */
3693 fd_move(&sub->cached.acquire_fence_fd,
3694 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003695 /* zwp_surface_synchronization_v1.get_release */
3696 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3697 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003698 }
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003699 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003700 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003701 sub->cached.sx += surface->pending.sx;
3702 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003703
Derek Foreman152254b2015-11-26 14:17:48 -06003704 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3705
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003706 sub->cached.buffer_viewport.changed |=
3707 surface->pending.buffer_viewport.changed;
3708 sub->cached.buffer_viewport.buffer =
3709 surface->pending.buffer_viewport.buffer;
3710 sub->cached.buffer_viewport.surface =
3711 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003712
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003713 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003714
3715 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3716
3717 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3718
3719 wl_list_insert_list(&sub->cached.frame_callback_list,
3720 &surface->pending.frame_callback_list);
3721 wl_list_init(&surface->pending.frame_callback_list);
3722
Pekka Paalanen133e4392014-09-23 22:08:46 -04003723 wl_list_insert_list(&sub->cached.feedback_list,
3724 &surface->pending.feedback_list);
3725 wl_list_init(&surface->pending.feedback_list);
3726
Jason Ekstrand7b982072014-05-20 14:33:03 -05003727 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003728}
3729
Derek Foreman280e7dd2014-10-03 13:13:42 -05003730static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003731weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3732{
3733 while (sub) {
3734 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003735 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003736
3737 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003738 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003739
3740 sub = weston_surface_to_subsurface(sub->parent);
3741 }
3742
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003743 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003744}
3745
3746static void
3747weston_subsurface_commit(struct weston_subsurface *sub)
3748{
3749 struct weston_surface *surface = sub->surface;
3750 struct weston_subsurface *tmp;
3751
3752 /* Recursive check for effectively synchronized. */
3753 if (weston_subsurface_is_synchronized(sub)) {
3754 weston_subsurface_commit_to_cache(sub);
3755 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003756 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003757 /* flush accumulated state from cache */
3758 weston_subsurface_commit_to_cache(sub);
3759 weston_subsurface_commit_from_cache(sub);
3760 } else {
3761 weston_surface_commit(surface);
3762 }
3763
3764 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3765 if (tmp->surface != surface)
3766 weston_subsurface_parent_commit(tmp, 0);
3767 }
3768 }
3769}
3770
3771static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003772weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003773{
3774 struct weston_surface *surface = sub->surface;
3775 struct weston_subsurface *tmp;
3776
Pekka Paalanene67858b2013-04-25 13:57:42 +03003777 /* From now on, commit_from_cache the whole sub-tree, regardless of
3778 * the synchronized mode of each child. This sub-surface or some
3779 * of its ancestors were synchronized, so we are synchronized
3780 * all the way down.
3781 */
3782
Jason Ekstrand7b982072014-05-20 14:33:03 -05003783 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003784 weston_subsurface_commit_from_cache(sub);
3785
3786 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3787 if (tmp->surface != surface)
3788 weston_subsurface_parent_commit(tmp, 1);
3789 }
3790}
3791
3792static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003793weston_subsurface_parent_commit(struct weston_subsurface *sub,
3794 int parent_is_synchronized)
3795{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003796 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003797 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003798 wl_list_for_each(view, &sub->surface->views, surface_link)
3799 weston_view_set_position(view,
3800 sub->position.x,
3801 sub->position.y);
3802
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003803 sub->position.set = 0;
3804 }
3805
3806 if (parent_is_synchronized || sub->synchronized)
3807 weston_subsurface_synchronized_commit(sub);
3808}
3809
Pekka Paalanen8274d902014-08-06 19:36:51 +03003810static int
3811subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3812{
3813 return snprintf(buf, len, "sub-surface");
3814}
3815
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003816static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003817subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003818{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003819 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003820
Jason Ekstranda7af7042013-10-12 22:38:11 -05003821 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003822 weston_view_set_position(view,
3823 view->geometry.x + dx,
3824 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003825
3826 /* No need to check parent mappedness, because if parent is not
3827 * mapped, parent is not in a visible layer, so this sub-surface
3828 * will not be drawn either.
3829 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003830
Pekka Paalanene67858b2013-04-25 13:57:42 +03003831 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003832 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003833
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003834 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003835 * because that would call it also for the parent surface,
3836 * which might not be mapped yet. That would lead to
3837 * inconsistent state, where the window could never be
3838 * mapped.
3839 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003840 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003841 * weston_surface_is_mapped() return true, so that when the
3842 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003843 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003844 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003845 }
3846}
3847
3848static struct weston_subsurface *
3849weston_surface_to_subsurface(struct weston_surface *surface)
3850{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003851 if (surface->committed == subsurface_committed)
3852 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003853
3854 return NULL;
3855}
3856
Pekka Paalanen01388e22013-04-25 13:57:44 +03003857WL_EXPORT struct weston_surface *
3858weston_surface_get_main_surface(struct weston_surface *surface)
3859{
3860 struct weston_subsurface *sub;
3861
3862 while (surface && (sub = weston_surface_to_subsurface(surface)))
3863 surface = sub->parent;
3864
3865 return surface;
3866}
3867
Pekka Paalanen50b67472014-10-01 15:02:41 +03003868WL_EXPORT int
3869weston_surface_set_role(struct weston_surface *surface,
3870 const char *role_name,
3871 struct wl_resource *error_resource,
3872 uint32_t error_code)
3873{
3874 assert(role_name);
3875
3876 if (surface->role_name == NULL ||
3877 surface->role_name == role_name ||
3878 strcmp(surface->role_name, role_name) == 0) {
3879 surface->role_name = role_name;
3880
3881 return 0;
3882 }
3883
3884 wl_resource_post_error(error_resource, error_code,
3885 "Cannot assign role %s to wl_surface@%d,"
3886 " already has role %s\n",
3887 role_name,
3888 wl_resource_get_id(surface->resource),
3889 surface->role_name);
3890 return -1;
3891}
3892
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003893WL_EXPORT const char *
3894weston_surface_get_role(struct weston_surface *surface)
3895{
3896 return surface->role_name;
3897}
3898
Pekka Paalanen8274d902014-08-06 19:36:51 +03003899WL_EXPORT void
3900weston_surface_set_label_func(struct weston_surface *surface,
3901 int (*desc)(struct weston_surface *,
3902 char *, size_t))
3903{
3904 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003905 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003906}
3907
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003908/** Get the size of surface contents
3909 *
3910 * \param surface The surface to query.
3911 * \param width Returns the width of raw contents.
3912 * \param height Returns the height of raw contents.
3913 *
3914 * Retrieves the raw surface content size in pixels for the given surface.
3915 * This is the whole content size in buffer pixels. If the surface
3916 * has no content or the renderer does not implement this feature,
3917 * zeroes are returned.
3918 *
3919 * This function is used to determine the buffer size needed for
3920 * a weston_surface_copy_content() call.
3921 */
3922WL_EXPORT void
3923weston_surface_get_content_size(struct weston_surface *surface,
3924 int *width, int *height)
3925{
3926 struct weston_renderer *rer = surface->compositor->renderer;
3927
3928 if (!rer->surface_get_content_size) {
3929 *width = 0;
3930 *height = 0;
3931 return;
3932 }
3933
3934 rer->surface_get_content_size(surface, width, height);
3935}
3936
Quentin Glidic248dd102016-08-12 10:41:34 +02003937/** Get the bounding box of a surface and its subsurfaces
3938 *
3939 * \param surface The surface to query.
3940 * \return The bounding box relative to the surface origin.
3941 *
3942 */
3943WL_EXPORT struct weston_geometry
3944weston_surface_get_bounding_box(struct weston_surface *surface)
3945{
3946 pixman_region32_t region;
3947 pixman_box32_t *box;
3948 struct weston_subsurface *subsurface;
3949
3950 pixman_region32_init_rect(&region,
3951 0, 0,
3952 surface->width, surface->height);
3953
3954 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3955 pixman_region32_union_rect(&region, &region,
3956 subsurface->position.x,
3957 subsurface->position.y,
3958 subsurface->surface->width,
3959 subsurface->surface->height);
3960
3961 box = pixman_region32_extents(&region);
3962 struct weston_geometry geometry = {
3963 .x = box->x1,
3964 .y = box->y1,
3965 .width = box->x2 - box->x1,
3966 .height = box->y2 - box->y1,
3967 };
3968
3969 pixman_region32_fini(&region);
3970
3971 return geometry;
3972}
3973
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003974/** Copy surface contents to system memory.
3975 *
3976 * \param surface The surface to copy from.
3977 * \param target Pointer to the target memory buffer.
3978 * \param size Size of the target buffer in bytes.
3979 * \param src_x X location on contents to copy from.
3980 * \param src_y Y location on contents to copy from.
3981 * \param width Width in pixels of the area to copy.
3982 * \param height Height in pixels of the area to copy.
3983 * \return 0 for success, -1 for failure.
3984 *
3985 * Surface contents are maintained by the renderer. They can be in a
3986 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3987 * else.
3988 *
3989 * Surface contents are copied into memory pointed to by target,
3990 * which has size bytes of space available. The target memory
3991 * may be larger than needed, but being smaller returns an error.
3992 * The extra bytes in target may or may not be written; their content is
3993 * unspecified. Size must be large enough to hold the image.
3994 *
3995 * The image in the target memory will be arranged in rows from
3996 * top to bottom, and pixels on a row from left to right. The pixel
3997 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3998 * width * 4.
3999 *
4000 * Parameters src_x and src_y define the upper-left corner in buffer
4001 * coordinates (pixels) to copy from. Parameters width and height
4002 * define the size of the area to copy in pixels.
4003 *
4004 * The rectangle defined by src_x, src_y, width, height must fit in
4005 * the surface contents. Otherwise an error is returned.
4006 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004007 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004008 * needed target buffer size and rectangle limits.
4009 *
4010 * CURRENT IMPLEMENTATION RESTRICTIONS:
4011 * - the machine must be little-endian due to Pixman formats.
4012 *
4013 * NOTE: Pixman formats are premultiplied.
4014 */
4015WL_EXPORT int
4016weston_surface_copy_content(struct weston_surface *surface,
4017 void *target, size_t size,
4018 int src_x, int src_y,
4019 int width, int height)
4020{
4021 struct weston_renderer *rer = surface->compositor->renderer;
4022 int cw, ch;
4023 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4024
4025 if (!rer->surface_copy_content)
4026 return -1;
4027
4028 weston_surface_get_content_size(surface, &cw, &ch);
4029
4030 if (src_x < 0 || src_y < 0)
4031 return -1;
4032
4033 if (width <= 0 || height <= 0)
4034 return -1;
4035
4036 if (src_x + width > cw || src_y + height > ch)
4037 return -1;
4038
4039 if (width * bytespp * height > size)
4040 return -1;
4041
4042 return rer->surface_copy_content(surface, target, size,
4043 src_x, src_y, width, height);
4044}
4045
Pekka Paalanene67858b2013-04-25 13:57:42 +03004046static void
4047subsurface_set_position(struct wl_client *client,
4048 struct wl_resource *resource, int32_t x, int32_t y)
4049{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004050 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004051
4052 if (!sub)
4053 return;
4054
4055 sub->position.x = x;
4056 sub->position.y = y;
4057 sub->position.set = 1;
4058}
4059
4060static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004061subsurface_find_sibling(struct weston_subsurface *sub,
4062 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004063{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004064 struct weston_surface *parent = sub->parent;
4065 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004066
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004067 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4068 if (sibling->surface == surface && sibling != sub)
4069 return sibling;
4070 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004071
4072 return NULL;
4073}
4074
4075static struct weston_subsurface *
4076subsurface_sibling_check(struct weston_subsurface *sub,
4077 struct weston_surface *surface,
4078 const char *request)
4079{
4080 struct weston_subsurface *sibling;
4081
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004082 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004083 if (!sibling) {
4084 wl_resource_post_error(sub->resource,
4085 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4086 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004087 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004088 return NULL;
4089 }
4090
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004091 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004092
4093 return sibling;
4094}
4095
4096static void
4097subsurface_place_above(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_above");
4110 if (!sibling)
4111 return;
4112
4113 wl_list_remove(&sub->parent_link_pending);
4114 wl_list_insert(sibling->parent_link_pending.prev,
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_place_below(struct wl_client *client,
4122 struct wl_resource *resource,
4123 struct wl_resource *sibling_resource)
4124{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004125 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004126 struct weston_surface *surface =
4127 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004128 struct weston_subsurface *sibling;
4129
4130 if (!sub)
4131 return;
4132
4133 sibling = subsurface_sibling_check(sub, surface, "place_below");
4134 if (!sibling)
4135 return;
4136
4137 wl_list_remove(&sub->parent_link_pending);
4138 wl_list_insert(&sibling->parent_link_pending,
4139 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004140
4141 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004142}
4143
4144static void
4145subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4146{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004147 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004148
4149 if (sub)
4150 sub->synchronized = 1;
4151}
4152
4153static void
4154subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4155{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004156 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004157
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004158 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004159 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004160
4161 /* If sub became effectively desynchronized, flush. */
4162 if (!weston_subsurface_is_synchronized(sub))
4163 weston_subsurface_synchronized_commit(sub);
4164 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004165}
4166
4167static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004168weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4169{
4170 wl_list_remove(&sub->parent_link);
4171 wl_list_remove(&sub->parent_link_pending);
4172 wl_list_remove(&sub->parent_destroy_listener.link);
4173 sub->parent = NULL;
4174}
4175
4176static void
4177weston_subsurface_destroy(struct weston_subsurface *sub);
4178
4179static void
4180subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4181{
4182 struct weston_subsurface *sub =
4183 container_of(listener, struct weston_subsurface,
4184 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004185 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004186
4187 /* The protocol object (wl_resource) is left inert. */
4188 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004189 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004190
4191 weston_subsurface_destroy(sub);
4192}
4193
4194static void
4195subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4196{
4197 struct weston_subsurface *sub =
4198 container_of(listener, struct weston_subsurface,
4199 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004200 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004201 assert(sub->surface != sub->parent);
4202
4203 if (weston_surface_is_mapped(sub->surface))
4204 weston_surface_unmap(sub->surface);
4205
4206 weston_subsurface_unlink_parent(sub);
4207}
4208
4209static void
4210subsurface_resource_destroy(struct wl_resource *resource)
4211{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004212 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004213
4214 if (sub)
4215 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004216}
4217
4218static void
4219subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4220{
4221 wl_resource_destroy(resource);
4222}
4223
4224static void
4225weston_subsurface_link_parent(struct weston_subsurface *sub,
4226 struct weston_surface *parent)
4227{
4228 sub->parent = parent;
4229 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004230 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004231 &sub->parent_destroy_listener);
4232
4233 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4234 wl_list_insert(&parent->subsurface_list_pending,
4235 &sub->parent_link_pending);
4236}
4237
4238static void
4239weston_subsurface_link_surface(struct weston_subsurface *sub,
4240 struct weston_surface *surface)
4241{
4242 sub->surface = surface;
4243 sub->surface_destroy_listener.notify =
4244 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004245 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004246 &sub->surface_destroy_listener);
4247}
4248
4249static void
4250weston_subsurface_destroy(struct weston_subsurface *sub)
4251{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004252 struct weston_view *view, *next;
4253
Pekka Paalanene67858b2013-04-25 13:57:42 +03004254 assert(sub->surface);
4255
4256 if (sub->resource) {
4257 assert(weston_surface_to_subsurface(sub->surface) == sub);
4258 assert(sub->parent_destroy_listener.notify ==
4259 subsurface_handle_parent_destroy);
4260
George Kiagiadakised04d382014-06-13 18:10:26 +02004261 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4262 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004263 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004264 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004265
Pekka Paalanene67858b2013-04-25 13:57:42 +03004266 if (sub->parent)
4267 weston_subsurface_unlink_parent(sub);
4268
Jason Ekstrand7b982072014-05-20 14:33:03 -05004269 weston_surface_state_fini(&sub->cached);
4270 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004271
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004272 sub->surface->committed = NULL;
4273 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004274 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004275 } else {
4276 /* the dummy weston_subsurface for the parent itself */
4277 assert(sub->parent_destroy_listener.notify == NULL);
4278 wl_list_remove(&sub->parent_link);
4279 wl_list_remove(&sub->parent_link_pending);
4280 }
4281
4282 wl_list_remove(&sub->surface_destroy_listener.link);
4283 free(sub);
4284}
4285
4286static const struct wl_subsurface_interface subsurface_implementation = {
4287 subsurface_destroy,
4288 subsurface_set_position,
4289 subsurface_place_above,
4290 subsurface_place_below,
4291 subsurface_set_sync,
4292 subsurface_set_desync
4293};
4294
4295static struct weston_subsurface *
4296weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4297 struct weston_surface *parent)
4298{
4299 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004300 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004301
Bryce Harringtonde16d892014-11-20 22:21:57 -08004302 sub = zalloc(sizeof *sub);
4303 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004304 return NULL;
4305
Jason Ekstranda7af7042013-10-12 22:38:11 -05004306 wl_list_init(&sub->unused_views);
4307
Jason Ekstranda85118c2013-06-27 20:17:02 -05004308 sub->resource =
4309 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004310 if (!sub->resource) {
4311 free(sub);
4312 return NULL;
4313 }
4314
Jason Ekstranda85118c2013-06-27 20:17:02 -05004315 wl_resource_set_implementation(sub->resource,
4316 &subsurface_implementation,
4317 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004318 weston_subsurface_link_surface(sub, surface);
4319 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004320 weston_surface_state_init(&sub->cached);
4321 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004322 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004323
4324 return sub;
4325}
4326
4327/* Create a dummy subsurface for having the parent itself in its
4328 * sub-surface lists. Makes stacking order manipulation easy.
4329 */
4330static struct weston_subsurface *
4331weston_subsurface_create_for_parent(struct weston_surface *parent)
4332{
4333 struct weston_subsurface *sub;
4334
Bryce Harringtonde16d892014-11-20 22:21:57 -08004335 sub = zalloc(sizeof *sub);
4336 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004337 return NULL;
4338
4339 weston_subsurface_link_surface(sub, parent);
4340 sub->parent = parent;
4341 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4342 wl_list_insert(&parent->subsurface_list_pending,
4343 &sub->parent_link_pending);
4344
4345 return sub;
4346}
4347
4348static void
4349subcompositor_get_subsurface(struct wl_client *client,
4350 struct wl_resource *resource,
4351 uint32_t id,
4352 struct wl_resource *surface_resource,
4353 struct wl_resource *parent_resource)
4354{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004355 struct weston_surface *surface =
4356 wl_resource_get_user_data(surface_resource);
4357 struct weston_surface *parent =
4358 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004359 struct weston_subsurface *sub;
4360 static const char where[] = "get_subsurface: wl_subsurface@";
4361
4362 if (surface == parent) {
4363 wl_resource_post_error(resource,
4364 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4365 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004366 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004367 return;
4368 }
4369
4370 if (weston_surface_to_subsurface(surface)) {
4371 wl_resource_post_error(resource,
4372 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4373 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004374 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004375 return;
4376 }
4377
Pekka Paalanen50b67472014-10-01 15:02:41 +03004378 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4379 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004380 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004381
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004382 if (weston_surface_get_main_surface(parent) == surface) {
4383 wl_resource_post_error(resource,
4384 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4385 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004386 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004387 return;
4388 }
4389
Pekka Paalanene67858b2013-04-25 13:57:42 +03004390 /* make sure the parent is in its own list */
4391 if (wl_list_empty(&parent->subsurface_list)) {
4392 if (!weston_subsurface_create_for_parent(parent)) {
4393 wl_resource_post_no_memory(resource);
4394 return;
4395 }
4396 }
4397
4398 sub = weston_subsurface_create(id, surface, parent);
4399 if (!sub) {
4400 wl_resource_post_no_memory(resource);
4401 return;
4402 }
4403
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004404 surface->committed = subsurface_committed;
4405 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004406 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004407}
4408
4409static void
4410subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4411{
4412 wl_resource_destroy(resource);
4413}
4414
4415static const struct wl_subcompositor_interface subcompositor_interface = {
4416 subcompositor_destroy,
4417 subcompositor_get_subsurface
4418};
4419
4420static void
4421bind_subcompositor(struct wl_client *client,
4422 void *data, uint32_t version, uint32_t id)
4423{
4424 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004425 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004426
Jason Ekstranda85118c2013-06-27 20:17:02 -05004427 resource =
4428 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004429 if (resource == NULL) {
4430 wl_client_post_no_memory(client);
4431 return;
4432 }
4433 wl_resource_set_implementation(resource, &subcompositor_interface,
4434 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004435}
4436
Bryce Harrington0795ece2016-08-30 12:04:26 -07004437/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004438 *
4439 * \param compositor The compositor instance
4440 * \param state The DPMS state the outputs will be set to
4441 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004442static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004443weston_compositor_dpms(struct weston_compositor *compositor,
4444 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004445{
4446 struct weston_output *output;
4447
Bryce Harrington08976ac2016-08-30 12:05:16 -07004448 wl_list_for_each(output, &compositor->output_list, link)
4449 if (output->set_dpms)
4450 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004451}
4452
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004453/** Restores the compositor to active status
4454 *
4455 * \param compositor The compositor instance
4456 *
4457 * If the compositor was in a sleeping mode, all outputs are powered
4458 * back on via DPMS. Otherwise if the compositor was inactive
4459 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4460 * signal will fire.
4461 *
4462 * Restarts the idle timer.
4463 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004464WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004465weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004466{
Neil Roberts8b62e202013-09-30 13:14:47 +01004467 uint32_t old_state = compositor->state;
4468
4469 /* The state needs to be changed before emitting the wake
4470 * signal because that may try to schedule a repaint which
4471 * will not work if the compositor is still sleeping */
4472 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4473
4474 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004475 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004476 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004477 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004478 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004479 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004480 /* fall through */
4481 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004482 wl_event_source_timer_update(compositor->idle_source,
4483 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004484 }
4485}
4486
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004487/** Turns off rendering and frame events for the compositor.
4488 *
4489 * \param compositor The compositor instance
4490 *
4491 * This is used for example to prevent further rendering while the
4492 * compositor is shutting down.
4493 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004494 * Stops the idle timer.
4495 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004496WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004497weston_compositor_offscreen(struct weston_compositor *compositor)
4498{
4499 switch (compositor->state) {
4500 case WESTON_COMPOSITOR_OFFSCREEN:
4501 return;
4502 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004503 default:
4504 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4505 wl_event_source_timer_update(compositor->idle_source, 0);
4506 }
4507}
4508
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004509/** Powers down all attached output devices
4510 *
4511 * \param compositor The compositor instance
4512 *
4513 * Causes rendering to the outputs to cease, and no frame events to be
4514 * sent. Only powers down the outputs if the compositor is not already
4515 * in sleep mode.
4516 *
4517 * Stops the idle timer.
4518 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004519WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004520weston_compositor_sleep(struct weston_compositor *compositor)
4521{
4522 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4523 return;
4524
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004525 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004526 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4527 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4528}
4529
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004530/** Sets compositor to idle mode
4531 *
4532 * \param data The compositor instance
4533 *
4534 * This is called when the idle timer fires. Once the compositor is in
4535 * idle mode it requires a wake action (e.g. via
4536 * weston_compositor_wake()) to restore it. The compositor's
4537 * idle_signal will be triggered when the idle event occurs.
4538 *
4539 * Idleness can be inhibited by setting the compositor's idle_inhibit
4540 * property.
4541 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004542static int
4543idle_handler(void *data)
4544{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004545 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004546
4547 if (compositor->idle_inhibit)
4548 return 1;
4549
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004550 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004551 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004552
4553 return 1;
4554}
4555
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004556WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004557weston_plane_init(struct weston_plane *plane,
4558 struct weston_compositor *ec,
4559 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004560{
4561 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004562 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004563 plane->x = x;
4564 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004565 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004566
4567 /* Init the link so that the call to wl_list_remove() when releasing
4568 * the plane without ever stacking doesn't lead to a crash */
4569 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004570}
4571
4572WL_EXPORT void
4573weston_plane_release(struct weston_plane *plane)
4574{
Xiong Zhang97116532013-10-23 13:58:31 +08004575 struct weston_view *view;
4576
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004577 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004578 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004579
Xiong Zhang97116532013-10-23 13:58:31 +08004580 wl_list_for_each(view, &plane->compositor->view_list, link) {
4581 if (view->plane == plane)
4582 view->plane = NULL;
4583 }
4584
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004585 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004586}
4587
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004588WL_EXPORT void
4589weston_compositor_stack_plane(struct weston_compositor *ec,
4590 struct weston_plane *plane,
4591 struct weston_plane *above)
4592{
4593 if (above)
4594 wl_list_insert(above->link.prev, &plane->link);
4595 else
4596 wl_list_insert(&ec->plane_list, &plane->link);
4597}
4598
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004599static void
4600output_release(struct wl_client *client, struct wl_resource *resource)
4601{
4602 wl_resource_destroy(resource);
4603}
4604
4605static const struct wl_output_interface output_interface = {
4606 output_release,
4607};
4608
4609
Casey Dahlin9074db52012-04-19 22:50:09 -04004610static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004611{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004612 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004613}
4614
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004615static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004616bind_output(struct wl_client *client,
4617 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004618{
Pekka Paalanen05347622017-03-27 12:24:34 +03004619 struct weston_head *head = data;
4620 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004621 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004622 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004623
Jason Ekstranda85118c2013-06-27 20:17:02 -05004624 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004625 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004626 if (resource == NULL) {
4627 wl_client_post_no_memory(client);
4628 return;
4629 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004630
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004631 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004632 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004633 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004634
Pekka Paalanen05347622017-03-27 12:24:34 +03004635 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004636 wl_output_send_geometry(resource,
4637 output->x,
4638 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004639 head->mm_width,
4640 head->mm_height,
4641 head->subpixel,
4642 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004643 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004644 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004645 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004646 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004647
4648 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004649 wl_output_send_mode(resource,
4650 mode->flags,
4651 mode->width,
4652 mode->height,
4653 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004654 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004655
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004656 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004657 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004658}
4659
Pekka Paalanendcac3512017-12-08 14:13:34 +02004660static void
4661weston_head_add_global(struct weston_head *head)
4662{
4663 head->global = wl_global_create(head->compositor->wl_display,
4664 &wl_output_interface, 3,
4665 head, bind_output);
4666}
4667
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004668/** Remove the global wl_output protocol object
4669 *
4670 * \param head The head whose global to remove.
4671 *
4672 * Also orphans the wl_resources for this head (wl_output).
4673 */
4674static void
4675weston_head_remove_global(struct weston_head *head)
4676{
4677 struct wl_resource *resource, *tmp;
4678
4679 if (head->global)
4680 wl_global_destroy(head->global);
4681 head->global = NULL;
4682
4683 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4684 unbind_resource(resource);
4685 wl_resource_set_destructor(resource, NULL);
4686 wl_resource_set_user_data(resource, NULL);
4687 }
4688}
4689
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004690/** Get the backing object of wl_output
4691 *
4692 * \param resource A wl_output protocol object.
4693 * \return The backing object (user data) of a wl_resource representing a
4694 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004695 *
4696 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004697 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004698WL_EXPORT struct weston_head *
4699weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004700{
4701 assert(wl_resource_instance_of(resource, &wl_output_interface,
4702 &output_interface));
4703
4704 return wl_resource_get_user_data(resource);
4705}
4706
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004707/** Initialize a pre-allocated weston_head
4708 *
4709 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004710 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004711 *
4712 * The head will be safe to attach, detach and release.
4713 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004714 * The name is used in logs, and can be used by compositors as a configuration
4715 * identifier.
4716 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004717 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004718 * \internal
4719 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004720WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004721weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004722{
4723 /* Add some (in)sane defaults which can be used
4724 * for checking if an output was properly configured
4725 */
4726 memset(head, 0, sizeof *head);
4727
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004728 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004729 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004730 wl_list_init(&head->output_link);
4731 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004732 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004733}
4734
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004735/** Send output heads changed signal
4736 *
4737 * \param output The output that changed.
4738 *
4739 * Notify that the enabled output gained and/or lost heads, or that the
4740 * associated heads may have changed their connection status. This does not
4741 * include cases where the output becomes enabled or disabled. The registered
4742 * callbacks are called after the change has successfully happened.
4743 *
4744 * If connection status change causes the compositor to attach or detach a head
4745 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03004746 *
4747 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004748 */
4749static void
4750weston_output_emit_heads_changed(struct weston_output *output)
4751{
4752 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4753 output);
4754}
4755
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004756/** Idle task for emitting heads_changed_signal */
4757static void
4758weston_compositor_call_heads_changed(void *data)
4759{
4760 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004761 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004762
4763 compositor->heads_changed_source = NULL;
4764
4765 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004766
4767 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4768 if (head->output && head->output->enabled)
4769 weston_output_emit_heads_changed(head->output);
4770 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004771}
4772
4773/** Schedule a call on idle to heads_changed callback
4774 *
4775 * \param compositor The Compositor.
4776 *
4777 * \memberof weston_compositor
4778 * \internal
4779 */
4780static void
4781weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4782{
4783 struct wl_event_loop *loop;
4784
4785 if (compositor->heads_changed_source)
4786 return;
4787
4788 loop = wl_display_get_event_loop(compositor->wl_display);
4789 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4790 weston_compositor_call_heads_changed, compositor);
4791}
4792
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004793/** Register a new head
4794 *
4795 * \param compositor The compositor.
4796 * \param head The head to register, must not be already registered.
4797 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004798 * This signals the core that a new head has become available, leading to
4799 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004800 *
4801 * \memberof weston_compositor
4802 * \internal
4803 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004804WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004805weston_compositor_add_head(struct weston_compositor *compositor,
4806 struct weston_head *head)
4807{
4808 assert(wl_list_empty(&head->compositor_link));
4809 assert(head->name);
4810
4811 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4812 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004813 weston_compositor_schedule_heads_changed(compositor);
4814}
4815
4816/** Adds a listener to be called when heads change
4817 *
4818 * \param compositor The compositor.
4819 * \param listener The listener to add.
4820 *
Marius Vlada2dace22019-06-12 16:05:44 +03004821 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004822 *
4823 * The listener function will be called after heads are added or their
4824 * connection status has changed. Several changes may be accumulated into a
4825 * single call. The user is expected to iterate over the existing heads and
4826 * check their statuses to find out what changed.
4827 *
4828 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4829 * weston_head_is_enabled
4830 * \memberof weston_compositor
4831 */
4832WL_EXPORT void
4833weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4834 struct wl_listener *listener)
4835{
4836 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004837}
4838
4839/** Iterate over available heads
4840 *
4841 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03004842 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004843 * \return The next available head in the list.
4844 *
4845 * Returns all available heads, regardless of being connected or enabled.
4846 *
4847 * You can iterate over all heads as follows:
4848 * \code
4849 * struct weston_head *head = NULL;
4850 *
4851 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4852 * ...
4853 * }
4854 * \endcode
4855 *
4856 * If you cause \c iter to be removed from the list, you cannot use it to
4857 * continue iterating. Removing any other item is safe.
4858 *
4859 * \memberof weston_compositor
4860 */
4861WL_EXPORT struct weston_head *
4862weston_compositor_iterate_heads(struct weston_compositor *compositor,
4863 struct weston_head *iter)
4864{
4865 struct wl_list *list = &compositor->head_list;
4866 struct wl_list *node;
4867
4868 assert(compositor);
4869 assert(!iter || iter->compositor == compositor);
4870
4871 if (iter)
4872 node = iter->compositor_link.next;
4873 else
4874 node = list->next;
4875
4876 assert(node);
4877 assert(!iter || node != &iter->compositor_link);
4878
4879 if (node == list)
4880 return NULL;
4881
4882 return container_of(node, struct weston_head, compositor_link);
4883}
4884
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004885/** Iterate over attached heads
4886 *
4887 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03004888 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004889 * \return The next attached head in the list.
4890 *
4891 * Returns all heads currently attached to the output.
4892 *
4893 * You can iterate over all heads as follows:
4894 * \code
4895 * struct weston_head *head = NULL;
4896 *
4897 * while ((head = weston_output_iterate_heads(output, head))) {
4898 * ...
4899 * }
4900 * \endcode
4901 *
4902 * If you cause \c iter to be removed from the list, you cannot use it to
4903 * continue iterating. Removing any other item is safe.
4904 *
Marius Vlad55d87362019-06-11 01:15:35 +03004905 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004906 */
4907WL_EXPORT struct weston_head *
4908weston_output_iterate_heads(struct weston_output *output,
4909 struct weston_head *iter)
4910{
4911 struct wl_list *list = &output->head_list;
4912 struct wl_list *node;
4913
4914 assert(output);
4915 assert(!iter || iter->output == output);
4916
4917 if (iter)
4918 node = iter->output_link.next;
4919 else
4920 node = list->next;
4921
4922 assert(node);
4923 assert(!iter || node != &iter->output_link);
4924
4925 if (node == list)
4926 return NULL;
4927
4928 return container_of(node, struct weston_head, output_link);
4929}
4930
Pekka Paalanendcac3512017-12-08 14:13:34 +02004931/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004932 *
4933 * \param output The output to attach to.
4934 * \param head The head that is not yet attached.
4935 * \return 0 on success, -1 on failure.
4936 *
4937 * Attaches the given head to the output. All heads of an output are clones
4938 * and share the resolution and timings.
4939 *
4940 * Cloning heads this way uses less resources than creating an output for
4941 * each head, but is not always possible due to environment, driver and hardware
4942 * limitations.
4943 *
4944 * On failure, the head remains unattached. Success of this function does not
4945 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004946 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004947 *
Marius Vlad55d87362019-06-11 01:15:35 +03004948 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004949 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004950WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004951weston_output_attach_head(struct weston_output *output,
4952 struct weston_head *head)
4953{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004954 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004955
4956 if (!wl_list_empty(&head->output_link))
4957 return -1;
4958
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004959 if (output->attach_head) {
4960 if (output->attach_head(output, head) < 0)
4961 return -1;
4962 } else if (!wl_list_empty(&output->head_list)) {
4963 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004964 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004965 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004966
4967 head->output = output;
4968 wl_list_insert(output->head_list.prev, &head->output_link);
4969
Pekka Paalanendcac3512017-12-08 14:13:34 +02004970 if (output->enabled) {
4971 weston_head_add_global(head);
4972
4973 head_names = weston_output_create_heads_string(output);
4974 weston_log("Output '%s' updated to have head(s) %s\n",
4975 output->name, head_names);
4976 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004977
4978 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02004979 }
4980
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004981 return 0;
4982}
4983
4984/** Detach a head from its output
4985 *
4986 * \param head The head to detach.
4987 *
4988 * It is safe to detach a non-attached head.
4989 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004990 * If the head is attached to an enabled output and the output will be left
4991 * with no heads, the output will be disabled.
4992 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004993 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004994 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004995 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004996WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004997weston_head_detach(struct weston_head *head)
4998{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004999 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005000 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005001
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005002 wl_list_remove(&head->output_link);
5003 wl_list_init(&head->output_link);
5004 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005005
5006 if (!output)
5007 return;
5008
5009 if (output->detach_head)
5010 output->detach_head(output, head);
5011
5012 if (output->enabled) {
5013 weston_head_remove_global(head);
5014
Pekka Paalanena0106992017-12-08 16:11:17 +02005015 if (wl_list_empty(&output->head_list)) {
5016 weston_log("Output '%s' no heads left, disabling.\n",
5017 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005018 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005019 } else {
5020 head_names = weston_output_create_heads_string(output);
5021 weston_log("Output '%s' updated to have head(s) %s\n",
5022 output->name, head_names);
5023 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005024
5025 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005026 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005027 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005028}
5029
5030/** Destroy a head
5031 *
5032 * \param head The head to be released.
5033 *
5034 * Destroys the head. The caller is responsible for freeing the memory pointed
5035 * to by \c head.
5036 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005037 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005038 * \internal
5039 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005040WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005041weston_head_release(struct weston_head *head)
5042{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005043 wl_signal_emit(&head->destroy_signal, head);
5044
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005045 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005046
5047 free(head->make);
5048 free(head->model);
5049 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005050 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005051
5052 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005053}
5054
Pekka Paalanene19970f2017-08-28 14:11:02 +03005055static void
5056weston_head_set_device_changed(struct weston_head *head)
5057{
5058 head->device_changed = true;
5059
5060 if (head->compositor)
5061 weston_compositor_schedule_heads_changed(head->compositor);
5062}
5063
5064/** String equal comparison with NULLs being equal */
5065static bool
5066str_null_eq(const char *a, const char *b)
5067{
5068 if (!a && !b)
5069 return true;
5070
5071 if (!!a != !!b)
5072 return false;
5073
5074 return strcmp(a, b) == 0;
5075}
5076
Pekka Paalanen01f60212017-03-24 15:39:24 +02005077/** Store monitor make, model and serial number
5078 *
5079 * \param head The head to modify.
5080 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5081 * any string, or NULL for none.
5082 * \param model The monitor model or name, or a made-up string, or NULL for
5083 * none.
5084 * \param serialno The monitor serial number, a made-up string, or NULL for
5085 * none.
5086 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005087 * This may set the device_changed flag.
5088 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005089 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005090 * \internal
5091 */
5092WL_EXPORT void
5093weston_head_set_monitor_strings(struct weston_head *head,
5094 const char *make,
5095 const char *model,
5096 const char *serialno)
5097{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005098 if (str_null_eq(head->make, make) &&
5099 str_null_eq(head->model, model) &&
5100 str_null_eq(head->serial_number, serialno))
5101 return;
5102
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005103 free(head->make);
5104 free(head->model);
5105 free(head->serial_number);
5106
5107 head->make = make ? strdup(make) : NULL;
5108 head->model = model ? strdup(model) : NULL;
5109 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005110
5111 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005112}
5113
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005114/** Store display non-desktop status
5115 *
5116 * \param head The head to modify.
5117 * \param non_desktop Whether the head connects to a non-desktop display.
5118 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005119 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005120 * \internal
5121 */
5122WL_EXPORT void
5123weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5124{
5125 if (head->non_desktop == non_desktop)
5126 return;
5127
5128 head->non_desktop = non_desktop;
5129
5130 weston_head_set_device_changed(head);
5131}
5132
Pekka Paalanen01f60212017-03-24 15:39:24 +02005133/** Store physical image size
5134 *
5135 * \param head The head to modify.
5136 * \param mm_width Image area width in millimeters.
5137 * \param mm_height Image area height in millimeters.
5138 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005139 * This may set the device_changed flag.
5140 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005141 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005142 * \internal
5143 */
5144WL_EXPORT void
5145weston_head_set_physical_size(struct weston_head *head,
5146 int32_t mm_width, int32_t mm_height)
5147{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005148 if (head->mm_width == mm_width &&
5149 head->mm_height == mm_height)
5150 return;
5151
Pekka Paalanen01f60212017-03-24 15:39:24 +02005152 head->mm_width = mm_width;
5153 head->mm_height = mm_height;
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/** Store monitor sub-pixel layout
5159 *
5160 * \param head The head to modify.
5161 * \param sp Sub-pixel layout. The possible values are:
5162 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5163 * - WL_OUTPUT_SUBPIXEL_NONE,
5164 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5165 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5166 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5167 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5168 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005169 * This may set the device_changed flag.
5170 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005171 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005172 * \internal
5173 */
5174WL_EXPORT void
5175weston_head_set_subpixel(struct weston_head *head,
5176 enum wl_output_subpixel sp)
5177{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005178 if (head->subpixel == sp)
5179 return;
5180
Pekka Paalanen01f60212017-03-24 15:39:24 +02005181 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005182
5183 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005184}
5185
5186/** Mark the monitor as internal
5187 *
5188 * This is used for embedded screens, like laptop panels.
5189 *
5190 * \param head The head to mark as internal.
5191 *
5192 * By default a head is external. The type is often inferred from the physical
5193 * connector type.
5194 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005195 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005196 * \internal
5197 */
5198WL_EXPORT void
5199weston_head_set_internal(struct weston_head *head)
5200{
5201 head->connection_internal = true;
5202}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005203
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005204/** Store connector status
5205 *
5206 * \param head The head to modify.
5207 * \param connected Whether the head is connected.
5208 *
5209 * Connectors are created as disconnected. This function can be used to
5210 * set the connector status.
5211 *
5212 * The status should be set to true when a physical connector is connected to
5213 * a video sink device like a monitor and to false when the connector is
5214 * disconnected. For nested backends, the connection status should reflect the
5215 * connection to the parent display server.
5216 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005217 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005218 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005219 *
5220 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005221 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005222 * \internal
5223 */
5224WL_EXPORT void
5225weston_head_set_connection_status(struct weston_head *head, bool connected)
5226{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005227 if (head->connected == connected)
5228 return;
5229
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005230 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005231
Pekka Paalanene19970f2017-08-28 14:11:02 +03005232 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005233}
5234
5235/** Is the head currently connected?
5236 *
5237 * \param head The head to query.
5238 * \return Connection status.
5239 *
5240 * Returns true if the head is physically connected to a monitor, or in
5241 * case of a nested backend returns true when there is a connection to the
5242 * parent display server.
5243 *
5244 * This is independent from the head being enabled.
5245 *
5246 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005247 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005248 */
5249WL_EXPORT bool
5250weston_head_is_connected(struct weston_head *head)
5251{
5252 return head->connected;
5253}
5254
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005255/** Is the head currently enabled?
5256 *
5257 * \param head The head to query.
5258 * \return Video status.
5259 *
5260 * Returns true if the head is currently transmitting a video stream.
5261 *
5262 * This is independent of the head being connected.
5263 *
5264 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005265 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005266 */
5267WL_EXPORT bool
5268weston_head_is_enabled(struct weston_head *head)
5269{
5270 if (!head->output)
5271 return false;
5272
5273 return head->output->enabled;
5274}
5275
Pekka Paalanene19970f2017-08-28 14:11:02 +03005276/** Has the device information changed?
5277 *
5278 * \param head The head to query.
5279 * \return True if the device information has changed since last reset.
5280 *
5281 * The information about the connected display device, e.g. a monitor, may
5282 * change without being disconnected in between. Changing information
5283 * causes a call to the heads_changed hook.
5284 *
5285 * The information includes make, model, serial number, physical size,
5286 * and sub-pixel type. The connection status is also included.
5287 *
5288 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005289 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005290 */
5291WL_EXPORT bool
5292weston_head_is_device_changed(struct weston_head *head)
5293{
5294 return head->device_changed;
5295}
5296
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005297/** Does the head represent a non-desktop display?
5298 *
5299 * \param head The head to query.
5300 * \return True if the device is a non-desktop display.
5301 *
5302 * Non-desktop heads are not attached to outputs by default.
5303 * This stops weston from extending the desktop onto head mounted displays.
5304 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005305 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005306 */
5307WL_EXPORT bool
5308weston_head_is_non_desktop(struct weston_head *head)
5309{
5310 return head->non_desktop;
5311}
5312
Pekka Paalanene19970f2017-08-28 14:11:02 +03005313/** Acknowledge device information change
5314 *
5315 * \param head The head to acknowledge.
5316 *
5317 * Clears the device changed flag on this head. When a compositor has processed
5318 * device information, it should call this to be able to notice further
5319 * changes.
5320 *
5321 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005322 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005323 */
5324WL_EXPORT void
5325weston_head_reset_device_changed(struct weston_head *head)
5326{
5327 head->device_changed = false;
5328}
5329
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005330/** Get the name of a head
5331 *
5332 * \param head The head to query.
5333 * \return The head's name, not NULL.
5334 *
5335 * The name depends on the backend. The DRM backend uses connector names,
5336 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005337 *
5338 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005339 */
5340WL_EXPORT const char *
5341weston_head_get_name(struct weston_head *head)
5342{
5343 return head->name;
5344}
5345
5346/** Get the output the head is attached to
5347 *
5348 * \param head The head to query.
5349 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005350 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005351 */
5352WL_EXPORT struct weston_output *
5353weston_head_get_output(struct weston_head *head)
5354{
5355 return head->output;
5356}
5357
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005358/** Add destroy callback for a head
5359 *
5360 * \param head The head to watch for.
5361 * \param listener The listener to add. The \c notify member must be set.
5362 *
5363 * Heads may get destroyed for various reasons by the backends. If a head is
5364 * attached to an output, the compositor should listen for head destruction
5365 * and reconfigure or destroy the output if necessary.
5366 *
5367 * The destroy callbacks will be called on weston_head destruction before any
5368 * automatic detaching from an associated weston_output and before any
5369 * weston_head information is lost.
5370 *
5371 * The \c data argument to the notify callback is the weston_head being
5372 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005373 *
5374 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005375 */
5376WL_EXPORT void
5377weston_head_add_destroy_listener(struct weston_head *head,
5378 struct wl_listener *listener)
5379{
5380 wl_signal_add(&head->destroy_signal, listener);
5381}
5382
5383/** Look up destroy listener for a head
5384 *
5385 * \param head The head to query.
5386 * \param notify The notify function used used for the added destroy listener.
5387 * \return The listener, or NULL if not found.
5388 *
5389 * This looks up the previously added destroy listener struct based on the
5390 * notify function it has. The listener can be used to access user data
5391 * through \c container_of().
5392 *
5393 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005394 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005395 */
5396WL_EXPORT struct wl_listener *
5397weston_head_get_destroy_listener(struct weston_head *head,
5398 wl_notify_func_t notify)
5399{
5400 return wl_signal_get(&head->destroy_signal, notify);
5401}
5402
David Fort0de859e2016-05-27 23:22:57 +02005403/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005404static void
David Fort0de859e2016-05-27 23:22:57 +02005405weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5406 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005407{
5408 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005409 bool start_resizing = false;
5410
5411 if (!delta_width)
5412 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005413
5414 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005415 if (output == resized_output) {
5416 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005417 continue;
5418 }
5419
David Fort0de859e2016-05-27 23:22:57 +02005420 if (start_resizing) {
5421 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005422 output->dirty = 1;
5423 }
5424 }
5425}
5426
Pekka Paalanend72bad22017-03-29 17:01:41 +03005427static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005428weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005429{
Scott Moreau850ca422012-05-21 15:21:25 -06005430 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005431
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005432 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005433 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005434
Scott Moreauccbf29d2012-02-22 14:21:41 -07005435 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005436 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005437 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005438 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005439 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005440 weston_matrix_scale(&output->matrix, magnification,
5441 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005442 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005443
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005444 switch (output->transform) {
5445 case WL_OUTPUT_TRANSFORM_FLIPPED:
5446 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5447 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5448 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5449 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5450 weston_matrix_scale(&output->matrix, -1, 1, 1);
5451 break;
5452 }
5453
5454 switch (output->transform) {
5455 default:
5456 case WL_OUTPUT_TRANSFORM_NORMAL:
5457 case WL_OUTPUT_TRANSFORM_FLIPPED:
5458 break;
5459 case WL_OUTPUT_TRANSFORM_90:
5460 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5461 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5462 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5463 break;
5464 case WL_OUTPUT_TRANSFORM_180:
5465 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5466 weston_matrix_translate(&output->matrix,
5467 -output->width, -output->height, 0);
5468 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5469 break;
5470 case WL_OUTPUT_TRANSFORM_270:
5471 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5472 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5473 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5474 break;
5475 }
5476
5477 if (output->current_scale != 1)
5478 weston_matrix_scale(&output->matrix,
5479 output->current_scale,
5480 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005481
Scott Moreauccbf29d2012-02-22 14:21:41 -07005482 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005483
5484 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005485}
5486
Scott Moreau1bad5db2012-08-18 01:04:05 -06005487static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005488weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005489{
5490 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005491 output->native_scale = scale;
5492 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005493
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005494 convert_size_by_transform_scale(&output->width, &output->height,
5495 output->current_mode->width,
5496 output->current_mode->height,
5497 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005498}
5499
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005500static void
5501weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005502{
5503 output->x = x;
5504 output->y = y;
5505
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005506 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005507 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005508
5509 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005510 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005511 output->width,
5512 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005513}
5514
Marius Vlad55d87362019-06-11 01:15:35 +03005515/**
5516 * \ingroup output
5517 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005518WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005519weston_output_move(struct weston_output *output, int x, int y)
5520{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005521 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005522 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005523 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005524
5525 output->move_x = x - output->x;
5526 output->move_y = y - output->y;
5527
5528 if (output->move_x == 0 && output->move_y == 0)
5529 return;
5530
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005531 weston_output_init_geometry(output, x, y);
5532
5533 output->dirty = 1;
5534
5535 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005536 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005537
5538 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005539 wl_list_for_each(head, &output->head_list, output_link) {
5540 wl_resource_for_each(resource, &head->resource_list) {
5541 wl_output_send_geometry(resource,
5542 output->x,
5543 output->y,
5544 head->mm_width,
5545 head->mm_height,
5546 head->subpixel,
5547 head->make,
5548 head->model,
5549 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005550
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005551 ver = wl_resource_get_version(resource);
5552 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5553 wl_output_send_done(resource);
5554 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005555 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005556}
5557
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005558/** Signal that a pending output is taken into use.
5559 *
5560 * Removes the output from the pending list and adds it to the compositor's
5561 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005562 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005563 * The output gets an internal ID assigned, and the wl_output global is
5564 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005565 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005566 * \param compositor The compositor instance.
5567 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005568 *
5569 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005570 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005571static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005572weston_compositor_add_output(struct weston_compositor *compositor,
5573 struct weston_output *output)
5574{
Armin Krezoviće5403842016-08-05 15:28:29 +02005575 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005576 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005577
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005578 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005579
5580 /* Verify we haven't reached the limit of 32 available output IDs */
5581 assert(ffs(~compositor->output_id_pool) > 0);
5582
5583 /* Invert the output id pool and look for the lowest numbered
5584 * switch (the least significant bit). Take that bit's position
5585 * as our ID, and mark it used in the compositor's output_id_pool.
5586 */
5587 output->id = ffs(~compositor->output_id_pool) - 1;
5588 compositor->output_id_pool |= 1u << output->id;
5589
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005590 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005591 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005592 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005593
Pekka Paalanendcac3512017-12-08 14:13:34 +02005594 wl_list_for_each(head, &output->head_list, output_link)
5595 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005596
Giulio Camuffob1147152015-05-06 21:41:57 +03005597 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005598
5599 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5600 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005601}
5602
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005603/** Transform device coordinates into global coordinates
5604 *
Marius Vlada2dace22019-06-12 16:05:44 +03005605 * \param output the weston_output object
5606 * \param[in] device_x X coordinate in device units.
5607 * \param[in] device_y Y coordinate in device units.
5608 * \param[out] x X coordinate in the global space.
5609 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005610 *
Marius Vlada2dace22019-06-12 16:05:44 +03005611 * Transforms coordinates from the device coordinate space (physical pixel
5612 * units) to the global coordinate space (logical pixel units). This takes
5613 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005614 *
Marius Vlad55d87362019-06-11 01:15:35 +03005615 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005616 * \internal
5617 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005618WL_EXPORT void
5619weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005620 double device_x, double device_y,
5621 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005622{
Derek Foreman0f679412014-10-02 13:41:17 -05005623 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005624 device_x,
5625 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005626 0.0,
5627 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005628
Derek Foreman67a18b92015-03-24 11:36:14 -05005629 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005630
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005631 *x = p.f[0] / p.f[3];
5632 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005633}
5634
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005635/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005636 *
5637 * \param output The weston_output object that is being removed.
5638 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005639 * The following happens:
5640 *
5641 * - The output assignments of all views in the current scenegraph are
5642 * recomputed.
5643 *
5644 * - Presentation feedback is discarded.
5645 *
5646 * - Compositor is notified that outputs were changed and
5647 * applies the necessary changes to re-layout outputs.
5648 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005649 * - The output is put back in the pending outputs list.
5650 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005651 * - Signal is emitted to notify all users of the weston_output
5652 * object that the output is being destroyed.
5653 *
5654 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005655 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005656 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005657 * - The output's internal ID is released.
5658 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005659 * \memberof weston_output
5660 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005661 */
5662static void
5663weston_compositor_remove_output(struct weston_output *output)
5664{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005665 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005666 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005667 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005668
5669 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005670 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005671
Pekka Paalanenbccda712017-03-29 16:16:04 +03005672 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005673 if (view->output_mask & (1u << output->id))
5674 weston_view_assign_output(view);
5675 }
5676
5677 weston_presentation_feedback_discard_list(&output->feedback_list);
5678
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005679 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005680
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005681 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005682 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005683 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005684
Pekka Paalanenbccda712017-03-29 16:16:04 +03005685 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005686 wl_signal_emit(&output->destroy_signal, output);
5687
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005688 wl_list_for_each(head, &output->head_list, output_link)
5689 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005690
5691 compositor->output_id_pool &= ~(1u << output->id);
5692 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005693}
5694
5695/** Sets the output scale for a given output.
5696 *
5697 * \param output The weston_output object that the scale is set for.
5698 * \param scale Scale factor for the given output.
5699 *
5700 * It only supports setting scale for an output that
5701 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005702 *
Marius Vlad55d87362019-06-11 01:15:35 +03005703 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005704 */
5705WL_EXPORT void
5706weston_output_set_scale(struct weston_output *output,
5707 int32_t scale)
5708{
5709 /* We can only set scale on a disabled output */
5710 assert(!output->enabled);
5711
5712 /* We only want to set scale once */
5713 assert(!output->scale);
5714
5715 output->scale = scale;
5716}
5717
5718/** Sets the output transform for a given output.
5719 *
5720 * \param output The weston_output object that the transform is set for.
5721 * \param transform Transform value for the given output.
5722 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005723 * Refer to wl_output::transform section located at
5724 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5725 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005726 *
Marius Vlad55d87362019-06-11 01:15:35 +03005727 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005728 */
5729WL_EXPORT void
5730weston_output_set_transform(struct weston_output *output,
5731 uint32_t transform)
5732{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005733 struct weston_pointer_motion_event ev;
5734 struct wl_resource *resource;
5735 struct weston_seat *seat;
5736 pixman_region32_t old_region;
5737 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005738 struct weston_head *head;
5739 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005740
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005741 if (!output->enabled && output->transform == UINT32_MAX) {
5742 output->transform = transform;
5743 return;
5744 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005745
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005746 weston_output_transform_scale_init(output, transform, output->scale);
5747
5748 pixman_region32_init(&old_region);
5749 pixman_region32_copy(&old_region, &output->region);
5750
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005751 weston_output_init_geometry(output, output->x, output->y);
5752
5753 output->dirty = 1;
5754
5755 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005756 wl_list_for_each(head, &output->head_list, output_link) {
5757 wl_resource_for_each(resource, &head->resource_list) {
5758 wl_output_send_geometry(resource,
5759 output->x,
5760 output->y,
5761 head->mm_width,
5762 head->mm_height,
5763 head->subpixel,
5764 head->make,
5765 head->model,
5766 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005767
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005768 ver = wl_resource_get_version(resource);
5769 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5770 wl_output_send_done(resource);
5771 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005772 }
5773
5774 /* we must ensure that pointers are inside output, otherwise they disappear */
5775 mid_x = output->x + output->width / 2;
5776 mid_y = output->y + output->height / 2;
5777
5778 ev.mask = WESTON_POINTER_MOTION_ABS;
5779 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5780 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5781
5782 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5783 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5784
5785 if (pointer && pixman_region32_contains_point(&old_region,
5786 wl_fixed_to_int(pointer->x),
5787 wl_fixed_to_int(pointer->y),
5788 NULL))
5789 weston_pointer_move(pointer, &ev);
5790 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005791}
5792
5793/** Initializes a weston_output object with enough data so
5794 ** an output can be configured.
5795 *
5796 * \param output The weston_output object to initialize
5797 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005798 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005799 *
5800 * Sets initial values for fields that are expected to be
5801 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005802 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005803 * The name is used in logs, and can be used by compositors as a configuration
5804 * identifier.
5805 *
Marius Vlad55d87362019-06-11 01:15:35 +03005806 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005807 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005808 */
5809WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005810weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005811 struct weston_compositor *compositor,
5812 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005813{
5814 output->compositor = compositor;
5815 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005816 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005817 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005818 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005819 output->enabled = false;
5820
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005821 wl_list_init(&output->head_list);
5822
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005823 /* Add some (in)sane defaults which can be used
5824 * for checking if an output was properly configured
5825 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005826 output->scale = 0;
5827 /* Can't use -1 on uint32_t and 0 is valid enum value */
5828 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005829
5830 pixman_region32_init(&output->previous_damage);
5831 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005832 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005833}
5834
5835/** Adds weston_output object to pending output list.
5836 *
5837 * \param output The weston_output object to add
5838 * \param compositor The compositor instance.
5839 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005840 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005841 *
Marius Vlad55d87362019-06-11 01:15:35 +03005842 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005843 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005844 */
5845WL_EXPORT void
5846weston_compositor_add_pending_output(struct weston_output *output,
5847 struct weston_compositor *compositor)
5848{
Pekka Paalanene952a012017-03-29 17:14:00 +03005849 assert(output->disable);
5850 assert(output->enable);
5851
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005852 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005853 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005854}
5855
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005856/** Create a string with the attached heads' names.
5857 *
5858 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03005859 *
5860 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005861 */
5862static char *
5863weston_output_create_heads_string(struct weston_output *output)
5864{
5865 FILE *fp;
5866 char *str = NULL;
5867 size_t size = 0;
5868 struct weston_head *head;
5869 const char *sep = "";
5870
5871 fp = open_memstream(&str, &size);
5872 if (!fp)
5873 return NULL;
5874
5875 wl_list_for_each(head, &output->head_list, output_link) {
5876 fprintf(fp, "%s%s", sep, head->name);
5877 sep = ", ";
5878 }
5879 fclose(fp);
5880
5881 return str;
5882}
5883
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005884/** Constructs a weston_output object that can be used by the compositor.
5885 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005886 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005887 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005888 *
5889 * Output coordinates are calculated and each new output is by default
5890 * assigned to the right of previous one.
5891 *
5892 * Sets up the transformation, zoom, and geometry of the output using
5893 * the properties that need to be configured by the compositor.
5894 *
5895 * Establishes a repaint timer for the output with the relevant display
5896 * object's event loop. See output_repaint_timer_handler().
5897 *
5898 * The output is assigned an ID. Weston can support up to 32 distinct
5899 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5900 * is referred to and used to find the first available ID number, and
5901 * then this ID is marked as used in output_id_pool.
5902 *
5903 * The output is also assigned a Wayland global with the wl_output
5904 * external interface.
5905 *
5906 * Backend specific function is called to set up the output output.
5907 *
5908 * Output is added to the compositor's output list
5909 *
5910 * If the backend specific function fails, the weston_output object
5911 * is returned to a state it was before calling this function and
5912 * is added to the compositor's pending_output_list in case it needs
5913 * to be reconfigured or just so it can be destroyed at shutdown.
5914 *
5915 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03005916 *
5917 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005918 */
5919WL_EXPORT int
5920weston_output_enable(struct weston_output *output)
5921{
Armin Krezović782f5df2016-09-30 14:11:11 +02005922 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005923 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005924 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005925 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005926 int x = 0, y = 0;
5927
Pekka Paalanencc201e42017-03-30 15:11:25 +03005928 if (output->enabled) {
5929 weston_log("Error: attempt to enable an enabled output '%s'\n",
5930 output->name);
5931 return -1;
5932 }
5933
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005934 if (wl_list_empty(&output->head_list)) {
5935 weston_log("Error: cannot enable output '%s' without heads.\n",
5936 output->name);
5937 return -1;
5938 }
5939
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005940 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5941 weston_log("Error: no video mode for output '%s'.\n",
5942 output->name);
5943 return -1;
5944 }
5945
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005946 wl_list_for_each(head, &output->head_list, output_link) {
5947 assert(head->make);
5948 assert(head->model);
5949 }
5950
Armin Krezović782f5df2016-09-30 14:11:11 +02005951 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005952 struct weston_output, link);
5953
Armin Krezović782f5df2016-09-30 14:11:11 +02005954 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005955 x = iterator->x + iterator->width;
5956
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005957 /* Make sure the scale is set up */
5958 assert(output->scale);
5959
5960 /* Make sure we have a transform set */
5961 assert(output->transform != UINT32_MAX);
5962
Armin Krezović782f5df2016-09-30 14:11:11 +02005963 output->x = x;
5964 output->y = y;
5965 output->dirty = 1;
5966 output->original_scale = output->scale;
5967
5968 weston_output_transform_scale_init(output, output->transform, output->scale);
5969 weston_output_init_zoom(output);
5970
5971 weston_output_init_geometry(output, x, y);
5972 weston_output_damage(output);
5973
5974 wl_signal_init(&output->frame_signal);
5975 wl_signal_init(&output->destroy_signal);
5976 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005977 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005978
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005979 /* Enable the output (set up the crtc or create a
5980 * window representing the output, set up the
5981 * renderer, etc)
5982 */
5983 if (output->enable(output) < 0) {
5984 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005985 return -1;
5986 }
5987
5988 weston_compositor_add_output(output->compositor, output);
5989
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005990 head_names = weston_output_create_heads_string(output);
5991 weston_log("Output '%s' enabled with head(s) %s\n",
5992 output->name, head_names);
5993 free(head_names);
5994
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005995 return 0;
5996}
5997
5998/** Converts a weston_output object to a pending output state, so it
5999 ** can be configured again or destroyed.
6000 *
6001 * \param output The weston_output object that needs to be disabled.
6002 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006003 * Calls a backend specific function to disable an output, in case
6004 * such function exists.
6005 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006006 * The backend specific disable function may choose to postpone the disabling
6007 * by returning a negative value, in which case this function returns early.
6008 * In that case the backend will guarantee the output will be disabled soon
6009 * by the backend calling this function again. One must not attempt to re-enable
6010 * the output until that happens.
6011 *
6012 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006013 * from weston's output_list (see weston_compositor_remove_output())
6014 * and is returned to a state it was before weston_output_enable()
6015 * was ran (see weston_output_enable_undo()).
6016 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006017 * See weston_output_init() for more information on the
6018 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006019 *
6020 * If the output has never been enabled yet, this function can still be
6021 * called to ensure that the output is actually turned off rather than left
6022 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006023 *
6024 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006025 */
6026WL_EXPORT void
6027weston_output_disable(struct weston_output *output)
6028{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006029 /* Should we rename this? */
6030 output->destroying = 1;
6031
Pekka Paalanenc65df642017-03-29 15:45:46 +03006032 /* Disable is called unconditionally also for not-enabled outputs,
6033 * because at compositor start-up, if there is an output that is
6034 * already on but the compositor wants to turn it off, we have to
6035 * forward the turn-off to the backend so it knows to do it.
6036 * The backend cannot initially turn off everything, because it
6037 * would cause unnecessary mode-sets for all outputs the compositor
6038 * wants to be on.
6039 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006040 if (output->disable(output) < 0)
6041 return;
6042
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006043 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006044 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006045
6046 output->destroying = 0;
6047}
6048
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006049/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006050 *
6051 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006052 *
6053 * If there are new or changed heads, calls the heads_changed hook and
6054 * returns after the hook returns.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006055 */
6056WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006057weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006058{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006059 if (compositor->heads_changed_source) {
6060 wl_event_source_remove(compositor->heads_changed_source);
6061 weston_compositor_call_heads_changed(compositor);
6062 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006063}
6064
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006065/** Add destroy callback for an output
6066 *
6067 * \param output The output to watch.
6068 * \param listener The listener to add. The \c notify member must be set.
6069 *
6070 * The listener callback will be called when user destroys an output. This
6071 * may be delayed by a backend in some cases. The main purpose of the
6072 * listener is to allow hooking up custom data to the output. The custom data
6073 * can be fetched via weston_output_get_destroy_listener() followed by
6074 * container_of().
6075 *
6076 * The \c data argument to the notify callback is the weston_output being
6077 * destroyed.
6078 *
6079 * @note This is for the final destruction of an output, not when it gets
6080 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006081 *
6082 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006083 */
6084WL_EXPORT void
6085weston_output_add_destroy_listener(struct weston_output *output,
6086 struct wl_listener *listener)
6087{
6088 wl_signal_add(&output->user_destroy_signal, listener);
6089}
6090
6091/** Look up destroy listener for an output
6092 *
6093 * \param output The output to query.
6094 * \param notify The notify function used used for the added destroy listener.
6095 * \return The listener, or NULL if not found.
6096 *
6097 * This looks up the previously added destroy listener struct based on the
6098 * notify function it has. The listener can be used to access user data
6099 * through \c container_of().
6100 *
6101 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006102 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006103 */
6104WL_EXPORT struct wl_listener *
6105weston_output_get_destroy_listener(struct weston_output *output,
6106 wl_notify_func_t notify)
6107{
6108 return wl_signal_get(&output->user_destroy_signal, notify);
6109}
6110
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006111/** Uninitialize an output
6112 *
6113 * Removes the output from the list of enabled outputs if necessary, but
6114 * does not call the backend's output disable function. The output will no
6115 * longer be in the list of pending outputs either.
6116 *
6117 * All fields of weston_output become uninitialized, i.e. should not be used
6118 * anymore. The caller can free the memory after this.
6119 *
Marius Vlad55d87362019-06-11 01:15:35 +03006120 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006121 * \internal
6122 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006123WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006124weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006125{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006126 struct weston_head *head, *tmp;
6127
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006128 output->destroying = 1;
6129
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006130 wl_signal_emit(&output->user_destroy_signal, output);
6131
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006132 if (output->idle_repaint_source)
6133 wl_event_source_remove(output->idle_repaint_source);
6134
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006135 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006136 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006137
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006138 pixman_region32_fini(&output->region);
6139 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006140 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006141
6142 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6143 weston_head_detach(head);
6144
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006145 free(output->name);
6146}
6147
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006148/** Find an output by its given name
6149 *
6150 * \param compositor The compositor to search in.
6151 * \param name The output name to search for.
6152 * \return An existing output with the given name, or NULL if not found.
6153 *
6154 * \memberof weston_compositor
6155 */
6156WL_EXPORT struct weston_output *
6157weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6158 const char *name)
6159{
6160 struct weston_output *output;
6161
6162 wl_list_for_each(output, &compositor->output_list, link)
6163 if (strcmp(output->name, name) == 0)
6164 return output;
6165
6166 wl_list_for_each(output, &compositor->pending_output_list, link)
6167 if (strcmp(output->name, name) == 0)
6168 return output;
6169
6170 return NULL;
6171}
6172
6173/** Create a named output
6174 *
6175 * \param compositor The compositor.
6176 * \param name The name for the output.
6177 * \return A new \c weston_output, or NULL on failure.
6178 *
6179 * This creates a new weston_output that starts with no heads attached.
6180 *
6181 * An output must be configured and it must have at least one head before
6182 * it can be enabled.
6183 *
6184 * \memberof weston_compositor
6185 */
6186WL_EXPORT struct weston_output *
6187weston_compositor_create_output(struct weston_compositor *compositor,
6188 const char *name)
6189{
6190 assert(compositor->backend->create_output);
6191
6192 if (weston_compositor_find_output_by_name(compositor, name)) {
6193 weston_log("Warning: attempted to create an output with a "
6194 "duplicate name '%s'.\n", name);
6195 return NULL;
6196 }
6197
6198 return compositor->backend->create_output(compositor, name);
6199}
6200
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006201/** Create an output for an unused head
6202 *
6203 * \param compositor The compositor.
6204 * \param head The head to attach to the output.
6205 * \return A new \c weston_output, or NULL on failure.
6206 *
6207 * This creates a new weston_output that starts with the given head attached.
6208 * The output inherits the name of the head. The head must not be already
6209 * attached to another output.
6210 *
6211 * An output must be configured before it can be enabled.
6212 *
6213 * \memberof weston_compositor
6214 */
6215WL_EXPORT struct weston_output *
6216weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6217 struct weston_head *head)
6218{
6219 struct weston_output *output;
6220
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006221 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006222 if (!output)
6223 return NULL;
6224
6225 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006226 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006227 return NULL;
6228 }
6229
6230 return output;
6231}
6232
6233/** Destroy an output
6234 *
6235 * \param output The output to destroy.
6236 *
6237 * The heads attached to the given output are detached and become unused again.
6238 *
6239 * It is not necessary to explicitly destroy all outputs at compositor exit.
6240 * weston_compositor_destroy() will automatically destroy any remaining
6241 * outputs.
6242 *
Marius Vlad55d87362019-06-11 01:15:35 +03006243 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006244 */
6245WL_EXPORT void
6246weston_output_destroy(struct weston_output *output)
6247{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006248 output->destroy(output);
6249}
6250
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006251/** When you need a head...
6252 *
6253 * This function is a hack, used until all code has been converted to become
6254 * multi-head aware.
6255 *
6256 * \param output The weston_output whose head to get.
6257 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006258 *
6259 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006260 */
6261WL_EXPORT struct weston_head *
6262weston_output_get_first_head(struct weston_output *output)
6263{
6264 if (wl_list_empty(&output->head_list))
6265 return NULL;
6266
6267 return container_of(output->head_list.next,
6268 struct weston_head, output_link);
6269}
6270
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006271static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006272destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006273{
Jonny Lamb74130762013-11-26 18:19:46 +01006274 struct weston_surface *surface =
6275 wl_resource_get_user_data(resource);
6276
Pekka Paalanen4826f872016-04-22 14:14:38 +03006277 if (!surface)
6278 return;
6279
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006280 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006281 surface->pending.buffer_viewport.buffer.src_width =
6282 wl_fixed_from_int(-1);
6283 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006284 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006285}
6286
6287static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006288viewport_destroy(struct wl_client *client,
6289 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006290{
6291 wl_resource_destroy(resource);
6292}
6293
6294static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006295viewport_set_source(struct wl_client *client,
6296 struct wl_resource *resource,
6297 wl_fixed_t src_x,
6298 wl_fixed_t src_y,
6299 wl_fixed_t src_width,
6300 wl_fixed_t src_height)
6301{
6302 struct weston_surface *surface =
6303 wl_resource_get_user_data(resource);
6304
Pekka Paalanen4826f872016-04-22 14:14:38 +03006305 if (!surface) {
6306 wl_resource_post_error(resource,
6307 WP_VIEWPORT_ERROR_NO_SURFACE,
6308 "wl_surface for this viewport is no longer exists");
6309 return;
6310 }
6311
6312 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006313 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006314
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006315 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006316 src_height == wl_fixed_from_int(-1) &&
6317 src_x == wl_fixed_from_int(-1) &&
6318 src_y == wl_fixed_from_int(-1)) {
6319 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006320 surface->pending.buffer_viewport.buffer.src_width =
6321 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006322 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006323 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006324 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006325
Pekka Paalanen201769a2016-04-26 14:42:11 +03006326 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006327 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006328 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006329 "wl_surface@%d viewport source "
6330 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6331 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006332 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006333 wl_fixed_to_double(src_height),
6334 wl_fixed_to_double(src_x),
6335 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006336 return;
6337 }
6338
6339 surface->pending.buffer_viewport.buffer.src_x = src_x;
6340 surface->pending.buffer_viewport.buffer.src_y = src_y;
6341 surface->pending.buffer_viewport.buffer.src_width = src_width;
6342 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006343 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006344}
6345
6346static void
6347viewport_set_destination(struct wl_client *client,
6348 struct wl_resource *resource,
6349 int32_t dst_width,
6350 int32_t dst_height)
6351{
6352 struct weston_surface *surface =
6353 wl_resource_get_user_data(resource);
6354
Pekka Paalanen4826f872016-04-22 14:14:38 +03006355 if (!surface) {
6356 wl_resource_post_error(resource,
6357 WP_VIEWPORT_ERROR_NO_SURFACE,
6358 "wl_surface for this viewport no longer exists");
6359 return;
6360 }
6361
6362 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006363
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006364 if (dst_width == -1 && dst_height == -1) {
6365 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006366 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006367 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006368 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006369 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006370
6371 if (dst_width <= 0 || dst_height <= 0) {
6372 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006373 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006374 "destination size must be positive (%dx%d)",
6375 dst_width, dst_height);
6376 return;
6377 }
6378
6379 surface->pending.buffer_viewport.surface.width = dst_width;
6380 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006381 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006382}
6383
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006384static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006385 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006386 viewport_set_source,
6387 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006388};
6389
6390static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006391viewporter_destroy(struct wl_client *client,
6392 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006393{
6394 wl_resource_destroy(resource);
6395}
6396
6397static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006398viewporter_get_viewport(struct wl_client *client,
6399 struct wl_resource *viewporter,
6400 uint32_t id,
6401 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006402{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006403 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006404 struct weston_surface *surface =
6405 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006406 struct wl_resource *resource;
6407
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006408 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006409 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006410 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006411 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006412 return;
6413 }
6414
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006415 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006416 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006417 if (resource == NULL) {
6418 wl_client_post_no_memory(client);
6419 return;
6420 }
6421
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006422 wl_resource_set_implementation(resource, &viewport_interface,
6423 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006424
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006425 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006426}
6427
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006428static const struct wp_viewporter_interface viewporter_interface = {
6429 viewporter_destroy,
6430 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006431};
6432
6433static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006434bind_viewporter(struct wl_client *client,
6435 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006436{
6437 struct wl_resource *resource;
6438
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006439 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006440 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006441 if (resource == NULL) {
6442 wl_client_post_no_memory(client);
6443 return;
6444 }
6445
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006446 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006447 NULL, NULL);
6448}
6449
6450static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006451destroy_presentation_feedback(struct wl_resource *feedback_resource)
6452{
6453 struct weston_presentation_feedback *feedback;
6454
6455 feedback = wl_resource_get_user_data(feedback_resource);
6456
6457 wl_list_remove(&feedback->link);
6458 free(feedback);
6459}
6460
6461static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006462presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6463{
6464 wl_resource_destroy(resource);
6465}
6466
6467static void
6468presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006469 struct wl_resource *presentation_resource,
6470 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006471 uint32_t callback)
6472{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006473 struct weston_surface *surface;
6474 struct weston_presentation_feedback *feedback;
6475
6476 surface = wl_resource_get_user_data(surface_resource);
6477
Bryce Harringtonde16d892014-11-20 22:21:57 -08006478 feedback = zalloc(sizeof *feedback);
6479 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006480 goto err_calloc;
6481
6482 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006483 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006484 1, callback);
6485 if (!feedback->resource)
6486 goto err_create;
6487
6488 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6489 destroy_presentation_feedback);
6490
6491 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6492
6493 return;
6494
6495err_create:
6496 free(feedback);
6497
6498err_calloc:
6499 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006500}
6501
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006502static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006503 presentation_destroy,
6504 presentation_feedback
6505};
6506
6507static void
6508bind_presentation(struct wl_client *client,
6509 void *data, uint32_t version, uint32_t id)
6510{
6511 struct weston_compositor *compositor = data;
6512 struct wl_resource *resource;
6513
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006514 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006515 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006516 if (resource == NULL) {
6517 wl_client_post_no_memory(client);
6518 return;
6519 }
6520
6521 wl_resource_set_implementation(resource, &presentation_implementation,
6522 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006523 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006524}
6525
6526static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006527compositor_bind(struct wl_client *client,
6528 void *data, uint32_t version, uint32_t id)
6529{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006530 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006531 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006532
Jason Ekstranda85118c2013-06-27 20:17:02 -05006533 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006534 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006535 if (resource == NULL) {
6536 wl_client_post_no_memory(client);
6537 return;
6538 }
6539
6540 wl_resource_set_implementation(resource, &compositor_interface,
6541 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006542}
6543
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006544WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006545weston_environment_get_fd(const char *env)
6546{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006547 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006548 int fd, flags;
6549
6550 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006551 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006552 return -1;
6553
6554 flags = fcntl(fd, F_GETFD);
6555 if (flags == -1)
6556 return -1;
6557
6558 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6559 unsetenv(env);
6560
6561 return fd;
6562}
6563
Pekka Paalanenb5026542014-11-12 15:09:24 +02006564static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006565timeline_key_binding_handler(struct weston_keyboard *keyboard,
6566 const struct timespec *time, uint32_t key,
6567 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006568{
6569 struct weston_compositor *compositor = data;
6570
6571 if (weston_timeline_enabled_)
6572 weston_timeline_close();
6573 else
6574 weston_timeline_open(compositor);
6575}
6576
Daniel Stonece62cb32018-07-20 09:46:24 +01006577static const char *
6578output_repaint_status_text(struct weston_output *output)
6579{
6580 switch (output->repaint_status) {
6581 case REPAINT_NOT_SCHEDULED:
6582 return "no repaint";
6583 case REPAINT_BEGIN_FROM_IDLE:
6584 return "start_repaint_loop scheduled";
6585 case REPAINT_SCHEDULED:
6586 return "repaint scheduled";
6587 case REPAINT_AWAITING_COMPLETION:
6588 return "awaiting completion";
6589 }
6590
6591 assert(!"output_repaint_status_text missing enum");
6592 return NULL;
6593}
6594
6595static void
6596debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6597{
6598 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6599 struct wl_shm_buffer *shm;
6600 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006601 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006602
6603 if (!buffer) {
6604 fprintf(fp, "\t\t[buffer not available]\n");
6605 return;
6606 }
6607
6608 shm = wl_shm_buffer_get(buffer->resource);
6609 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006610 uint32_t _format = wl_shm_buffer_get_format(shm);
6611 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006612 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006613 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6614 (unsigned long) _format,
6615 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006616 return;
6617 }
6618
6619 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6620 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006621 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006622 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006623 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6624 (unsigned long) dmabuf->attributes.format,
6625 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006626 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6627 (unsigned long long) dmabuf->attributes.modifier[0]);
6628 return;
6629 }
6630
Marius Vlad253ba9a2019-03-17 18:22:21 +02006631 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006632}
6633
6634static void
6635debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6636{
6637 struct weston_compositor *ec = view->surface->compositor;
6638 struct weston_output *output;
6639 char desc[512];
6640 pixman_box32_t *box;
6641 uint32_t surface_id = 0;
6642 pid_t pid = 0;
6643
6644 if (view->surface->resource) {
6645 struct wl_resource *resource = view->surface->resource;
6646 wl_client_get_credentials(wl_resource_get_client(resource),
6647 &pid, NULL, NULL);
6648 surface_id = wl_resource_get_id(view->surface->resource);
6649 }
6650
6651 if (!view->surface->get_label ||
6652 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6653 strcpy(desc, "[no description available]");
6654 }
6655 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6656 view_idx, view->surface->role_name, pid, surface_id,
6657 desc, view);
6658
6659 box = pixman_region32_extents(&view->transform.boundingbox);
6660 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6661 box->x1, box->y1, box->x2, box->y2);
6662 box = pixman_region32_extents(&view->transform.opaque);
6663
6664 if (pixman_region32_equal(&view->transform.opaque,
6665 &view->transform.boundingbox)) {
6666 fprintf(fp, "\t\t[fully opaque]\n");
6667 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6668 fprintf(fp, "\t\t[not opaque]\n");
6669 } else {
6670 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6671 box->x1, box->y1, box->x2, box->y2);
6672 }
6673
6674 if (view->alpha < 1.0)
6675 fprintf(fp, "\t\talpha: %f\n", view->alpha);
6676
6677 if (view->output_mask != 0) {
6678 bool first_output = true;
6679 fprintf(fp, "\t\toutputs: ");
6680 wl_list_for_each(output, &ec->output_list, link) {
6681 if (!(view->output_mask & (1 << output->id)))
6682 continue;
6683 fprintf(fp, "%s%d (%s)%s",
6684 (first_output) ? "" : ", ",
6685 output->id, output->name,
6686 (view->output == output) ? " (primary)" : "");
6687 first_output = false;
6688 }
6689 } else {
6690 fprintf(fp, "\t\t[no outputs]");
6691 }
6692
6693 fprintf(fp, "\n");
6694
6695 debug_scene_view_print_buffer(fp, view);
6696}
6697
Marius Vlad433f4e72019-02-17 22:14:23 +02006698static void
6699debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02006700 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02006701{
6702 struct weston_subsurface *sub;
6703 struct weston_view *ev;
6704
6705 /*
6706 * print the view first, then we recursively go on printing
6707 * sub-surfaces. We bail out once no more sub-surfaces are available.
6708 */
Marius Vlada6acfa82019-03-17 18:10:09 +02006709 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02006710
6711 /* no more sub-surfaces */
6712 if (wl_list_empty(&view->surface->subsurface_list))
6713 return;
6714
6715 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
6716 wl_list_for_each(ev, &sub->surface->views, surface_link) {
6717 /* do not print again the parent view */
6718 if (view == ev)
6719 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02006720
6721 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02006722 debug_scene_view_print_tree(ev, fp, view_idx);
6723 }
6724 }
6725}
6726
Daniel Stonece62cb32018-07-20 09:46:24 +01006727/**
6728 * Output information on how libweston is currently composing the scene
6729 * graph.
6730 */
6731WL_EXPORT char *
6732weston_compositor_print_scene_graph(struct weston_compositor *ec)
6733{
6734 struct weston_output *output;
6735 struct weston_layer *layer;
6736 struct timespec now;
6737 int layer_idx = 0;
6738 FILE *fp;
6739 char *ret;
6740 size_t len;
6741 int err;
6742
6743 fp = open_memstream(&ret, &len);
6744 assert(fp);
6745
6746 weston_compositor_read_presentation_clock(ec, &now);
6747 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
6748 now.tv_sec, now.tv_nsec);
6749
6750 wl_list_for_each(output, &ec->output_list, link) {
6751 struct weston_head *head;
6752 int head_idx = 0;
6753
6754 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
6755 assert(output->enabled);
6756
6757 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
6758 output->x, output->y,
6759 output->x + output->width,
6760 output->y + output->height);
6761 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
6762 output->current_mode->width,
6763 output->current_mode->height,
6764 output->current_mode->refresh / 1000.0);
6765 fprintf(fp, "\tscale: %d\n", output->scale);
6766
6767 fprintf(fp, "\trepaint status: %s\n",
6768 output_repaint_status_text(output));
6769 if (output->repaint_status == REPAINT_SCHEDULED)
6770 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
6771 output->next_repaint.tv_sec,
6772 output->next_repaint.tv_nsec);
6773
6774 wl_list_for_each(head, &output->head_list, output_link) {
6775 fprintf(fp, "\tHead %d (%s): %sconnected\n",
6776 head_idx++, head->name,
6777 (head->connected) ? "" : "not ");
6778 }
6779 }
6780
6781 fprintf(fp, "\n");
6782
6783 wl_list_for_each(layer, &ec->layer_list, link) {
6784 struct weston_view *view;
6785 int view_idx = 0;
6786
6787 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
6788 (unsigned long) layer->position);
6789
6790 if (!weston_layer_mask_is_infinite(layer)) {
6791 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
6792 layer->mask.x1, layer->mask.y1,
6793 layer->mask.x2, layer->mask.y2);
6794 }
6795
Marius Vlada6acfa82019-03-17 18:10:09 +02006796 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
6797 debug_scene_view_print_tree(view, fp, &view_idx);
6798 view_idx++;
6799 }
Daniel Stonece62cb32018-07-20 09:46:24 +01006800
6801 if (wl_list_empty(&layer->view_list.link))
6802 fprintf(fp, "\t[no views]\n");
6803
6804 fprintf(fp, "\n");
6805 }
6806
6807 err = fclose(fp);
6808 assert(err == 0);
6809
6810 return ret;
6811}
6812
6813/**
6814 * Called when the 'scene-graph' debug scope is bound by a client. This
6815 * one-shot weston-debug scope prints the current scene graph when bound,
6816 * and then terminates the stream.
6817 */
6818static void
6819debug_scene_graph_cb(struct weston_debug_stream *stream, void *data)
6820{
6821 struct weston_compositor *ec = data;
6822 char *str = weston_compositor_print_scene_graph(ec);
6823
6824 weston_debug_stream_printf(stream, "%s", str);
6825 free(str);
6826 weston_debug_stream_complete(stream);
6827}
6828
Giulio Camuffo459137b2014-10-11 23:56:24 +03006829/** Create the compositor.
6830 *
6831 * This functions creates and initializes a compositor instance.
6832 *
6833 * \param display The Wayland display to be used.
6834 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03006835 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03006836 * using the \ref weston_compositor_get_user_data function.
6837 * \return The compositor instance on success or NULL on failure.
6838 */
6839WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03006840weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03006841 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03006842 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006843{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006844 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006845 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006846
Giulio Camuffo459137b2014-10-11 23:56:24 +03006847 ec = zalloc(sizeof *ec);
6848 if (!ec)
6849 return NULL;
6850
6851 ec->wl_display = display;
6852 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006853 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006854 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006855 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006856 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006857 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006858 wl_signal_init(&ec->idle_signal);
6859 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006860 wl_signal_init(&ec->show_input_panel_signal);
6861 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006862 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006863 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006864 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006865 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006866 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006867 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006868 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02006869 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006870 wl_signal_init(&ec->session_signal);
6871 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006872
Casey Dahlin58ba1372012-04-19 22:50:08 -04006873 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006874 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006875
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006876 ec->activate_serial = 1;
6877
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05006878 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
6879
Derek Foreman152254b2015-11-26 14:17:48 -06006880 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006881 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006882 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006883
Giulio Camuffo954f1832014-10-11 18:27:30 +03006884 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006885 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006886 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006887
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006888 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006889 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006890 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006891
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006892 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006893 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006894 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006895
Marius Vlad3d7d9782019-04-17 12:35:38 +03006896 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02006897 goto fail;
6898
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006899 if (weston_input_init(ec) != 0)
6900 goto fail;
6901
Jason Ekstranda7af7042013-10-12 22:38:11 -05006902 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006903 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006904 wl_list_init(&ec->layer_list);
6905 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006906 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006907 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006908 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006909 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006910 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006911 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006912 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006913 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006914 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006915
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006916 wl_list_init(&ec->plugin_api_list);
6917
Xiong Zhang97116532013-10-23 13:58:31 +08006918 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006919 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006920
Giulio Camuffo459137b2014-10-11 23:56:24 +03006921 wl_data_device_manager_init(ec->wl_display);
6922
6923 wl_display_init_shm(ec->wl_display);
6924
6925 loop = wl_display_get_event_loop(ec->wl_display);
6926 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006927 ec->repaint_timer =
6928 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6929 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006930
Quentin Glidic82681572016-12-17 13:40:51 +01006931 weston_layer_init(&ec->fade_layer, ec);
6932 weston_layer_init(&ec->cursor_layer, ec);
6933
6934 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6935 weston_layer_set_position(&ec->cursor_layer,
6936 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006937
6938 weston_compositor_add_debug_binding(ec, KEY_T,
6939 timeline_key_binding_handler, ec);
6940
Daniel Stonece62cb32018-07-20 09:46:24 +01006941 ec->debug_scene =
Marius Vlad7e4db952019-04-17 13:47:06 +03006942 weston_compositor_add_log_scope(ec->weston_log_ctx, "scene-graph",
6943 "Scene graph details\n",
6944 debug_scene_graph_cb,
6945 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01006946
Giulio Camuffo459137b2014-10-11 23:56:24 +03006947 return ec;
6948
6949fail:
6950 free(ec);
6951 return NULL;
6952}
6953
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006954WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006955weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006956{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006957 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006958
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006959 wl_event_source_remove(ec->idle_source);
6960
Matt Roper361d2ad2011-08-29 13:52:23 -07006961 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006962 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006963 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006964
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006965 /* Destroy all pending outputs associated with this compositor */
6966 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6967 output->destroy(output);
6968
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006969 if (ec->renderer)
6970 ec->renderer->destroy(ec);
6971
Daniel Stone325fc2d2012-05-30 16:31:58 +01006972 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006973 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006974 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006975 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006976 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006977 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006978
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006979 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006980}
6981
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006982WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006983weston_compositor_exit_with_code(struct weston_compositor *compositor,
6984 int exit_code)
6985{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006986 if (compositor->exit_code == EXIT_SUCCESS)
6987 compositor->exit_code = exit_code;
6988
Giulio Camuffo459137b2014-10-11 23:56:24 +03006989 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006990}
6991
6992WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006993weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6994 const struct weston_pointer_grab_interface *interface)
6995{
6996 struct weston_seat *seat;
6997
6998 ec->default_pointer_grab = interface;
6999 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007000 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7001
7002 if (pointer)
7003 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007004 }
7005}
7006
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007007WL_EXPORT int
7008weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7009 clockid_t clk_id)
7010{
7011 struct timespec ts;
7012
7013 if (clock_gettime(clk_id, &ts) < 0)
7014 return -1;
7015
7016 compositor->presentation_clock = clk_id;
7017
7018 return 0;
7019}
7020
7021/*
7022 * For choosing the software clock, when the display hardware or API
7023 * does not expose a compatible presentation timestamp.
7024 */
7025WL_EXPORT int
7026weston_compositor_set_presentation_clock_software(
7027 struct weston_compositor *compositor)
7028{
7029 /* In order of preference */
7030 static const clockid_t clocks[] = {
7031 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7032 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7033 CLOCK_MONOTONIC, /* no jumps, may crawl */
7034 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7035 CLOCK_REALTIME /* may jump and crawl */
7036 };
7037 unsigned i;
7038
7039 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7040 if (weston_compositor_set_presentation_clock(compositor,
7041 clocks[i]) == 0)
7042 return 0;
7043
7044 weston_log("Error: no suitable presentation clock available.\n");
7045
7046 return -1;
7047}
7048
Pekka Paalanen662f3842015-03-18 12:17:26 +02007049/** Read the current time from the Presentation clock
7050 *
7051 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007052 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007053 *
7054 * \note Reading the current time in user space is always imprecise to some
7055 * degree.
7056 *
7057 * This function is never meant to fail. If reading the clock does fail,
7058 * an error message is logged and a zero time is returned. Callers are not
7059 * supposed to detect or react to failures.
7060 */
7061WL_EXPORT void
7062weston_compositor_read_presentation_clock(
7063 const struct weston_compositor *compositor,
7064 struct timespec *ts)
7065{
7066 static bool warned;
7067 int ret;
7068
7069 ret = clock_gettime(compositor->presentation_clock, ts);
7070 if (ret < 0) {
7071 ts->tv_sec = 0;
7072 ts->tv_nsec = 0;
7073
7074 if (!warned)
7075 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007076 "the presentation clock %#x: '%s' (%d)\n",
7077 compositor->presentation_clock,
7078 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007079 warned = true;
7080 }
7081}
7082
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007083/** Import dmabuf buffer into current renderer
7084 *
7085 * \param compositor
7086 * \param buffer the dmabuf buffer to import
7087 * \return true on usable buffers, false otherwise
7088 *
7089 * This function tests that the linux_dmabuf_buffer is usable
7090 * for the current renderer. Returns false on unusable buffers. Usually
7091 * usability is tested by importing the dmabufs for composition.
7092 *
7093 * This hook is also used for detecting if the renderer supports
7094 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7095 * supported.
7096 * */
7097WL_EXPORT bool
7098weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7099 struct linux_dmabuf_buffer *buffer)
7100{
7101 struct weston_renderer *renderer;
7102
7103 renderer = compositor->renderer;
7104
7105 if (renderer->import_dmabuf == NULL)
7106 return false;
7107
7108 return renderer->import_dmabuf(compositor, buffer);
7109}
7110
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007111WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007112weston_version(int *major, int *minor, int *micro)
7113{
7114 *major = WESTON_VERSION_MAJOR;
7115 *minor = WESTON_VERSION_MINOR;
7116 *micro = WESTON_VERSION_MICRO;
7117}
7118
Daniel Stonee03c1112016-11-24 20:45:45 +00007119/**
7120 * Attempts to find a module path from the module map specified in the
7121 * environment. If found, writes the full path into the path variable.
7122 *
7123 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7124 * each entry is of the form "name=path" and entries are separated by
7125 * semicolons. Whitespace is significant.
7126 *
7127 * \param name The name to search for.
7128 * \param path Where the path is written to if found.
7129 * \param path_len Allocated bytes at \c path .
7130 * \returns The length of the string written to path on success, or 0 if the
7131 * module was not specified in the environment map or path_len was too small.
7132 */
7133WL_EXPORT size_t
7134weston_module_path_from_env(const char *name, char *path, size_t path_len)
7135{
7136 const char *mapping = getenv("WESTON_MODULE_MAP");
7137 const char *end;
7138 const int name_len = strlen(name);
7139
7140 if (!mapping)
7141 return 0;
7142
7143 end = mapping + strlen(mapping);
7144 while (mapping < end && *mapping) {
7145 const char *filename, *next;
7146
7147 /* early out: impossibly short string */
7148 if (end - mapping < name_len + 1)
7149 return 0;
7150
7151 filename = &mapping[name_len + 1];
7152 next = strchrnul(mapping, ';');
7153
7154 if (strncmp(mapping, name, name_len) == 0 &&
7155 mapping[name_len] == '=') {
7156 size_t file_len = next - filename; /* no trailing NUL */
7157 if (file_len >= path_len)
7158 return 0;
7159 strncpy(path, filename, file_len);
7160 path[file_len] = '\0';
7161 return file_len;
7162 }
7163
7164 mapping = next + 1;
7165 }
7166
7167 return 0;
7168}
7169
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007170WL_EXPORT void *
7171weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007172{
7173 char path[PATH_MAX];
7174 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007175 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007176
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007177 if (name == NULL)
7178 return NULL;
7179
Derek Foreman3f86e502015-06-08 11:46:54 -05007180 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007181 len = weston_module_path_from_env(name, path, sizeof path);
7182 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007183 len = snprintf(path, sizeof path, "%s/%s",
7184 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007185 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007186 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007187 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007188
Daniel Stonebeb97e52016-11-28 12:13:54 +00007189 /* snprintf returns the length of the string it would've written,
7190 * _excluding_ the NUL byte. So even being equal to the size of
7191 * our buffer is an error here. */
7192 if (len >= sizeof path)
7193 return NULL;
7194
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007195 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7196 if (module) {
7197 weston_log("Module '%s' already loaded\n", path);
7198 dlclose(module);
7199 return NULL;
7200 }
7201
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007202 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007203 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007204 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007205 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007206 return NULL;
7207 }
7208
7209 init = dlsym(module, entrypoint);
7210 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007211 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007212 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007213 return NULL;
7214 }
7215
7216 return init;
7217}
7218
Giulio Camuffo459137b2014-10-11 23:56:24 +03007219
7220/** Destroys the compositor.
7221 *
7222 * This function cleans up the compositor state and destroys it.
7223 *
7224 * \param compositor The compositor to be destroyed.
7225 */
7226WL_EXPORT void
7227weston_compositor_destroy(struct weston_compositor *compositor)
7228{
7229 /* prevent further rendering while shutting down */
7230 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7231
7232 wl_signal_emit(&compositor->destroy_signal, compositor);
7233
7234 weston_compositor_xkb_destroy(compositor);
7235
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007236 if (compositor->backend)
7237 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007238
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007239 /* The backend is responsible for destroying the heads. */
7240 assert(wl_list_empty(&compositor->head_list));
7241
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007242 weston_plugin_api_destroy_list(compositor);
7243
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007244 if (compositor->heads_changed_source)
7245 wl_event_source_remove(compositor->heads_changed_source);
7246
Marius Vlad7e4db952019-04-17 13:47:06 +03007247 weston_compositor_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007248 compositor->debug_scene = NULL;
Marius Vlad3d7d9782019-04-17 12:35:38 +03007249 weston_log_ctx_compositor_destroy(compositor);
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007250
Giulio Camuffo459137b2014-10-11 23:56:24 +03007251 free(compositor);
7252}
7253
7254/** Instruct the compositor to exit.
7255 *
7256 * This functions does not directly destroy the compositor object, it merely
7257 * command it to start the tear down process. It is not guaranteed that the
7258 * tear down will happen immediately.
7259 *
7260 * \param compositor The compositor to tear down.
7261 */
7262WL_EXPORT void
7263weston_compositor_exit(struct weston_compositor *compositor)
7264{
7265 compositor->exit(compositor);
7266}
7267
7268/** Return the user data stored in the compositor.
7269 *
7270 * This function returns the user data pointer set with user_data parameter
7271 * to the \ref weston_compositor_create function.
7272 */
7273WL_EXPORT void *
7274weston_compositor_get_user_data(struct weston_compositor *compositor)
7275{
7276 return compositor->user_data;
7277}
Pekka Paalanendd186732016-06-03 14:49:54 +03007278
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007279static const char * const backend_map[] = {
7280 [WESTON_BACKEND_DRM] = "drm-backend.so",
7281 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7282 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7283 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7284 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7285 [WESTON_BACKEND_X11] = "x11-backend.so",
7286};
7287
Pekka Paalanendd186732016-06-03 14:49:54 +03007288/** Load a backend into a weston_compositor
7289 *
7290 * A backend must be loaded to make a weston_compositor work. A backend
7291 * provides input and output capabilities, and determines the renderer to use.
7292 *
7293 * \param compositor A compositor that has not had a backend loaded yet.
7294 * \param backend Name of the backend file.
7295 * \param config_base A pointer to a backend-specific configuration
7296 * structure's 'base' member.
7297 *
7298 * \return 0 on success, or -1 on error.
7299 */
7300WL_EXPORT int
7301weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007302 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007303 struct weston_backend_config *config_base)
7304{
7305 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007306 struct weston_backend_config *config_base);
7307
Pekka Paalanend7e35112017-08-29 17:04:12 +03007308 if (compositor->backend) {
7309 weston_log("Error: attempt to load a backend when one is already loaded\n");
7310 return -1;
7311 }
7312
Quentin Glidic887c0182016-07-10 11:00:53 +02007313 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007314 return -1;
7315
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007316 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007317 if (!backend_init)
7318 return -1;
7319
Pekka Paalanend7e35112017-08-29 17:04:12 +03007320 if (backend_init(compositor, config_base) < 0) {
7321 compositor->backend = NULL;
7322 return -1;
7323 }
7324
7325 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007326}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007327
7328WL_EXPORT int
7329weston_compositor_load_xwayland(struct weston_compositor *compositor)
7330{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007331 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007332
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007333 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007334 if (!module_init)
7335 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007336 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007337 return -1;
7338 return 0;
7339}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007340
7341/** Resolve an internal compositor error by disconnecting the client.
7342 *
7343 * This function is used in cases when the wl_buffer turns out
7344 * unusable and there is no fallback path.
7345 *
7346 * It is possible the fault is caused by a compositor bug, the underlying
7347 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7348 * In any case, the options are to either composite garbage or nothing,
7349 * or disconnect the client. This is a helper function for the latter.
7350 *
7351 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7352 *
7353 * \param buffer The weston buffer that is unusable.
7354 * \param msg A custom error message attached to the protocol error.
7355 */
7356WL_EXPORT void
7357weston_buffer_send_server_error(struct weston_buffer *buffer,
7358 const char *msg)
7359{
7360 struct wl_client *client;
7361 struct wl_resource *display_resource;
7362 uint32_t id;
7363
7364 assert(buffer->resource);
7365 id = wl_resource_get_id(buffer->resource);
7366 client = wl_resource_get_client(buffer->resource);
7367 display_resource = wl_client_get_object(client, 1);
7368
7369 assert(display_resource);
7370 wl_resource_post_error(display_resource,
7371 WL_DISPLAY_ERROR_INVALID_OBJECT,
7372 "server error with "
7373 "wl_buffer@%u: %s", id, msg);
7374}