blob: 68bf11c62693999401e75bf9f48e5a6a5de76c97 [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 Paalanenc647ed72015-02-09 13:16:57 +02004 * Copyright © 2012-2015 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050026 */
27
Kristian Høgsberga9410222011-01-14 17:22:35 -050028#include "config.h"
29
Daniel Stoneb7452fe2012-06-01 12:14:06 +010030#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010035#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020037#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040038#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010039#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040040#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020041#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020042#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020043#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050045#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040046#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040047#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040048#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050049#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040050#include <sys/time.h>
51#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030052#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050053
Pekka Paalanenb5026542014-11-12 15:09:24 +020054#include "timeline.h"
55
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030057#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020058#include "presentation-time-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070059#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070060#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070061#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030062#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040063#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050064#include "version.h"
Pekka Paalanen827b5d22016-06-29 11:54:26 +020065#include "plugin-registry.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050066
Pekka Paalanen0513a952014-05-21 16:17:27 +030067#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
68
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020069static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030070weston_output_update_matrix(struct weston_output *output);
71
72static void
Alexander Larsson0b135062013-05-28 16:23:36 +020073weston_output_transform_scale_init(struct weston_output *output,
74 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020075
Rob Bradford27b17932013-06-26 18:08:46 +010076static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050077weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010078
Pekka Paalanen6528c032017-03-28 15:27:10 +030079/** Send wl_output events for mode and scale changes
80 *
81 * \param head Send on all resources bound to this head.
82 * \param mode_changed If true, send the current mode.
83 * \param scale_changed If true, send the current scale.
84 */
85static void
86weston_mode_switch_send_events(struct weston_head *head,
87 bool mode_changed, bool scale_changed)
88{
89 struct weston_output *output = head->output;
90 struct wl_resource *resource;
91 int version;
92
93 wl_resource_for_each(resource, &head->resource_list) {
94 if (mode_changed) {
95 wl_output_send_mode(resource,
96 output->current_mode->flags,
97 output->current_mode->width,
98 output->current_mode->height,
99 output->current_mode->refresh);
100 }
101
102 version = wl_resource_get_version(resource);
103 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
104 wl_output_send_scale(resource, output->current_scale);
105
106 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
107 wl_output_send_done(resource);
108 }
109}
110
111static void
112weston_mode_switch_finish(struct weston_output *output,
113 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800114{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200115 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300116 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200117 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200118
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200119 pixman_region32_init(&old_output_region);
120 pixman_region32_copy(&old_output_region, &output->region);
121
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200122 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200123 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200124
125 pixman_region32_init(&output->previous_damage);
126 pixman_region32_init_rect(&output->region, output->x, output->y,
127 output->width, output->height);
128
129 weston_output_update_matrix(output);
130
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200131 /* If a pointer falls outside the outputs new geometry, move it to its
132 * lower-right corner */
133 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500134 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200135 int32_t x, y;
136
137 if (!pointer)
138 continue;
139
140 x = wl_fixed_to_int(pointer->x);
141 y = wl_fixed_to_int(pointer->y);
142
143 if (!pixman_region32_contains_point(&old_output_region,
144 x, y, NULL) ||
145 pixman_region32_contains_point(&output->region,
146 x, y, NULL))
147 continue;
148
149 if (x >= output->x + output->width)
150 x = output->x + output->width - 1;
151 if (y >= output->y + output->height)
152 y = output->y + output->height - 1;
153
154 pointer->x = wl_fixed_from_int(x);
155 pointer->y = wl_fixed_from_int(y);
156 }
157
158 pixman_region32_fini(&old_output_region);
159
Derek Foremandd4cd332014-11-10 10:29:59 -0600160 if (!mode_changed && !scale_changed)
161 return;
162
Hardening57388e42013-09-18 23:56:36 +0200163 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300164 wl_list_for_each(head, &output->head_list, output_link)
165 weston_mode_switch_send_events(head,
166 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600167}
168
David Fort0de859e2016-05-27 23:22:57 +0200169static void
170weston_compositor_reflow_outputs(struct weston_compositor *compositor,
171 struct weston_output *resized_output, int delta_width);
172
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600173WL_EXPORT int
174weston_output_mode_set_native(struct weston_output *output,
175 struct weston_mode *mode,
176 int32_t scale)
177{
178 int ret;
179 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200180 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600181
182 if (!output->switch_mode)
183 return -1;
184
185 if (!output->original_mode) {
186 mode_changed = 1;
187 ret = output->switch_mode(output, mode);
188 if (ret < 0)
189 return ret;
190 if (output->current_scale != scale) {
191 scale_changed = 1;
192 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200193 }
194 }
195
David Fort0de859e2016-05-27 23:22:57 +0200196 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600197 output->native_mode = mode;
198 output->native_scale = scale;
199
200 weston_mode_switch_finish(output, mode_changed, scale_changed);
201
David Fort0de859e2016-05-27 23:22:57 +0200202 if (mode_changed || scale_changed) {
203 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
204
205 wl_signal_emit(&output->compositor->output_resized_signal, output);
206 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600207 return 0;
208}
209
210WL_EXPORT int
211weston_output_mode_switch_to_native(struct weston_output *output)
212{
213 int ret;
214 int mode_changed = 0, scale_changed = 0;
215
216 if (!output->switch_mode)
217 return -1;
218
219 if (!output->original_mode) {
220 weston_log("already in the native mode\n");
221 return -1;
222 }
223 /* the non fullscreen clients haven't seen a mode set since we
224 * switched into a temporary, so we need to notify them if the
225 * mode at that time is different from the native mode now.
226 */
227 mode_changed = (output->original_mode != output->native_mode);
228 scale_changed = (output->original_scale != output->native_scale);
229
230 ret = output->switch_mode(output, output->native_mode);
231 if (ret < 0)
232 return ret;
233
234 output->current_scale = output->native_scale;
235
236 output->original_mode = NULL;
237 output->original_scale = 0;
238
239 weston_mode_switch_finish(output, mode_changed, scale_changed);
240
241 return 0;
242}
243
244WL_EXPORT int
245weston_output_mode_switch_to_temporary(struct weston_output *output,
246 struct weston_mode *mode,
247 int32_t scale)
248{
249 int ret;
250
251 if (!output->switch_mode)
252 return -1;
253
254 /* original_mode is the last mode non full screen clients have seen,
255 * so we shouldn't change it if we already have one set.
256 */
257 if (!output->original_mode) {
258 output->original_mode = output->native_mode;
259 output->original_scale = output->native_scale;
260 }
261 ret = output->switch_mode(output, mode);
262 if (ret < 0)
263 return ret;
264
265 output->current_scale = scale;
266
267 weston_mode_switch_finish(output, 0, 0);
268
269 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800270}
271
Benjamin Franzke06286262011-05-06 19:12:33 +0200272static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300273region_init_infinite(pixman_region32_t *region)
274{
275 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
276 UINT32_MAX, UINT32_MAX);
277}
278
Pekka Paalanene67858b2013-04-25 13:57:42 +0300279static struct weston_subsurface *
280weston_surface_to_subsurface(struct weston_surface *surface);
281
Jason Ekstranda7af7042013-10-12 22:38:11 -0500282WL_EXPORT struct weston_view *
283weston_view_create(struct weston_surface *surface)
284{
285 struct weston_view *view;
286
Bryce Harringtonde16d892014-11-20 22:21:57 -0800287 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500288 if (view == NULL)
289 return NULL;
290
291 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000292 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500293
Jason Ekstranda7af7042013-10-12 22:38:11 -0500294 /* Assign to surface */
295 wl_list_insert(&surface->views, &view->surface_link);
296
297 wl_signal_init(&view->destroy_signal);
298 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300299 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500300
Jason Ekstranda7af7042013-10-12 22:38:11 -0500301 pixman_region32_init(&view->clip);
302
303 view->alpha = 1.0;
304 pixman_region32_init(&view->transform.opaque);
305
306 wl_list_init(&view->geometry.transformation_list);
307 wl_list_insert(&view->geometry.transformation_list,
308 &view->transform.position.link);
309 weston_matrix_init(&view->transform.position.matrix);
310 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200311 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500312 pixman_region32_init(&view->transform.boundingbox);
313 view->transform.dirty = 1;
314
Jason Ekstranda7af7042013-10-12 22:38:11 -0500315 return view;
316}
317
Jason Ekstrand108865d2014-06-26 10:04:49 -0700318struct weston_frame_callback {
319 struct wl_resource *resource;
320 struct wl_list link;
321};
322
Pekka Paalanen133e4392014-09-23 22:08:46 -0400323struct weston_presentation_feedback {
324 struct wl_resource *resource;
325
326 /* XXX: could use just wl_resource_get_link() instead */
327 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200328
329 /* The per-surface feedback flags */
330 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400331};
332
333static void
334weston_presentation_feedback_discard(
335 struct weston_presentation_feedback *feedback)
336{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200337 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400338 wl_resource_destroy(feedback->resource);
339}
340
341static void
342weston_presentation_feedback_discard_list(struct wl_list *list)
343{
344 struct weston_presentation_feedback *feedback, *tmp;
345
346 wl_list_for_each_safe(feedback, tmp, list, link)
347 weston_presentation_feedback_discard(feedback);
348}
349
350static void
351weston_presentation_feedback_present(
352 struct weston_presentation_feedback *feedback,
353 struct weston_output *output,
354 uint32_t refresh_nsec,
355 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200356 uint64_t seq,
357 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400358{
359 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300360 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400361 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200362 uint32_t tv_sec_hi;
363 uint32_t tv_sec_lo;
364 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300365 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400366
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300367 wl_list_for_each(head, &output->head_list, output_link) {
368 wl_resource_for_each(o, &head->resource_list) {
369 if (wl_resource_get_client(o) != client)
370 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400371
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300372 wp_presentation_feedback_send_sync_output(feedback->resource, o);
373 done = true;
374 }
375
376 /* For clone mode, send it for just one wl_output global,
377 * they are all equivalent anyway.
378 */
379 if (done)
380 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400381 }
382
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200383 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200384 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200385 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200386 refresh_nsec,
387 seq >> 32, seq & 0xffffffff,
388 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400389 wl_resource_destroy(feedback->resource);
390}
391
392static void
393weston_presentation_feedback_present_list(struct wl_list *list,
394 struct weston_output *output,
395 uint32_t refresh_nsec,
396 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200397 uint64_t seq,
398 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400399{
400 struct weston_presentation_feedback *feedback, *tmp;
401
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200402 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200403 wl_list_empty(list));
404
Pekka Paalanen133e4392014-09-23 22:08:46 -0400405 wl_list_for_each_safe(feedback, tmp, list, link)
406 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200407 refresh_nsec, ts, seq,
408 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400409}
410
Jason Ekstrand7b982072014-05-20 14:33:03 -0500411static void
412surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
413{
414 struct weston_surface_state *state =
415 container_of(listener, struct weston_surface_state,
416 buffer_destroy_listener);
417
418 state->buffer = NULL;
419}
420
421static void
422weston_surface_state_init(struct weston_surface_state *state)
423{
424 state->newly_attached = 0;
425 state->buffer = NULL;
426 state->buffer_destroy_listener.notify =
427 surface_state_handle_buffer_destroy;
428 state->sx = 0;
429 state->sy = 0;
430
Derek Foreman152254b2015-11-26 14:17:48 -0600431 pixman_region32_init(&state->damage_surface);
432 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500433 pixman_region32_init(&state->opaque);
434 region_init_infinite(&state->input);
435
436 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400437 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500438
439 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
440 state->buffer_viewport.buffer.scale = 1;
441 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
442 state->buffer_viewport.surface.width = -1;
443 state->buffer_viewport.changed = 0;
444}
445
446static void
447weston_surface_state_fini(struct weston_surface_state *state)
448{
449 struct weston_frame_callback *cb, *next;
450
451 wl_list_for_each_safe(cb, next,
452 &state->frame_callback_list, link)
453 wl_resource_destroy(cb->resource);
454
Pekka Paalanen133e4392014-09-23 22:08:46 -0400455 weston_presentation_feedback_discard_list(&state->feedback_list);
456
Jason Ekstrand7b982072014-05-20 14:33:03 -0500457 pixman_region32_fini(&state->input);
458 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600459 pixman_region32_fini(&state->damage_surface);
460 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500461
462 if (state->buffer)
463 wl_list_remove(&state->buffer_destroy_listener.link);
464 state->buffer = NULL;
465}
466
467static void
468weston_surface_state_set_buffer(struct weston_surface_state *state,
469 struct weston_buffer *buffer)
470{
471 if (state->buffer == buffer)
472 return;
473
474 if (state->buffer)
475 wl_list_remove(&state->buffer_destroy_listener.link);
476 state->buffer = buffer;
477 if (state->buffer)
478 wl_signal_add(&state->buffer->destroy_signal,
479 &state->buffer_destroy_listener);
480}
481
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500482WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500483weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500484{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500485 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400486
Bryce Harringtonde16d892014-11-20 22:21:57 -0800487 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400488 if (surface == NULL)
489 return NULL;
490
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500491 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800492 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500493
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500494 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200495 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400496
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200497 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
498 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200499 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
500 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500501
502 weston_surface_state_init(&surface->pending);
503
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400504 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500505 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300506 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400507
Jason Ekstranda7af7042013-10-12 22:38:11 -0500508 wl_list_init(&surface->views);
509
510 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400511 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500512
Pekka Paalanene67858b2013-04-25 13:57:42 +0300513 wl_list_init(&surface->subsurface_list);
514 wl_list_init(&surface->subsurface_list_pending);
515
Jason Ekstrand1e059042014-10-16 10:55:19 -0500516 weston_matrix_init(&surface->buffer_to_surface_matrix);
517 weston_matrix_init(&surface->surface_to_buffer_matrix);
518
Jonas Ådahld3414f22016-07-22 17:56:31 +0800519 wl_list_init(&surface->pointer_constraints);
520
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400521 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500522}
523
Alex Wu8811bf92012-02-28 18:07:54 +0800524WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500525weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200526 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500527{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100528 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500529}
530
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400531WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500532weston_view_to_global_float(struct weston_view *view,
533 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200534{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500535 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200536 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
537
Jason Ekstranda7af7042013-10-12 22:38:11 -0500538 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200539
540 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200541 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700542 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200543 v.f[3]);
544 *x = 0;
545 *y = 0;
546 return;
547 }
548
549 *x = v.f[0] / v.f[3];
550 *y = v.f[1] / v.f[3];
551 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500552 *x = sx + view->geometry.x;
553 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200554 }
555}
556
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500557WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200558weston_transformed_coord(int width, int height,
559 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200560 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200561 float sx, float sy, float *bx, float *by)
562{
563 switch (transform) {
564 case WL_OUTPUT_TRANSFORM_NORMAL:
565 default:
566 *bx = sx;
567 *by = sy;
568 break;
569 case WL_OUTPUT_TRANSFORM_FLIPPED:
570 *bx = width - sx;
571 *by = sy;
572 break;
573 case WL_OUTPUT_TRANSFORM_90:
574 *bx = height - sy;
575 *by = sx;
576 break;
577 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
578 *bx = height - sy;
579 *by = width - sx;
580 break;
581 case WL_OUTPUT_TRANSFORM_180:
582 *bx = width - sx;
583 *by = height - sy;
584 break;
585 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
586 *bx = sx;
587 *by = height - sy;
588 break;
589 case WL_OUTPUT_TRANSFORM_270:
590 *bx = sy;
591 *by = width - sx;
592 break;
593 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
594 *bx = sy;
595 *by = sx;
596 break;
597 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200598
599 *bx *= scale;
600 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200601}
602
603WL_EXPORT pixman_box32_t
604weston_transformed_rect(int width, int height,
605 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200606 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200607 pixman_box32_t rect)
608{
609 float x1, x2, y1, y2;
610
611 pixman_box32_t ret;
612
Alexander Larsson4ea95522013-05-22 14:41:37 +0200613 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200614 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200615 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200616 rect.x2, rect.y2, &x2, &y2);
617
618 if (x1 <= x2) {
619 ret.x1 = x1;
620 ret.x2 = x2;
621 } else {
622 ret.x1 = x2;
623 ret.x2 = x1;
624 }
625
626 if (y1 <= y2) {
627 ret.y1 = y1;
628 ret.y2 = y2;
629 } else {
630 ret.y1 = y2;
631 ret.y2 = y1;
632 }
633
634 return ret;
635}
636
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600637/** Transform a region by a matrix, restricted to axis-aligned transformations
638 *
639 * Warning: This function does not work for projective, affine, or matrices
640 * that encode arbitrary rotations. Only 90-degree step rotations are
641 * supported.
642 */
643WL_EXPORT void
644weston_matrix_transform_region(pixman_region32_t *dest,
645 struct weston_matrix *matrix,
646 pixman_region32_t *src)
647{
648 pixman_box32_t *src_rects, *dest_rects;
649 int nrects, i;
650
651 src_rects = pixman_region32_rectangles(src, &nrects);
652 dest_rects = malloc(nrects * sizeof(*dest_rects));
653 if (!dest_rects)
654 return;
655
656 for (i = 0; i < nrects; i++) {
657 struct weston_vector vec1 = {{
658 src_rects[i].x1, src_rects[i].y1, 0, 1
659 }};
660 weston_matrix_transform(matrix, &vec1);
661 vec1.f[0] /= vec1.f[3];
662 vec1.f[1] /= vec1.f[3];
663
664 struct weston_vector vec2 = {{
665 src_rects[i].x2, src_rects[i].y2, 0, 1
666 }};
667 weston_matrix_transform(matrix, &vec2);
668 vec2.f[0] /= vec2.f[3];
669 vec2.f[1] /= vec2.f[3];
670
671 if (vec1.f[0] < vec2.f[0]) {
672 dest_rects[i].x1 = floor(vec1.f[0]);
673 dest_rects[i].x2 = ceil(vec2.f[0]);
674 } else {
675 dest_rects[i].x1 = floor(vec2.f[0]);
676 dest_rects[i].x2 = ceil(vec1.f[0]);
677 }
678
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600679 if (vec1.f[1] < vec2.f[1]) {
680 dest_rects[i].y1 = floor(vec1.f[1]);
681 dest_rects[i].y2 = ceil(vec2.f[1]);
682 } else {
683 dest_rects[i].y1 = floor(vec2.f[1]);
684 dest_rects[i].y2 = ceil(vec1.f[1]);
685 }
686 }
687
688 pixman_region32_clear(dest);
689 pixman_region32_init_rects(dest, dest_rects, nrects);
690 free(dest_rects);
691}
692
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200693WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500694weston_transformed_region(int width, int height,
695 enum wl_output_transform transform,
696 int32_t scale,
697 pixman_region32_t *src, pixman_region32_t *dest)
698{
699 pixman_box32_t *src_rects, *dest_rects;
700 int nrects, i;
701
702 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
703 if (src != dest)
704 pixman_region32_copy(dest, src);
705 return;
706 }
707
708 src_rects = pixman_region32_rectangles(src, &nrects);
709 dest_rects = malloc(nrects * sizeof(*dest_rects));
710 if (!dest_rects)
711 return;
712
713 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
714 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
715 } else {
716 for (i = 0; i < nrects; i++) {
717 switch (transform) {
718 default:
719 case WL_OUTPUT_TRANSFORM_NORMAL:
720 dest_rects[i].x1 = src_rects[i].x1;
721 dest_rects[i].y1 = src_rects[i].y1;
722 dest_rects[i].x2 = src_rects[i].x2;
723 dest_rects[i].y2 = src_rects[i].y2;
724 break;
725 case WL_OUTPUT_TRANSFORM_90:
726 dest_rects[i].x1 = height - src_rects[i].y2;
727 dest_rects[i].y1 = src_rects[i].x1;
728 dest_rects[i].x2 = height - src_rects[i].y1;
729 dest_rects[i].y2 = src_rects[i].x2;
730 break;
731 case WL_OUTPUT_TRANSFORM_180:
732 dest_rects[i].x1 = width - src_rects[i].x2;
733 dest_rects[i].y1 = height - src_rects[i].y2;
734 dest_rects[i].x2 = width - src_rects[i].x1;
735 dest_rects[i].y2 = height - src_rects[i].y1;
736 break;
737 case WL_OUTPUT_TRANSFORM_270:
738 dest_rects[i].x1 = src_rects[i].y1;
739 dest_rects[i].y1 = width - src_rects[i].x2;
740 dest_rects[i].x2 = src_rects[i].y2;
741 dest_rects[i].y2 = width - src_rects[i].x1;
742 break;
743 case WL_OUTPUT_TRANSFORM_FLIPPED:
744 dest_rects[i].x1 = width - src_rects[i].x2;
745 dest_rects[i].y1 = src_rects[i].y1;
746 dest_rects[i].x2 = width - src_rects[i].x1;
747 dest_rects[i].y2 = src_rects[i].y2;
748 break;
749 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
750 dest_rects[i].x1 = height - src_rects[i].y2;
751 dest_rects[i].y1 = width - src_rects[i].x2;
752 dest_rects[i].x2 = height - src_rects[i].y1;
753 dest_rects[i].y2 = width - src_rects[i].x1;
754 break;
755 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
756 dest_rects[i].x1 = src_rects[i].x1;
757 dest_rects[i].y1 = height - src_rects[i].y2;
758 dest_rects[i].x2 = src_rects[i].x2;
759 dest_rects[i].y2 = height - src_rects[i].y1;
760 break;
761 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
762 dest_rects[i].x1 = src_rects[i].y1;
763 dest_rects[i].y1 = src_rects[i].x1;
764 dest_rects[i].x2 = src_rects[i].y2;
765 dest_rects[i].y2 = src_rects[i].x2;
766 break;
767 }
768 }
769 }
770
771 if (scale != 1) {
772 for (i = 0; i < nrects; i++) {
773 dest_rects[i].x1 *= scale;
774 dest_rects[i].x2 *= scale;
775 dest_rects[i].y1 *= scale;
776 dest_rects[i].y2 *= scale;
777 }
778 }
779
780 pixman_region32_clear(dest);
781 pixman_region32_init_rects(dest, dest_rects, nrects);
782 free(dest_rects);
783}
784
Jonny Lamb74130762013-11-26 18:19:46 +0100785static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300786viewport_surface_to_buffer(struct weston_surface *surface,
787 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100788{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200789 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200790 double src_width, src_height;
791 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200792
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200793 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
794 if (vp->surface.width == -1) {
795 *bx = sx;
796 *by = sy;
797 return;
798 }
Jonny Lamb74130762013-11-26 18:19:46 +0100799
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200800 src_x = 0.0;
801 src_y = 0.0;
802 src_width = surface->width_from_buffer;
803 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100804 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200805 src_x = wl_fixed_to_double(vp->buffer.src_x);
806 src_y = wl_fixed_to_double(vp->buffer.src_y);
807 src_width = wl_fixed_to_double(vp->buffer.src_width);
808 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100809 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200810
811 *bx = sx * src_width / surface->width + src_x;
812 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100813}
814
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500815WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200816weston_surface_to_buffer_float(struct weston_surface *surface,
817 float sx, float sy, float *bx, float *by)
818{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200819 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
820
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300821 /* first transform coordinates if the viewport is set */
822 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100823
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500824 weston_transformed_coord(surface->width_from_buffer,
825 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200826 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100827 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200828}
829
Derek Foreman99739672015-12-03 16:38:11 -0600830/** Transform a rectangle from surface coordinates to buffer coordinates
831 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300832 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600833 * from.
834 * \param rect The rectangle to transform.
835 * \return The transformed rectangle.
836 *
837 * Viewport and buffer transformations can only do translation, scaling,
838 * and rotations in 90-degree steps. Therefore the only loss in the
839 * conversion is coordinate rounding.
840 *
841 * However, some coordinate rounding takes place as an intermediate
842 * step before the buffer scale factor is applied, so the rectangle
843 * boundary may not be exactly as expected.
844 *
845 * This is OK for damage tracking since a little extra coverage is
846 * not a problem.
847 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200848WL_EXPORT pixman_box32_t
849weston_surface_to_buffer_rect(struct weston_surface *surface,
850 pixman_box32_t rect)
851{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200852 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100853 float xf, yf;
854
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300855 /* first transform box coordinates if the viewport is set */
856 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100857 rect.x1 = floorf(xf);
858 rect.y1 = floorf(yf);
859
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300860 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600861 rect.x2 = ceilf(xf);
862 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100863
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500864 return weston_transformed_rect(surface->width_from_buffer,
865 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200866 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200867 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200868}
869
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200870/** Transform a region from surface coordinates to buffer coordinates
871 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300872 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200873 * from.
874 * \param surface_region[in] The region in surface coordinates.
875 * \param buffer_region[out] The region converted to buffer coordinates.
876 *
877 * Buffer_region must be init'd, but will be completely overwritten.
878 *
879 * Viewport and buffer transformations can only do translation, scaling,
880 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600881 * conversion is from the coordinate rounding that takes place in
882 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200883 */
884WL_EXPORT void
885weston_surface_to_buffer_region(struct weston_surface *surface,
886 pixman_region32_t *surface_region,
887 pixman_region32_t *buffer_region)
888{
889 pixman_box32_t *src_rects, *dest_rects;
890 int nrects, i;
891
892 src_rects = pixman_region32_rectangles(surface_region, &nrects);
893 dest_rects = malloc(nrects * sizeof(*dest_rects));
894 if (!dest_rects)
895 return;
896
897 for (i = 0; i < nrects; i++) {
898 dest_rects[i] = weston_surface_to_buffer_rect(surface,
899 src_rects[i]);
900 }
901
902 pixman_region32_fini(buffer_region);
903 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
904 free(dest_rects);
905}
906
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200907WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500908weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400909 struct weston_plane *plane)
910{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400912 return;
913
Jason Ekstranda7af7042013-10-12 22:38:11 -0500914 weston_view_damage_below(view);
915 view->plane = plane;
916 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400917}
918
Pekka Paalanen51723d52015-02-17 13:10:01 +0200919/** Inflict damage on the plane where the view is visible.
920 *
921 * \param view The view that causes the damage.
922 *
923 * If the view is currently on a plane (including the primary plane),
924 * take the view's boundingbox, subtract all the opaque views that cover it,
925 * and add the remaining region as damage to the plane. This corresponds
926 * to the damage inflicted to the plane if this view disappeared.
927 *
928 * A repaint is scheduled for this view.
929 *
930 * The region of all opaque views covering this view is stored in
931 * weston_view::clip and updated by view_accumulate_damage() during
932 * weston_output_repaint(). Specifically, that region matches the
933 * scenegraph as it was last painted.
934 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400935WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500936weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200937{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500938 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200939
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500940 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200941 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500942 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800943 if (view->plane)
944 pixman_region32_union(&view->plane->damage,
945 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500946 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800947 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200948}
949
Pekka Paalanen01e00682017-03-24 16:21:06 +0200950/** Send wl_surface.enter/leave events
951 *
952 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300953 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200954 * \param enter True if entered.
955 * \param left True if left.
956 *
957 * Send the enter/leave events for all protocol objects bound to the given
958 * output by the client owning the surface.
959 */
960static void
961weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300962 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +0200963 bool enter,
964 bool leave)
965{
966 struct wl_resource *wloutput;
967 struct wl_client *client;
968
969 assert(enter != leave);
970
971 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300972 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +0200973 if (wl_resource_get_client(wloutput) != client)
974 continue;
975
976 if (enter)
977 wl_surface_send_enter(surface->resource, wloutput);
978 if (leave)
979 wl_surface_send_leave(surface->resource, wloutput);
980 }
981}
982
Bryce Harrington3f650b82015-12-23 11:01:58 -0800983/**
984 * \param es The surface
985 * \param mask The new set of outputs for the surface
986 *
987 * Sets the surface's set of outputs to the ones specified by
988 * the new output mask provided. Identifies the outputs that
989 * have changed, the posts enter and leave events for these
990 * outputs as appropriate.
991 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400992static void
993weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
994{
995 uint32_t different = es->output_mask ^ mask;
996 uint32_t entered = mask & different;
997 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +0200998 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400999 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001000 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001001
1002 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001003 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001004 return;
1005 if (different == 0)
1006 return;
1007
1008 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001009 output_bit = 1u << output->id;
1010 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001011 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001012
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001013 wl_list_for_each(head, &output->head_list, output_link) {
1014 weston_surface_send_enter_leave(es, head,
1015 output_bit & entered,
1016 output_bit & left);
1017 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001018 }
1019}
1020
Bryce Harrington3f650b82015-12-23 11:01:58 -08001021/** Recalculate which output(s) the surface has views displayed on
1022 *
1023 * \param es The surface to remap to outputs
1024 *
1025 * Finds the output that is showing the largest amount of one
1026 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001027 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001028 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001029 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001030 * in the output_mask for the surface.
1031 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001032static void
1033weston_surface_assign_output(struct weston_surface *es)
1034{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001035 struct weston_output *new_output;
1036 struct weston_view *view;
1037 pixman_region32_t region;
1038 uint32_t max, area, mask;
1039 pixman_box32_t *e;
1040
1041 new_output = NULL;
1042 max = 0;
1043 mask = 0;
1044 pixman_region32_init(&region);
1045 wl_list_for_each(view, &es->views, surface_link) {
1046 if (!view->output)
1047 continue;
1048
1049 pixman_region32_intersect(&region, &view->transform.boundingbox,
1050 &view->output->region);
1051
1052 e = pixman_region32_extents(&region);
1053 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1054
1055 mask |= view->output_mask;
1056
1057 if (area >= max) {
1058 new_output = view->output;
1059 max = area;
1060 }
1061 }
1062 pixman_region32_fini(&region);
1063
1064 es->output = new_output;
1065 weston_surface_update_output_mask(es, mask);
1066}
1067
Bryce Harrington3f650b82015-12-23 11:01:58 -08001068/** Recalculate which output(s) the view is displayed on
1069 *
1070 * \param ev The view to remap to outputs
1071 *
1072 * Identifies the set of outputs that the view is visible on,
1073 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001074 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001075 *
1076 * Also does the same for the view's surface. See
1077 * weston_surface_assign_output().
1078 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079static void
1080weston_view_assign_output(struct weston_view *ev)
1081{
1082 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001083 struct weston_output *output, *new_output;
1084 pixman_region32_t region;
1085 uint32_t max, area, mask;
1086 pixman_box32_t *e;
1087
1088 new_output = NULL;
1089 max = 0;
1090 mask = 0;
1091 pixman_region32_init(&region);
1092 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001093 if (output->destroying)
1094 continue;
1095
Jason Ekstranda7af7042013-10-12 22:38:11 -05001096 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001097 &output->region);
1098
1099 e = pixman_region32_extents(&region);
1100 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1101
1102 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001103 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001104
1105 if (area >= max) {
1106 new_output = output;
1107 max = area;
1108 }
1109 }
1110 pixman_region32_fini(&region);
1111
Jason Ekstranda7af7042013-10-12 22:38:11 -05001112 ev->output = new_output;
1113 ev->output_mask = mask;
1114
1115 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001116}
1117
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001118static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001119weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1120 int from_x, int from_y, int *to_x, int *to_y)
1121{
1122 float x, y;
1123
1124 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1125 weston_view_from_global_float(to, x, y, &x, &y);
1126
1127 *to_x = round(x);
1128 *to_y = round(y);
1129}
1130
1131static void
1132weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1133{
1134 pixman_box32_t *a;
1135 pixman_box32_t b;
1136
1137 a = pixman_region32_extents(&from->geometry.scissor);
1138
1139 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1140 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1141
1142 pixman_region32_fini(&to->geometry.scissor);
1143 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1144}
1145
1146static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001147view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001149{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001150 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1151 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001152 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001153 { inbox->x1, inbox->y1 },
1154 { inbox->x1, inbox->y2 },
1155 { inbox->x2, inbox->y1 },
1156 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001157 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001158 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001159 int i;
1160
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001161 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001162 /* avoid rounding empty bbox to 1x1 */
1163 pixman_region32_init(bbox);
1164 return;
1165 }
1166
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001167 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001168 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001170 if (x < min_x)
1171 min_x = x;
1172 if (x > max_x)
1173 max_x = x;
1174 if (y < min_y)
1175 min_y = y;
1176 if (y > max_y)
1177 max_y = y;
1178 }
1179
Pekka Paalanen219b9822012-02-08 15:38:37 +02001180 int_x = floorf(min_x);
1181 int_y = floorf(min_y);
1182 pixman_region32_init_rect(bbox, int_x, int_y,
1183 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001184}
1185
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001186static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001188{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001190
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001191 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 view->geometry.x = roundf(view->geometry.x);
1193 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001194
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001195 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1197 view->transform.position.matrix.d[12] = view->geometry.x;
1198 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001199
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001201
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202 view->transform.inverse = view->transform.position.matrix;
1203 view->transform.inverse.d[12] = -view->geometry.x;
1204 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001205
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001207 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001208 view->surface->width,
1209 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001210 if (view->geometry.scissor_enabled)
1211 pixman_region32_intersect(&view->transform.boundingbox,
1212 &view->transform.boundingbox,
1213 &view->geometry.scissor);
1214
1215 pixman_region32_translate(&view->transform.boundingbox,
1216 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001217
Jason Ekstranda7af7042013-10-12 22:38:11 -05001218 if (view->alpha == 1.0) {
1219 pixman_region32_copy(&view->transform.opaque,
1220 &view->surface->opaque);
1221 pixman_region32_translate(&view->transform.opaque,
1222 view->geometry.x,
1223 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001224 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001225}
1226
1227static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001229{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 struct weston_view *parent = view->geometry.parent;
1231 struct weston_matrix *matrix = &view->transform.matrix;
1232 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001233 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001234 pixman_region32_t surfregion;
1235 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001236
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001238
1239 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1241 view->transform.position.matrix.d[12] = view->geometry.x;
1242 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001243
1244 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001246 weston_matrix_multiply(matrix, &tform->matrix);
1247
Pekka Paalanen483243f2013-03-08 14:56:50 +02001248 if (parent)
1249 weston_matrix_multiply(matrix, &parent->transform.matrix);
1250
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001251 if (weston_matrix_invert(inverse, matrix) < 0) {
1252 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 weston_log("error: weston_view %p"
1254 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001255 return -1;
1256 }
1257
Giulio Camuffo148c1992016-09-04 18:50:46 +03001258 if (view->alpha == 1.0 &&
1259 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1260 pixman_region32_copy(&view->transform.opaque,
1261 &view->surface->opaque);
1262 pixman_region32_translate(&view->transform.opaque,
1263 matrix->d[12],
1264 matrix->d[13]);
1265 }
1266
Pekka Paalanen380adf52015-02-16 14:39:11 +02001267 pixman_region32_init_rect(&surfregion, 0, 0,
1268 view->surface->width, view->surface->height);
1269 if (view->geometry.scissor_enabled)
1270 pixman_region32_intersect(&surfregion, &surfregion,
1271 &view->geometry.scissor);
1272 surfbox = pixman_region32_extents(&surfregion);
1273
1274 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1275 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001276
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001277 return 0;
1278}
1279
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001280static struct weston_layer *
1281get_view_layer(struct weston_view *view)
1282{
1283 if (view->parent_view)
1284 return get_view_layer(view->parent_view);
1285 return view->layer_link.layer;
1286}
1287
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001288WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001290{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001292 struct weston_layer *layer;
1293 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001294
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001296 return;
1297
Pekka Paalanen483243f2013-03-08 14:56:50 +02001298 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001300
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001302
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001304
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 pixman_region32_fini(&view->transform.boundingbox);
1306 pixman_region32_fini(&view->transform.opaque);
1307 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001308
Pekka Paalanencd403622012-01-25 13:37:39 +02001309 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 if (view->geometry.transformation_list.next ==
1311 &view->transform.position.link &&
1312 view->geometry.transformation_list.prev ==
1313 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001314 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001316 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317 if (weston_view_update_transform_enable(view) < 0)
1318 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001319 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001320
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001321 layer = get_view_layer(view);
1322 if (layer) {
1323 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001324 pixman_region32_intersect(&view->transform.boundingbox,
1325 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001326 pixman_region32_intersect(&view->transform.opaque,
1327 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001328 pixman_region32_fini(&mask);
1329 }
1330
Pekka Paalanen380adf52015-02-16 14:39:11 +02001331 if (parent) {
1332 if (parent->geometry.scissor_enabled) {
1333 view->geometry.scissor_enabled = true;
1334 weston_view_transfer_scissor(parent, view);
1335 } else {
1336 view->geometry.scissor_enabled = false;
1337 }
1338 }
1339
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001341
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001343
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 wl_signal_emit(&view->surface->compositor->transform_signal,
1345 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001346}
1347
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001348WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001350{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001351 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001352
1353 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354 * The invariant: if view->geometry.dirty, then all views
1355 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001356 * Corollary: if not parent->geometry.dirty, then all ancestors
1357 * are not dirty.
1358 */
1359
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001361 return;
1362
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001364
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001366 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001368}
1369
1370WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371weston_view_to_global_fixed(struct weston_view *view,
1372 wl_fixed_t vx, wl_fixed_t vy,
1373 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001374{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001375 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001376
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 weston_view_to_global_float(view,
1378 wl_fixed_to_double(vx),
1379 wl_fixed_to_double(vy),
1380 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001381 *x = wl_fixed_from_double(xf);
1382 *y = wl_fixed_from_double(yf);
1383}
1384
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001385WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386weston_view_from_global_float(struct weston_view *view,
1387 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001388{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001390 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1391
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001393
1394 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001395 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001397 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 *vx = 0;
1399 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001400 return;
1401 }
1402
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403 *vx = v.f[0] / v.f[3];
1404 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001405 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 *vx = x - view->geometry.x;
1407 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001408 }
1409}
1410
1411WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001412weston_view_from_global_fixed(struct weston_view *view,
1413 wl_fixed_t x, wl_fixed_t y,
1414 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001415{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001417
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 weston_view_from_global_float(view,
1419 wl_fixed_to_double(x),
1420 wl_fixed_to_double(y),
1421 &vxf, &vyf);
1422 *vx = wl_fixed_from_double(vxf);
1423 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001424}
1425
1426WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001427weston_view_from_global(struct weston_view *view,
1428 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001429{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001430 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1433 *vx = floorf(vxf);
1434 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001435}
1436
Bryce Harrington3f650b82015-12-23 11:01:58 -08001437/**
1438 * \param surface The surface to be repainted
1439 *
1440 * Marks the output(s) that the surface is shown on as needing to be
1441 * repainted. See weston_output_schedule_repaint().
1442 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001443WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001444weston_surface_schedule_repaint(struct weston_surface *surface)
1445{
1446 struct weston_output *output;
1447
1448 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001449 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001450 weston_output_schedule_repaint(output);
1451}
1452
Bryce Harrington3f650b82015-12-23 11:01:58 -08001453/**
1454 * \param view The view to be repainted
1455 *
1456 * Marks the output(s) that the view is shown on as needing to be
1457 * repainted. See weston_output_schedule_repaint().
1458 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001459WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460weston_view_schedule_repaint(struct weston_view *view)
1461{
1462 struct weston_output *output;
1463
1464 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001465 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 weston_output_schedule_repaint(output);
1467}
1468
Pekka Paalanene508ce62015-02-19 13:59:55 +02001469/**
1470 * XXX: This function does it the wrong way.
1471 * surface->damage is the damage from the client, and causes
1472 * surface_flush_damage() to copy pixels. No window management action can
1473 * cause damage to the client-provided content, warranting re-upload!
1474 *
1475 * Instead of surface->damage, this function should record the damage
1476 * with all the views for this surface to avoid extraneous texture
1477 * uploads.
1478 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001479WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001480weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001481{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001482 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 0, 0, surface->width,
1484 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001485
Kristian Høgsberg98238702012-08-03 16:29:12 -04001486 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001487}
1488
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001489WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001491{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001492 if (view->geometry.x == x && view->geometry.y == y)
1493 return;
1494
Jason Ekstranda7af7042013-10-12 22:38:11 -05001495 view->geometry.x = x;
1496 view->geometry.y = y;
1497 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001498}
1499
Pekka Paalanen483243f2013-03-08 14:56:50 +02001500static void
1501transform_parent_handle_parent_destroy(struct wl_listener *listener,
1502 void *data)
1503{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001504 struct weston_view *view =
1505 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001506 geometry.parent_destroy_listener);
1507
Jason Ekstranda7af7042013-10-12 22:38:11 -05001508 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001509}
1510
1511WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001512weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001513 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001514{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515 if (view->geometry.parent) {
1516 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1517 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001518
1519 if (!parent)
1520 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001521 }
1522
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001524
Jason Ekstranda7af7042013-10-12 22:38:11 -05001525 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001526 transform_parent_handle_parent_destroy;
1527 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001528 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001530 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001532 }
1533
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 weston_view_geometry_dirty(view);
1535}
1536
Pekka Paalanen380adf52015-02-16 14:39:11 +02001537/** Set a clip mask rectangle on a view
1538 *
1539 * \param view The view to set the clip mask on.
1540 * \param x Top-left corner X coordinate of the clip rectangle.
1541 * \param y Top-left corner Y coordinate of the clip rectangle.
1542 * \param width Width of the clip rectangle, non-negative.
1543 * \param height Height of the clip rectangle, non-negative.
1544 *
1545 * A shell may set a clip mask rectangle on a view. Everything outside
1546 * the rectangle is cut away for input and output purposes: it is
1547 * not drawn and cannot be hit by hit-test based input like pointer
1548 * motion or touch-downs. Everything inside the rectangle will behave
1549 * normally. Clients are unaware of clipping.
1550 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001551 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001552 * mask rectangle does not affect the view position, the view is positioned
1553 * as it would be without a clip. The clip also does not change
1554 * weston_surface::width,height.
1555 *
1556 * The clip mask rectangle is part of transformation inheritance
1557 * (weston_view_set_transform_parent()). A clip set in the root of the
1558 * transformation inheritance tree will affect all views in the tree.
1559 * A clip can be set only on the root view. Attempting to set a clip
1560 * on view that has a transformation parent will fail. Assigning a parent
1561 * to a view that has a clip set will cause the clip to be forgotten.
1562 *
1563 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1564 * on the additional transformations in the child views. These transformations
1565 * may not rotate the coordinate axes, i.e., only translation and scaling
1566 * are allowed. Violating this restriction causes the clipping to malfunction.
1567 * Furthermore, using scaling may cause rounding errors in child clipping.
1568 *
1569 * The clip mask rectangle is not automatically adjusted based on
1570 * wl_surface.attach dx and dy arguments.
1571 *
1572 * A clip mask rectangle can be set only if the compositor capability
1573 * WESTON_CAP_VIEW_CLIP_MASK is present.
1574 *
1575 * This function sets the clip mask rectangle and schedules a repaint for
1576 * the view.
1577 */
1578WL_EXPORT void
1579weston_view_set_mask(struct weston_view *view,
1580 int x, int y, int width, int height)
1581{
1582 struct weston_compositor *compositor = view->surface->compositor;
1583
1584 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1585 weston_log("%s not allowed without capability!\n", __func__);
1586 return;
1587 }
1588
1589 if (view->geometry.parent) {
1590 weston_log("view %p has a parent, clip forbidden!\n", view);
1591 return;
1592 }
1593
1594 if (width < 0 || height < 0) {
1595 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1596 x, y, width, height);
1597 return;
1598 }
1599
1600 pixman_region32_fini(&view->geometry.scissor);
1601 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1602 view->geometry.scissor_enabled = true;
1603 weston_view_geometry_dirty(view);
1604 weston_view_schedule_repaint(view);
1605}
1606
1607/** Remove the clip mask from a view
1608 *
1609 * \param view The view to remove the clip mask from.
1610 *
1611 * Removed the clip mask rectangle and schedules a repaint.
1612 *
1613 * \sa weston_view_set_mask
1614 */
1615WL_EXPORT void
1616weston_view_set_mask_infinite(struct weston_view *view)
1617{
1618 view->geometry.scissor_enabled = false;
1619 weston_view_geometry_dirty(view);
1620 weston_view_schedule_repaint(view);
1621}
1622
Armin Krezović0da12b82016-06-30 06:04:33 +02001623/* Check if view should be displayed
1624 *
1625 * The indicator is set manually when assigning
1626 * a view to a surface.
1627 *
1628 * This needs reworking. See the thread starting at:
1629 *
1630 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1631 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001632WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001633weston_view_is_mapped(struct weston_view *view)
1634{
Armin Krezović0da12b82016-06-30 06:04:33 +02001635 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001636}
1637
Armin Krezović0da12b82016-06-30 06:04:33 +02001638/* Check if a surface has a view assigned to it
1639 *
1640 * The indicator is set manually when mapping
1641 * a surface and creating a view for it.
1642 *
1643 * This needs to go. See the thread starting at:
1644 *
1645 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1646 *
1647 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001648WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001649weston_surface_is_mapped(struct weston_surface *surface)
1650{
Armin Krezović0da12b82016-06-30 06:04:33 +02001651 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001652}
1653
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001654static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001655surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001656{
1657 struct weston_view *view;
1658
1659 if (surface->width == width && surface->height == height)
1660 return;
1661
1662 surface->width = width;
1663 surface->height = height;
1664
1665 wl_list_for_each(view, &surface->views, surface_link)
1666 weston_view_geometry_dirty(view);
1667}
1668
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001669WL_EXPORT void
1670weston_surface_set_size(struct weston_surface *surface,
1671 int32_t width, int32_t height)
1672{
1673 assert(!surface->resource);
1674 surface_set_size(surface, width, height);
1675}
1676
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001677static int
1678fixed_round_up_to_int(wl_fixed_t f)
1679{
1680 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1681}
1682
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001683static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001684convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1685 int32_t width, int32_t height,
1686 uint32_t transform,
1687 int32_t scale)
1688{
1689 assert(scale > 0);
1690
1691 switch (transform) {
1692 case WL_OUTPUT_TRANSFORM_NORMAL:
1693 case WL_OUTPUT_TRANSFORM_180:
1694 case WL_OUTPUT_TRANSFORM_FLIPPED:
1695 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1696 *width_out = width / scale;
1697 *height_out = height / scale;
1698 break;
1699 case WL_OUTPUT_TRANSFORM_90:
1700 case WL_OUTPUT_TRANSFORM_270:
1701 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1702 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1703 *width_out = height / scale;
1704 *height_out = width / scale;
1705 break;
1706 default:
1707 assert(0 && "invalid transform");
1708 }
1709}
1710
1711static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001712weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001713{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001714 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001715
1716 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001717 surface->width_from_buffer = 0;
1718 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001719 return;
1720 }
1721
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001722 convert_size_by_transform_scale(&surface->width_from_buffer,
1723 &surface->height_from_buffer,
1724 surface->buffer_ref.buffer->width,
1725 surface->buffer_ref.buffer->height,
1726 vp->buffer.transform,
1727 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001728}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001729
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001730static void
1731weston_surface_update_size(struct weston_surface *surface)
1732{
1733 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1734 int32_t width, height;
1735
1736 width = surface->width_from_buffer;
1737 height = surface->height_from_buffer;
1738
1739 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001740 surface_set_size(surface,
1741 vp->surface.width, vp->surface.height);
1742 return;
1743 }
1744
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001745 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001746 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1747 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1748
1749 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001750 return;
1751 }
1752
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001753 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001754}
1755
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001756WL_EXPORT void
1757weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001758{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001759 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001760}
1761
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762WL_EXPORT struct weston_view *
1763weston_compositor_pick_view(struct weston_compositor *compositor,
1764 wl_fixed_t x, wl_fixed_t y,
1765 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001766{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001767 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001768 wl_fixed_t view_x, view_y;
1769 int view_ix, view_iy;
1770 int ix = wl_fixed_to_int(x);
1771 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001772
Jason Ekstranda7af7042013-10-12 22:38:11 -05001773 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001774 if (!pixman_region32_contains_point(
1775 &view->transform.boundingbox, ix, iy, NULL))
1776 continue;
1777
1778 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1779 view_ix = wl_fixed_to_int(view_x);
1780 view_iy = wl_fixed_to_int(view_y);
1781
1782 if (!pixman_region32_contains_point(&view->surface->input,
1783 view_ix, view_iy, NULL))
1784 continue;
1785
Pekka Paalanen380adf52015-02-16 14:39:11 +02001786 if (view->geometry.scissor_enabled &&
1787 !pixman_region32_contains_point(&view->geometry.scissor,
1788 view_ix, view_iy, NULL))
1789 continue;
1790
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001791 *vx = view_x;
1792 *vy = view_y;
1793 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001794 }
1795
Derek Foremanf9318d12015-05-11 15:40:11 -05001796 *vx = wl_fixed_from_int(-1000000);
1797 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001798 return NULL;
1799}
1800
1801static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001802weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001803{
Daniel Stone37816df2012-05-16 18:45:18 +01001804 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001805
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001806 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001807 return;
1808
Daniel Stone37816df2012-05-16 18:45:18 +01001809 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001810 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001811}
1812
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001813WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001814weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001815{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001816 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001817
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 if (!weston_view_is_mapped(view))
1819 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 weston_view_damage_below(view);
1822 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001823 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001824 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001825 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826 wl_list_remove(&view->link);
1827 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828 view->output_mask = 0;
1829 weston_surface_assign_output(view->surface);
1830
1831 if (weston_surface_is_mapped(view->surface))
1832 return;
1833
1834 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001835 struct weston_touch *touch = weston_seat_get_touch(seat);
1836 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1837 struct weston_keyboard *keyboard =
1838 weston_seat_get_keyboard(seat);
1839
1840 if (keyboard && keyboard->focus == view->surface)
1841 weston_keyboard_set_focus(keyboard, NULL);
1842 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001843 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001844 if (touch && touch->focus == view)
1845 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001846 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001847}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001848
Jason Ekstranda7af7042013-10-12 22:38:11 -05001849WL_EXPORT void
1850weston_surface_unmap(struct weston_surface *surface)
1851{
1852 struct weston_view *view;
1853
Armin Krezovićf8486c32016-06-30 06:04:28 +02001854 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001855 wl_list_for_each(view, &surface->views, surface_link)
1856 weston_view_unmap(view);
1857 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001858}
1859
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001860static void
1861weston_surface_reset_pending_buffer(struct weston_surface *surface)
1862{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001863 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001864 surface->pending.sx = 0;
1865 surface->pending.sy = 0;
1866 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001867 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001868}
1869
Jason Ekstranda7af7042013-10-12 22:38:11 -05001870WL_EXPORT void
1871weston_view_destroy(struct weston_view *view)
1872{
1873 wl_signal_emit(&view->destroy_signal, view);
1874
1875 assert(wl_list_empty(&view->geometry.child_list));
1876
1877 if (weston_view_is_mapped(view)) {
1878 weston_view_unmap(view);
1879 weston_compositor_build_view_list(view->surface->compositor);
1880 }
1881
1882 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001883 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001884
1885 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001886 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001887 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001888 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001889
1890 weston_view_set_transform_parent(view, NULL);
1891
Jason Ekstranda7af7042013-10-12 22:38:11 -05001892 wl_list_remove(&view->surface_link);
1893
1894 free(view);
1895}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001896
1897WL_EXPORT void
1898weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001899{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001900 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001901 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001902 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001903
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001904 if (--surface->ref_count > 0)
1905 return;
1906
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001907 assert(surface->resource == NULL);
1908
Pekka Paalanenca790762015-04-17 14:23:38 +03001909 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001910
Pekka Paalanene67858b2013-04-25 13:57:42 +03001911 assert(wl_list_empty(&surface->subsurface_list_pending));
1912 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001913
Jason Ekstranda7af7042013-10-12 22:38:11 -05001914 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1915 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001916
Jason Ekstrand7b982072014-05-20 14:33:03 -05001917 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001918
Pekka Paalanende685b82012-12-04 15:58:12 +02001919 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001920
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001921 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001922 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001923 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001924
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001925 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001926 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001927
Pekka Paalanen133e4392014-09-23 22:08:46 -04001928 weston_presentation_feedback_discard_list(&surface->feedback_list);
1929
Jonas Ådahld3414f22016-07-22 17:56:31 +08001930 wl_list_for_each_safe(constraint, next_constraint,
1931 &surface->pointer_constraints,
1932 link)
1933 weston_pointer_constraint_destroy(constraint);
1934
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001935 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001936}
1937
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001938static void
1939destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001940{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001941 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001942
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001943 assert(surface);
1944
Giulio Camuffo0d379742013-11-15 22:06:15 +01001945 /* Set the resource to NULL, since we don't want to leave a
1946 * dangling pointer if the surface was refcounted and survives
1947 * the weston_surface_destroy() call. */
1948 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001949
1950 if (surface->viewport_resource)
1951 wl_resource_set_user_data(surface->viewport_resource, NULL);
1952
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001953 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001954}
1955
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001956static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001957weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1958{
1959 struct weston_buffer *buffer =
1960 container_of(listener, struct weston_buffer, destroy_listener);
1961
1962 wl_signal_emit(&buffer->destroy_signal, buffer);
1963 free(buffer);
1964}
1965
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001966WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001967weston_buffer_from_resource(struct wl_resource *resource)
1968{
1969 struct weston_buffer *buffer;
1970 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001971
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001972 listener = wl_resource_get_destroy_listener(resource,
1973 weston_buffer_destroy_handler);
1974
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001975 if (listener)
1976 return container_of(listener, struct weston_buffer,
1977 destroy_listener);
1978
1979 buffer = zalloc(sizeof *buffer);
1980 if (buffer == NULL)
1981 return NULL;
1982
1983 buffer->resource = resource;
1984 wl_signal_init(&buffer->destroy_signal);
1985 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001986 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001987 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001988
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001989 return buffer;
1990}
1991
1992static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001993weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1994 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001995{
Pekka Paalanende685b82012-12-04 15:58:12 +02001996 struct weston_buffer_reference *ref =
1997 container_of(listener, struct weston_buffer_reference,
1998 destroy_listener);
1999
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002000 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002001 ref->buffer = NULL;
2002}
2003
2004WL_EXPORT void
2005weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002006 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002007{
2008 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002009 ref->buffer->busy_count--;
2010 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002011 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002012 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002013 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002014 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002015 }
2016
Pekka Paalanende685b82012-12-04 15:58:12 +02002017 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002018 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002019 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002020 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002021 }
2022
Pekka Paalanende685b82012-12-04 15:58:12 +02002023 ref->buffer = buffer;
2024 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2025}
2026
2027static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002028weston_surface_attach(struct weston_surface *surface,
2029 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002030{
2031 weston_buffer_reference(&surface->buffer_ref, buffer);
2032
Pekka Paalanena6421c42012-12-04 15:58:10 +02002033 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002034 if (weston_surface_is_mapped(surface))
2035 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002036 }
2037
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002038 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002039
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002040 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002041 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002042}
2043
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002044WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002045weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002046{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002047 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002048
2049 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002050 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002051}
2052
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002053WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002054weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002055{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002056 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002057
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002058 pixman_region32_union(&compositor->primary_plane.damage,
2059 &compositor->primary_plane.damage,
2060 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002061 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002062}
2063
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002064static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002065surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002066{
Pekka Paalanende685b82012-12-04 15:58:12 +02002067 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002068 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002069 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002070
Pekka Paalanenb5026542014-11-12 15:09:24 +02002071 if (weston_timeline_enabled_ &&
2072 pixman_region32_not_empty(&surface->damage))
2073 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2074 TLP_OUTPUT(surface->output), TLP_END);
2075
Jason Ekstrandef540082014-06-26 10:37:36 -07002076 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002077}
2078
2079static void
2080view_accumulate_damage(struct weston_view *view,
2081 pixman_region32_t *opaque)
2082{
2083 pixman_region32_t damage;
2084
2085 pixman_region32_init(&damage);
2086 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002087 pixman_box32_t *extents;
2088
Jason Ekstranda7af7042013-10-12 22:38:11 -05002089 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002090 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002091 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002092 pixman_region32_copy(&damage, &view->surface->damage);
2093 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002094 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002095 }
2096
Pekka Paalanen380adf52015-02-16 14:39:11 +02002097 pixman_region32_intersect(&damage, &damage,
2098 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002099 pixman_region32_subtract(&damage, &damage, opaque);
2100 pixman_region32_union(&view->plane->damage,
2101 &view->plane->damage, &damage);
2102 pixman_region32_fini(&damage);
2103 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002104 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002105}
2106
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002107static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002108compositor_accumulate_damage(struct weston_compositor *ec)
2109{
2110 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002111 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002112 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002113
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002114 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002115
2116 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002117 pixman_region32_copy(&plane->clip, &clip);
2118
2119 pixman_region32_init(&opaque);
2120
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 wl_list_for_each(ev, &ec->view_list, link) {
2122 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002123 continue;
2124
Jason Ekstranda7af7042013-10-12 22:38:11 -05002125 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002126 }
2127
2128 pixman_region32_union(&clip, &clip, &opaque);
2129 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002130 }
2131
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002132 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002133
Jason Ekstranda7af7042013-10-12 22:38:11 -05002134 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002135 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136
2137 wl_list_for_each(ev, &ec->view_list, link) {
2138 if (ev->surface->touched)
2139 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002140 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002141
2142 surface_flush_damage(ev->surface);
2143
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002144 /* Both the renderer and the backend have seen the buffer
2145 * by now. If renderer needs the buffer, it has its own
2146 * reference set. If the backend wants to keep the buffer
2147 * around for migrating the surface into a non-primary plane
2148 * later, keep_buffer is true. Otherwise, drop the core
2149 * reference now, and allow early buffer release. This enables
2150 * clients to use single-buffering.
2151 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002152 if (!ev->surface->keep_buffer)
2153 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002154 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002155}
2156
2157static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002158surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002159{
2160 struct weston_subsurface *sub;
2161
Pekka Paalanene67858b2013-04-25 13:57:42 +03002162 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002163 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002164 continue;
2165
Jason Ekstranda7af7042013-10-12 22:38:11 -05002166 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2167 wl_list_init(&sub->surface->views);
2168
2169 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002170 }
2171}
2172
2173static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002174surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002175{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002176 struct weston_subsurface *sub;
2177 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002178
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2180 if (sub->surface == surface)
2181 continue;
2182
George Kiagiadakised04d382014-06-13 18:10:26 +02002183 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2184 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002185 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002186 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002187
2188 surface_free_unused_subsurface_views(sub->surface);
2189 }
2190}
2191
2192static void
2193view_list_add_subsurface_view(struct weston_compositor *compositor,
2194 struct weston_subsurface *sub,
2195 struct weston_view *parent)
2196{
2197 struct weston_subsurface *child;
2198 struct weston_view *view = NULL, *iv;
2199
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002200 if (!weston_surface_is_mapped(sub->surface))
2201 return;
2202
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2204 if (iv->geometry.parent == parent) {
2205 view = iv;
2206 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002207 }
2208 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002209
2210 if (view) {
2211 /* Put it back in the surface's list of views */
2212 wl_list_remove(&view->surface_link);
2213 wl_list_insert(&sub->surface->views, &view->surface_link);
2214 } else {
2215 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002216 weston_view_set_position(view,
2217 sub->position.x,
2218 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002219 weston_view_set_transform_parent(view, parent);
2220 }
2221
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002222 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002223 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002224 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225
Pekka Paalanenb188e912013-11-19 14:03:35 +02002226 if (wl_list_empty(&sub->surface->subsurface_list)) {
2227 wl_list_insert(compositor->view_list.prev, &view->link);
2228 return;
2229 }
2230
2231 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2232 if (child->surface == sub->surface)
2233 wl_list_insert(compositor->view_list.prev, &view->link);
2234 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002235 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002236 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002237}
2238
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002239/* This recursively adds the sub-surfaces for a view, relying on the
2240 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2241 * change first happens to the sub-surface list, and then automatically
2242 * propagates here. See weston_surface_damage_subsurfaces() for how the
2243 * sub-surfaces receive damage when the client changes the state.
2244 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002245static void
2246view_list_add(struct weston_compositor *compositor,
2247 struct weston_view *view)
2248{
2249 struct weston_subsurface *sub;
2250
2251 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002252
Pekka Paalanenb188e912013-11-19 14:03:35 +02002253 if (wl_list_empty(&view->surface->subsurface_list)) {
2254 wl_list_insert(compositor->view_list.prev, &view->link);
2255 return;
2256 }
2257
2258 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2259 if (sub->surface == view->surface)
2260 wl_list_insert(compositor->view_list.prev, &view->link);
2261 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002262 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002263 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002264}
2265
2266static void
2267weston_compositor_build_view_list(struct weston_compositor *compositor)
2268{
2269 struct weston_view *view;
2270 struct weston_layer *layer;
2271
2272 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002273 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002274 surface_stash_subsurface_views(view->surface);
2275
2276 wl_list_init(&compositor->view_list);
2277 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002278 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002279 view_list_add(compositor, view);
2280 }
2281 }
2282
2283 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002284 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002285 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002286}
2287
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002288static void
2289weston_output_take_feedback_list(struct weston_output *output,
2290 struct weston_surface *surface)
2291{
2292 struct weston_view *view;
2293 struct weston_presentation_feedback *feedback;
2294 uint32_t flags = 0xffffffff;
2295
2296 if (wl_list_empty(&surface->feedback_list))
2297 return;
2298
2299 /* All views must have the flag for the flag to survive. */
2300 wl_list_for_each(view, &surface->views, surface_link) {
2301 /* ignore views that are not on this output at all */
2302 if (view->output_mask & (1u << output->id))
2303 flags &= view->psf_flags;
2304 }
2305
2306 wl_list_for_each(feedback, &surface->feedback_list, link)
2307 feedback->psf_flags = flags;
2308
2309 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2310 wl_list_init(&surface->feedback_list);
2311}
2312
David Herrmann1edf44c2013-10-22 17:11:26 +02002313static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002314weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002315{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002316 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002317 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002318 struct weston_animation *animation, *next;
2319 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002320 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002321 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002322 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002323 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002324
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002325 if (output->destroying)
2326 return 0;
2327
Pekka Paalanenb5026542014-11-12 15:09:24 +02002328 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2329
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002330 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002331 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002332
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002333 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002334 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002335 } else {
2336 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002337 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002338 ev->psf_flags = 0;
2339 }
2340 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002341
Pekka Paalanene67858b2013-04-25 13:57:42 +03002342 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002343 wl_list_for_each(ev, &ec->view_list, link) {
2344 /* Note: This operation is safe to do multiple times on the
2345 * same surface.
2346 */
2347 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002348 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002349 &ev->surface->frame_callback_list);
2350 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002351
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002352 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002353 }
2354 }
2355
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002356 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002357
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002358 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002359 pixman_region32_intersect(&output_damage,
2360 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002361 pixman_region32_subtract(&output_damage,
2362 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002363
Scott Moreauccbf29d2012-02-22 14:21:41 -07002364 if (output->dirty)
2365 weston_output_update_matrix(output);
2366
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002367 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002368
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002369 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002370
Daniel Stone09a97e22017-03-01 11:34:06 +00002371 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002372 if (r == 0)
2373 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002374
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002375 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002376
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002377 frame_time_msec = timespec_to_msec(&output->frame_time);
2378
Jonas Ådahldb773762012-06-13 00:01:21 +02002379 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002380 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002381 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002382 }
2383
Scott Moreaud64cf212012-06-08 19:40:54 -06002384 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002385 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002386 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002387 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002388
Pekka Paalanenb5026542014-11-12 15:09:24 +02002389 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2390
David Herrmann1edf44c2013-10-22 17:11:26 +02002391 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002392}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002393
Pekka Paalanen82919792014-05-21 13:51:49 +03002394static void
2395weston_output_schedule_repaint_reset(struct weston_output *output)
2396{
Daniel Stone05df8c12017-03-03 16:59:42 +00002397 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002398 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002399}
2400
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002401static int
2402weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2403 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002404{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002405 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002406 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002407 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002408
Daniel Stone6847b852017-03-01 11:34:08 +00002409 /* We're not ready yet; come back to make a decision later. */
2410 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002411 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002412
2413 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2414 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002415 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002416
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002417 /* If we're sleeping, drop the repaint machinery entirely; we will
2418 * explicitly repaint all outputs when we come back. */
2419 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2420 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2421 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002422
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002423 /* We don't actually need to repaint this output; drop it from
2424 * repaint until something causes damage. */
2425 if (!output->repaint_needed)
2426 goto err;
2427
2428 /* If repaint fails, we aren't going to get weston_output_finish_frame
2429 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002430 * something schedules a successful repaint later. As repainting may
2431 * take some time, re-read our clock as a courtesy to the next
2432 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002433 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002434 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002435 if (ret != 0)
2436 goto err;
2437
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002438 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002439
2440err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002441 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002442 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002443}
2444
2445static void
2446output_repaint_timer_arm(struct weston_compositor *compositor)
2447{
2448 struct weston_output *output;
2449 bool any_should_repaint = false;
2450 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002451 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002452
2453 weston_compositor_read_presentation_clock(compositor, &now);
2454
2455 wl_list_for_each(output, &compositor->output_list, link) {
2456 int64_t msec_to_this;
2457
2458 if (output->repaint_status != REPAINT_SCHEDULED)
2459 continue;
2460
2461 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2462 &now);
2463 if (!any_should_repaint || msec_to_this < msec_to_next)
2464 msec_to_next = msec_to_this;
2465
2466 any_should_repaint = true;
2467 }
2468
2469 if (!any_should_repaint)
2470 return;
2471
2472 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2473 * This is a workaround to allow coalescing multiple output repaints
2474 * particularly from weston_output_finish_frame()
2475 * into the same call, which would not happen if we called
2476 * output_repaint_timer_handler() directly.
2477 */
2478 if (msec_to_next < 1)
2479 msec_to_next = 1;
2480
2481 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2482}
2483
2484static int
2485output_repaint_timer_handler(void *data)
2486{
2487 struct weston_compositor *compositor = data;
2488 struct weston_output *output;
2489 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002490 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002491 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002492
2493 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002494
2495 if (compositor->backend->repaint_begin)
2496 repaint_data = compositor->backend->repaint_begin(compositor);
2497
2498 wl_list_for_each(output, &compositor->output_list, link) {
2499 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2500 if (ret)
2501 break;
2502 }
2503
2504 if (ret == 0) {
2505 if (compositor->backend->repaint_flush)
2506 compositor->backend->repaint_flush(compositor,
2507 repaint_data);
2508 } else {
2509 if (compositor->backend->repaint_cancel)
2510 compositor->backend->repaint_cancel(compositor,
2511 repaint_data);
2512 }
Daniel Stone6847b852017-03-01 11:34:08 +00002513
2514 output_repaint_timer_arm(compositor);
2515
Pekka Paalanen0513a952014-05-21 16:17:27 +03002516 return 0;
2517}
2518
Kristian Høgsbergef044142011-06-21 15:02:12 -04002519WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002520weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002521 const struct timespec *stamp,
2522 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002523{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002524 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002525 int32_t refresh_nsec;
2526 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002527 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002528
Pekka Paalanenb5026542014-11-12 15:09:24 +02002529
Daniel Stone05df8c12017-03-03 16:59:42 +00002530 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002531 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2532
Daniel Stone6847b852017-03-01 11:34:08 +00002533 weston_compositor_read_presentation_clock(compositor, &now);
2534
Daniel Stone3615ce12017-03-01 11:34:05 +00002535 /* If we haven't been supplied any timestamp at all, we don't have a
2536 * timebase to work against, so any delay just wastes time. Push a
2537 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002538 if (!stamp) {
2539 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002540 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002541 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002542
Marius Vladdf9278a2018-03-06 18:56:23 +02002543 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2544 TLP_VBLANK(stamp), TLP_END);
2545
Pekka Paalanend7894d02015-07-03 15:08:53 +03002546 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002547 weston_presentation_feedback_present_list(&output->feedback_list,
2548 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002549 output->msc,
2550 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002551
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002552 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002553
Daniel Stone6847b852017-03-01 11:34:08 +00002554 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2555 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2556 -compositor->repaint_msec);
2557 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002558
2559 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002560 static bool warned;
2561
2562 if (!warned)
2563 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002564 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002565 warned = true;
2566
Daniel Stone6847b852017-03-01 11:34:08 +00002567 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002568 }
2569
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002570 /* Called from restart_repaint_loop and restart happens already after
2571 * the deadline given by repaint_msec? In that case we delay until
2572 * the deadline of the next frame, to give clients a more predictable
2573 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002574 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2575 msec_rel < 0) {
2576 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2577 timespec_add_nsec(&output->next_repaint,
2578 &output->next_repaint,
2579 refresh_nsec);
2580 }
2581 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002582
Daniel Stone3615ce12017-03-01 11:34:05 +00002583out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002584 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002585 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002586}
2587
2588static void
2589idle_repaint(void *data)
2590{
2591 struct weston_output *output = data;
2592
Daniel Stone05df8c12017-03-03 16:59:42 +00002593 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2594 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002595 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002596}
2597
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002598WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002599weston_layer_entry_insert(struct weston_layer_entry *list,
2600 struct weston_layer_entry *entry)
2601{
2602 wl_list_insert(&list->link, &entry->link);
2603 entry->layer = list->layer;
2604}
2605
2606WL_EXPORT void
2607weston_layer_entry_remove(struct weston_layer_entry *entry)
2608{
2609 wl_list_remove(&entry->link);
2610 wl_list_init(&entry->link);
2611 entry->layer = NULL;
2612}
2613
Quentin Glidic82681572016-12-17 13:40:51 +01002614
2615/** Initialize the weston_layer struct.
2616 *
2617 * \param compositor The compositor instance
2618 * \param layer The layer to initialize
2619 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002620WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002621weston_layer_init(struct weston_layer *layer,
2622 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002623{
Quentin Glidic82681572016-12-17 13:40:51 +01002624 layer->compositor = compositor;
2625 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002626 wl_list_init(&layer->view_list.link);
2627 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002628 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002629}
2630
2631/** Sets the position of the layer in the layer list. The layer will be placed
2632 * below any layer with the same position value, if any.
2633 * This function is safe to call if the layer is already on the list, but the
2634 * layer may be moved below other layers at the same position, if any.
2635 *
2636 * \param layer The layer to modify
2637 * \param position The position the layer will be placed at
2638 */
2639WL_EXPORT void
2640weston_layer_set_position(struct weston_layer *layer,
2641 enum weston_layer_position position)
2642{
2643 struct weston_layer *below;
2644
2645 wl_list_remove(&layer->link);
2646
2647 /* layer_list is ordered from top to bottom, the last layer being the
2648 * background with the smallest position value */
2649
2650 layer->position = position;
2651 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2652 if (below->position >= layer->position) {
2653 wl_list_insert(&below->link, &layer->link);
2654 return;
2655 }
2656 }
2657 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2658}
2659
2660/** Hide a layer by taking it off the layer list.
2661 * This function is safe to call if the layer is not on the list.
2662 *
2663 * \param layer The layer to hide
2664 */
2665WL_EXPORT void
2666weston_layer_unset_position(struct weston_layer *layer)
2667{
2668 wl_list_remove(&layer->link);
2669 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002670}
2671
2672WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002673weston_layer_set_mask(struct weston_layer *layer,
2674 int x, int y, int width, int height)
2675{
2676 struct weston_view *view;
2677
2678 layer->mask.x1 = x;
2679 layer->mask.x2 = x + width;
2680 layer->mask.y1 = y;
2681 layer->mask.y2 = y + height;
2682
2683 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2684 weston_view_geometry_dirty(view);
2685 }
2686}
2687
2688WL_EXPORT void
2689weston_layer_set_mask_infinite(struct weston_layer *layer)
2690{
2691 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2692 UINT32_MAX, UINT32_MAX);
2693}
2694
2695WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002696weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002697{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002698 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002699 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002700
Bryce Harrington08976ac2016-08-30 12:05:16 -07002701 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2702 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002703 return;
2704
Pekka Paalanenb5026542014-11-12 15:09:24 +02002705 if (!output->repaint_needed)
2706 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2707
Kristian Høgsbergef044142011-06-21 15:02:12 -04002708 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002709 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002710
2711 /* If we already have a repaint scheduled for our idle handler,
2712 * no need to set it again. If the repaint has been called but
2713 * not finished, then weston_output_finish_frame() will notice
2714 * that a repaint is needed and schedule one. */
2715 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002716 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002717
Daniel Stone05df8c12017-03-03 16:59:42 +00002718 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002719 wl_event_loop_add_idle(loop, idle_repaint, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002720 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002721}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002722
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002723WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002724weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2725{
2726 struct weston_output *output;
2727
2728 wl_list_for_each(output, &compositor->output_list, link)
2729 weston_output_schedule_repaint(output);
2730}
2731
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002732static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002733surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002734{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002735 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002736}
2737
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002738static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002739surface_attach(struct wl_client *client,
2740 struct wl_resource *resource,
2741 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2742{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002743 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002744 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002745
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002746 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002747 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002748 if (buffer == NULL) {
2749 wl_client_post_no_memory(client);
2750 return;
2751 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002752 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002753
Pekka Paalanende685b82012-12-04 15:58:12 +02002754 /* Attach, attach, without commit in between does not send
2755 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002756 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002757
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002758 surface->pending.sx = sx;
2759 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002760 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002761}
2762
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002763static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002764surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002765 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002766 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002767{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002768 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002769
Derek Foreman57e92ed2015-11-17 14:11:35 -06002770 if (width <= 0 || height <= 0)
2771 return;
2772
Derek Foreman152254b2015-11-26 14:17:48 -06002773 pixman_region32_union_rect(&surface->pending.damage_surface,
2774 &surface->pending.damage_surface,
2775 x, y, width, height);
2776}
2777
2778static void
2779surface_damage_buffer(struct wl_client *client,
2780 struct wl_resource *resource,
2781 int32_t x, int32_t y, int32_t width, int32_t height)
2782{
2783 struct weston_surface *surface = wl_resource_get_user_data(resource);
2784
2785 if (width <= 0 || height <= 0)
2786 return;
2787
2788 pixman_region32_union_rect(&surface->pending.damage_buffer,
2789 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002790 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002791}
2792
Kristian Høgsberg33418202011-08-16 23:01:28 -04002793static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002794destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002795{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002796 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002797
2798 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002799 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002800}
2801
2802static void
2803surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002804 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002805{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002806 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002807 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002808
2809 cb = malloc(sizeof *cb);
2810 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002811 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002812 return;
2813 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002814
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002815 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2816 callback);
2817 if (cb->resource == NULL) {
2818 free(cb);
2819 wl_resource_post_no_memory(resource);
2820 return;
2821 }
2822
Jason Ekstranda85118c2013-06-27 20:17:02 -05002823 wl_resource_set_implementation(cb->resource, NULL, cb,
2824 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002825
Pekka Paalanenbc106382012-10-10 12:49:31 +03002826 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002827}
2828
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002829static void
2830surface_set_opaque_region(struct wl_client *client,
2831 struct wl_resource *resource,
2832 struct wl_resource *region_resource)
2833{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002834 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002835 struct weston_region *region;
2836
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002837 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002838 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002839 pixman_region32_copy(&surface->pending.opaque,
2840 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002841 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002842 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002843 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002844}
2845
2846static void
2847surface_set_input_region(struct wl_client *client,
2848 struct wl_resource *resource,
2849 struct wl_resource *region_resource)
2850{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002851 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002852 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002853
2854 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002855 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002856 pixman_region32_copy(&surface->pending.input,
2857 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002858 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002859 pixman_region32_fini(&surface->pending.input);
2860 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002861 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002862}
2863
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002864/* Cause damage to this sub-surface and all its children.
2865 *
2866 * This is useful when there are state changes that need an implicit
2867 * damage, e.g. a z-order change.
2868 */
2869static void
2870weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2871{
2872 struct weston_subsurface *child;
2873
2874 weston_surface_damage(sub->surface);
2875 sub->reordered = false;
2876
2877 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2878 if (child != sub)
2879 weston_surface_damage_subsurfaces(child);
2880}
2881
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002882static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002883weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002884{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002885 struct weston_subsurface *sub;
2886
2887 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2888 parent_link_pending) {
2889 wl_list_remove(&sub->parent_link);
2890 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002891
2892 if (sub->reordered)
2893 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002894 }
2895}
2896
2897static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002898weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002899 struct weston_matrix *matrix)
2900{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002901 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002902 double src_width, src_height, dest_width, dest_height;
2903
2904 weston_matrix_init(matrix);
2905
2906 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2907 src_width = surface->width_from_buffer;
2908 src_height = surface->height_from_buffer;
2909 } else {
2910 src_width = wl_fixed_to_double(vp->buffer.src_width);
2911 src_height = wl_fixed_to_double(vp->buffer.src_height);
2912 }
2913
2914 if (vp->surface.width == -1) {
2915 dest_width = src_width;
2916 dest_height = src_height;
2917 } else {
2918 dest_width = vp->surface.width;
2919 dest_height = vp->surface.height;
2920 }
2921
2922 if (src_width != dest_width || src_height != dest_height)
2923 weston_matrix_scale(matrix,
2924 src_width / dest_width,
2925 src_height / dest_height, 1);
2926
2927 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2928 weston_matrix_translate(matrix,
2929 wl_fixed_to_double(vp->buffer.src_x),
2930 wl_fixed_to_double(vp->buffer.src_y),
2931 0);
2932
2933 switch (vp->buffer.transform) {
2934 case WL_OUTPUT_TRANSFORM_FLIPPED:
2935 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2936 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2937 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2938 weston_matrix_scale(matrix, -1, 1, 1);
2939 weston_matrix_translate(matrix,
2940 surface->width_from_buffer, 0, 0);
2941 break;
2942 }
2943
2944 switch (vp->buffer.transform) {
2945 default:
2946 case WL_OUTPUT_TRANSFORM_NORMAL:
2947 case WL_OUTPUT_TRANSFORM_FLIPPED:
2948 break;
2949 case WL_OUTPUT_TRANSFORM_90:
2950 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2951 weston_matrix_rotate_xy(matrix, 0, 1);
2952 weston_matrix_translate(matrix,
2953 surface->height_from_buffer, 0, 0);
2954 break;
2955 case WL_OUTPUT_TRANSFORM_180:
2956 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2957 weston_matrix_rotate_xy(matrix, -1, 0);
2958 weston_matrix_translate(matrix,
2959 surface->width_from_buffer,
2960 surface->height_from_buffer, 0);
2961 break;
2962 case WL_OUTPUT_TRANSFORM_270:
2963 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2964 weston_matrix_rotate_xy(matrix, 0, -1);
2965 weston_matrix_translate(matrix,
2966 0, surface->width_from_buffer, 0);
2967 break;
2968 }
2969
2970 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2971}
2972
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002973/**
2974 * Compute a + b > c while being safe to overflows.
2975 */
2976static bool
2977fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2978{
2979 return (int64_t)a + (int64_t)b > (int64_t)c;
2980}
2981
2982static bool
2983weston_surface_is_pending_viewport_source_valid(
2984 const struct weston_surface *surface)
2985{
2986 const struct weston_surface_state *pend = &surface->pending;
2987 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2988 int width_from_buffer = 0;
2989 int height_from_buffer = 0;
2990 wl_fixed_t w;
2991 wl_fixed_t h;
2992
2993 /* If viewport source rect is not set, it is always ok. */
2994 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2995 return true;
2996
2997 if (pend->newly_attached) {
2998 if (pend->buffer) {
2999 convert_size_by_transform_scale(&width_from_buffer,
3000 &height_from_buffer,
3001 pend->buffer->width,
3002 pend->buffer->height,
3003 vp->buffer.transform,
3004 vp->buffer.scale);
3005 } else {
3006 /* No buffer: viewport is irrelevant. */
3007 return true;
3008 }
3009 } else {
3010 width_from_buffer = surface->width_from_buffer;
3011 height_from_buffer = surface->height_from_buffer;
3012 }
3013
3014 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3015 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3016
3017 /* No buffer: viewport is irrelevant. */
3018 if (width_from_buffer == 0 || height_from_buffer == 0)
3019 return true;
3020
3021 /* overflow checks for wl_fixed_from_int() */
3022 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3023 return false;
3024 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3025 return false;
3026
3027 w = wl_fixed_from_int(width_from_buffer);
3028 h = wl_fixed_from_int(height_from_buffer);
3029
3030 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3031 return false;
3032 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3033 return false;
3034
3035 return true;
3036}
3037
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003038static bool
3039fixed_is_integer(wl_fixed_t v)
3040{
3041 return (v & 0xff) == 0;
3042}
3043
3044static bool
3045weston_surface_is_pending_viewport_dst_size_int(
3046 const struct weston_surface *surface)
3047{
3048 const struct weston_buffer_viewport *vp =
3049 &surface->pending.buffer_viewport;
3050
3051 if (vp->surface.width != -1) {
3052 assert(vp->surface.width > 0 && vp->surface.height > 0);
3053 return true;
3054 }
3055
3056 return fixed_is_integer(vp->buffer.src_width) &&
3057 fixed_is_integer(vp->buffer.src_height);
3058}
3059
Derek Foreman152254b2015-11-26 14:17:48 -06003060/* Translate pending damage in buffer co-ordinates to surface
3061 * co-ordinates and union it with a pixman_region32_t.
3062 * This should only be called after the buffer is attached.
3063 */
3064static void
3065apply_damage_buffer(pixman_region32_t *dest,
3066 struct weston_surface *surface,
3067 struct weston_surface_state *state)
3068{
3069 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3070
3071 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3072 * translated into surface co-ordinates and unioned with
3073 * any other surface damage.
3074 * None of this makes sense if there is no buffer though.
3075 */
3076 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3077 pixman_region32_t buffer_damage;
3078
3079 pixman_region32_intersect_rect(&state->damage_buffer,
3080 &state->damage_buffer,
3081 0, 0, buffer->width,
3082 buffer->height);
3083 pixman_region32_init(&buffer_damage);
3084 weston_matrix_transform_region(&buffer_damage,
3085 &surface->buffer_to_surface_matrix,
3086 &state->damage_buffer);
3087 pixman_region32_union(dest, dest, &buffer_damage);
3088 pixman_region32_fini(&buffer_damage);
3089 }
3090 /* We should clear this on commit even if there was no buffer */
3091 pixman_region32_clear(&state->damage_buffer);
3092}
3093
Jason Ekstrand1e059042014-10-16 10:55:19 -05003094static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003095weston_surface_commit_state(struct weston_surface *surface,
3096 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003097{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003098 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003099 pixman_region32_t opaque;
3100
Alexander Larsson4ea95522013-05-22 14:41:37 +02003101 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003102 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003103 /* wp_viewport.set_source */
3104 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003105 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003106
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003107 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003108 if (state->newly_attached)
3109 weston_surface_attach(surface, state->buffer);
3110 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003111
Jason Ekstrand1e059042014-10-16 10:55:19 -05003112 weston_surface_build_buffer_matrix(surface,
3113 &surface->surface_to_buffer_matrix);
3114 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3115 &surface->surface_to_buffer_matrix);
3116
Jason Ekstrand7b982072014-05-20 14:33:03 -05003117 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003118 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003119 if (surface->committed)
3120 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003121 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003122
Jason Ekstrand7b982072014-05-20 14:33:03 -05003123 state->sx = 0;
3124 state->sy = 0;
3125 state->newly_attached = 0;
3126 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003127
Derek Foreman152254b2015-11-26 14:17:48 -06003128 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003129 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003130 (pixman_region32_not_empty(&state->damage_surface) ||
3131 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003132 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003133
Pekka Paalanen8e159182012-10-10 12:49:25 +03003134 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003135 &state->damage_surface);
3136
3137 apply_damage_buffer(&surface->damage, surface, state);
3138
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003139 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003140 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003141 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003142
3143 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003144 pixman_region32_init(&opaque);
3145 pixman_region32_intersect_rect(&opaque, &state->opaque,
3146 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003147
3148 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3149 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003150 wl_list_for_each(view, &surface->views, surface_link)
3151 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003152 }
3153
3154 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003155
3156 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003157 pixman_region32_intersect_rect(&surface->input, &state->input,
3158 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003159
Pekka Paalanenbc106382012-10-10 12:49:31 +03003160 /* wl_surface.frame */
3161 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003162 &state->frame_callback_list);
3163 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003164
3165 /* XXX:
3166 * What should happen with a feedback request, if there
3167 * is no wl_buffer attached for this commit?
3168 */
3169
3170 /* presentation.feedback */
3171 wl_list_insert_list(&surface->feedback_list,
3172 &state->feedback_list);
3173 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003174
3175 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003176}
3177
3178static void
3179weston_surface_commit(struct weston_surface *surface)
3180{
3181 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003182
Pekka Paalanene67858b2013-04-25 13:57:42 +03003183 weston_surface_commit_subsurface_order(surface);
3184
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003185 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003186}
3187
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003188static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189weston_subsurface_commit(struct weston_subsurface *sub);
3190
3191static void
3192weston_subsurface_parent_commit(struct weston_subsurface *sub,
3193 int parent_is_synchronized);
3194
3195static void
3196surface_commit(struct wl_client *client, struct wl_resource *resource)
3197{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003198 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003199 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3200
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003201 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3202 assert(surface->viewport_resource);
3203
3204 wl_resource_post_error(surface->viewport_resource,
3205 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3206 "wl_surface@%d has viewport source outside buffer",
3207 wl_resource_get_id(resource));
3208 return;
3209 }
3210
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003211 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3212 assert(surface->viewport_resource);
3213
3214 wl_resource_post_error(surface->viewport_resource,
3215 WP_VIEWPORT_ERROR_BAD_SIZE,
3216 "wl_surface@%d viewport dst size not integer",
3217 wl_resource_get_id(resource));
3218 return;
3219 }
3220
Pekka Paalanene67858b2013-04-25 13:57:42 +03003221 if (sub) {
3222 weston_subsurface_commit(sub);
3223 return;
3224 }
3225
3226 weston_surface_commit(surface);
3227
3228 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3229 if (sub->surface != surface)
3230 weston_subsurface_parent_commit(sub, 0);
3231 }
3232}
3233
3234static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003235surface_set_buffer_transform(struct wl_client *client,
3236 struct wl_resource *resource, int transform)
3237{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003238 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003239
Jonny Lamba55f1392014-05-30 12:07:15 +02003240 /* if wl_output.transform grows more members this will need to be updated. */
3241 if (transform < 0 ||
3242 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3243 wl_resource_post_error(resource,
3244 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3245 "buffer transform must be a valid transform "
3246 "('%d' specified)", transform);
3247 return;
3248 }
3249
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003250 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003251 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003252}
3253
Alexander Larsson4ea95522013-05-22 14:41:37 +02003254static void
3255surface_set_buffer_scale(struct wl_client *client,
3256 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003257 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003258{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003259 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003260
Jonny Lamba55f1392014-05-30 12:07:15 +02003261 if (scale < 1) {
3262 wl_resource_post_error(resource,
3263 WL_SURFACE_ERROR_INVALID_SCALE,
3264 "buffer scale must be at least one "
3265 "('%d' specified)", scale);
3266 return;
3267 }
3268
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003269 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003270 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003271}
3272
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003273static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003274 surface_destroy,
3275 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003276 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003277 surface_frame,
3278 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003279 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003280 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003281 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003282 surface_set_buffer_scale,
3283 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003284};
3285
3286static void
3287compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003288 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003289{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003290 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003291 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003292
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003293 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003294 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003295 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003296 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003297 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003298
Jason Ekstranda85118c2013-06-27 20:17:02 -05003299 surface->resource =
3300 wl_resource_create(client, &wl_surface_interface,
3301 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003302 if (surface->resource == NULL) {
3303 weston_surface_destroy(surface);
3304 wl_resource_post_no_memory(resource);
3305 return;
3306 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003307 wl_resource_set_implementation(surface->resource, &surface_interface,
3308 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003309
3310 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003311}
3312
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003313static void
3314destroy_region(struct wl_resource *resource)
3315{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003316 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003317
3318 pixman_region32_fini(&region->region);
3319 free(region);
3320}
3321
3322static void
3323region_destroy(struct wl_client *client, struct wl_resource *resource)
3324{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003325 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003326}
3327
3328static void
3329region_add(struct wl_client *client, struct wl_resource *resource,
3330 int32_t x, int32_t y, int32_t width, int32_t height)
3331{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003332 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003333
3334 pixman_region32_union_rect(&region->region, &region->region,
3335 x, y, width, height);
3336}
3337
3338static void
3339region_subtract(struct wl_client *client, struct wl_resource *resource,
3340 int32_t x, int32_t y, int32_t width, int32_t height)
3341{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003342 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003343 pixman_region32_t rect;
3344
3345 pixman_region32_init_rect(&rect, x, y, width, height);
3346 pixman_region32_subtract(&region->region, &region->region, &rect);
3347 pixman_region32_fini(&rect);
3348}
3349
3350static const struct wl_region_interface region_interface = {
3351 region_destroy,
3352 region_add,
3353 region_subtract
3354};
3355
3356static void
3357compositor_create_region(struct wl_client *client,
3358 struct wl_resource *resource, uint32_t id)
3359{
3360 struct weston_region *region;
3361
3362 region = malloc(sizeof *region);
3363 if (region == NULL) {
3364 wl_resource_post_no_memory(resource);
3365 return;
3366 }
3367
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003368 pixman_region32_init(&region->region);
3369
Jason Ekstranda85118c2013-06-27 20:17:02 -05003370 region->resource =
3371 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003372 if (region->resource == NULL) {
3373 free(region);
3374 wl_resource_post_no_memory(resource);
3375 return;
3376 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003377 wl_resource_set_implementation(region->resource, &region_interface,
3378 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003379}
3380
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003381static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003382 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003383 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003384};
3385
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003386static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003387weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3388{
3389 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003390
Jason Ekstrand7b982072014-05-20 14:33:03 -05003391 weston_surface_commit_state(surface, &sub->cached);
3392 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003393
3394 weston_surface_commit_subsurface_order(surface);
3395
3396 weston_surface_schedule_repaint(surface);
3397
Jason Ekstrand7b982072014-05-20 14:33:03 -05003398 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003399}
3400
3401static void
3402weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3403{
3404 struct weston_surface *surface = sub->surface;
3405
3406 /*
3407 * If this commit would cause the surface to move by the
3408 * attach(dx, dy) parameters, the old damage region must be
3409 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003410 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003411 */
Derek Foreman152254b2015-11-26 14:17:48 -06003412 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003413 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003414 pixman_region32_union(&sub->cached.damage_surface,
3415 &sub->cached.damage_surface,
3416 &surface->pending.damage_surface);
3417 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003418
3419 if (surface->pending.newly_attached) {
3420 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003421 weston_surface_state_set_buffer(&sub->cached,
3422 surface->pending.buffer);
3423 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003424 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003425 weston_presentation_feedback_discard_list(
3426 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003427 }
3428 sub->cached.sx += surface->pending.sx;
3429 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003430
Derek Foreman152254b2015-11-26 14:17:48 -06003431 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3432
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003433 sub->cached.buffer_viewport.changed |=
3434 surface->pending.buffer_viewport.changed;
3435 sub->cached.buffer_viewport.buffer =
3436 surface->pending.buffer_viewport.buffer;
3437 sub->cached.buffer_viewport.surface =
3438 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003439
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003440 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003441
3442 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3443
3444 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3445
3446 wl_list_insert_list(&sub->cached.frame_callback_list,
3447 &surface->pending.frame_callback_list);
3448 wl_list_init(&surface->pending.frame_callback_list);
3449
Pekka Paalanen133e4392014-09-23 22:08:46 -04003450 wl_list_insert_list(&sub->cached.feedback_list,
3451 &surface->pending.feedback_list);
3452 wl_list_init(&surface->pending.feedback_list);
3453
Jason Ekstrand7b982072014-05-20 14:33:03 -05003454 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003455}
3456
Derek Foreman280e7dd2014-10-03 13:13:42 -05003457static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003458weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3459{
3460 while (sub) {
3461 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003462 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003463
3464 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003465 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003466
3467 sub = weston_surface_to_subsurface(sub->parent);
3468 }
3469
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003470 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003471}
3472
3473static void
3474weston_subsurface_commit(struct weston_subsurface *sub)
3475{
3476 struct weston_surface *surface = sub->surface;
3477 struct weston_subsurface *tmp;
3478
3479 /* Recursive check for effectively synchronized. */
3480 if (weston_subsurface_is_synchronized(sub)) {
3481 weston_subsurface_commit_to_cache(sub);
3482 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003483 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003484 /* flush accumulated state from cache */
3485 weston_subsurface_commit_to_cache(sub);
3486 weston_subsurface_commit_from_cache(sub);
3487 } else {
3488 weston_surface_commit(surface);
3489 }
3490
3491 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3492 if (tmp->surface != surface)
3493 weston_subsurface_parent_commit(tmp, 0);
3494 }
3495 }
3496}
3497
3498static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003499weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003500{
3501 struct weston_surface *surface = sub->surface;
3502 struct weston_subsurface *tmp;
3503
Pekka Paalanene67858b2013-04-25 13:57:42 +03003504 /* From now on, commit_from_cache the whole sub-tree, regardless of
3505 * the synchronized mode of each child. This sub-surface or some
3506 * of its ancestors were synchronized, so we are synchronized
3507 * all the way down.
3508 */
3509
Jason Ekstrand7b982072014-05-20 14:33:03 -05003510 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003511 weston_subsurface_commit_from_cache(sub);
3512
3513 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3514 if (tmp->surface != surface)
3515 weston_subsurface_parent_commit(tmp, 1);
3516 }
3517}
3518
3519static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003520weston_subsurface_parent_commit(struct weston_subsurface *sub,
3521 int parent_is_synchronized)
3522{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003523 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003524 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003525 wl_list_for_each(view, &sub->surface->views, surface_link)
3526 weston_view_set_position(view,
3527 sub->position.x,
3528 sub->position.y);
3529
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003530 sub->position.set = 0;
3531 }
3532
3533 if (parent_is_synchronized || sub->synchronized)
3534 weston_subsurface_synchronized_commit(sub);
3535}
3536
Pekka Paalanen8274d902014-08-06 19:36:51 +03003537static int
3538subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3539{
3540 return snprintf(buf, len, "sub-surface");
3541}
3542
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003543static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003544subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003545{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003546 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003547
Jason Ekstranda7af7042013-10-12 22:38:11 -05003548 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003549 weston_view_set_position(view,
3550 view->geometry.x + dx,
3551 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003552
3553 /* No need to check parent mappedness, because if parent is not
3554 * mapped, parent is not in a visible layer, so this sub-surface
3555 * will not be drawn either.
3556 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003557
Pekka Paalanene67858b2013-04-25 13:57:42 +03003558 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003559 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003560
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003561 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003562 * because that would call it also for the parent surface,
3563 * which might not be mapped yet. That would lead to
3564 * inconsistent state, where the window could never be
3565 * mapped.
3566 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003567 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003568 * weston_surface_is_mapped() return true, so that when the
3569 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003570 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003571 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003572 }
3573}
3574
3575static struct weston_subsurface *
3576weston_surface_to_subsurface(struct weston_surface *surface)
3577{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003578 if (surface->committed == subsurface_committed)
3579 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003580
3581 return NULL;
3582}
3583
Pekka Paalanen01388e22013-04-25 13:57:44 +03003584WL_EXPORT struct weston_surface *
3585weston_surface_get_main_surface(struct weston_surface *surface)
3586{
3587 struct weston_subsurface *sub;
3588
3589 while (surface && (sub = weston_surface_to_subsurface(surface)))
3590 surface = sub->parent;
3591
3592 return surface;
3593}
3594
Pekka Paalanen50b67472014-10-01 15:02:41 +03003595WL_EXPORT int
3596weston_surface_set_role(struct weston_surface *surface,
3597 const char *role_name,
3598 struct wl_resource *error_resource,
3599 uint32_t error_code)
3600{
3601 assert(role_name);
3602
3603 if (surface->role_name == NULL ||
3604 surface->role_name == role_name ||
3605 strcmp(surface->role_name, role_name) == 0) {
3606 surface->role_name = role_name;
3607
3608 return 0;
3609 }
3610
3611 wl_resource_post_error(error_resource, error_code,
3612 "Cannot assign role %s to wl_surface@%d,"
3613 " already has role %s\n",
3614 role_name,
3615 wl_resource_get_id(surface->resource),
3616 surface->role_name);
3617 return -1;
3618}
3619
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003620WL_EXPORT const char *
3621weston_surface_get_role(struct weston_surface *surface)
3622{
3623 return surface->role_name;
3624}
3625
Pekka Paalanen8274d902014-08-06 19:36:51 +03003626WL_EXPORT void
3627weston_surface_set_label_func(struct weston_surface *surface,
3628 int (*desc)(struct weston_surface *,
3629 char *, size_t))
3630{
3631 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003632 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003633}
3634
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003635/** Get the size of surface contents
3636 *
3637 * \param surface The surface to query.
3638 * \param width Returns the width of raw contents.
3639 * \param height Returns the height of raw contents.
3640 *
3641 * Retrieves the raw surface content size in pixels for the given surface.
3642 * This is the whole content size in buffer pixels. If the surface
3643 * has no content or the renderer does not implement this feature,
3644 * zeroes are returned.
3645 *
3646 * This function is used to determine the buffer size needed for
3647 * a weston_surface_copy_content() call.
3648 */
3649WL_EXPORT void
3650weston_surface_get_content_size(struct weston_surface *surface,
3651 int *width, int *height)
3652{
3653 struct weston_renderer *rer = surface->compositor->renderer;
3654
3655 if (!rer->surface_get_content_size) {
3656 *width = 0;
3657 *height = 0;
3658 return;
3659 }
3660
3661 rer->surface_get_content_size(surface, width, height);
3662}
3663
Quentin Glidic248dd102016-08-12 10:41:34 +02003664/** Get the bounding box of a surface and its subsurfaces
3665 *
3666 * \param surface The surface to query.
3667 * \return The bounding box relative to the surface origin.
3668 *
3669 */
3670WL_EXPORT struct weston_geometry
3671weston_surface_get_bounding_box(struct weston_surface *surface)
3672{
3673 pixman_region32_t region;
3674 pixman_box32_t *box;
3675 struct weston_subsurface *subsurface;
3676
3677 pixman_region32_init_rect(&region,
3678 0, 0,
3679 surface->width, surface->height);
3680
3681 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3682 pixman_region32_union_rect(&region, &region,
3683 subsurface->position.x,
3684 subsurface->position.y,
3685 subsurface->surface->width,
3686 subsurface->surface->height);
3687
3688 box = pixman_region32_extents(&region);
3689 struct weston_geometry geometry = {
3690 .x = box->x1,
3691 .y = box->y1,
3692 .width = box->x2 - box->x1,
3693 .height = box->y2 - box->y1,
3694 };
3695
3696 pixman_region32_fini(&region);
3697
3698 return geometry;
3699}
3700
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003701/** Copy surface contents to system memory.
3702 *
3703 * \param surface The surface to copy from.
3704 * \param target Pointer to the target memory buffer.
3705 * \param size Size of the target buffer in bytes.
3706 * \param src_x X location on contents to copy from.
3707 * \param src_y Y location on contents to copy from.
3708 * \param width Width in pixels of the area to copy.
3709 * \param height Height in pixels of the area to copy.
3710 * \return 0 for success, -1 for failure.
3711 *
3712 * Surface contents are maintained by the renderer. They can be in a
3713 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3714 * else.
3715 *
3716 * Surface contents are copied into memory pointed to by target,
3717 * which has size bytes of space available. The target memory
3718 * may be larger than needed, but being smaller returns an error.
3719 * The extra bytes in target may or may not be written; their content is
3720 * unspecified. Size must be large enough to hold the image.
3721 *
3722 * The image in the target memory will be arranged in rows from
3723 * top to bottom, and pixels on a row from left to right. The pixel
3724 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3725 * width * 4.
3726 *
3727 * Parameters src_x and src_y define the upper-left corner in buffer
3728 * coordinates (pixels) to copy from. Parameters width and height
3729 * define the size of the area to copy in pixels.
3730 *
3731 * The rectangle defined by src_x, src_y, width, height must fit in
3732 * the surface contents. Otherwise an error is returned.
3733 *
3734 * Use surface_get_data_size to determine the content size; the
3735 * needed target buffer size and rectangle limits.
3736 *
3737 * CURRENT IMPLEMENTATION RESTRICTIONS:
3738 * - the machine must be little-endian due to Pixman formats.
3739 *
3740 * NOTE: Pixman formats are premultiplied.
3741 */
3742WL_EXPORT int
3743weston_surface_copy_content(struct weston_surface *surface,
3744 void *target, size_t size,
3745 int src_x, int src_y,
3746 int width, int height)
3747{
3748 struct weston_renderer *rer = surface->compositor->renderer;
3749 int cw, ch;
3750 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3751
3752 if (!rer->surface_copy_content)
3753 return -1;
3754
3755 weston_surface_get_content_size(surface, &cw, &ch);
3756
3757 if (src_x < 0 || src_y < 0)
3758 return -1;
3759
3760 if (width <= 0 || height <= 0)
3761 return -1;
3762
3763 if (src_x + width > cw || src_y + height > ch)
3764 return -1;
3765
3766 if (width * bytespp * height > size)
3767 return -1;
3768
3769 return rer->surface_copy_content(surface, target, size,
3770 src_x, src_y, width, height);
3771}
3772
Pekka Paalanene67858b2013-04-25 13:57:42 +03003773static void
3774subsurface_set_position(struct wl_client *client,
3775 struct wl_resource *resource, int32_t x, int32_t y)
3776{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003777 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003778
3779 if (!sub)
3780 return;
3781
3782 sub->position.x = x;
3783 sub->position.y = y;
3784 sub->position.set = 1;
3785}
3786
3787static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003788subsurface_find_sibling(struct weston_subsurface *sub,
3789 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003790{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003791 struct weston_surface *parent = sub->parent;
3792 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003793
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003794 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3795 if (sibling->surface == surface && sibling != sub)
3796 return sibling;
3797 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003798
3799 return NULL;
3800}
3801
3802static struct weston_subsurface *
3803subsurface_sibling_check(struct weston_subsurface *sub,
3804 struct weston_surface *surface,
3805 const char *request)
3806{
3807 struct weston_subsurface *sibling;
3808
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003809 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003810 if (!sibling) {
3811 wl_resource_post_error(sub->resource,
3812 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3813 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003814 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003815 return NULL;
3816 }
3817
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003818 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003819
3820 return sibling;
3821}
3822
3823static void
3824subsurface_place_above(struct wl_client *client,
3825 struct wl_resource *resource,
3826 struct wl_resource *sibling_resource)
3827{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003828 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003829 struct weston_surface *surface =
3830 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003831 struct weston_subsurface *sibling;
3832
3833 if (!sub)
3834 return;
3835
3836 sibling = subsurface_sibling_check(sub, surface, "place_above");
3837 if (!sibling)
3838 return;
3839
3840 wl_list_remove(&sub->parent_link_pending);
3841 wl_list_insert(sibling->parent_link_pending.prev,
3842 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003843
3844 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003845}
3846
3847static void
3848subsurface_place_below(struct wl_client *client,
3849 struct wl_resource *resource,
3850 struct wl_resource *sibling_resource)
3851{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003852 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003853 struct weston_surface *surface =
3854 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003855 struct weston_subsurface *sibling;
3856
3857 if (!sub)
3858 return;
3859
3860 sibling = subsurface_sibling_check(sub, surface, "place_below");
3861 if (!sibling)
3862 return;
3863
3864 wl_list_remove(&sub->parent_link_pending);
3865 wl_list_insert(&sibling->parent_link_pending,
3866 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003867
3868 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003869}
3870
3871static void
3872subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3873{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003874 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003875
3876 if (sub)
3877 sub->synchronized = 1;
3878}
3879
3880static void
3881subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3882{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003883 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003884
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003885 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003886 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003887
3888 /* If sub became effectively desynchronized, flush. */
3889 if (!weston_subsurface_is_synchronized(sub))
3890 weston_subsurface_synchronized_commit(sub);
3891 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003892}
3893
3894static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003895weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3896{
3897 wl_list_remove(&sub->parent_link);
3898 wl_list_remove(&sub->parent_link_pending);
3899 wl_list_remove(&sub->parent_destroy_listener.link);
3900 sub->parent = NULL;
3901}
3902
3903static void
3904weston_subsurface_destroy(struct weston_subsurface *sub);
3905
3906static void
3907subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3908{
3909 struct weston_subsurface *sub =
3910 container_of(listener, struct weston_subsurface,
3911 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003912 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003913
3914 /* The protocol object (wl_resource) is left inert. */
3915 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003916 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003917
3918 weston_subsurface_destroy(sub);
3919}
3920
3921static void
3922subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3923{
3924 struct weston_subsurface *sub =
3925 container_of(listener, struct weston_subsurface,
3926 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003927 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003928 assert(sub->surface != sub->parent);
3929
3930 if (weston_surface_is_mapped(sub->surface))
3931 weston_surface_unmap(sub->surface);
3932
3933 weston_subsurface_unlink_parent(sub);
3934}
3935
3936static void
3937subsurface_resource_destroy(struct wl_resource *resource)
3938{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003939 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003940
3941 if (sub)
3942 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003943}
3944
3945static void
3946subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3947{
3948 wl_resource_destroy(resource);
3949}
3950
3951static void
3952weston_subsurface_link_parent(struct weston_subsurface *sub,
3953 struct weston_surface *parent)
3954{
3955 sub->parent = parent;
3956 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003957 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003958 &sub->parent_destroy_listener);
3959
3960 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3961 wl_list_insert(&parent->subsurface_list_pending,
3962 &sub->parent_link_pending);
3963}
3964
3965static void
3966weston_subsurface_link_surface(struct weston_subsurface *sub,
3967 struct weston_surface *surface)
3968{
3969 sub->surface = surface;
3970 sub->surface_destroy_listener.notify =
3971 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003972 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003973 &sub->surface_destroy_listener);
3974}
3975
3976static void
3977weston_subsurface_destroy(struct weston_subsurface *sub)
3978{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003979 struct weston_view *view, *next;
3980
Pekka Paalanene67858b2013-04-25 13:57:42 +03003981 assert(sub->surface);
3982
3983 if (sub->resource) {
3984 assert(weston_surface_to_subsurface(sub->surface) == sub);
3985 assert(sub->parent_destroy_listener.notify ==
3986 subsurface_handle_parent_destroy);
3987
George Kiagiadakised04d382014-06-13 18:10:26 +02003988 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3989 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003990 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003991 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003992
Pekka Paalanene67858b2013-04-25 13:57:42 +03003993 if (sub->parent)
3994 weston_subsurface_unlink_parent(sub);
3995
Jason Ekstrand7b982072014-05-20 14:33:03 -05003996 weston_surface_state_fini(&sub->cached);
3997 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003998
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003999 sub->surface->committed = NULL;
4000 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004001 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004002 } else {
4003 /* the dummy weston_subsurface for the parent itself */
4004 assert(sub->parent_destroy_listener.notify == NULL);
4005 wl_list_remove(&sub->parent_link);
4006 wl_list_remove(&sub->parent_link_pending);
4007 }
4008
4009 wl_list_remove(&sub->surface_destroy_listener.link);
4010 free(sub);
4011}
4012
4013static const struct wl_subsurface_interface subsurface_implementation = {
4014 subsurface_destroy,
4015 subsurface_set_position,
4016 subsurface_place_above,
4017 subsurface_place_below,
4018 subsurface_set_sync,
4019 subsurface_set_desync
4020};
4021
4022static struct weston_subsurface *
4023weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4024 struct weston_surface *parent)
4025{
4026 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004027 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004028
Bryce Harringtonde16d892014-11-20 22:21:57 -08004029 sub = zalloc(sizeof *sub);
4030 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004031 return NULL;
4032
Jason Ekstranda7af7042013-10-12 22:38:11 -05004033 wl_list_init(&sub->unused_views);
4034
Jason Ekstranda85118c2013-06-27 20:17:02 -05004035 sub->resource =
4036 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004037 if (!sub->resource) {
4038 free(sub);
4039 return NULL;
4040 }
4041
Jason Ekstranda85118c2013-06-27 20:17:02 -05004042 wl_resource_set_implementation(sub->resource,
4043 &subsurface_implementation,
4044 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004045 weston_subsurface_link_surface(sub, surface);
4046 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004047 weston_surface_state_init(&sub->cached);
4048 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004049 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004050
4051 return sub;
4052}
4053
4054/* Create a dummy subsurface for having the parent itself in its
4055 * sub-surface lists. Makes stacking order manipulation easy.
4056 */
4057static struct weston_subsurface *
4058weston_subsurface_create_for_parent(struct weston_surface *parent)
4059{
4060 struct weston_subsurface *sub;
4061
Bryce Harringtonde16d892014-11-20 22:21:57 -08004062 sub = zalloc(sizeof *sub);
4063 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004064 return NULL;
4065
4066 weston_subsurface_link_surface(sub, parent);
4067 sub->parent = parent;
4068 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4069 wl_list_insert(&parent->subsurface_list_pending,
4070 &sub->parent_link_pending);
4071
4072 return sub;
4073}
4074
4075static void
4076subcompositor_get_subsurface(struct wl_client *client,
4077 struct wl_resource *resource,
4078 uint32_t id,
4079 struct wl_resource *surface_resource,
4080 struct wl_resource *parent_resource)
4081{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004082 struct weston_surface *surface =
4083 wl_resource_get_user_data(surface_resource);
4084 struct weston_surface *parent =
4085 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004086 struct weston_subsurface *sub;
4087 static const char where[] = "get_subsurface: wl_subsurface@";
4088
4089 if (surface == parent) {
4090 wl_resource_post_error(resource,
4091 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4092 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004093 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004094 return;
4095 }
4096
4097 if (weston_surface_to_subsurface(surface)) {
4098 wl_resource_post_error(resource,
4099 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4100 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004101 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004102 return;
4103 }
4104
Pekka Paalanen50b67472014-10-01 15:02:41 +03004105 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4106 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004107 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004108
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004109 if (weston_surface_get_main_surface(parent) == surface) {
4110 wl_resource_post_error(resource,
4111 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4112 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004113 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004114 return;
4115 }
4116
Pekka Paalanene67858b2013-04-25 13:57:42 +03004117 /* make sure the parent is in its own list */
4118 if (wl_list_empty(&parent->subsurface_list)) {
4119 if (!weston_subsurface_create_for_parent(parent)) {
4120 wl_resource_post_no_memory(resource);
4121 return;
4122 }
4123 }
4124
4125 sub = weston_subsurface_create(id, surface, parent);
4126 if (!sub) {
4127 wl_resource_post_no_memory(resource);
4128 return;
4129 }
4130
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004131 surface->committed = subsurface_committed;
4132 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004133 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004134}
4135
4136static void
4137subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4138{
4139 wl_resource_destroy(resource);
4140}
4141
4142static const struct wl_subcompositor_interface subcompositor_interface = {
4143 subcompositor_destroy,
4144 subcompositor_get_subsurface
4145};
4146
4147static void
4148bind_subcompositor(struct wl_client *client,
4149 void *data, uint32_t version, uint32_t id)
4150{
4151 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004152 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004153
Jason Ekstranda85118c2013-06-27 20:17:02 -05004154 resource =
4155 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004156 if (resource == NULL) {
4157 wl_client_post_no_memory(client);
4158 return;
4159 }
4160 wl_resource_set_implementation(resource, &subcompositor_interface,
4161 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004162}
4163
Bryce Harrington0795ece2016-08-30 12:04:26 -07004164/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004165 *
4166 * \param compositor The compositor instance
4167 * \param state The DPMS state the outputs will be set to
4168 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004169static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004170weston_compositor_dpms(struct weston_compositor *compositor,
4171 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004172{
4173 struct weston_output *output;
4174
Bryce Harrington08976ac2016-08-30 12:05:16 -07004175 wl_list_for_each(output, &compositor->output_list, link)
4176 if (output->set_dpms)
4177 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004178}
4179
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004180/** Restores the compositor to active status
4181 *
4182 * \param compositor The compositor instance
4183 *
4184 * If the compositor was in a sleeping mode, all outputs are powered
4185 * back on via DPMS. Otherwise if the compositor was inactive
4186 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4187 * signal will fire.
4188 *
4189 * Restarts the idle timer.
4190 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004191WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004192weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004193{
Neil Roberts8b62e202013-09-30 13:14:47 +01004194 uint32_t old_state = compositor->state;
4195
4196 /* The state needs to be changed before emitting the wake
4197 * signal because that may try to schedule a repaint which
4198 * will not work if the compositor is still sleeping */
4199 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4200
4201 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004202 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004203 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004204 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004205 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004206 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004207 /* fall through */
4208 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004209 wl_event_source_timer_update(compositor->idle_source,
4210 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004211 }
4212}
4213
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004214/** Turns off rendering and frame events for the compositor.
4215 *
4216 * \param compositor The compositor instance
4217 *
4218 * This is used for example to prevent further rendering while the
4219 * compositor is shutting down.
4220 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004221 * Stops the idle timer.
4222 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004223WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004224weston_compositor_offscreen(struct weston_compositor *compositor)
4225{
4226 switch (compositor->state) {
4227 case WESTON_COMPOSITOR_OFFSCREEN:
4228 return;
4229 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004230 default:
4231 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4232 wl_event_source_timer_update(compositor->idle_source, 0);
4233 }
4234}
4235
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004236/** Powers down all attached output devices
4237 *
4238 * \param compositor The compositor instance
4239 *
4240 * Causes rendering to the outputs to cease, and no frame events to be
4241 * sent. Only powers down the outputs if the compositor is not already
4242 * in sleep mode.
4243 *
4244 * Stops the idle timer.
4245 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004246WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004247weston_compositor_sleep(struct weston_compositor *compositor)
4248{
4249 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4250 return;
4251
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004252 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004253 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4254 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4255}
4256
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004257/** Sets compositor to idle mode
4258 *
4259 * \param data The compositor instance
4260 *
4261 * This is called when the idle timer fires. Once the compositor is in
4262 * idle mode it requires a wake action (e.g. via
4263 * weston_compositor_wake()) to restore it. The compositor's
4264 * idle_signal will be triggered when the idle event occurs.
4265 *
4266 * Idleness can be inhibited by setting the compositor's idle_inhibit
4267 * property.
4268 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004269static int
4270idle_handler(void *data)
4271{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004272 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004273
4274 if (compositor->idle_inhibit)
4275 return 1;
4276
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004277 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004278 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004279
4280 return 1;
4281}
4282
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004283WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004284weston_plane_init(struct weston_plane *plane,
4285 struct weston_compositor *ec,
4286 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004287{
4288 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004289 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004290 plane->x = x;
4291 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004292 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004293
4294 /* Init the link so that the call to wl_list_remove() when releasing
4295 * the plane without ever stacking doesn't lead to a crash */
4296 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004297}
4298
4299WL_EXPORT void
4300weston_plane_release(struct weston_plane *plane)
4301{
Xiong Zhang97116532013-10-23 13:58:31 +08004302 struct weston_view *view;
4303
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004304 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004305 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004306
Xiong Zhang97116532013-10-23 13:58:31 +08004307 wl_list_for_each(view, &plane->compositor->view_list, link) {
4308 if (view->plane == plane)
4309 view->plane = NULL;
4310 }
4311
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004312 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004313}
4314
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004315WL_EXPORT void
4316weston_compositor_stack_plane(struct weston_compositor *ec,
4317 struct weston_plane *plane,
4318 struct weston_plane *above)
4319{
4320 if (above)
4321 wl_list_insert(above->link.prev, &plane->link);
4322 else
4323 wl_list_insert(&ec->plane_list, &plane->link);
4324}
4325
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004326static void
4327output_release(struct wl_client *client, struct wl_resource *resource)
4328{
4329 wl_resource_destroy(resource);
4330}
4331
4332static const struct wl_output_interface output_interface = {
4333 output_release,
4334};
4335
4336
Casey Dahlin9074db52012-04-19 22:50:09 -04004337static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004338{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004339 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004340}
4341
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004342static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004343bind_output(struct wl_client *client,
4344 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004345{
Pekka Paalanen05347622017-03-27 12:24:34 +03004346 struct weston_head *head = data;
4347 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004348 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004349 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004350
Jason Ekstranda85118c2013-06-27 20:17:02 -05004351 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004352 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004353 if (resource == NULL) {
4354 wl_client_post_no_memory(client);
4355 return;
4356 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004357
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004358 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004359 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004360 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004361
Pekka Paalanen05347622017-03-27 12:24:34 +03004362 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004363 wl_output_send_geometry(resource,
4364 output->x,
4365 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004366 head->mm_width,
4367 head->mm_height,
4368 head->subpixel,
4369 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004370 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004371 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004372 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004373 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004374
4375 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004376 wl_output_send_mode(resource,
4377 mode->flags,
4378 mode->width,
4379 mode->height,
4380 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004381 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004382
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004383 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004384 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004385}
4386
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004387/** Remove the global wl_output protocol object
4388 *
4389 * \param head The head whose global to remove.
4390 *
4391 * Also orphans the wl_resources for this head (wl_output).
4392 */
4393static void
4394weston_head_remove_global(struct weston_head *head)
4395{
4396 struct wl_resource *resource, *tmp;
4397
4398 if (head->global)
4399 wl_global_destroy(head->global);
4400 head->global = NULL;
4401
4402 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4403 unbind_resource(resource);
4404 wl_resource_set_destructor(resource, NULL);
4405 wl_resource_set_user_data(resource, NULL);
4406 }
4407}
4408
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004409/** Get the backing object of wl_output
4410 *
4411 * \param resource A wl_output protocol object.
4412 * \return The backing object (user data) of a wl_resource representing a
4413 * wl_output protocol object.
4414 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004415WL_EXPORT struct weston_head *
4416weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004417{
4418 assert(wl_resource_instance_of(resource, &wl_output_interface,
4419 &output_interface));
4420
4421 return wl_resource_get_user_data(resource);
4422}
4423
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004424/** Initialize a pre-allocated weston_head
4425 *
4426 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004427 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004428 *
4429 * The head will be safe to attach, detach and release.
4430 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004431 * The name is used in logs, and can be used by compositors as a configuration
4432 * identifier.
4433 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004434 * \memberof weston_head
4435 * \internal
4436 */
4437static void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004438weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004439{
4440 /* Add some (in)sane defaults which can be used
4441 * for checking if an output was properly configured
4442 */
4443 memset(head, 0, sizeof *head);
4444
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004445 wl_list_init(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004446 wl_list_init(&head->output_link);
4447 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004448 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004449}
4450
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004451/** Idle task for emitting heads_changed_signal */
4452static void
4453weston_compositor_call_heads_changed(void *data)
4454{
4455 struct weston_compositor *compositor = data;
4456
4457 compositor->heads_changed_source = NULL;
4458
4459 wl_signal_emit(&compositor->heads_changed_signal, compositor);
4460}
4461
4462/** Schedule a call on idle to heads_changed callback
4463 *
4464 * \param compositor The Compositor.
4465 *
4466 * \memberof weston_compositor
4467 * \internal
4468 */
4469static void
4470weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4471{
4472 struct wl_event_loop *loop;
4473
4474 if (compositor->heads_changed_source)
4475 return;
4476
4477 loop = wl_display_get_event_loop(compositor->wl_display);
4478 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4479 weston_compositor_call_heads_changed, compositor);
4480}
4481
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004482/** Register a new head
4483 *
4484 * \param compositor The compositor.
4485 * \param head The head to register, must not be already registered.
4486 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004487 * This signals the core that a new head has become available, leading to
4488 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004489 *
4490 * \memberof weston_compositor
4491 * \internal
4492 */
4493static void
4494weston_compositor_add_head(struct weston_compositor *compositor,
4495 struct weston_head *head)
4496{
4497 assert(wl_list_empty(&head->compositor_link));
4498 assert(head->name);
4499
4500 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4501 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004502 weston_compositor_schedule_heads_changed(compositor);
4503}
4504
4505/** Adds a listener to be called when heads change
4506 *
4507 * \param compositor The compositor.
4508 * \param listener The listener to add.
4509 *
4510 * The listener notify function argument is the \var compositor.
4511 *
4512 * The listener function will be called after heads are added or their
4513 * connection status has changed. Several changes may be accumulated into a
4514 * single call. The user is expected to iterate over the existing heads and
4515 * check their statuses to find out what changed.
4516 *
4517 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4518 * weston_head_is_enabled
4519 * \memberof weston_compositor
4520 */
4521WL_EXPORT void
4522weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4523 struct wl_listener *listener)
4524{
4525 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004526}
4527
4528/** Iterate over available heads
4529 *
4530 * \param compositor The compositor.
4531 * \param item The iterator, or NULL for start.
4532 * \return The next available head in the list.
4533 *
4534 * Returns all available heads, regardless of being connected or enabled.
4535 *
4536 * You can iterate over all heads as follows:
4537 * \code
4538 * struct weston_head *head = NULL;
4539 *
4540 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4541 * ...
4542 * }
4543 * \endcode
4544 *
4545 * If you cause \c iter to be removed from the list, you cannot use it to
4546 * continue iterating. Removing any other item is safe.
4547 *
4548 * \memberof weston_compositor
4549 */
4550WL_EXPORT struct weston_head *
4551weston_compositor_iterate_heads(struct weston_compositor *compositor,
4552 struct weston_head *iter)
4553{
4554 struct wl_list *list = &compositor->head_list;
4555 struct wl_list *node;
4556
4557 assert(compositor);
4558 assert(!iter || iter->compositor == compositor);
4559
4560 if (iter)
4561 node = iter->compositor_link.next;
4562 else
4563 node = list->next;
4564
4565 assert(node);
4566 assert(!iter || node != &iter->compositor_link);
4567
4568 if (node == list)
4569 return NULL;
4570
4571 return container_of(node, struct weston_head, compositor_link);
4572}
4573
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004574/** Attach a head to an inactive output
4575 *
4576 * \param output The output to attach to.
4577 * \param head The head that is not yet attached.
4578 * \return 0 on success, -1 on failure.
4579 *
4580 * Attaches the given head to the output. All heads of an output are clones
4581 * and share the resolution and timings.
4582 *
4583 * Cloning heads this way uses less resources than creating an output for
4584 * each head, but is not always possible due to environment, driver and hardware
4585 * limitations.
4586 *
4587 * On failure, the head remains unattached. Success of this function does not
4588 * guarantee the output configuration is actually valid. The final checks are
4589 * made on weston_output_enable().
4590 *
4591 * \memberof weston_output
4592 */
4593static int
4594weston_output_attach_head(struct weston_output *output,
4595 struct weston_head *head)
4596{
4597 if (output->enabled)
4598 return -1;
4599
4600 if (!wl_list_empty(&head->output_link))
4601 return -1;
4602
4603 /* XXX: no support for multi-head yet */
4604 if (!wl_list_empty(&output->head_list))
4605 return -1;
4606
4607 head->output = output;
4608 wl_list_insert(output->head_list.prev, &head->output_link);
4609
4610 return 0;
4611}
4612
4613/** Detach a head from its output
4614 *
4615 * \param head The head to detach.
4616 *
4617 * It is safe to detach a non-attached head.
4618 *
4619 * \memberof weston_head
4620 */
4621static void
4622weston_head_detach(struct weston_head *head)
4623{
4624 wl_list_remove(&head->output_link);
4625 wl_list_init(&head->output_link);
4626 head->output = NULL;
4627}
4628
4629/** Destroy a head
4630 *
4631 * \param head The head to be released.
4632 *
4633 * Destroys the head. The caller is responsible for freeing the memory pointed
4634 * to by \c head.
4635 *
4636 * \memberof weston_head
4637 * \internal
4638 */
4639static void
4640weston_head_release(struct weston_head *head)
4641{
4642 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004643
4644 free(head->make);
4645 free(head->model);
4646 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004647 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004648
4649 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004650}
4651
Pekka Paalanen01f60212017-03-24 15:39:24 +02004652/** Store monitor make, model and serial number
4653 *
4654 * \param head The head to modify.
4655 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
4656 * any string, or NULL for none.
4657 * \param model The monitor model or name, or a made-up string, or NULL for
4658 * none.
4659 * \param serialno The monitor serial number, a made-up string, or NULL for
4660 * none.
4661 *
4662 * \memberof weston_head
4663 * \internal
4664 */
4665WL_EXPORT void
4666weston_head_set_monitor_strings(struct weston_head *head,
4667 const char *make,
4668 const char *model,
4669 const char *serialno)
4670{
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004671 free(head->make);
4672 free(head->model);
4673 free(head->serial_number);
4674
4675 head->make = make ? strdup(make) : NULL;
4676 head->model = model ? strdup(model) : NULL;
4677 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanen01f60212017-03-24 15:39:24 +02004678}
4679
4680/** Store physical image size
4681 *
4682 * \param head The head to modify.
4683 * \param mm_width Image area width in millimeters.
4684 * \param mm_height Image area height in millimeters.
4685 *
4686 * \memberof weston_head
4687 * \internal
4688 */
4689WL_EXPORT void
4690weston_head_set_physical_size(struct weston_head *head,
4691 int32_t mm_width, int32_t mm_height)
4692{
4693 head->mm_width = mm_width;
4694 head->mm_height = mm_height;
4695}
4696
4697/** Store monitor sub-pixel layout
4698 *
4699 * \param head The head to modify.
4700 * \param sp Sub-pixel layout. The possible values are:
4701 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
4702 * - WL_OUTPUT_SUBPIXEL_NONE,
4703 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
4704 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
4705 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
4706 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
4707 *
4708 * \memberof weston_head
4709 * \internal
4710 */
4711WL_EXPORT void
4712weston_head_set_subpixel(struct weston_head *head,
4713 enum wl_output_subpixel sp)
4714{
4715 head->subpixel = sp;
4716}
4717
4718/** Mark the monitor as internal
4719 *
4720 * This is used for embedded screens, like laptop panels.
4721 *
4722 * \param head The head to mark as internal.
4723 *
4724 * By default a head is external. The type is often inferred from the physical
4725 * connector type.
4726 *
4727 * \memberof weston_head
4728 * \internal
4729 */
4730WL_EXPORT void
4731weston_head_set_internal(struct weston_head *head)
4732{
4733 head->connection_internal = true;
4734}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004735
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004736/** Store connector status
4737 *
4738 * \param head The head to modify.
4739 * \param connected Whether the head is connected.
4740 *
4741 * Connectors are created as disconnected. This function can be used to
4742 * set the connector status.
4743 *
4744 * The status should be set to true when a physical connector is connected to
4745 * a video sink device like a monitor and to false when the connector is
4746 * disconnected. For nested backends, the connection status should reflect the
4747 * connection to the parent display server.
4748 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004749 * When the connection status changes, it schedules a call to the heads_changed
4750 * hook.
4751 *
4752 * \sa weston_compositor_set_heads_changed_cb
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004753 * \memberof weston_head
4754 * \internal
4755 */
4756WL_EXPORT void
4757weston_head_set_connection_status(struct weston_head *head, bool connected)
4758{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004759 if (head->connected == connected)
4760 return;
4761
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004762 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004763
4764 if (head->compositor)
4765 weston_compositor_schedule_heads_changed(head->compositor);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004766}
4767
4768/** Is the head currently connected?
4769 *
4770 * \param head The head to query.
4771 * \return Connection status.
4772 *
4773 * Returns true if the head is physically connected to a monitor, or in
4774 * case of a nested backend returns true when there is a connection to the
4775 * parent display server.
4776 *
4777 * This is independent from the head being enabled.
4778 *
4779 * \sa weston_head_is_enabled
4780 * \memberof weston_head
4781 */
4782WL_EXPORT bool
4783weston_head_is_connected(struct weston_head *head)
4784{
4785 return head->connected;
4786}
4787
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02004788/** Is the head currently enabled?
4789 *
4790 * \param head The head to query.
4791 * \return Video status.
4792 *
4793 * Returns true if the head is currently transmitting a video stream.
4794 *
4795 * This is independent of the head being connected.
4796 *
4797 * \sa weston_head_is_connected
4798 * \memberof weston_head
4799 */
4800WL_EXPORT bool
4801weston_head_is_enabled(struct weston_head *head)
4802{
4803 if (!head->output)
4804 return false;
4805
4806 return head->output->enabled;
4807}
4808
David Fort0de859e2016-05-27 23:22:57 +02004809/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004810static void
David Fort0de859e2016-05-27 23:22:57 +02004811weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4812 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004813{
4814 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004815 bool start_resizing = false;
4816
4817 if (!delta_width)
4818 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004819
4820 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004821 if (output == resized_output) {
4822 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004823 continue;
4824 }
4825
David Fort0de859e2016-05-27 23:22:57 +02004826 if (start_resizing) {
4827 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004828 output->dirty = 1;
4829 }
4830 }
4831}
4832
Pekka Paalanend72bad22017-03-29 17:01:41 +03004833static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004834weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004835{
Scott Moreau850ca422012-05-21 15:21:25 -06004836 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004837
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004838 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004839 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004840
Scott Moreauccbf29d2012-02-22 14:21:41 -07004841 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004842 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004843 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004844 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004845 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004846 weston_matrix_scale(&output->matrix, magnification,
4847 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004848 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004849
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004850 switch (output->transform) {
4851 case WL_OUTPUT_TRANSFORM_FLIPPED:
4852 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4853 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4854 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4855 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4856 weston_matrix_scale(&output->matrix, -1, 1, 1);
4857 break;
4858 }
4859
4860 switch (output->transform) {
4861 default:
4862 case WL_OUTPUT_TRANSFORM_NORMAL:
4863 case WL_OUTPUT_TRANSFORM_FLIPPED:
4864 break;
4865 case WL_OUTPUT_TRANSFORM_90:
4866 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4867 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4868 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4869 break;
4870 case WL_OUTPUT_TRANSFORM_180:
4871 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4872 weston_matrix_translate(&output->matrix,
4873 -output->width, -output->height, 0);
4874 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4875 break;
4876 case WL_OUTPUT_TRANSFORM_270:
4877 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4878 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4879 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4880 break;
4881 }
4882
4883 if (output->current_scale != 1)
4884 weston_matrix_scale(&output->matrix,
4885 output->current_scale,
4886 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004887
Scott Moreauccbf29d2012-02-22 14:21:41 -07004888 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004889
4890 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004891}
4892
Scott Moreau1bad5db2012-08-18 01:04:05 -06004893static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004894weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004895{
4896 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004897 output->native_scale = scale;
4898 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004899
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004900 convert_size_by_transform_scale(&output->width, &output->height,
4901 output->current_mode->width,
4902 output->current_mode->height,
4903 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004904}
4905
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004906static void
4907weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004908{
4909 output->x = x;
4910 output->y = y;
4911
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004912 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004913 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004914
4915 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004916 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004917 output->width,
4918 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004919}
4920
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004921WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004922weston_output_move(struct weston_output *output, int x, int y)
4923{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004924 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004925 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004926 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004927
4928 output->move_x = x - output->x;
4929 output->move_y = y - output->y;
4930
4931 if (output->move_x == 0 && output->move_y == 0)
4932 return;
4933
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004934 weston_output_init_geometry(output, x, y);
4935
4936 output->dirty = 1;
4937
4938 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004939 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004940
4941 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004942 wl_list_for_each(head, &output->head_list, output_link) {
4943 wl_resource_for_each(resource, &head->resource_list) {
4944 wl_output_send_geometry(resource,
4945 output->x,
4946 output->y,
4947 head->mm_width,
4948 head->mm_height,
4949 head->subpixel,
4950 head->make,
4951 head->model,
4952 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004953
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004954 ver = wl_resource_get_version(resource);
4955 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
4956 wl_output_send_done(resource);
4957 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08004958 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004959}
4960
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004961/** Signal that a pending output is taken into use.
4962 *
4963 * Removes the output from the pending list and adds it to the compositor's
4964 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03004965 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004966 * The output gets an internal ID assigned, and the wl_output global is
4967 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004968 *
Giulio Camuffob1147152015-05-06 21:41:57 +03004969 * \param compositor The compositor instance.
4970 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004971 *
4972 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03004973 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03004974static void
Giulio Camuffob1147152015-05-06 21:41:57 +03004975weston_compositor_add_output(struct weston_compositor *compositor,
4976 struct weston_output *output)
4977{
Armin Krezoviće5403842016-08-05 15:28:29 +02004978 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004979 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02004980
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004981 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004982
4983 /* Verify we haven't reached the limit of 32 available output IDs */
4984 assert(ffs(~compositor->output_id_pool) > 0);
4985
4986 /* Invert the output id pool and look for the lowest numbered
4987 * switch (the least significant bit). Take that bit's position
4988 * as our ID, and mark it used in the compositor's output_id_pool.
4989 */
4990 output->id = ffs(~compositor->output_id_pool) - 1;
4991 compositor->output_id_pool |= 1u << output->id;
4992
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004993 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03004994 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004995 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004996
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004997 wl_list_for_each(head, &output->head_list, output_link) {
4998 head->global = wl_global_create(compositor->wl_display,
4999 &wl_output_interface, 3,
5000 head, bind_output);
5001 }
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005002
Giulio Camuffob1147152015-05-06 21:41:57 +03005003 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005004
5005 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5006 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005007}
5008
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005009/** Transform device coordinates into global coordinates
5010 *
5011 * \param device_x[in] X coordinate in device units.
5012 * \param device_y[in] Y coordinate in device units.
5013 * \param x[out] X coordinate in the global space.
5014 * \param y[out] Y coordinate in the global space.
5015 *
5016 * Transforms coordinates from the device coordinate space
5017 * (physical pixel units) to the global coordinate space (logical pixel units).
5018 * This takes into account output transform and scale.
5019 *
5020 * \memberof weston_output
5021 * \internal
5022 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005023WL_EXPORT void
5024weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005025 double device_x, double device_y,
5026 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005027{
Derek Foreman0f679412014-10-02 13:41:17 -05005028 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005029 device_x,
5030 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005031 0.0,
5032 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005033
Derek Foreman67a18b92015-03-24 11:36:14 -05005034 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005035
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005036 *x = p.f[0] / p.f[3];
5037 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005038}
5039
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005040/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005041 *
5042 * \param output The weston_output object that is being removed.
5043 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005044 * The following happens:
5045 *
5046 * - The output assignments of all views in the current scenegraph are
5047 * recomputed.
5048 *
5049 * - Presentation feedback is discarded.
5050 *
5051 * - Compositor is notified that outputs were changed and
5052 * applies the necessary changes to re-layout outputs.
5053 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005054 * - The output is put back in the pending outputs list.
5055 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005056 * - Signal is emitted to notify all users of the weston_output
5057 * object that the output is being destroyed.
5058 *
5059 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005060 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005061 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005062 * - The output's internal ID is released.
5063 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005064 * \memberof weston_output
5065 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005066 */
5067static void
5068weston_compositor_remove_output(struct weston_output *output)
5069{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005070 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005071 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005072 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005073
5074 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005075 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005076
Pekka Paalanenbccda712017-03-29 16:16:04 +03005077 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005078 if (view->output_mask & (1u << output->id))
5079 weston_view_assign_output(view);
5080 }
5081
5082 weston_presentation_feedback_discard_list(&output->feedback_list);
5083
Pekka Paalanenbccda712017-03-29 16:16:04 +03005084 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005085
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005086 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005087 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005088 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005089
Pekka Paalanenbccda712017-03-29 16:16:04 +03005090 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005091 wl_signal_emit(&output->destroy_signal, output);
5092
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005093 wl_list_for_each(head, &output->head_list, output_link)
5094 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005095
5096 compositor->output_id_pool &= ~(1u << output->id);
5097 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005098}
5099
5100/** Sets the output scale for a given output.
5101 *
5102 * \param output The weston_output object that the scale is set for.
5103 * \param scale Scale factor for the given output.
5104 *
5105 * It only supports setting scale for an output that
5106 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005107 *
5108 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005109 */
5110WL_EXPORT void
5111weston_output_set_scale(struct weston_output *output,
5112 int32_t scale)
5113{
5114 /* We can only set scale on a disabled output */
5115 assert(!output->enabled);
5116
5117 /* We only want to set scale once */
5118 assert(!output->scale);
5119
5120 output->scale = scale;
5121}
5122
5123/** Sets the output transform for a given output.
5124 *
5125 * \param output The weston_output object that the transform is set for.
5126 * \param transform Transform value for the given output.
5127 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005128 * Refer to wl_output::transform section located at
5129 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5130 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005131 *
5132 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005133 */
5134WL_EXPORT void
5135weston_output_set_transform(struct weston_output *output,
5136 uint32_t transform)
5137{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005138 struct weston_pointer_motion_event ev;
5139 struct wl_resource *resource;
5140 struct weston_seat *seat;
5141 pixman_region32_t old_region;
5142 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005143 struct weston_head *head;
5144 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005145
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005146 if (!output->enabled && output->transform == UINT32_MAX) {
5147 output->transform = transform;
5148 return;
5149 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005150
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005151 weston_output_transform_scale_init(output, transform, output->scale);
5152
5153 pixman_region32_init(&old_region);
5154 pixman_region32_copy(&old_region, &output->region);
5155
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005156 weston_output_init_geometry(output, output->x, output->y);
5157
5158 output->dirty = 1;
5159
5160 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005161 wl_list_for_each(head, &output->head_list, output_link) {
5162 wl_resource_for_each(resource, &head->resource_list) {
5163 wl_output_send_geometry(resource,
5164 output->x,
5165 output->y,
5166 head->mm_width,
5167 head->mm_height,
5168 head->subpixel,
5169 head->make,
5170 head->model,
5171 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005172
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005173 ver = wl_resource_get_version(resource);
5174 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5175 wl_output_send_done(resource);
5176 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005177 }
5178
5179 /* we must ensure that pointers are inside output, otherwise they disappear */
5180 mid_x = output->x + output->width / 2;
5181 mid_y = output->y + output->height / 2;
5182
5183 ev.mask = WESTON_POINTER_MOTION_ABS;
5184 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5185 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5186
5187 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5188 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5189
5190 if (pointer && pixman_region32_contains_point(&old_region,
5191 wl_fixed_to_int(pointer->x),
5192 wl_fixed_to_int(pointer->y),
5193 NULL))
5194 weston_pointer_move(pointer, &ev);
5195 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005196}
5197
5198/** Initializes a weston_output object with enough data so
5199 ** an output can be configured.
5200 *
5201 * \param output The weston_output object to initialize
5202 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005203 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005204 *
5205 * Sets initial values for fields that are expected to be
5206 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005207 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005208 * The name is used in logs, and can be used by compositors as a configuration
5209 * identifier.
5210 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005211 * \memberof weston_output
5212 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005213 */
5214WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005215weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005216 struct weston_compositor *compositor,
5217 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005218{
5219 output->compositor = compositor;
5220 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005221 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005222 wl_list_init(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005223 output->enabled = false;
5224
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005225 wl_list_init(&output->head_list);
5226
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005227 weston_head_init(&output->head, name);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005228 weston_head_set_connection_status(&output->head, true);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005229 weston_compositor_add_head(compositor, &output->head);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005230
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005231 /* Add some (in)sane defaults which can be used
5232 * for checking if an output was properly configured
5233 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005234 output->scale = 0;
5235 /* Can't use -1 on uint32_t and 0 is valid enum value */
5236 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005237
5238 pixman_region32_init(&output->previous_damage);
5239 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005240 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005241}
5242
5243/** Adds weston_output object to pending output list.
5244 *
5245 * \param output The weston_output object to add
5246 * \param compositor The compositor instance.
5247 *
5248 * Also notifies the compositor that an output is pending for
5249 * configuration.
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005250 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005251 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005252 *
5253 * \memberof weston_output
5254 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005255 */
5256WL_EXPORT void
5257weston_compositor_add_pending_output(struct weston_output *output,
5258 struct weston_compositor *compositor)
5259{
Pekka Paalanene952a012017-03-29 17:14:00 +03005260 assert(output->disable);
5261 assert(output->enable);
5262
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005263 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005264 wl_list_insert(compositor->pending_output_list.prev, &output->link);
5265 wl_signal_emit(&compositor->output_pending_signal, output);
5266}
5267
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005268/** Constructs a weston_output object that can be used by the compositor.
5269 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005270 * \param output The weston_output object that needs to be enabled. Must not
5271 * be enabled already.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005272 *
5273 * Output coordinates are calculated and each new output is by default
5274 * assigned to the right of previous one.
5275 *
5276 * Sets up the transformation, zoom, and geometry of the output using
5277 * the properties that need to be configured by the compositor.
5278 *
5279 * Establishes a repaint timer for the output with the relevant display
5280 * object's event loop. See output_repaint_timer_handler().
5281 *
5282 * The output is assigned an ID. Weston can support up to 32 distinct
5283 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5284 * is referred to and used to find the first available ID number, and
5285 * then this ID is marked as used in output_id_pool.
5286 *
5287 * The output is also assigned a Wayland global with the wl_output
5288 * external interface.
5289 *
5290 * Backend specific function is called to set up the output output.
5291 *
5292 * Output is added to the compositor's output list
5293 *
5294 * If the backend specific function fails, the weston_output object
5295 * is returned to a state it was before calling this function and
5296 * is added to the compositor's pending_output_list in case it needs
5297 * to be reconfigured or just so it can be destroyed at shutdown.
5298 *
5299 * 0 is returned on success, -1 on failure.
5300 */
5301WL_EXPORT int
5302weston_output_enable(struct weston_output *output)
5303{
Armin Krezović782f5df2016-09-30 14:11:11 +02005304 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005305 struct weston_output *iterator;
5306 int x = 0, y = 0;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005307 int ret;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005308
Pekka Paalanencc201e42017-03-30 15:11:25 +03005309 if (output->enabled) {
5310 weston_log("Error: attempt to enable an enabled output '%s'\n",
5311 output->name);
5312 return -1;
5313 }
5314
Armin Krezović782f5df2016-09-30 14:11:11 +02005315 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005316 struct weston_output, link);
5317
Armin Krezović782f5df2016-09-30 14:11:11 +02005318 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005319 x = iterator->x + iterator->width;
5320
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005321 /* Make sure the scale is set up */
5322 assert(output->scale);
5323
5324 /* Make sure we have a transform set */
5325 assert(output->transform != UINT32_MAX);
5326
Armin Krezović782f5df2016-09-30 14:11:11 +02005327 output->x = x;
5328 output->y = y;
5329 output->dirty = 1;
5330 output->original_scale = output->scale;
5331
5332 weston_output_transform_scale_init(output, output->transform, output->scale);
5333 weston_output_init_zoom(output);
5334
5335 weston_output_init_geometry(output, x, y);
5336 weston_output_damage(output);
5337
5338 wl_signal_init(&output->frame_signal);
5339 wl_signal_init(&output->destroy_signal);
5340 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005341 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005342
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005343 /* XXX: Temporary until all backends are converted. */
5344 if (wl_list_empty(&output->head_list)) {
5345 ret = weston_output_attach_head(output, &output->head);
5346 assert(ret == 0);
5347 }
5348
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005349 /* Enable the output (set up the crtc or create a
5350 * window representing the output, set up the
5351 * renderer, etc)
5352 */
5353 if (output->enable(output) < 0) {
5354 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005355 return -1;
5356 }
5357
5358 weston_compositor_add_output(output->compositor, output);
5359
5360 return 0;
5361}
5362
5363/** Converts a weston_output object to a pending output state, so it
5364 ** can be configured again or destroyed.
5365 *
5366 * \param output The weston_output object that needs to be disabled.
5367 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005368 * Calls a backend specific function to disable an output, in case
5369 * such function exists.
5370 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005371 * The backend specific disable function may choose to postpone the disabling
5372 * by returning a negative value, in which case this function returns early.
5373 * In that case the backend will guarantee the output will be disabled soon
5374 * by the backend calling this function again. One must not attempt to re-enable
5375 * the output until that happens.
5376 *
5377 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005378 * from weston's output_list (see weston_compositor_remove_output())
5379 * and is returned to a state it was before weston_output_enable()
5380 * was ran (see weston_output_enable_undo()).
5381 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005382 * See weston_output_init() for more information on the
5383 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005384 *
5385 * If the output has never been enabled yet, this function can still be
5386 * called to ensure that the output is actually turned off rather than left
5387 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005388 */
5389WL_EXPORT void
5390weston_output_disable(struct weston_output *output)
5391{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005392 /* Should we rename this? */
5393 output->destroying = 1;
5394
Pekka Paalanenc65df642017-03-29 15:45:46 +03005395 /* Disable is called unconditionally also for not-enabled outputs,
5396 * because at compositor start-up, if there is an output that is
5397 * already on but the compositor wants to turn it off, we have to
5398 * forward the turn-off to the backend so it knows to do it.
5399 * The backend cannot initially turn off everything, because it
5400 * would cause unnecessary mode-sets for all outputs the compositor
5401 * wants to be on.
5402 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005403 if (output->disable(output) < 0)
5404 return;
5405
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005406 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005407 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005408
5409 output->destroying = 0;
5410}
5411
5412/** Emits a signal to indicate that there are outputs waiting to be configured.
5413 *
5414 * \param compositor The compositor instance
5415 */
5416WL_EXPORT void
5417weston_pending_output_coldplug(struct weston_compositor *compositor)
5418{
5419 struct weston_output *output, *next;
5420
5421 wl_list_for_each_safe(output, next, &compositor->pending_output_list, link)
5422 wl_signal_emit(&compositor->output_pending_signal, output);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005423
5424 /* Execute the heads changed callback manually to ensure it is
5425 * processed before any plugins get their start-up idle tasks ran.
5426 * This ensures the plugins see all the initial outputs.
5427 */
5428 if (compositor->heads_changed_source) {
5429 wl_event_source_remove(compositor->heads_changed_source);
5430 weston_compositor_call_heads_changed(compositor);
5431 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005432}
5433
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005434/** Uninitialize an output
5435 *
5436 * Removes the output from the list of enabled outputs if necessary, but
5437 * does not call the backend's output disable function. The output will no
5438 * longer be in the list of pending outputs either.
5439 *
5440 * All fields of weston_output become uninitialized, i.e. should not be used
5441 * anymore. The caller can free the memory after this.
5442 *
5443 * \memberof weston_output
5444 * \internal
5445 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005446WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005447weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005448{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005449 struct weston_head *head, *tmp;
5450
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005451 output->destroying = 1;
5452
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005453 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005454 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005455
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005456 pixman_region32_fini(&output->region);
5457 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005458 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005459
5460 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
5461 weston_head_detach(head);
5462
5463 weston_head_release(&output->head);
5464
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005465 free(output->name);
5466}
5467
Pekka Paalanencf0a4762017-04-04 16:36:07 +03005468/** When you need a head...
5469 *
5470 * This function is a hack, used until all code has been converted to become
5471 * multi-head aware.
5472 *
5473 * \param output The weston_output whose head to get.
5474 * \return The first head in the output's list.
5475 */
5476WL_EXPORT struct weston_head *
5477weston_output_get_first_head(struct weston_output *output)
5478{
5479 if (wl_list_empty(&output->head_list))
5480 return NULL;
5481
5482 return container_of(output->head_list.next,
5483 struct weston_head, output_link);
5484}
5485
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01005486static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005487destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005488{
Jonny Lamb74130762013-11-26 18:19:46 +01005489 struct weston_surface *surface =
5490 wl_resource_get_user_data(resource);
5491
Pekka Paalanen4826f872016-04-22 14:14:38 +03005492 if (!surface)
5493 return;
5494
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005495 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02005496 surface->pending.buffer_viewport.buffer.src_width =
5497 wl_fixed_from_int(-1);
5498 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005499 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005500}
5501
5502static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005503viewport_destroy(struct wl_client *client,
5504 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005505{
5506 wl_resource_destroy(resource);
5507}
5508
5509static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005510viewport_set_source(struct wl_client *client,
5511 struct wl_resource *resource,
5512 wl_fixed_t src_x,
5513 wl_fixed_t src_y,
5514 wl_fixed_t src_width,
5515 wl_fixed_t src_height)
5516{
5517 struct weston_surface *surface =
5518 wl_resource_get_user_data(resource);
5519
Pekka Paalanen4826f872016-04-22 14:14:38 +03005520 if (!surface) {
5521 wl_resource_post_error(resource,
5522 WP_VIEWPORT_ERROR_NO_SURFACE,
5523 "wl_surface for this viewport is no longer exists");
5524 return;
5525 }
5526
5527 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03005528 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005529
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005530 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03005531 src_height == wl_fixed_from_int(-1) &&
5532 src_x == wl_fixed_from_int(-1) &&
5533 src_y == wl_fixed_from_int(-1)) {
5534 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005535 surface->pending.buffer_viewport.buffer.src_width =
5536 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005537 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005538 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005539 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005540
Pekka Paalanen201769a2016-04-26 14:42:11 +03005541 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005542 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005543 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03005544 "wl_surface@%d viewport source "
5545 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
5546 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005547 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03005548 wl_fixed_to_double(src_height),
5549 wl_fixed_to_double(src_x),
5550 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005551 return;
5552 }
5553
5554 surface->pending.buffer_viewport.buffer.src_x = src_x;
5555 surface->pending.buffer_viewport.buffer.src_y = src_y;
5556 surface->pending.buffer_viewport.buffer.src_width = src_width;
5557 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005558 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005559}
5560
5561static void
5562viewport_set_destination(struct wl_client *client,
5563 struct wl_resource *resource,
5564 int32_t dst_width,
5565 int32_t dst_height)
5566{
5567 struct weston_surface *surface =
5568 wl_resource_get_user_data(resource);
5569
Pekka Paalanen4826f872016-04-22 14:14:38 +03005570 if (!surface) {
5571 wl_resource_post_error(resource,
5572 WP_VIEWPORT_ERROR_NO_SURFACE,
5573 "wl_surface for this viewport no longer exists");
5574 return;
5575 }
5576
5577 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005578
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005579 if (dst_width == -1 && dst_height == -1) {
5580 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005581 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005582 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005583 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005584 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005585
5586 if (dst_width <= 0 || dst_height <= 0) {
5587 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005588 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005589 "destination size must be positive (%dx%d)",
5590 dst_width, dst_height);
5591 return;
5592 }
5593
5594 surface->pending.buffer_viewport.surface.width = dst_width;
5595 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005596 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005597}
5598
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005599static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005600 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005601 viewport_set_source,
5602 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01005603};
5604
5605static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005606viewporter_destroy(struct wl_client *client,
5607 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005608{
5609 wl_resource_destroy(resource);
5610}
5611
5612static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005613viewporter_get_viewport(struct wl_client *client,
5614 struct wl_resource *viewporter,
5615 uint32_t id,
5616 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005617{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005618 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005619 struct weston_surface *surface =
5620 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005621 struct wl_resource *resource;
5622
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005623 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005624 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005625 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005626 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01005627 return;
5628 }
5629
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005630 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005631 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005632 if (resource == NULL) {
5633 wl_client_post_no_memory(client);
5634 return;
5635 }
5636
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005637 wl_resource_set_implementation(resource, &viewport_interface,
5638 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01005639
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005640 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005641}
5642
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005643static const struct wp_viewporter_interface viewporter_interface = {
5644 viewporter_destroy,
5645 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01005646};
5647
5648static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005649bind_viewporter(struct wl_client *client,
5650 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005651{
5652 struct wl_resource *resource;
5653
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005654 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005655 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005656 if (resource == NULL) {
5657 wl_client_post_no_memory(client);
5658 return;
5659 }
5660
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005661 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01005662 NULL, NULL);
5663}
5664
5665static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04005666destroy_presentation_feedback(struct wl_resource *feedback_resource)
5667{
5668 struct weston_presentation_feedback *feedback;
5669
5670 feedback = wl_resource_get_user_data(feedback_resource);
5671
5672 wl_list_remove(&feedback->link);
5673 free(feedback);
5674}
5675
5676static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005677presentation_destroy(struct wl_client *client, struct wl_resource *resource)
5678{
5679 wl_resource_destroy(resource);
5680}
5681
5682static void
5683presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005684 struct wl_resource *presentation_resource,
5685 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005686 uint32_t callback)
5687{
Pekka Paalanen133e4392014-09-23 22:08:46 -04005688 struct weston_surface *surface;
5689 struct weston_presentation_feedback *feedback;
5690
5691 surface = wl_resource_get_user_data(surface_resource);
5692
Bryce Harringtonde16d892014-11-20 22:21:57 -08005693 feedback = zalloc(sizeof *feedback);
5694 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04005695 goto err_calloc;
5696
5697 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005698 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005699 1, callback);
5700 if (!feedback->resource)
5701 goto err_create;
5702
5703 wl_resource_set_implementation(feedback->resource, NULL, feedback,
5704 destroy_presentation_feedback);
5705
5706 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
5707
5708 return;
5709
5710err_create:
5711 free(feedback);
5712
5713err_calloc:
5714 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005715}
5716
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005717static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005718 presentation_destroy,
5719 presentation_feedback
5720};
5721
5722static void
5723bind_presentation(struct wl_client *client,
5724 void *data, uint32_t version, uint32_t id)
5725{
5726 struct weston_compositor *compositor = data;
5727 struct wl_resource *resource;
5728
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005729 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005730 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005731 if (resource == NULL) {
5732 wl_client_post_no_memory(client);
5733 return;
5734 }
5735
5736 wl_resource_set_implementation(resource, &presentation_implementation,
5737 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005738 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005739}
5740
5741static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05005742compositor_bind(struct wl_client *client,
5743 void *data, uint32_t version, uint32_t id)
5744{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005745 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005746 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05005747
Jason Ekstranda85118c2013-06-27 20:17:02 -05005748 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005749 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005750 if (resource == NULL) {
5751 wl_client_post_no_memory(client);
5752 return;
5753 }
5754
5755 wl_resource_set_implementation(resource, &compositor_interface,
5756 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05005757}
5758
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005759WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005760weston_environment_get_fd(const char *env)
5761{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005762 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005763 int fd, flags;
5764
5765 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005766 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005767 return -1;
5768
5769 flags = fcntl(fd, F_GETFD);
5770 if (flags == -1)
5771 return -1;
5772
5773 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
5774 unsetenv(env);
5775
5776 return fd;
5777}
5778
Pekka Paalanenb5026542014-11-12 15:09:24 +02005779static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005780timeline_key_binding_handler(struct weston_keyboard *keyboard,
5781 const struct timespec *time, uint32_t key,
5782 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02005783{
5784 struct weston_compositor *compositor = data;
5785
5786 if (weston_timeline_enabled_)
5787 weston_timeline_close();
5788 else
5789 weston_timeline_open(compositor);
5790}
5791
Giulio Camuffo459137b2014-10-11 23:56:24 +03005792/** Create the compositor.
5793 *
5794 * This functions creates and initializes a compositor instance.
5795 *
5796 * \param display The Wayland display to be used.
5797 * \param user_data A pointer to an object that can later be retrieved
5798 * using the \ref weston_compositor_get_user_data function.
5799 * \return The compositor instance on success or NULL on failure.
5800 */
5801WL_EXPORT struct weston_compositor *
5802weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005803{
Giulio Camuffo459137b2014-10-11 23:56:24 +03005804 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05005805 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07005806
Giulio Camuffo459137b2014-10-11 23:56:24 +03005807 ec = zalloc(sizeof *ec);
5808 if (!ec)
5809 return NULL;
5810
5811 ec->wl_display = display;
5812 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005813 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07005814 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005815 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005816 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005817 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005818 wl_signal_init(&ec->idle_signal);
5819 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005820 wl_signal_init(&ec->show_input_panel_signal);
5821 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005822 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01005823 wl_signal_init(&ec->seat_created_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005824 wl_signal_init(&ec->output_pending_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01005825 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02005826 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005827 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02005828 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005829 wl_signal_init(&ec->heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005830 wl_signal_init(&ec->session_signal);
5831 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005832
Casey Dahlin58ba1372012-04-19 22:50:08 -04005833 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03005834 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04005835
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02005836 ec->activate_serial = 1;
5837
Derek Foreman152254b2015-11-26 14:17:48 -06005838 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005839 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005840 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05005841
Giulio Camuffo954f1832014-10-11 18:27:30 +03005842 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005843 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005844 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03005845
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005846 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005847 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005848 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005849
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005850 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005851 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005852 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005853
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005854 if (weston_input_init(ec) != 0)
5855 goto fail;
5856
Jason Ekstranda7af7042013-10-12 22:38:11 -05005857 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005858 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005859 wl_list_init(&ec->layer_list);
5860 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005861 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005862 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005863 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005864 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01005865 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005866 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005867 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005868 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005869 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005870
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005871 wl_list_init(&ec->plugin_api_list);
5872
Xiong Zhang97116532013-10-23 13:58:31 +08005873 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005874 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005875
Giulio Camuffo459137b2014-10-11 23:56:24 +03005876 wl_data_device_manager_init(ec->wl_display);
5877
5878 wl_display_init_shm(ec->wl_display);
5879
5880 loop = wl_display_get_event_loop(ec->wl_display);
5881 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00005882 ec->repaint_timer =
5883 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
5884 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005885
Quentin Glidic82681572016-12-17 13:40:51 +01005886 weston_layer_init(&ec->fade_layer, ec);
5887 weston_layer_init(&ec->cursor_layer, ec);
5888
5889 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
5890 weston_layer_set_position(&ec->cursor_layer,
5891 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005892
5893 weston_compositor_add_debug_binding(ec, KEY_T,
5894 timeline_key_binding_handler, ec);
5895
Giulio Camuffo459137b2014-10-11 23:56:24 +03005896 return ec;
5897
5898fail:
5899 free(ec);
5900 return NULL;
5901}
5902
Benjamin Franzkeb8263022011-08-30 11:32:47 +02005903WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005904weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07005905{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005906 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07005907
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005908 wl_event_source_remove(ec->idle_source);
5909
Matt Roper361d2ad2011-08-29 13:52:23 -07005910 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02005911 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07005912 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02005913
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005914 /* Destroy all pending outputs associated with this compositor */
5915 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
5916 output->destroy(output);
5917
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02005918 if (ec->renderer)
5919 ec->renderer->destroy(ec);
5920
Daniel Stone325fc2d2012-05-30 16:31:58 +01005921 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09005922 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005923 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005924 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005925 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005926 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005927
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005928 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07005929}
5930
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005931WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04005932weston_compositor_exit_with_code(struct weston_compositor *compositor,
5933 int exit_code)
5934{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02005935 if (compositor->exit_code == EXIT_SUCCESS)
5936 compositor->exit_code = exit_code;
5937
Giulio Camuffo459137b2014-10-11 23:56:24 +03005938 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04005939}
5940
5941WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005942weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
5943 const struct weston_pointer_grab_interface *interface)
5944{
5945 struct weston_seat *seat;
5946
5947 ec->default_pointer_grab = interface;
5948 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005949 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5950
5951 if (pointer)
5952 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005953 }
5954}
5955
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04005956WL_EXPORT int
5957weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
5958 clockid_t clk_id)
5959{
5960 struct timespec ts;
5961
5962 if (clock_gettime(clk_id, &ts) < 0)
5963 return -1;
5964
5965 compositor->presentation_clock = clk_id;
5966
5967 return 0;
5968}
5969
5970/*
5971 * For choosing the software clock, when the display hardware or API
5972 * does not expose a compatible presentation timestamp.
5973 */
5974WL_EXPORT int
5975weston_compositor_set_presentation_clock_software(
5976 struct weston_compositor *compositor)
5977{
5978 /* In order of preference */
5979 static const clockid_t clocks[] = {
5980 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
5981 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
5982 CLOCK_MONOTONIC, /* no jumps, may crawl */
5983 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
5984 CLOCK_REALTIME /* may jump and crawl */
5985 };
5986 unsigned i;
5987
5988 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
5989 if (weston_compositor_set_presentation_clock(compositor,
5990 clocks[i]) == 0)
5991 return 0;
5992
5993 weston_log("Error: no suitable presentation clock available.\n");
5994
5995 return -1;
5996}
5997
Pekka Paalanen662f3842015-03-18 12:17:26 +02005998/** Read the current time from the Presentation clock
5999 *
6000 * \param compositor
6001 * \param ts[out] The current time.
6002 *
6003 * \note Reading the current time in user space is always imprecise to some
6004 * degree.
6005 *
6006 * This function is never meant to fail. If reading the clock does fail,
6007 * an error message is logged and a zero time is returned. Callers are not
6008 * supposed to detect or react to failures.
6009 */
6010WL_EXPORT void
6011weston_compositor_read_presentation_clock(
6012 const struct weston_compositor *compositor,
6013 struct timespec *ts)
6014{
6015 static bool warned;
6016 int ret;
6017
6018 ret = clock_gettime(compositor->presentation_clock, ts);
6019 if (ret < 0) {
6020 ts->tv_sec = 0;
6021 ts->tv_nsec = 0;
6022
6023 if (!warned)
6024 weston_log("Error: failure to read "
6025 "the presentation clock %#x: '%m' (%d)\n",
6026 compositor->presentation_clock, errno);
6027 warned = true;
6028 }
6029}
6030
Pekka Paalanen230f3b12014-09-29 14:18:40 -04006031/** Import dmabuf buffer into current renderer
6032 *
6033 * \param compositor
6034 * \param buffer the dmabuf buffer to import
6035 * \return true on usable buffers, false otherwise
6036 *
6037 * This function tests that the linux_dmabuf_buffer is usable
6038 * for the current renderer. Returns false on unusable buffers. Usually
6039 * usability is tested by importing the dmabufs for composition.
6040 *
6041 * This hook is also used for detecting if the renderer supports
6042 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
6043 * supported.
6044 * */
6045WL_EXPORT bool
6046weston_compositor_import_dmabuf(struct weston_compositor *compositor,
6047 struct linux_dmabuf_buffer *buffer)
6048{
6049 struct weston_renderer *renderer;
6050
6051 renderer = compositor->renderer;
6052
6053 if (renderer->import_dmabuf == NULL)
6054 return false;
6055
6056 return renderer->import_dmabuf(compositor, buffer);
6057}
6058
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006059WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006060weston_version(int *major, int *minor, int *micro)
6061{
6062 *major = WESTON_VERSION_MAJOR;
6063 *minor = WESTON_VERSION_MINOR;
6064 *micro = WESTON_VERSION_MICRO;
6065}
6066
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03006067WL_EXPORT void *
6068weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006069{
Derek Foreman3f86e502015-06-08 11:46:54 -05006070 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006071 char path[PATH_MAX];
6072 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00006073 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006074
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08006075 if (name == NULL)
6076 return NULL;
6077
Derek Foreman3f86e502015-06-08 11:46:54 -05006078 if (name[0] != '/') {
6079 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00006080 len = snprintf(path, sizeof path, "%s/.libs/%s",
6081 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006082 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00006083 len = snprintf(path, sizeof path, "%s/%s",
6084 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006085 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00006086 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006087 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006088
Daniel Stonebeb97e52016-11-28 12:13:54 +00006089 /* snprintf returns the length of the string it would've written,
6090 * _excluding_ the NUL byte. So even being equal to the size of
6091 * our buffer is an error here. */
6092 if (len >= sizeof path)
6093 return NULL;
6094
Kristian Høgsberga6813d22012-09-12 12:21:01 -04006095 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
6096 if (module) {
6097 weston_log("Module '%s' already loaded\n", path);
6098 dlclose(module);
6099 return NULL;
6100 }
6101
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006102 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04006103 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006104 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006105 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006106 return NULL;
6107 }
6108
6109 init = dlsym(module, entrypoint);
6110 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006111 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00006112 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006113 return NULL;
6114 }
6115
6116 return init;
6117}
6118
Giulio Camuffo459137b2014-10-11 23:56:24 +03006119
6120/** Destroys the compositor.
6121 *
6122 * This function cleans up the compositor state and destroys it.
6123 *
6124 * \param compositor The compositor to be destroyed.
6125 */
6126WL_EXPORT void
6127weston_compositor_destroy(struct weston_compositor *compositor)
6128{
6129 /* prevent further rendering while shutting down */
6130 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
6131
6132 wl_signal_emit(&compositor->destroy_signal, compositor);
6133
6134 weston_compositor_xkb_destroy(compositor);
6135
Giulio Camuffo2d24e642015-10-03 16:25:15 +03006136 if (compositor->backend)
6137 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006138
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006139 /* The backend is responsible for destroying the heads. */
6140 assert(wl_list_empty(&compositor->head_list));
6141
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006142 weston_plugin_api_destroy_list(compositor);
6143
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006144 if (compositor->heads_changed_source)
6145 wl_event_source_remove(compositor->heads_changed_source);
6146
Giulio Camuffo459137b2014-10-11 23:56:24 +03006147 free(compositor);
6148}
6149
6150/** Instruct the compositor to exit.
6151 *
6152 * This functions does not directly destroy the compositor object, it merely
6153 * command it to start the tear down process. It is not guaranteed that the
6154 * tear down will happen immediately.
6155 *
6156 * \param compositor The compositor to tear down.
6157 */
6158WL_EXPORT void
6159weston_compositor_exit(struct weston_compositor *compositor)
6160{
6161 compositor->exit(compositor);
6162}
6163
6164/** Return the user data stored in the compositor.
6165 *
6166 * This function returns the user data pointer set with user_data parameter
6167 * to the \ref weston_compositor_create function.
6168 */
6169WL_EXPORT void *
6170weston_compositor_get_user_data(struct weston_compositor *compositor)
6171{
6172 return compositor->user_data;
6173}
Pekka Paalanendd186732016-06-03 14:49:54 +03006174
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006175static const char * const backend_map[] = {
6176 [WESTON_BACKEND_DRM] = "drm-backend.so",
6177 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
6178 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
6179 [WESTON_BACKEND_RDP] = "rdp-backend.so",
6180 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
6181 [WESTON_BACKEND_X11] = "x11-backend.so",
6182};
6183
Pekka Paalanendd186732016-06-03 14:49:54 +03006184/** Load a backend into a weston_compositor
6185 *
6186 * A backend must be loaded to make a weston_compositor work. A backend
6187 * provides input and output capabilities, and determines the renderer to use.
6188 *
6189 * \param compositor A compositor that has not had a backend loaded yet.
6190 * \param backend Name of the backend file.
6191 * \param config_base A pointer to a backend-specific configuration
6192 * structure's 'base' member.
6193 *
6194 * \return 0 on success, or -1 on error.
6195 */
6196WL_EXPORT int
6197weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006198 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03006199 struct weston_backend_config *config_base)
6200{
6201 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03006202 struct weston_backend_config *config_base);
6203
Pekka Paalanend7e35112017-08-29 17:04:12 +03006204 if (compositor->backend) {
6205 weston_log("Error: attempt to load a backend when one is already loaded\n");
6206 return -1;
6207 }
6208
Quentin Glidic887c0182016-07-10 11:00:53 +02006209 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006210 return -1;
6211
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01006212 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03006213 if (!backend_init)
6214 return -1;
6215
Pekka Paalanend7e35112017-08-29 17:04:12 +03006216 if (backend_init(compositor, config_base) < 0) {
6217 compositor->backend = NULL;
6218 return -1;
6219 }
6220
6221 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03006222}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006223
6224WL_EXPORT int
6225weston_compositor_load_xwayland(struct weston_compositor *compositor)
6226{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006227 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006228
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006229 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006230 if (!module_init)
6231 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006232 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006233 return -1;
6234 return 0;
6235}