blob: 4a1d568a9ca9f8e5d1a3df21290c5414e75ba44a [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;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002595 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002596 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002597}
2598
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002599WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002600weston_layer_entry_insert(struct weston_layer_entry *list,
2601 struct weston_layer_entry *entry)
2602{
2603 wl_list_insert(&list->link, &entry->link);
2604 entry->layer = list->layer;
2605}
2606
2607WL_EXPORT void
2608weston_layer_entry_remove(struct weston_layer_entry *entry)
2609{
2610 wl_list_remove(&entry->link);
2611 wl_list_init(&entry->link);
2612 entry->layer = NULL;
2613}
2614
Quentin Glidic82681572016-12-17 13:40:51 +01002615
2616/** Initialize the weston_layer struct.
2617 *
2618 * \param compositor The compositor instance
2619 * \param layer The layer to initialize
2620 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002621WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002622weston_layer_init(struct weston_layer *layer,
2623 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002624{
Quentin Glidic82681572016-12-17 13:40:51 +01002625 layer->compositor = compositor;
2626 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002627 wl_list_init(&layer->view_list.link);
2628 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002629 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002630}
2631
2632/** Sets the position of the layer in the layer list. The layer will be placed
2633 * below any layer with the same position value, if any.
2634 * This function is safe to call if the layer is already on the list, but the
2635 * layer may be moved below other layers at the same position, if any.
2636 *
2637 * \param layer The layer to modify
2638 * \param position The position the layer will be placed at
2639 */
2640WL_EXPORT void
2641weston_layer_set_position(struct weston_layer *layer,
2642 enum weston_layer_position position)
2643{
2644 struct weston_layer *below;
2645
2646 wl_list_remove(&layer->link);
2647
2648 /* layer_list is ordered from top to bottom, the last layer being the
2649 * background with the smallest position value */
2650
2651 layer->position = position;
2652 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2653 if (below->position >= layer->position) {
2654 wl_list_insert(&below->link, &layer->link);
2655 return;
2656 }
2657 }
2658 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2659}
2660
2661/** Hide a layer by taking it off the layer list.
2662 * This function is safe to call if the layer is not on the list.
2663 *
2664 * \param layer The layer to hide
2665 */
2666WL_EXPORT void
2667weston_layer_unset_position(struct weston_layer *layer)
2668{
2669 wl_list_remove(&layer->link);
2670 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002671}
2672
2673WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002674weston_layer_set_mask(struct weston_layer *layer,
2675 int x, int y, int width, int height)
2676{
2677 struct weston_view *view;
2678
2679 layer->mask.x1 = x;
2680 layer->mask.x2 = x + width;
2681 layer->mask.y1 = y;
2682 layer->mask.y2 = y + height;
2683
2684 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2685 weston_view_geometry_dirty(view);
2686 }
2687}
2688
2689WL_EXPORT void
2690weston_layer_set_mask_infinite(struct weston_layer *layer)
2691{
2692 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2693 UINT32_MAX, UINT32_MAX);
2694}
2695
2696WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002697weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002698{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002699 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002700 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002701
Bryce Harrington08976ac2016-08-30 12:05:16 -07002702 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2703 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002704 return;
2705
Pekka Paalanenb5026542014-11-12 15:09:24 +02002706 if (!output->repaint_needed)
2707 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2708
Kristian Høgsbergef044142011-06-21 15:02:12 -04002709 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002710 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002711
2712 /* If we already have a repaint scheduled for our idle handler,
2713 * no need to set it again. If the repaint has been called but
2714 * not finished, then weston_output_finish_frame() will notice
2715 * that a repaint is needed and schedule one. */
2716 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002717 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002718
Daniel Stone05df8c12017-03-03 16:59:42 +00002719 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002720 assert(!output->idle_repaint_source);
2721 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2722 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002723 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002724}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002725
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002726WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002727weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2728{
2729 struct weston_output *output;
2730
2731 wl_list_for_each(output, &compositor->output_list, link)
2732 weston_output_schedule_repaint(output);
2733}
2734
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002735static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002736surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002737{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002738 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002739}
2740
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002741static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002742surface_attach(struct wl_client *client,
2743 struct wl_resource *resource,
2744 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2745{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002746 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002747 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002748
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002749 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002750 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002751 if (buffer == NULL) {
2752 wl_client_post_no_memory(client);
2753 return;
2754 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002755 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002756
Pekka Paalanende685b82012-12-04 15:58:12 +02002757 /* Attach, attach, without commit in between does not send
2758 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002759 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002760
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002761 surface->pending.sx = sx;
2762 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002763 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002764}
2765
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002766static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002767surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002768 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002769 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002770{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002771 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002772
Derek Foreman57e92ed2015-11-17 14:11:35 -06002773 if (width <= 0 || height <= 0)
2774 return;
2775
Derek Foreman152254b2015-11-26 14:17:48 -06002776 pixman_region32_union_rect(&surface->pending.damage_surface,
2777 &surface->pending.damage_surface,
2778 x, y, width, height);
2779}
2780
2781static void
2782surface_damage_buffer(struct wl_client *client,
2783 struct wl_resource *resource,
2784 int32_t x, int32_t y, int32_t width, int32_t height)
2785{
2786 struct weston_surface *surface = wl_resource_get_user_data(resource);
2787
2788 if (width <= 0 || height <= 0)
2789 return;
2790
2791 pixman_region32_union_rect(&surface->pending.damage_buffer,
2792 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002793 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002794}
2795
Kristian Høgsberg33418202011-08-16 23:01:28 -04002796static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002797destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002798{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002799 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002800
2801 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002802 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002803}
2804
2805static void
2806surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002807 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002808{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002809 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002810 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002811
2812 cb = malloc(sizeof *cb);
2813 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002814 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002815 return;
2816 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002817
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002818 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2819 callback);
2820 if (cb->resource == NULL) {
2821 free(cb);
2822 wl_resource_post_no_memory(resource);
2823 return;
2824 }
2825
Jason Ekstranda85118c2013-06-27 20:17:02 -05002826 wl_resource_set_implementation(cb->resource, NULL, cb,
2827 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002828
Pekka Paalanenbc106382012-10-10 12:49:31 +03002829 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002830}
2831
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002832static void
2833surface_set_opaque_region(struct wl_client *client,
2834 struct wl_resource *resource,
2835 struct wl_resource *region_resource)
2836{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002837 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002838 struct weston_region *region;
2839
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002840 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002841 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002842 pixman_region32_copy(&surface->pending.opaque,
2843 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002844 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002845 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002846 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002847}
2848
2849static void
2850surface_set_input_region(struct wl_client *client,
2851 struct wl_resource *resource,
2852 struct wl_resource *region_resource)
2853{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002854 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002855 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002856
2857 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002858 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002859 pixman_region32_copy(&surface->pending.input,
2860 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002861 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002862 pixman_region32_fini(&surface->pending.input);
2863 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002864 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002865}
2866
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002867/* Cause damage to this sub-surface and all its children.
2868 *
2869 * This is useful when there are state changes that need an implicit
2870 * damage, e.g. a z-order change.
2871 */
2872static void
2873weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2874{
2875 struct weston_subsurface *child;
2876
2877 weston_surface_damage(sub->surface);
2878 sub->reordered = false;
2879
2880 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2881 if (child != sub)
2882 weston_surface_damage_subsurfaces(child);
2883}
2884
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002885static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002886weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002887{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002888 struct weston_subsurface *sub;
2889
2890 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2891 parent_link_pending) {
2892 wl_list_remove(&sub->parent_link);
2893 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002894
2895 if (sub->reordered)
2896 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002897 }
2898}
2899
2900static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002901weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002902 struct weston_matrix *matrix)
2903{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002904 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002905 double src_width, src_height, dest_width, dest_height;
2906
2907 weston_matrix_init(matrix);
2908
2909 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2910 src_width = surface->width_from_buffer;
2911 src_height = surface->height_from_buffer;
2912 } else {
2913 src_width = wl_fixed_to_double(vp->buffer.src_width);
2914 src_height = wl_fixed_to_double(vp->buffer.src_height);
2915 }
2916
2917 if (vp->surface.width == -1) {
2918 dest_width = src_width;
2919 dest_height = src_height;
2920 } else {
2921 dest_width = vp->surface.width;
2922 dest_height = vp->surface.height;
2923 }
2924
2925 if (src_width != dest_width || src_height != dest_height)
2926 weston_matrix_scale(matrix,
2927 src_width / dest_width,
2928 src_height / dest_height, 1);
2929
2930 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2931 weston_matrix_translate(matrix,
2932 wl_fixed_to_double(vp->buffer.src_x),
2933 wl_fixed_to_double(vp->buffer.src_y),
2934 0);
2935
2936 switch (vp->buffer.transform) {
2937 case WL_OUTPUT_TRANSFORM_FLIPPED:
2938 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2939 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2940 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2941 weston_matrix_scale(matrix, -1, 1, 1);
2942 weston_matrix_translate(matrix,
2943 surface->width_from_buffer, 0, 0);
2944 break;
2945 }
2946
2947 switch (vp->buffer.transform) {
2948 default:
2949 case WL_OUTPUT_TRANSFORM_NORMAL:
2950 case WL_OUTPUT_TRANSFORM_FLIPPED:
2951 break;
2952 case WL_OUTPUT_TRANSFORM_90:
2953 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2954 weston_matrix_rotate_xy(matrix, 0, 1);
2955 weston_matrix_translate(matrix,
2956 surface->height_from_buffer, 0, 0);
2957 break;
2958 case WL_OUTPUT_TRANSFORM_180:
2959 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2960 weston_matrix_rotate_xy(matrix, -1, 0);
2961 weston_matrix_translate(matrix,
2962 surface->width_from_buffer,
2963 surface->height_from_buffer, 0);
2964 break;
2965 case WL_OUTPUT_TRANSFORM_270:
2966 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2967 weston_matrix_rotate_xy(matrix, 0, -1);
2968 weston_matrix_translate(matrix,
2969 0, surface->width_from_buffer, 0);
2970 break;
2971 }
2972
2973 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2974}
2975
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002976/**
2977 * Compute a + b > c while being safe to overflows.
2978 */
2979static bool
2980fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2981{
2982 return (int64_t)a + (int64_t)b > (int64_t)c;
2983}
2984
2985static bool
2986weston_surface_is_pending_viewport_source_valid(
2987 const struct weston_surface *surface)
2988{
2989 const struct weston_surface_state *pend = &surface->pending;
2990 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2991 int width_from_buffer = 0;
2992 int height_from_buffer = 0;
2993 wl_fixed_t w;
2994 wl_fixed_t h;
2995
2996 /* If viewport source rect is not set, it is always ok. */
2997 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2998 return true;
2999
3000 if (pend->newly_attached) {
3001 if (pend->buffer) {
3002 convert_size_by_transform_scale(&width_from_buffer,
3003 &height_from_buffer,
3004 pend->buffer->width,
3005 pend->buffer->height,
3006 vp->buffer.transform,
3007 vp->buffer.scale);
3008 } else {
3009 /* No buffer: viewport is irrelevant. */
3010 return true;
3011 }
3012 } else {
3013 width_from_buffer = surface->width_from_buffer;
3014 height_from_buffer = surface->height_from_buffer;
3015 }
3016
3017 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3018 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3019
3020 /* No buffer: viewport is irrelevant. */
3021 if (width_from_buffer == 0 || height_from_buffer == 0)
3022 return true;
3023
3024 /* overflow checks for wl_fixed_from_int() */
3025 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3026 return false;
3027 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3028 return false;
3029
3030 w = wl_fixed_from_int(width_from_buffer);
3031 h = wl_fixed_from_int(height_from_buffer);
3032
3033 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3034 return false;
3035 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3036 return false;
3037
3038 return true;
3039}
3040
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003041static bool
3042fixed_is_integer(wl_fixed_t v)
3043{
3044 return (v & 0xff) == 0;
3045}
3046
3047static bool
3048weston_surface_is_pending_viewport_dst_size_int(
3049 const struct weston_surface *surface)
3050{
3051 const struct weston_buffer_viewport *vp =
3052 &surface->pending.buffer_viewport;
3053
3054 if (vp->surface.width != -1) {
3055 assert(vp->surface.width > 0 && vp->surface.height > 0);
3056 return true;
3057 }
3058
3059 return fixed_is_integer(vp->buffer.src_width) &&
3060 fixed_is_integer(vp->buffer.src_height);
3061}
3062
Derek Foreman152254b2015-11-26 14:17:48 -06003063/* Translate pending damage in buffer co-ordinates to surface
3064 * co-ordinates and union it with a pixman_region32_t.
3065 * This should only be called after the buffer is attached.
3066 */
3067static void
3068apply_damage_buffer(pixman_region32_t *dest,
3069 struct weston_surface *surface,
3070 struct weston_surface_state *state)
3071{
3072 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3073
3074 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3075 * translated into surface co-ordinates and unioned with
3076 * any other surface damage.
3077 * None of this makes sense if there is no buffer though.
3078 */
3079 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3080 pixman_region32_t buffer_damage;
3081
3082 pixman_region32_intersect_rect(&state->damage_buffer,
3083 &state->damage_buffer,
3084 0, 0, buffer->width,
3085 buffer->height);
3086 pixman_region32_init(&buffer_damage);
3087 weston_matrix_transform_region(&buffer_damage,
3088 &surface->buffer_to_surface_matrix,
3089 &state->damage_buffer);
3090 pixman_region32_union(dest, dest, &buffer_damage);
3091 pixman_region32_fini(&buffer_damage);
3092 }
3093 /* We should clear this on commit even if there was no buffer */
3094 pixman_region32_clear(&state->damage_buffer);
3095}
3096
Jason Ekstrand1e059042014-10-16 10:55:19 -05003097static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003098weston_surface_commit_state(struct weston_surface *surface,
3099 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003100{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003101 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003102 pixman_region32_t opaque;
3103
Alexander Larsson4ea95522013-05-22 14:41:37 +02003104 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003105 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003106 /* wp_viewport.set_source */
3107 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003108 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003109
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003110 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003111 if (state->newly_attached)
3112 weston_surface_attach(surface, state->buffer);
3113 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003114
Jason Ekstrand1e059042014-10-16 10:55:19 -05003115 weston_surface_build_buffer_matrix(surface,
3116 &surface->surface_to_buffer_matrix);
3117 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3118 &surface->surface_to_buffer_matrix);
3119
Jason Ekstrand7b982072014-05-20 14:33:03 -05003120 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003121 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003122 if (surface->committed)
3123 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003124 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003125
Jason Ekstrand7b982072014-05-20 14:33:03 -05003126 state->sx = 0;
3127 state->sy = 0;
3128 state->newly_attached = 0;
3129 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003130
Derek Foreman152254b2015-11-26 14:17:48 -06003131 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003132 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003133 (pixman_region32_not_empty(&state->damage_surface) ||
3134 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003135 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003136
Pekka Paalanen8e159182012-10-10 12:49:25 +03003137 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003138 &state->damage_surface);
3139
3140 apply_damage_buffer(&surface->damage, surface, state);
3141
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003142 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003143 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003144 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003145
3146 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003147 pixman_region32_init(&opaque);
3148 pixman_region32_intersect_rect(&opaque, &state->opaque,
3149 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003150
3151 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3152 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003153 wl_list_for_each(view, &surface->views, surface_link)
3154 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003155 }
3156
3157 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003158
3159 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003160 pixman_region32_intersect_rect(&surface->input, &state->input,
3161 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003162
Pekka Paalanenbc106382012-10-10 12:49:31 +03003163 /* wl_surface.frame */
3164 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003165 &state->frame_callback_list);
3166 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003167
3168 /* XXX:
3169 * What should happen with a feedback request, if there
3170 * is no wl_buffer attached for this commit?
3171 */
3172
3173 /* presentation.feedback */
3174 wl_list_insert_list(&surface->feedback_list,
3175 &state->feedback_list);
3176 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003177
3178 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003179}
3180
3181static void
3182weston_surface_commit(struct weston_surface *surface)
3183{
3184 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003185
Pekka Paalanene67858b2013-04-25 13:57:42 +03003186 weston_surface_commit_subsurface_order(surface);
3187
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003188 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003189}
3190
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003191static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003192weston_subsurface_commit(struct weston_subsurface *sub);
3193
3194static void
3195weston_subsurface_parent_commit(struct weston_subsurface *sub,
3196 int parent_is_synchronized);
3197
3198static void
3199surface_commit(struct wl_client *client, struct wl_resource *resource)
3200{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003201 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003202 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3203
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003204 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3205 assert(surface->viewport_resource);
3206
3207 wl_resource_post_error(surface->viewport_resource,
3208 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3209 "wl_surface@%d has viewport source outside buffer",
3210 wl_resource_get_id(resource));
3211 return;
3212 }
3213
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003214 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3215 assert(surface->viewport_resource);
3216
3217 wl_resource_post_error(surface->viewport_resource,
3218 WP_VIEWPORT_ERROR_BAD_SIZE,
3219 "wl_surface@%d viewport dst size not integer",
3220 wl_resource_get_id(resource));
3221 return;
3222 }
3223
Pekka Paalanene67858b2013-04-25 13:57:42 +03003224 if (sub) {
3225 weston_subsurface_commit(sub);
3226 return;
3227 }
3228
3229 weston_surface_commit(surface);
3230
3231 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3232 if (sub->surface != surface)
3233 weston_subsurface_parent_commit(sub, 0);
3234 }
3235}
3236
3237static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003238surface_set_buffer_transform(struct wl_client *client,
3239 struct wl_resource *resource, int transform)
3240{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003241 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003242
Jonny Lamba55f1392014-05-30 12:07:15 +02003243 /* if wl_output.transform grows more members this will need to be updated. */
3244 if (transform < 0 ||
3245 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3246 wl_resource_post_error(resource,
3247 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3248 "buffer transform must be a valid transform "
3249 "('%d' specified)", transform);
3250 return;
3251 }
3252
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003253 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003254 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003255}
3256
Alexander Larsson4ea95522013-05-22 14:41:37 +02003257static void
3258surface_set_buffer_scale(struct wl_client *client,
3259 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003260 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003261{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003262 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003263
Jonny Lamba55f1392014-05-30 12:07:15 +02003264 if (scale < 1) {
3265 wl_resource_post_error(resource,
3266 WL_SURFACE_ERROR_INVALID_SCALE,
3267 "buffer scale must be at least one "
3268 "('%d' specified)", scale);
3269 return;
3270 }
3271
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003272 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003273 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003274}
3275
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003276static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003277 surface_destroy,
3278 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003279 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003280 surface_frame,
3281 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003282 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003283 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003284 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003285 surface_set_buffer_scale,
3286 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003287};
3288
3289static void
3290compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003291 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003292{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003293 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003294 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003295
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003296 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003297 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003298 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003299 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003300 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003301
Jason Ekstranda85118c2013-06-27 20:17:02 -05003302 surface->resource =
3303 wl_resource_create(client, &wl_surface_interface,
3304 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003305 if (surface->resource == NULL) {
3306 weston_surface_destroy(surface);
3307 wl_resource_post_no_memory(resource);
3308 return;
3309 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003310 wl_resource_set_implementation(surface->resource, &surface_interface,
3311 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003312
3313 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003314}
3315
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003316static void
3317destroy_region(struct wl_resource *resource)
3318{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003319 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003320
3321 pixman_region32_fini(&region->region);
3322 free(region);
3323}
3324
3325static void
3326region_destroy(struct wl_client *client, struct wl_resource *resource)
3327{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003328 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003329}
3330
3331static void
3332region_add(struct wl_client *client, struct wl_resource *resource,
3333 int32_t x, int32_t y, int32_t width, int32_t height)
3334{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003335 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003336
3337 pixman_region32_union_rect(&region->region, &region->region,
3338 x, y, width, height);
3339}
3340
3341static void
3342region_subtract(struct wl_client *client, struct wl_resource *resource,
3343 int32_t x, int32_t y, int32_t width, int32_t height)
3344{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003345 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003346 pixman_region32_t rect;
3347
3348 pixman_region32_init_rect(&rect, x, y, width, height);
3349 pixman_region32_subtract(&region->region, &region->region, &rect);
3350 pixman_region32_fini(&rect);
3351}
3352
3353static const struct wl_region_interface region_interface = {
3354 region_destroy,
3355 region_add,
3356 region_subtract
3357};
3358
3359static void
3360compositor_create_region(struct wl_client *client,
3361 struct wl_resource *resource, uint32_t id)
3362{
3363 struct weston_region *region;
3364
3365 region = malloc(sizeof *region);
3366 if (region == NULL) {
3367 wl_resource_post_no_memory(resource);
3368 return;
3369 }
3370
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003371 pixman_region32_init(&region->region);
3372
Jason Ekstranda85118c2013-06-27 20:17:02 -05003373 region->resource =
3374 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003375 if (region->resource == NULL) {
3376 free(region);
3377 wl_resource_post_no_memory(resource);
3378 return;
3379 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003380 wl_resource_set_implementation(region->resource, &region_interface,
3381 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003382}
3383
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003384static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003385 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003386 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003387};
3388
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003389static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003390weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3391{
3392 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003393
Jason Ekstrand7b982072014-05-20 14:33:03 -05003394 weston_surface_commit_state(surface, &sub->cached);
3395 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003396
3397 weston_surface_commit_subsurface_order(surface);
3398
3399 weston_surface_schedule_repaint(surface);
3400
Jason Ekstrand7b982072014-05-20 14:33:03 -05003401 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003402}
3403
3404static void
3405weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3406{
3407 struct weston_surface *surface = sub->surface;
3408
3409 /*
3410 * If this commit would cause the surface to move by the
3411 * attach(dx, dy) parameters, the old damage region must be
3412 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003413 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003414 */
Derek Foreman152254b2015-11-26 14:17:48 -06003415 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003416 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003417 pixman_region32_union(&sub->cached.damage_surface,
3418 &sub->cached.damage_surface,
3419 &surface->pending.damage_surface);
3420 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003421
3422 if (surface->pending.newly_attached) {
3423 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003424 weston_surface_state_set_buffer(&sub->cached,
3425 surface->pending.buffer);
3426 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003427 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003428 weston_presentation_feedback_discard_list(
3429 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003430 }
3431 sub->cached.sx += surface->pending.sx;
3432 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003433
Derek Foreman152254b2015-11-26 14:17:48 -06003434 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3435
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003436 sub->cached.buffer_viewport.changed |=
3437 surface->pending.buffer_viewport.changed;
3438 sub->cached.buffer_viewport.buffer =
3439 surface->pending.buffer_viewport.buffer;
3440 sub->cached.buffer_viewport.surface =
3441 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003442
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003443 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003444
3445 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3446
3447 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3448
3449 wl_list_insert_list(&sub->cached.frame_callback_list,
3450 &surface->pending.frame_callback_list);
3451 wl_list_init(&surface->pending.frame_callback_list);
3452
Pekka Paalanen133e4392014-09-23 22:08:46 -04003453 wl_list_insert_list(&sub->cached.feedback_list,
3454 &surface->pending.feedback_list);
3455 wl_list_init(&surface->pending.feedback_list);
3456
Jason Ekstrand7b982072014-05-20 14:33:03 -05003457 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003458}
3459
Derek Foreman280e7dd2014-10-03 13:13:42 -05003460static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003461weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3462{
3463 while (sub) {
3464 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003465 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003466
3467 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003468 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003469
3470 sub = weston_surface_to_subsurface(sub->parent);
3471 }
3472
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003473 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003474}
3475
3476static void
3477weston_subsurface_commit(struct weston_subsurface *sub)
3478{
3479 struct weston_surface *surface = sub->surface;
3480 struct weston_subsurface *tmp;
3481
3482 /* Recursive check for effectively synchronized. */
3483 if (weston_subsurface_is_synchronized(sub)) {
3484 weston_subsurface_commit_to_cache(sub);
3485 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003486 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003487 /* flush accumulated state from cache */
3488 weston_subsurface_commit_to_cache(sub);
3489 weston_subsurface_commit_from_cache(sub);
3490 } else {
3491 weston_surface_commit(surface);
3492 }
3493
3494 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3495 if (tmp->surface != surface)
3496 weston_subsurface_parent_commit(tmp, 0);
3497 }
3498 }
3499}
3500
3501static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003502weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003503{
3504 struct weston_surface *surface = sub->surface;
3505 struct weston_subsurface *tmp;
3506
Pekka Paalanene67858b2013-04-25 13:57:42 +03003507 /* From now on, commit_from_cache the whole sub-tree, regardless of
3508 * the synchronized mode of each child. This sub-surface or some
3509 * of its ancestors were synchronized, so we are synchronized
3510 * all the way down.
3511 */
3512
Jason Ekstrand7b982072014-05-20 14:33:03 -05003513 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003514 weston_subsurface_commit_from_cache(sub);
3515
3516 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3517 if (tmp->surface != surface)
3518 weston_subsurface_parent_commit(tmp, 1);
3519 }
3520}
3521
3522static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003523weston_subsurface_parent_commit(struct weston_subsurface *sub,
3524 int parent_is_synchronized)
3525{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003526 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003527 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003528 wl_list_for_each(view, &sub->surface->views, surface_link)
3529 weston_view_set_position(view,
3530 sub->position.x,
3531 sub->position.y);
3532
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003533 sub->position.set = 0;
3534 }
3535
3536 if (parent_is_synchronized || sub->synchronized)
3537 weston_subsurface_synchronized_commit(sub);
3538}
3539
Pekka Paalanen8274d902014-08-06 19:36:51 +03003540static int
3541subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3542{
3543 return snprintf(buf, len, "sub-surface");
3544}
3545
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003546static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003547subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003548{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003549 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003550
Jason Ekstranda7af7042013-10-12 22:38:11 -05003551 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003552 weston_view_set_position(view,
3553 view->geometry.x + dx,
3554 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003555
3556 /* No need to check parent mappedness, because if parent is not
3557 * mapped, parent is not in a visible layer, so this sub-surface
3558 * will not be drawn either.
3559 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003560
Pekka Paalanene67858b2013-04-25 13:57:42 +03003561 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003562 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003563
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003564 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003565 * because that would call it also for the parent surface,
3566 * which might not be mapped yet. That would lead to
3567 * inconsistent state, where the window could never be
3568 * mapped.
3569 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003570 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003571 * weston_surface_is_mapped() return true, so that when the
3572 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003573 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003574 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003575 }
3576}
3577
3578static struct weston_subsurface *
3579weston_surface_to_subsurface(struct weston_surface *surface)
3580{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003581 if (surface->committed == subsurface_committed)
3582 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003583
3584 return NULL;
3585}
3586
Pekka Paalanen01388e22013-04-25 13:57:44 +03003587WL_EXPORT struct weston_surface *
3588weston_surface_get_main_surface(struct weston_surface *surface)
3589{
3590 struct weston_subsurface *sub;
3591
3592 while (surface && (sub = weston_surface_to_subsurface(surface)))
3593 surface = sub->parent;
3594
3595 return surface;
3596}
3597
Pekka Paalanen50b67472014-10-01 15:02:41 +03003598WL_EXPORT int
3599weston_surface_set_role(struct weston_surface *surface,
3600 const char *role_name,
3601 struct wl_resource *error_resource,
3602 uint32_t error_code)
3603{
3604 assert(role_name);
3605
3606 if (surface->role_name == NULL ||
3607 surface->role_name == role_name ||
3608 strcmp(surface->role_name, role_name) == 0) {
3609 surface->role_name = role_name;
3610
3611 return 0;
3612 }
3613
3614 wl_resource_post_error(error_resource, error_code,
3615 "Cannot assign role %s to wl_surface@%d,"
3616 " already has role %s\n",
3617 role_name,
3618 wl_resource_get_id(surface->resource),
3619 surface->role_name);
3620 return -1;
3621}
3622
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003623WL_EXPORT const char *
3624weston_surface_get_role(struct weston_surface *surface)
3625{
3626 return surface->role_name;
3627}
3628
Pekka Paalanen8274d902014-08-06 19:36:51 +03003629WL_EXPORT void
3630weston_surface_set_label_func(struct weston_surface *surface,
3631 int (*desc)(struct weston_surface *,
3632 char *, size_t))
3633{
3634 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003635 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003636}
3637
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003638/** Get the size of surface contents
3639 *
3640 * \param surface The surface to query.
3641 * \param width Returns the width of raw contents.
3642 * \param height Returns the height of raw contents.
3643 *
3644 * Retrieves the raw surface content size in pixels for the given surface.
3645 * This is the whole content size in buffer pixels. If the surface
3646 * has no content or the renderer does not implement this feature,
3647 * zeroes are returned.
3648 *
3649 * This function is used to determine the buffer size needed for
3650 * a weston_surface_copy_content() call.
3651 */
3652WL_EXPORT void
3653weston_surface_get_content_size(struct weston_surface *surface,
3654 int *width, int *height)
3655{
3656 struct weston_renderer *rer = surface->compositor->renderer;
3657
3658 if (!rer->surface_get_content_size) {
3659 *width = 0;
3660 *height = 0;
3661 return;
3662 }
3663
3664 rer->surface_get_content_size(surface, width, height);
3665}
3666
Quentin Glidic248dd102016-08-12 10:41:34 +02003667/** Get the bounding box of a surface and its subsurfaces
3668 *
3669 * \param surface The surface to query.
3670 * \return The bounding box relative to the surface origin.
3671 *
3672 */
3673WL_EXPORT struct weston_geometry
3674weston_surface_get_bounding_box(struct weston_surface *surface)
3675{
3676 pixman_region32_t region;
3677 pixman_box32_t *box;
3678 struct weston_subsurface *subsurface;
3679
3680 pixman_region32_init_rect(&region,
3681 0, 0,
3682 surface->width, surface->height);
3683
3684 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3685 pixman_region32_union_rect(&region, &region,
3686 subsurface->position.x,
3687 subsurface->position.y,
3688 subsurface->surface->width,
3689 subsurface->surface->height);
3690
3691 box = pixman_region32_extents(&region);
3692 struct weston_geometry geometry = {
3693 .x = box->x1,
3694 .y = box->y1,
3695 .width = box->x2 - box->x1,
3696 .height = box->y2 - box->y1,
3697 };
3698
3699 pixman_region32_fini(&region);
3700
3701 return geometry;
3702}
3703
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003704/** Copy surface contents to system memory.
3705 *
3706 * \param surface The surface to copy from.
3707 * \param target Pointer to the target memory buffer.
3708 * \param size Size of the target buffer in bytes.
3709 * \param src_x X location on contents to copy from.
3710 * \param src_y Y location on contents to copy from.
3711 * \param width Width in pixels of the area to copy.
3712 * \param height Height in pixels of the area to copy.
3713 * \return 0 for success, -1 for failure.
3714 *
3715 * Surface contents are maintained by the renderer. They can be in a
3716 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3717 * else.
3718 *
3719 * Surface contents are copied into memory pointed to by target,
3720 * which has size bytes of space available. The target memory
3721 * may be larger than needed, but being smaller returns an error.
3722 * The extra bytes in target may or may not be written; their content is
3723 * unspecified. Size must be large enough to hold the image.
3724 *
3725 * The image in the target memory will be arranged in rows from
3726 * top to bottom, and pixels on a row from left to right. The pixel
3727 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3728 * width * 4.
3729 *
3730 * Parameters src_x and src_y define the upper-left corner in buffer
3731 * coordinates (pixels) to copy from. Parameters width and height
3732 * define the size of the area to copy in pixels.
3733 *
3734 * The rectangle defined by src_x, src_y, width, height must fit in
3735 * the surface contents. Otherwise an error is returned.
3736 *
3737 * Use surface_get_data_size to determine the content size; the
3738 * needed target buffer size and rectangle limits.
3739 *
3740 * CURRENT IMPLEMENTATION RESTRICTIONS:
3741 * - the machine must be little-endian due to Pixman formats.
3742 *
3743 * NOTE: Pixman formats are premultiplied.
3744 */
3745WL_EXPORT int
3746weston_surface_copy_content(struct weston_surface *surface,
3747 void *target, size_t size,
3748 int src_x, int src_y,
3749 int width, int height)
3750{
3751 struct weston_renderer *rer = surface->compositor->renderer;
3752 int cw, ch;
3753 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3754
3755 if (!rer->surface_copy_content)
3756 return -1;
3757
3758 weston_surface_get_content_size(surface, &cw, &ch);
3759
3760 if (src_x < 0 || src_y < 0)
3761 return -1;
3762
3763 if (width <= 0 || height <= 0)
3764 return -1;
3765
3766 if (src_x + width > cw || src_y + height > ch)
3767 return -1;
3768
3769 if (width * bytespp * height > size)
3770 return -1;
3771
3772 return rer->surface_copy_content(surface, target, size,
3773 src_x, src_y, width, height);
3774}
3775
Pekka Paalanene67858b2013-04-25 13:57:42 +03003776static void
3777subsurface_set_position(struct wl_client *client,
3778 struct wl_resource *resource, int32_t x, int32_t y)
3779{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003780 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003781
3782 if (!sub)
3783 return;
3784
3785 sub->position.x = x;
3786 sub->position.y = y;
3787 sub->position.set = 1;
3788}
3789
3790static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003791subsurface_find_sibling(struct weston_subsurface *sub,
3792 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003793{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003794 struct weston_surface *parent = sub->parent;
3795 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003796
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003797 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3798 if (sibling->surface == surface && sibling != sub)
3799 return sibling;
3800 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003801
3802 return NULL;
3803}
3804
3805static struct weston_subsurface *
3806subsurface_sibling_check(struct weston_subsurface *sub,
3807 struct weston_surface *surface,
3808 const char *request)
3809{
3810 struct weston_subsurface *sibling;
3811
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003812 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003813 if (!sibling) {
3814 wl_resource_post_error(sub->resource,
3815 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3816 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003817 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003818 return NULL;
3819 }
3820
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003821 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003822
3823 return sibling;
3824}
3825
3826static void
3827subsurface_place_above(struct wl_client *client,
3828 struct wl_resource *resource,
3829 struct wl_resource *sibling_resource)
3830{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003831 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003832 struct weston_surface *surface =
3833 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003834 struct weston_subsurface *sibling;
3835
3836 if (!sub)
3837 return;
3838
3839 sibling = subsurface_sibling_check(sub, surface, "place_above");
3840 if (!sibling)
3841 return;
3842
3843 wl_list_remove(&sub->parent_link_pending);
3844 wl_list_insert(sibling->parent_link_pending.prev,
3845 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003846
3847 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003848}
3849
3850static void
3851subsurface_place_below(struct wl_client *client,
3852 struct wl_resource *resource,
3853 struct wl_resource *sibling_resource)
3854{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003855 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003856 struct weston_surface *surface =
3857 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003858 struct weston_subsurface *sibling;
3859
3860 if (!sub)
3861 return;
3862
3863 sibling = subsurface_sibling_check(sub, surface, "place_below");
3864 if (!sibling)
3865 return;
3866
3867 wl_list_remove(&sub->parent_link_pending);
3868 wl_list_insert(&sibling->parent_link_pending,
3869 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003870
3871 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003872}
3873
3874static void
3875subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3876{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003877 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003878
3879 if (sub)
3880 sub->synchronized = 1;
3881}
3882
3883static void
3884subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3885{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003886 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003887
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003888 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003889 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003890
3891 /* If sub became effectively desynchronized, flush. */
3892 if (!weston_subsurface_is_synchronized(sub))
3893 weston_subsurface_synchronized_commit(sub);
3894 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003895}
3896
3897static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003898weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3899{
3900 wl_list_remove(&sub->parent_link);
3901 wl_list_remove(&sub->parent_link_pending);
3902 wl_list_remove(&sub->parent_destroy_listener.link);
3903 sub->parent = NULL;
3904}
3905
3906static void
3907weston_subsurface_destroy(struct weston_subsurface *sub);
3908
3909static void
3910subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3911{
3912 struct weston_subsurface *sub =
3913 container_of(listener, struct weston_subsurface,
3914 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003915 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003916
3917 /* The protocol object (wl_resource) is left inert. */
3918 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003919 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003920
3921 weston_subsurface_destroy(sub);
3922}
3923
3924static void
3925subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3926{
3927 struct weston_subsurface *sub =
3928 container_of(listener, struct weston_subsurface,
3929 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003930 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003931 assert(sub->surface != sub->parent);
3932
3933 if (weston_surface_is_mapped(sub->surface))
3934 weston_surface_unmap(sub->surface);
3935
3936 weston_subsurface_unlink_parent(sub);
3937}
3938
3939static void
3940subsurface_resource_destroy(struct wl_resource *resource)
3941{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003942 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003943
3944 if (sub)
3945 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003946}
3947
3948static void
3949subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3950{
3951 wl_resource_destroy(resource);
3952}
3953
3954static void
3955weston_subsurface_link_parent(struct weston_subsurface *sub,
3956 struct weston_surface *parent)
3957{
3958 sub->parent = parent;
3959 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003960 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003961 &sub->parent_destroy_listener);
3962
3963 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3964 wl_list_insert(&parent->subsurface_list_pending,
3965 &sub->parent_link_pending);
3966}
3967
3968static void
3969weston_subsurface_link_surface(struct weston_subsurface *sub,
3970 struct weston_surface *surface)
3971{
3972 sub->surface = surface;
3973 sub->surface_destroy_listener.notify =
3974 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003975 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003976 &sub->surface_destroy_listener);
3977}
3978
3979static void
3980weston_subsurface_destroy(struct weston_subsurface *sub)
3981{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003982 struct weston_view *view, *next;
3983
Pekka Paalanene67858b2013-04-25 13:57:42 +03003984 assert(sub->surface);
3985
3986 if (sub->resource) {
3987 assert(weston_surface_to_subsurface(sub->surface) == sub);
3988 assert(sub->parent_destroy_listener.notify ==
3989 subsurface_handle_parent_destroy);
3990
George Kiagiadakised04d382014-06-13 18:10:26 +02003991 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3992 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003993 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003994 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003995
Pekka Paalanene67858b2013-04-25 13:57:42 +03003996 if (sub->parent)
3997 weston_subsurface_unlink_parent(sub);
3998
Jason Ekstrand7b982072014-05-20 14:33:03 -05003999 weston_surface_state_fini(&sub->cached);
4000 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004001
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004002 sub->surface->committed = NULL;
4003 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004004 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004005 } else {
4006 /* the dummy weston_subsurface for the parent itself */
4007 assert(sub->parent_destroy_listener.notify == NULL);
4008 wl_list_remove(&sub->parent_link);
4009 wl_list_remove(&sub->parent_link_pending);
4010 }
4011
4012 wl_list_remove(&sub->surface_destroy_listener.link);
4013 free(sub);
4014}
4015
4016static const struct wl_subsurface_interface subsurface_implementation = {
4017 subsurface_destroy,
4018 subsurface_set_position,
4019 subsurface_place_above,
4020 subsurface_place_below,
4021 subsurface_set_sync,
4022 subsurface_set_desync
4023};
4024
4025static struct weston_subsurface *
4026weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4027 struct weston_surface *parent)
4028{
4029 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004030 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004031
Bryce Harringtonde16d892014-11-20 22:21:57 -08004032 sub = zalloc(sizeof *sub);
4033 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004034 return NULL;
4035
Jason Ekstranda7af7042013-10-12 22:38:11 -05004036 wl_list_init(&sub->unused_views);
4037
Jason Ekstranda85118c2013-06-27 20:17:02 -05004038 sub->resource =
4039 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004040 if (!sub->resource) {
4041 free(sub);
4042 return NULL;
4043 }
4044
Jason Ekstranda85118c2013-06-27 20:17:02 -05004045 wl_resource_set_implementation(sub->resource,
4046 &subsurface_implementation,
4047 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004048 weston_subsurface_link_surface(sub, surface);
4049 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004050 weston_surface_state_init(&sub->cached);
4051 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004052 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004053
4054 return sub;
4055}
4056
4057/* Create a dummy subsurface for having the parent itself in its
4058 * sub-surface lists. Makes stacking order manipulation easy.
4059 */
4060static struct weston_subsurface *
4061weston_subsurface_create_for_parent(struct weston_surface *parent)
4062{
4063 struct weston_subsurface *sub;
4064
Bryce Harringtonde16d892014-11-20 22:21:57 -08004065 sub = zalloc(sizeof *sub);
4066 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004067 return NULL;
4068
4069 weston_subsurface_link_surface(sub, parent);
4070 sub->parent = parent;
4071 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4072 wl_list_insert(&parent->subsurface_list_pending,
4073 &sub->parent_link_pending);
4074
4075 return sub;
4076}
4077
4078static void
4079subcompositor_get_subsurface(struct wl_client *client,
4080 struct wl_resource *resource,
4081 uint32_t id,
4082 struct wl_resource *surface_resource,
4083 struct wl_resource *parent_resource)
4084{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004085 struct weston_surface *surface =
4086 wl_resource_get_user_data(surface_resource);
4087 struct weston_surface *parent =
4088 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004089 struct weston_subsurface *sub;
4090 static const char where[] = "get_subsurface: wl_subsurface@";
4091
4092 if (surface == parent) {
4093 wl_resource_post_error(resource,
4094 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4095 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004096 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004097 return;
4098 }
4099
4100 if (weston_surface_to_subsurface(surface)) {
4101 wl_resource_post_error(resource,
4102 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4103 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004104 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004105 return;
4106 }
4107
Pekka Paalanen50b67472014-10-01 15:02:41 +03004108 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4109 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004110 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004111
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004112 if (weston_surface_get_main_surface(parent) == surface) {
4113 wl_resource_post_error(resource,
4114 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4115 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004116 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004117 return;
4118 }
4119
Pekka Paalanene67858b2013-04-25 13:57:42 +03004120 /* make sure the parent is in its own list */
4121 if (wl_list_empty(&parent->subsurface_list)) {
4122 if (!weston_subsurface_create_for_parent(parent)) {
4123 wl_resource_post_no_memory(resource);
4124 return;
4125 }
4126 }
4127
4128 sub = weston_subsurface_create(id, surface, parent);
4129 if (!sub) {
4130 wl_resource_post_no_memory(resource);
4131 return;
4132 }
4133
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004134 surface->committed = subsurface_committed;
4135 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004136 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004137}
4138
4139static void
4140subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4141{
4142 wl_resource_destroy(resource);
4143}
4144
4145static const struct wl_subcompositor_interface subcompositor_interface = {
4146 subcompositor_destroy,
4147 subcompositor_get_subsurface
4148};
4149
4150static void
4151bind_subcompositor(struct wl_client *client,
4152 void *data, uint32_t version, uint32_t id)
4153{
4154 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004155 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004156
Jason Ekstranda85118c2013-06-27 20:17:02 -05004157 resource =
4158 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004159 if (resource == NULL) {
4160 wl_client_post_no_memory(client);
4161 return;
4162 }
4163 wl_resource_set_implementation(resource, &subcompositor_interface,
4164 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004165}
4166
Bryce Harrington0795ece2016-08-30 12:04:26 -07004167/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004168 *
4169 * \param compositor The compositor instance
4170 * \param state The DPMS state the outputs will be set to
4171 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004172static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004173weston_compositor_dpms(struct weston_compositor *compositor,
4174 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004175{
4176 struct weston_output *output;
4177
Bryce Harrington08976ac2016-08-30 12:05:16 -07004178 wl_list_for_each(output, &compositor->output_list, link)
4179 if (output->set_dpms)
4180 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004181}
4182
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004183/** Restores the compositor to active status
4184 *
4185 * \param compositor The compositor instance
4186 *
4187 * If the compositor was in a sleeping mode, all outputs are powered
4188 * back on via DPMS. Otherwise if the compositor was inactive
4189 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4190 * signal will fire.
4191 *
4192 * Restarts the idle timer.
4193 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004194WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004195weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004196{
Neil Roberts8b62e202013-09-30 13:14:47 +01004197 uint32_t old_state = compositor->state;
4198
4199 /* The state needs to be changed before emitting the wake
4200 * signal because that may try to schedule a repaint which
4201 * will not work if the compositor is still sleeping */
4202 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4203
4204 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004205 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004206 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004207 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004208 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004209 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004210 /* fall through */
4211 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004212 wl_event_source_timer_update(compositor->idle_source,
4213 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004214 }
4215}
4216
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004217/** Turns off rendering and frame events for the compositor.
4218 *
4219 * \param compositor The compositor instance
4220 *
4221 * This is used for example to prevent further rendering while the
4222 * compositor is shutting down.
4223 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004224 * Stops the idle timer.
4225 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004226WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004227weston_compositor_offscreen(struct weston_compositor *compositor)
4228{
4229 switch (compositor->state) {
4230 case WESTON_COMPOSITOR_OFFSCREEN:
4231 return;
4232 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004233 default:
4234 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4235 wl_event_source_timer_update(compositor->idle_source, 0);
4236 }
4237}
4238
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004239/** Powers down all attached output devices
4240 *
4241 * \param compositor The compositor instance
4242 *
4243 * Causes rendering to the outputs to cease, and no frame events to be
4244 * sent. Only powers down the outputs if the compositor is not already
4245 * in sleep mode.
4246 *
4247 * Stops the idle timer.
4248 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004249WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004250weston_compositor_sleep(struct weston_compositor *compositor)
4251{
4252 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4253 return;
4254
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004255 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004256 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4257 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4258}
4259
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004260/** Sets compositor to idle mode
4261 *
4262 * \param data The compositor instance
4263 *
4264 * This is called when the idle timer fires. Once the compositor is in
4265 * idle mode it requires a wake action (e.g. via
4266 * weston_compositor_wake()) to restore it. The compositor's
4267 * idle_signal will be triggered when the idle event occurs.
4268 *
4269 * Idleness can be inhibited by setting the compositor's idle_inhibit
4270 * property.
4271 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004272static int
4273idle_handler(void *data)
4274{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004275 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004276
4277 if (compositor->idle_inhibit)
4278 return 1;
4279
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004280 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004281 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004282
4283 return 1;
4284}
4285
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004286WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004287weston_plane_init(struct weston_plane *plane,
4288 struct weston_compositor *ec,
4289 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004290{
4291 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004292 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004293 plane->x = x;
4294 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004295 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004296
4297 /* Init the link so that the call to wl_list_remove() when releasing
4298 * the plane without ever stacking doesn't lead to a crash */
4299 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004300}
4301
4302WL_EXPORT void
4303weston_plane_release(struct weston_plane *plane)
4304{
Xiong Zhang97116532013-10-23 13:58:31 +08004305 struct weston_view *view;
4306
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004307 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004308 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004309
Xiong Zhang97116532013-10-23 13:58:31 +08004310 wl_list_for_each(view, &plane->compositor->view_list, link) {
4311 if (view->plane == plane)
4312 view->plane = NULL;
4313 }
4314
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004315 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004316}
4317
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004318WL_EXPORT void
4319weston_compositor_stack_plane(struct weston_compositor *ec,
4320 struct weston_plane *plane,
4321 struct weston_plane *above)
4322{
4323 if (above)
4324 wl_list_insert(above->link.prev, &plane->link);
4325 else
4326 wl_list_insert(&ec->plane_list, &plane->link);
4327}
4328
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004329static void
4330output_release(struct wl_client *client, struct wl_resource *resource)
4331{
4332 wl_resource_destroy(resource);
4333}
4334
4335static const struct wl_output_interface output_interface = {
4336 output_release,
4337};
4338
4339
Casey Dahlin9074db52012-04-19 22:50:09 -04004340static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004341{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004342 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004343}
4344
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004345static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004346bind_output(struct wl_client *client,
4347 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004348{
Pekka Paalanen05347622017-03-27 12:24:34 +03004349 struct weston_head *head = data;
4350 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004351 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004352 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004353
Jason Ekstranda85118c2013-06-27 20:17:02 -05004354 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004355 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004356 if (resource == NULL) {
4357 wl_client_post_no_memory(client);
4358 return;
4359 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004360
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004361 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004362 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004363 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004364
Pekka Paalanen05347622017-03-27 12:24:34 +03004365 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004366 wl_output_send_geometry(resource,
4367 output->x,
4368 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004369 head->mm_width,
4370 head->mm_height,
4371 head->subpixel,
4372 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004373 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004374 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004375 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004376 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004377
4378 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004379 wl_output_send_mode(resource,
4380 mode->flags,
4381 mode->width,
4382 mode->height,
4383 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004384 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004385
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004386 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004387 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004388}
4389
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004390/** Remove the global wl_output protocol object
4391 *
4392 * \param head The head whose global to remove.
4393 *
4394 * Also orphans the wl_resources for this head (wl_output).
4395 */
4396static void
4397weston_head_remove_global(struct weston_head *head)
4398{
4399 struct wl_resource *resource, *tmp;
4400
4401 if (head->global)
4402 wl_global_destroy(head->global);
4403 head->global = NULL;
4404
4405 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4406 unbind_resource(resource);
4407 wl_resource_set_destructor(resource, NULL);
4408 wl_resource_set_user_data(resource, NULL);
4409 }
4410}
4411
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004412/** Get the backing object of wl_output
4413 *
4414 * \param resource A wl_output protocol object.
4415 * \return The backing object (user data) of a wl_resource representing a
4416 * wl_output protocol object.
4417 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004418WL_EXPORT struct weston_head *
4419weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004420{
4421 assert(wl_resource_instance_of(resource, &wl_output_interface,
4422 &output_interface));
4423
4424 return wl_resource_get_user_data(resource);
4425}
4426
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004427/** Initialize a pre-allocated weston_head
4428 *
4429 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004430 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004431 *
4432 * The head will be safe to attach, detach and release.
4433 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004434 * The name is used in logs, and can be used by compositors as a configuration
4435 * identifier.
4436 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004437 * \memberof weston_head
4438 * \internal
4439 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004440WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004441weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004442{
4443 /* Add some (in)sane defaults which can be used
4444 * for checking if an output was properly configured
4445 */
4446 memset(head, 0, sizeof *head);
4447
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004448 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004449 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004450 wl_list_init(&head->output_link);
4451 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004452 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004453}
4454
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004455/** Idle task for emitting heads_changed_signal */
4456static void
4457weston_compositor_call_heads_changed(void *data)
4458{
4459 struct weston_compositor *compositor = data;
4460
4461 compositor->heads_changed_source = NULL;
4462
4463 wl_signal_emit(&compositor->heads_changed_signal, compositor);
4464}
4465
4466/** Schedule a call on idle to heads_changed callback
4467 *
4468 * \param compositor The Compositor.
4469 *
4470 * \memberof weston_compositor
4471 * \internal
4472 */
4473static void
4474weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4475{
4476 struct wl_event_loop *loop;
4477
4478 if (compositor->heads_changed_source)
4479 return;
4480
4481 loop = wl_display_get_event_loop(compositor->wl_display);
4482 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4483 weston_compositor_call_heads_changed, compositor);
4484}
4485
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004486/** Register a new head
4487 *
4488 * \param compositor The compositor.
4489 * \param head The head to register, must not be already registered.
4490 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004491 * This signals the core that a new head has become available, leading to
4492 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004493 *
4494 * \memberof weston_compositor
4495 * \internal
4496 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004497WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004498weston_compositor_add_head(struct weston_compositor *compositor,
4499 struct weston_head *head)
4500{
4501 assert(wl_list_empty(&head->compositor_link));
4502 assert(head->name);
4503
4504 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4505 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004506 weston_compositor_schedule_heads_changed(compositor);
4507}
4508
4509/** Adds a listener to be called when heads change
4510 *
4511 * \param compositor The compositor.
4512 * \param listener The listener to add.
4513 *
4514 * The listener notify function argument is the \var compositor.
4515 *
4516 * The listener function will be called after heads are added or their
4517 * connection status has changed. Several changes may be accumulated into a
4518 * single call. The user is expected to iterate over the existing heads and
4519 * check their statuses to find out what changed.
4520 *
4521 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4522 * weston_head_is_enabled
4523 * \memberof weston_compositor
4524 */
4525WL_EXPORT void
4526weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4527 struct wl_listener *listener)
4528{
4529 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004530}
4531
4532/** Iterate over available heads
4533 *
4534 * \param compositor The compositor.
4535 * \param item The iterator, or NULL for start.
4536 * \return The next available head in the list.
4537 *
4538 * Returns all available heads, regardless of being connected or enabled.
4539 *
4540 * You can iterate over all heads as follows:
4541 * \code
4542 * struct weston_head *head = NULL;
4543 *
4544 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4545 * ...
4546 * }
4547 * \endcode
4548 *
4549 * If you cause \c iter to be removed from the list, you cannot use it to
4550 * continue iterating. Removing any other item is safe.
4551 *
4552 * \memberof weston_compositor
4553 */
4554WL_EXPORT struct weston_head *
4555weston_compositor_iterate_heads(struct weston_compositor *compositor,
4556 struct weston_head *iter)
4557{
4558 struct wl_list *list = &compositor->head_list;
4559 struct wl_list *node;
4560
4561 assert(compositor);
4562 assert(!iter || iter->compositor == compositor);
4563
4564 if (iter)
4565 node = iter->compositor_link.next;
4566 else
4567 node = list->next;
4568
4569 assert(node);
4570 assert(!iter || node != &iter->compositor_link);
4571
4572 if (node == list)
4573 return NULL;
4574
4575 return container_of(node, struct weston_head, compositor_link);
4576}
4577
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004578/** Iterate over attached heads
4579 *
4580 * \param output The output whose heads to iterate.
4581 * \param item The iterator, or NULL for start.
4582 * \return The next attached head in the list.
4583 *
4584 * Returns all heads currently attached to the output.
4585 *
4586 * You can iterate over all heads as follows:
4587 * \code
4588 * struct weston_head *head = NULL;
4589 *
4590 * while ((head = weston_output_iterate_heads(output, head))) {
4591 * ...
4592 * }
4593 * \endcode
4594 *
4595 * If you cause \c iter to be removed from the list, you cannot use it to
4596 * continue iterating. Removing any other item is safe.
4597 *
4598 * \memberof weston_compositor
4599 */
4600WL_EXPORT struct weston_head *
4601weston_output_iterate_heads(struct weston_output *output,
4602 struct weston_head *iter)
4603{
4604 struct wl_list *list = &output->head_list;
4605 struct wl_list *node;
4606
4607 assert(output);
4608 assert(!iter || iter->output == output);
4609
4610 if (iter)
4611 node = iter->output_link.next;
4612 else
4613 node = list->next;
4614
4615 assert(node);
4616 assert(!iter || node != &iter->output_link);
4617
4618 if (node == list)
4619 return NULL;
4620
4621 return container_of(node, struct weston_head, output_link);
4622}
4623
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004624/** Attach a head to an inactive output
4625 *
4626 * \param output The output to attach to.
4627 * \param head The head that is not yet attached.
4628 * \return 0 on success, -1 on failure.
4629 *
4630 * Attaches the given head to the output. All heads of an output are clones
4631 * and share the resolution and timings.
4632 *
4633 * Cloning heads this way uses less resources than creating an output for
4634 * each head, but is not always possible due to environment, driver and hardware
4635 * limitations.
4636 *
4637 * On failure, the head remains unattached. Success of this function does not
4638 * guarantee the output configuration is actually valid. The final checks are
4639 * made on weston_output_enable().
4640 *
4641 * \memberof weston_output
4642 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004643WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004644weston_output_attach_head(struct weston_output *output,
4645 struct weston_head *head)
4646{
4647 if (output->enabled)
4648 return -1;
4649
4650 if (!wl_list_empty(&head->output_link))
4651 return -1;
4652
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004653 if (output->attach_head) {
4654 if (output->attach_head(output, head) < 0)
4655 return -1;
4656 } else if (!wl_list_empty(&output->head_list)) {
4657 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004658 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004659 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004660
4661 head->output = output;
4662 wl_list_insert(output->head_list.prev, &head->output_link);
4663
4664 return 0;
4665}
4666
4667/** Detach a head from its output
4668 *
4669 * \param head The head to detach.
4670 *
4671 * It is safe to detach a non-attached head.
4672 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004673 * If the head is attached to an enabled output and the output will be left
4674 * with no heads, the output will be disabled.
4675 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004676 * \memberof weston_head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004677 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004678 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004679WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004680weston_head_detach(struct weston_head *head)
4681{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004682 struct weston_output *output = head->output;
4683
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004684 wl_list_remove(&head->output_link);
4685 wl_list_init(&head->output_link);
4686 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004687
4688 if (!output)
4689 return;
4690
4691 if (output->detach_head)
4692 output->detach_head(output, head);
4693
4694 if (output->enabled) {
4695 weston_head_remove_global(head);
4696
4697 if (wl_list_empty(&output->head_list))
4698 weston_output_disable(output);
4699 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004700}
4701
4702/** Destroy a head
4703 *
4704 * \param head The head to be released.
4705 *
4706 * Destroys the head. The caller is responsible for freeing the memory pointed
4707 * to by \c head.
4708 *
4709 * \memberof weston_head
4710 * \internal
4711 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004712WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004713weston_head_release(struct weston_head *head)
4714{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004715 wl_signal_emit(&head->destroy_signal, head);
4716
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004717 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004718
4719 free(head->make);
4720 free(head->model);
4721 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004722 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004723
4724 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004725}
4726
Pekka Paalanene19970f2017-08-28 14:11:02 +03004727static void
4728weston_head_set_device_changed(struct weston_head *head)
4729{
4730 head->device_changed = true;
4731
4732 if (head->compositor)
4733 weston_compositor_schedule_heads_changed(head->compositor);
4734}
4735
4736/** String equal comparison with NULLs being equal */
4737static bool
4738str_null_eq(const char *a, const char *b)
4739{
4740 if (!a && !b)
4741 return true;
4742
4743 if (!!a != !!b)
4744 return false;
4745
4746 return strcmp(a, b) == 0;
4747}
4748
Pekka Paalanen01f60212017-03-24 15:39:24 +02004749/** Store monitor make, model and serial number
4750 *
4751 * \param head The head to modify.
4752 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
4753 * any string, or NULL for none.
4754 * \param model The monitor model or name, or a made-up string, or NULL for
4755 * none.
4756 * \param serialno The monitor serial number, a made-up string, or NULL for
4757 * none.
4758 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004759 * This may set the device_changed flag.
4760 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004761 * \memberof weston_head
4762 * \internal
4763 */
4764WL_EXPORT void
4765weston_head_set_monitor_strings(struct weston_head *head,
4766 const char *make,
4767 const char *model,
4768 const char *serialno)
4769{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004770 if (str_null_eq(head->make, make) &&
4771 str_null_eq(head->model, model) &&
4772 str_null_eq(head->serial_number, serialno))
4773 return;
4774
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004775 free(head->make);
4776 free(head->model);
4777 free(head->serial_number);
4778
4779 head->make = make ? strdup(make) : NULL;
4780 head->model = model ? strdup(model) : NULL;
4781 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004782
4783 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004784}
4785
4786/** Store physical image size
4787 *
4788 * \param head The head to modify.
4789 * \param mm_width Image area width in millimeters.
4790 * \param mm_height Image area height in millimeters.
4791 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004792 * This may set the device_changed flag.
4793 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004794 * \memberof weston_head
4795 * \internal
4796 */
4797WL_EXPORT void
4798weston_head_set_physical_size(struct weston_head *head,
4799 int32_t mm_width, int32_t mm_height)
4800{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004801 if (head->mm_width == mm_width &&
4802 head->mm_height == mm_height)
4803 return;
4804
Pekka Paalanen01f60212017-03-24 15:39:24 +02004805 head->mm_width = mm_width;
4806 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004807
4808 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004809}
4810
4811/** Store monitor sub-pixel layout
4812 *
4813 * \param head The head to modify.
4814 * \param sp Sub-pixel layout. The possible values are:
4815 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
4816 * - WL_OUTPUT_SUBPIXEL_NONE,
4817 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
4818 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
4819 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
4820 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
4821 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004822 * This may set the device_changed flag.
4823 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004824 * \memberof weston_head
4825 * \internal
4826 */
4827WL_EXPORT void
4828weston_head_set_subpixel(struct weston_head *head,
4829 enum wl_output_subpixel sp)
4830{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004831 if (head->subpixel == sp)
4832 return;
4833
Pekka Paalanen01f60212017-03-24 15:39:24 +02004834 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004835
4836 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004837}
4838
4839/** Mark the monitor as internal
4840 *
4841 * This is used for embedded screens, like laptop panels.
4842 *
4843 * \param head The head to mark as internal.
4844 *
4845 * By default a head is external. The type is often inferred from the physical
4846 * connector type.
4847 *
4848 * \memberof weston_head
4849 * \internal
4850 */
4851WL_EXPORT void
4852weston_head_set_internal(struct weston_head *head)
4853{
4854 head->connection_internal = true;
4855}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004856
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004857/** Store connector status
4858 *
4859 * \param head The head to modify.
4860 * \param connected Whether the head is connected.
4861 *
4862 * Connectors are created as disconnected. This function can be used to
4863 * set the connector status.
4864 *
4865 * The status should be set to true when a physical connector is connected to
4866 * a video sink device like a monitor and to false when the connector is
4867 * disconnected. For nested backends, the connection status should reflect the
4868 * connection to the parent display server.
4869 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004870 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03004871 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004872 *
4873 * \sa weston_compositor_set_heads_changed_cb
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004874 * \memberof weston_head
4875 * \internal
4876 */
4877WL_EXPORT void
4878weston_head_set_connection_status(struct weston_head *head, bool connected)
4879{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004880 if (head->connected == connected)
4881 return;
4882
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004883 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004884
Pekka Paalanene19970f2017-08-28 14:11:02 +03004885 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004886}
4887
4888/** Is the head currently connected?
4889 *
4890 * \param head The head to query.
4891 * \return Connection status.
4892 *
4893 * Returns true if the head is physically connected to a monitor, or in
4894 * case of a nested backend returns true when there is a connection to the
4895 * parent display server.
4896 *
4897 * This is independent from the head being enabled.
4898 *
4899 * \sa weston_head_is_enabled
4900 * \memberof weston_head
4901 */
4902WL_EXPORT bool
4903weston_head_is_connected(struct weston_head *head)
4904{
4905 return head->connected;
4906}
4907
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02004908/** Is the head currently enabled?
4909 *
4910 * \param head The head to query.
4911 * \return Video status.
4912 *
4913 * Returns true if the head is currently transmitting a video stream.
4914 *
4915 * This is independent of the head being connected.
4916 *
4917 * \sa weston_head_is_connected
4918 * \memberof weston_head
4919 */
4920WL_EXPORT bool
4921weston_head_is_enabled(struct weston_head *head)
4922{
4923 if (!head->output)
4924 return false;
4925
4926 return head->output->enabled;
4927}
4928
Pekka Paalanene19970f2017-08-28 14:11:02 +03004929/** Has the device information changed?
4930 *
4931 * \param head The head to query.
4932 * \return True if the device information has changed since last reset.
4933 *
4934 * The information about the connected display device, e.g. a monitor, may
4935 * change without being disconnected in between. Changing information
4936 * causes a call to the heads_changed hook.
4937 *
4938 * The information includes make, model, serial number, physical size,
4939 * and sub-pixel type. The connection status is also included.
4940 *
4941 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
4942 * \memberof weston_head
4943 */
4944WL_EXPORT bool
4945weston_head_is_device_changed(struct weston_head *head)
4946{
4947 return head->device_changed;
4948}
4949
4950/** Acknowledge device information change
4951 *
4952 * \param head The head to acknowledge.
4953 *
4954 * Clears the device changed flag on this head. When a compositor has processed
4955 * device information, it should call this to be able to notice further
4956 * changes.
4957 *
4958 * \sa weston_head_is_device_changed
4959 * \memberof weston_head
4960 */
4961WL_EXPORT void
4962weston_head_reset_device_changed(struct weston_head *head)
4963{
4964 head->device_changed = false;
4965}
4966
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004967/** Get the name of a head
4968 *
4969 * \param head The head to query.
4970 * \return The head's name, not NULL.
4971 *
4972 * The name depends on the backend. The DRM backend uses connector names,
4973 * other backends may use hardcoded names or user-given names.
4974 */
4975WL_EXPORT const char *
4976weston_head_get_name(struct weston_head *head)
4977{
4978 return head->name;
4979}
4980
4981/** Get the output the head is attached to
4982 *
4983 * \param head The head to query.
4984 * \return The output the head is attached to, or NULL if detached.
4985 */
4986WL_EXPORT struct weston_output *
4987weston_head_get_output(struct weston_head *head)
4988{
4989 return head->output;
4990}
4991
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004992/** Add destroy callback for a head
4993 *
4994 * \param head The head to watch for.
4995 * \param listener The listener to add. The \c notify member must be set.
4996 *
4997 * Heads may get destroyed for various reasons by the backends. If a head is
4998 * attached to an output, the compositor should listen for head destruction
4999 * and reconfigure or destroy the output if necessary.
5000 *
5001 * The destroy callbacks will be called on weston_head destruction before any
5002 * automatic detaching from an associated weston_output and before any
5003 * weston_head information is lost.
5004 *
5005 * The \c data argument to the notify callback is the weston_head being
5006 * destroyed.
5007 */
5008WL_EXPORT void
5009weston_head_add_destroy_listener(struct weston_head *head,
5010 struct wl_listener *listener)
5011{
5012 wl_signal_add(&head->destroy_signal, listener);
5013}
5014
5015/** Look up destroy listener for a head
5016 *
5017 * \param head The head to query.
5018 * \param notify The notify function used used for the added destroy listener.
5019 * \return The listener, or NULL if not found.
5020 *
5021 * This looks up the previously added destroy listener struct based on the
5022 * notify function it has. The listener can be used to access user data
5023 * through \c container_of().
5024 *
5025 * \sa wl_signal_get()
5026 */
5027WL_EXPORT struct wl_listener *
5028weston_head_get_destroy_listener(struct weston_head *head,
5029 wl_notify_func_t notify)
5030{
5031 return wl_signal_get(&head->destroy_signal, notify);
5032}
5033
David Fort0de859e2016-05-27 23:22:57 +02005034/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005035static void
David Fort0de859e2016-05-27 23:22:57 +02005036weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5037 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005038{
5039 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005040 bool start_resizing = false;
5041
5042 if (!delta_width)
5043 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005044
5045 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005046 if (output == resized_output) {
5047 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005048 continue;
5049 }
5050
David Fort0de859e2016-05-27 23:22:57 +02005051 if (start_resizing) {
5052 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005053 output->dirty = 1;
5054 }
5055 }
5056}
5057
Pekka Paalanend72bad22017-03-29 17:01:41 +03005058static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005059weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005060{
Scott Moreau850ca422012-05-21 15:21:25 -06005061 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005062
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005063 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005064 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005065
Scott Moreauccbf29d2012-02-22 14:21:41 -07005066 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005067 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005068 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005069 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005070 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005071 weston_matrix_scale(&output->matrix, magnification,
5072 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005073 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005074
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005075 switch (output->transform) {
5076 case WL_OUTPUT_TRANSFORM_FLIPPED:
5077 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5078 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5079 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5080 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5081 weston_matrix_scale(&output->matrix, -1, 1, 1);
5082 break;
5083 }
5084
5085 switch (output->transform) {
5086 default:
5087 case WL_OUTPUT_TRANSFORM_NORMAL:
5088 case WL_OUTPUT_TRANSFORM_FLIPPED:
5089 break;
5090 case WL_OUTPUT_TRANSFORM_90:
5091 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5092 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5093 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5094 break;
5095 case WL_OUTPUT_TRANSFORM_180:
5096 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5097 weston_matrix_translate(&output->matrix,
5098 -output->width, -output->height, 0);
5099 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5100 break;
5101 case WL_OUTPUT_TRANSFORM_270:
5102 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5103 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5104 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5105 break;
5106 }
5107
5108 if (output->current_scale != 1)
5109 weston_matrix_scale(&output->matrix,
5110 output->current_scale,
5111 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005112
Scott Moreauccbf29d2012-02-22 14:21:41 -07005113 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005114
5115 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005116}
5117
Scott Moreau1bad5db2012-08-18 01:04:05 -06005118static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005119weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005120{
5121 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005122 output->native_scale = scale;
5123 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005124
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005125 convert_size_by_transform_scale(&output->width, &output->height,
5126 output->current_mode->width,
5127 output->current_mode->height,
5128 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005129}
5130
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005131static void
5132weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005133{
5134 output->x = x;
5135 output->y = y;
5136
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005137 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005138 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005139
5140 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005141 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005142 output->width,
5143 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005144}
5145
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005146WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005147weston_output_move(struct weston_output *output, int x, int y)
5148{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005149 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005150 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005151 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005152
5153 output->move_x = x - output->x;
5154 output->move_y = y - output->y;
5155
5156 if (output->move_x == 0 && output->move_y == 0)
5157 return;
5158
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005159 weston_output_init_geometry(output, x, y);
5160
5161 output->dirty = 1;
5162
5163 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005164 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005165
5166 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005167 wl_list_for_each(head, &output->head_list, output_link) {
5168 wl_resource_for_each(resource, &head->resource_list) {
5169 wl_output_send_geometry(resource,
5170 output->x,
5171 output->y,
5172 head->mm_width,
5173 head->mm_height,
5174 head->subpixel,
5175 head->make,
5176 head->model,
5177 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005178
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005179 ver = wl_resource_get_version(resource);
5180 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5181 wl_output_send_done(resource);
5182 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005183 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005184}
5185
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005186/** Signal that a pending output is taken into use.
5187 *
5188 * Removes the output from the pending list and adds it to the compositor's
5189 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005190 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005191 * The output gets an internal ID assigned, and the wl_output global is
5192 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005193 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005194 * \param compositor The compositor instance.
5195 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005196 *
5197 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005198 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005199static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005200weston_compositor_add_output(struct weston_compositor *compositor,
5201 struct weston_output *output)
5202{
Armin Krezoviće5403842016-08-05 15:28:29 +02005203 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005204 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005205
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005206 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005207
5208 /* Verify we haven't reached the limit of 32 available output IDs */
5209 assert(ffs(~compositor->output_id_pool) > 0);
5210
5211 /* Invert the output id pool and look for the lowest numbered
5212 * switch (the least significant bit). Take that bit's position
5213 * as our ID, and mark it used in the compositor's output_id_pool.
5214 */
5215 output->id = ffs(~compositor->output_id_pool) - 1;
5216 compositor->output_id_pool |= 1u << output->id;
5217
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005218 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005219 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005220 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005221
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005222 wl_list_for_each(head, &output->head_list, output_link) {
5223 head->global = wl_global_create(compositor->wl_display,
5224 &wl_output_interface, 3,
5225 head, bind_output);
5226 }
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005227
Giulio Camuffob1147152015-05-06 21:41:57 +03005228 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005229
5230 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5231 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005232}
5233
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005234/** Transform device coordinates into global coordinates
5235 *
5236 * \param device_x[in] X coordinate in device units.
5237 * \param device_y[in] Y coordinate in device units.
5238 * \param x[out] X coordinate in the global space.
5239 * \param y[out] Y coordinate in the global space.
5240 *
5241 * Transforms coordinates from the device coordinate space
5242 * (physical pixel units) to the global coordinate space (logical pixel units).
5243 * This takes into account output transform and scale.
5244 *
5245 * \memberof weston_output
5246 * \internal
5247 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005248WL_EXPORT void
5249weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005250 double device_x, double device_y,
5251 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005252{
Derek Foreman0f679412014-10-02 13:41:17 -05005253 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005254 device_x,
5255 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005256 0.0,
5257 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005258
Derek Foreman67a18b92015-03-24 11:36:14 -05005259 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005260
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005261 *x = p.f[0] / p.f[3];
5262 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005263}
5264
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005265/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005266 *
5267 * \param output The weston_output object that is being removed.
5268 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005269 * The following happens:
5270 *
5271 * - The output assignments of all views in the current scenegraph are
5272 * recomputed.
5273 *
5274 * - Presentation feedback is discarded.
5275 *
5276 * - Compositor is notified that outputs were changed and
5277 * applies the necessary changes to re-layout outputs.
5278 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005279 * - The output is put back in the pending outputs list.
5280 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005281 * - Signal is emitted to notify all users of the weston_output
5282 * object that the output is being destroyed.
5283 *
5284 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005285 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005286 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005287 * - The output's internal ID is released.
5288 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005289 * \memberof weston_output
5290 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005291 */
5292static void
5293weston_compositor_remove_output(struct weston_output *output)
5294{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005295 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005296 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005297 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005298
5299 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005300 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005301
Pekka Paalanenbccda712017-03-29 16:16:04 +03005302 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005303 if (view->output_mask & (1u << output->id))
5304 weston_view_assign_output(view);
5305 }
5306
5307 weston_presentation_feedback_discard_list(&output->feedback_list);
5308
Pekka Paalanenbccda712017-03-29 16:16:04 +03005309 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005310
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005311 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005312 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005313 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005314
Pekka Paalanenbccda712017-03-29 16:16:04 +03005315 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005316 wl_signal_emit(&output->destroy_signal, output);
5317
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005318 wl_list_for_each(head, &output->head_list, output_link)
5319 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005320
5321 compositor->output_id_pool &= ~(1u << output->id);
5322 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005323}
5324
5325/** Sets the output scale for a given output.
5326 *
5327 * \param output The weston_output object that the scale is set for.
5328 * \param scale Scale factor for the given output.
5329 *
5330 * It only supports setting scale for an output that
5331 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005332 *
5333 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005334 */
5335WL_EXPORT void
5336weston_output_set_scale(struct weston_output *output,
5337 int32_t scale)
5338{
5339 /* We can only set scale on a disabled output */
5340 assert(!output->enabled);
5341
5342 /* We only want to set scale once */
5343 assert(!output->scale);
5344
5345 output->scale = scale;
5346}
5347
5348/** Sets the output transform for a given output.
5349 *
5350 * \param output The weston_output object that the transform is set for.
5351 * \param transform Transform value for the given output.
5352 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005353 * Refer to wl_output::transform section located at
5354 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5355 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005356 *
5357 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005358 */
5359WL_EXPORT void
5360weston_output_set_transform(struct weston_output *output,
5361 uint32_t transform)
5362{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005363 struct weston_pointer_motion_event ev;
5364 struct wl_resource *resource;
5365 struct weston_seat *seat;
5366 pixman_region32_t old_region;
5367 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005368 struct weston_head *head;
5369 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005370
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005371 if (!output->enabled && output->transform == UINT32_MAX) {
5372 output->transform = transform;
5373 return;
5374 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005375
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005376 weston_output_transform_scale_init(output, transform, output->scale);
5377
5378 pixman_region32_init(&old_region);
5379 pixman_region32_copy(&old_region, &output->region);
5380
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005381 weston_output_init_geometry(output, output->x, output->y);
5382
5383 output->dirty = 1;
5384
5385 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005386 wl_list_for_each(head, &output->head_list, output_link) {
5387 wl_resource_for_each(resource, &head->resource_list) {
5388 wl_output_send_geometry(resource,
5389 output->x,
5390 output->y,
5391 head->mm_width,
5392 head->mm_height,
5393 head->subpixel,
5394 head->make,
5395 head->model,
5396 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005397
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005398 ver = wl_resource_get_version(resource);
5399 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5400 wl_output_send_done(resource);
5401 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005402 }
5403
5404 /* we must ensure that pointers are inside output, otherwise they disappear */
5405 mid_x = output->x + output->width / 2;
5406 mid_y = output->y + output->height / 2;
5407
5408 ev.mask = WESTON_POINTER_MOTION_ABS;
5409 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5410 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5411
5412 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5413 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5414
5415 if (pointer && pixman_region32_contains_point(&old_region,
5416 wl_fixed_to_int(pointer->x),
5417 wl_fixed_to_int(pointer->y),
5418 NULL))
5419 weston_pointer_move(pointer, &ev);
5420 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005421}
5422
5423/** Initializes a weston_output object with enough data so
5424 ** an output can be configured.
5425 *
5426 * \param output The weston_output object to initialize
5427 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005428 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005429 *
5430 * Sets initial values for fields that are expected to be
5431 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005432 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005433 * The name is used in logs, and can be used by compositors as a configuration
5434 * identifier.
5435 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005436 * \memberof weston_output
5437 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005438 */
5439WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005440weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005441 struct weston_compositor *compositor,
5442 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005443{
5444 output->compositor = compositor;
5445 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005446 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005447 wl_list_init(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005448 output->enabled = false;
5449
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005450 wl_list_init(&output->head_list);
5451
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005452 weston_head_init(&output->head, name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005453 output->head.allocator_output = output;
5454 if (!compositor->backend->create_output) {
5455 weston_head_set_connection_status(&output->head, true);
5456 weston_compositor_add_head(compositor, &output->head);
5457 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005458
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005459 /* Add some (in)sane defaults which can be used
5460 * for checking if an output was properly configured
5461 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005462 output->scale = 0;
5463 /* Can't use -1 on uint32_t and 0 is valid enum value */
5464 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005465
5466 pixman_region32_init(&output->previous_damage);
5467 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005468 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005469}
5470
5471/** Adds weston_output object to pending output list.
5472 *
5473 * \param output The weston_output object to add
5474 * \param compositor The compositor instance.
5475 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005476 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005477 *
5478 * \memberof weston_output
5479 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005480 */
5481WL_EXPORT void
5482weston_compositor_add_pending_output(struct weston_output *output,
5483 struct weston_compositor *compositor)
5484{
Pekka Paalanene952a012017-03-29 17:14:00 +03005485 assert(output->disable);
5486 assert(output->enable);
5487
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005488 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005489 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005490}
5491
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005492/** Constructs a weston_output object that can be used by the compositor.
5493 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005494 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005495 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005496 *
5497 * Output coordinates are calculated and each new output is by default
5498 * assigned to the right of previous one.
5499 *
5500 * Sets up the transformation, zoom, and geometry of the output using
5501 * the properties that need to be configured by the compositor.
5502 *
5503 * Establishes a repaint timer for the output with the relevant display
5504 * object's event loop. See output_repaint_timer_handler().
5505 *
5506 * The output is assigned an ID. Weston can support up to 32 distinct
5507 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5508 * is referred to and used to find the first available ID number, and
5509 * then this ID is marked as used in output_id_pool.
5510 *
5511 * The output is also assigned a Wayland global with the wl_output
5512 * external interface.
5513 *
5514 * Backend specific function is called to set up the output output.
5515 *
5516 * Output is added to the compositor's output list
5517 *
5518 * If the backend specific function fails, the weston_output object
5519 * is returned to a state it was before calling this function and
5520 * is added to the compositor's pending_output_list in case it needs
5521 * to be reconfigured or just so it can be destroyed at shutdown.
5522 *
5523 * 0 is returned on success, -1 on failure.
5524 */
5525WL_EXPORT int
5526weston_output_enable(struct weston_output *output)
5527{
Armin Krezović782f5df2016-09-30 14:11:11 +02005528 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005529 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005530 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005531 int x = 0, y = 0;
5532
Pekka Paalanencc201e42017-03-30 15:11:25 +03005533 if (output->enabled) {
5534 weston_log("Error: attempt to enable an enabled output '%s'\n",
5535 output->name);
5536 return -1;
5537 }
5538
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005539 if (wl_list_empty(&output->head_list)) {
5540 weston_log("Error: cannot enable output '%s' without heads.\n",
5541 output->name);
5542 return -1;
5543 }
5544
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005545 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5546 weston_log("Error: no video mode for output '%s'.\n",
5547 output->name);
5548 return -1;
5549 }
5550
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005551 wl_list_for_each(head, &output->head_list, output_link) {
5552 assert(head->make);
5553 assert(head->model);
5554 }
5555
Armin Krezović782f5df2016-09-30 14:11:11 +02005556 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005557 struct weston_output, link);
5558
Armin Krezović782f5df2016-09-30 14:11:11 +02005559 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005560 x = iterator->x + iterator->width;
5561
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005562 /* Make sure the scale is set up */
5563 assert(output->scale);
5564
5565 /* Make sure we have a transform set */
5566 assert(output->transform != UINT32_MAX);
5567
Armin Krezović782f5df2016-09-30 14:11:11 +02005568 output->x = x;
5569 output->y = y;
5570 output->dirty = 1;
5571 output->original_scale = output->scale;
5572
5573 weston_output_transform_scale_init(output, output->transform, output->scale);
5574 weston_output_init_zoom(output);
5575
5576 weston_output_init_geometry(output, x, y);
5577 weston_output_damage(output);
5578
5579 wl_signal_init(&output->frame_signal);
5580 wl_signal_init(&output->destroy_signal);
5581 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005582 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005583
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005584 /* Enable the output (set up the crtc or create a
5585 * window representing the output, set up the
5586 * renderer, etc)
5587 */
5588 if (output->enable(output) < 0) {
5589 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005590 return -1;
5591 }
5592
5593 weston_compositor_add_output(output->compositor, output);
5594
5595 return 0;
5596}
5597
5598/** Converts a weston_output object to a pending output state, so it
5599 ** can be configured again or destroyed.
5600 *
5601 * \param output The weston_output object that needs to be disabled.
5602 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005603 * Calls a backend specific function to disable an output, in case
5604 * such function exists.
5605 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005606 * The backend specific disable function may choose to postpone the disabling
5607 * by returning a negative value, in which case this function returns early.
5608 * In that case the backend will guarantee the output will be disabled soon
5609 * by the backend calling this function again. One must not attempt to re-enable
5610 * the output until that happens.
5611 *
5612 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005613 * from weston's output_list (see weston_compositor_remove_output())
5614 * and is returned to a state it was before weston_output_enable()
5615 * was ran (see weston_output_enable_undo()).
5616 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005617 * See weston_output_init() for more information on the
5618 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005619 *
5620 * If the output has never been enabled yet, this function can still be
5621 * called to ensure that the output is actually turned off rather than left
5622 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005623 */
5624WL_EXPORT void
5625weston_output_disable(struct weston_output *output)
5626{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005627 /* Should we rename this? */
5628 output->destroying = 1;
5629
Pekka Paalanenc65df642017-03-29 15:45:46 +03005630 /* Disable is called unconditionally also for not-enabled outputs,
5631 * because at compositor start-up, if there is an output that is
5632 * already on but the compositor wants to turn it off, we have to
5633 * forward the turn-off to the backend so it knows to do it.
5634 * The backend cannot initially turn off everything, because it
5635 * would cause unnecessary mode-sets for all outputs the compositor
5636 * wants to be on.
5637 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005638 if (output->disable(output) < 0)
5639 return;
5640
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005641 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005642 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005643
5644 output->destroying = 0;
5645}
5646
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005647/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005648 *
5649 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005650 *
5651 * If there are new or changed heads, calls the heads_changed hook and
5652 * returns after the hook returns.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005653 */
5654WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005655weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005656{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005657 if (compositor->heads_changed_source) {
5658 wl_event_source_remove(compositor->heads_changed_source);
5659 weston_compositor_call_heads_changed(compositor);
5660 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005661}
5662
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005663/** Uninitialize an output
5664 *
5665 * Removes the output from the list of enabled outputs if necessary, but
5666 * does not call the backend's output disable function. The output will no
5667 * longer be in the list of pending outputs either.
5668 *
5669 * All fields of weston_output become uninitialized, i.e. should not be used
5670 * anymore. The caller can free the memory after this.
5671 *
5672 * \memberof weston_output
5673 * \internal
5674 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005675WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005676weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005677{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005678 struct weston_head *head, *tmp;
5679
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005680 output->destroying = 1;
5681
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03005682 if (output->idle_repaint_source)
5683 wl_event_source_remove(output->idle_repaint_source);
5684
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005685 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005686 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005687
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005688 pixman_region32_fini(&output->region);
5689 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005690 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005691
5692 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
5693 weston_head_detach(head);
5694
5695 weston_head_release(&output->head);
5696
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005697 free(output->name);
5698}
5699
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005700/** Create an output for an unused head
5701 *
5702 * \param compositor The compositor.
5703 * \param head The head to attach to the output.
5704 * \return A new \c weston_output, or NULL on failure.
5705 *
5706 * This creates a new weston_output that starts with the given head attached.
5707 * The output inherits the name of the head. The head must not be already
5708 * attached to another output.
5709 *
5710 * An output must be configured before it can be enabled.
5711 *
5712 * \memberof weston_compositor
5713 */
5714WL_EXPORT struct weston_output *
5715weston_compositor_create_output_with_head(struct weston_compositor *compositor,
5716 struct weston_head *head)
5717{
5718 struct weston_output *output;
5719
5720 if (head->allocator_output) {
5721 /* XXX: compatibility path to be removed after all converted */
5722 output = head->allocator_output;
5723 } else {
5724 assert(compositor->backend->create_output);
5725 output = compositor->backend->create_output(compositor,
5726 head->name);
5727 }
5728
5729 if (!output)
5730 return NULL;
5731
5732 if (weston_output_attach_head(output, head) < 0) {
5733 if (!head->allocator_output)
5734 output->destroy(output);
5735
5736 return NULL;
5737 }
5738
5739 return output;
5740}
5741
5742/** Destroy an output
5743 *
5744 * \param output The output to destroy.
5745 *
5746 * The heads attached to the given output are detached and become unused again.
5747 *
5748 * It is not necessary to explicitly destroy all outputs at compositor exit.
5749 * weston_compositor_destroy() will automatically destroy any remaining
5750 * outputs.
5751 *
5752 * \memberof weston_output
5753 */
5754WL_EXPORT void
5755weston_output_destroy(struct weston_output *output)
5756{
5757 struct weston_head *head;
5758
5759 /* XXX: compatibility path to be removed after all converted */
5760 head = weston_output_get_first_head(output);
5761 if (head->allocator_output) {
5762 /* The old design: backend is responsible for destroying the
5763 * output, so just undo create_output_with_head()
5764 */
5765 weston_head_detach(head);
5766 return;
5767 }
5768
5769 output->destroy(output);
5770}
5771
Pekka Paalanencf0a4762017-04-04 16:36:07 +03005772/** When you need a head...
5773 *
5774 * This function is a hack, used until all code has been converted to become
5775 * multi-head aware.
5776 *
5777 * \param output The weston_output whose head to get.
5778 * \return The first head in the output's list.
5779 */
5780WL_EXPORT struct weston_head *
5781weston_output_get_first_head(struct weston_output *output)
5782{
5783 if (wl_list_empty(&output->head_list))
5784 return NULL;
5785
5786 return container_of(output->head_list.next,
5787 struct weston_head, output_link);
5788}
5789
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01005790static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005791destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005792{
Jonny Lamb74130762013-11-26 18:19:46 +01005793 struct weston_surface *surface =
5794 wl_resource_get_user_data(resource);
5795
Pekka Paalanen4826f872016-04-22 14:14:38 +03005796 if (!surface)
5797 return;
5798
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005799 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02005800 surface->pending.buffer_viewport.buffer.src_width =
5801 wl_fixed_from_int(-1);
5802 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005803 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005804}
5805
5806static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005807viewport_destroy(struct wl_client *client,
5808 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005809{
5810 wl_resource_destroy(resource);
5811}
5812
5813static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005814viewport_set_source(struct wl_client *client,
5815 struct wl_resource *resource,
5816 wl_fixed_t src_x,
5817 wl_fixed_t src_y,
5818 wl_fixed_t src_width,
5819 wl_fixed_t src_height)
5820{
5821 struct weston_surface *surface =
5822 wl_resource_get_user_data(resource);
5823
Pekka Paalanen4826f872016-04-22 14:14:38 +03005824 if (!surface) {
5825 wl_resource_post_error(resource,
5826 WP_VIEWPORT_ERROR_NO_SURFACE,
5827 "wl_surface for this viewport is no longer exists");
5828 return;
5829 }
5830
5831 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03005832 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005833
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005834 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03005835 src_height == wl_fixed_from_int(-1) &&
5836 src_x == wl_fixed_from_int(-1) &&
5837 src_y == wl_fixed_from_int(-1)) {
5838 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005839 surface->pending.buffer_viewport.buffer.src_width =
5840 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005841 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005842 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005843 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005844
Pekka Paalanen201769a2016-04-26 14:42:11 +03005845 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005846 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005847 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03005848 "wl_surface@%d viewport source "
5849 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
5850 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005851 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03005852 wl_fixed_to_double(src_height),
5853 wl_fixed_to_double(src_x),
5854 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005855 return;
5856 }
5857
5858 surface->pending.buffer_viewport.buffer.src_x = src_x;
5859 surface->pending.buffer_viewport.buffer.src_y = src_y;
5860 surface->pending.buffer_viewport.buffer.src_width = src_width;
5861 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005862 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005863}
5864
5865static void
5866viewport_set_destination(struct wl_client *client,
5867 struct wl_resource *resource,
5868 int32_t dst_width,
5869 int32_t dst_height)
5870{
5871 struct weston_surface *surface =
5872 wl_resource_get_user_data(resource);
5873
Pekka Paalanen4826f872016-04-22 14:14:38 +03005874 if (!surface) {
5875 wl_resource_post_error(resource,
5876 WP_VIEWPORT_ERROR_NO_SURFACE,
5877 "wl_surface for this viewport no longer exists");
5878 return;
5879 }
5880
5881 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005882
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005883 if (dst_width == -1 && dst_height == -1) {
5884 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005885 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005886 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005887 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005888 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005889
5890 if (dst_width <= 0 || dst_height <= 0) {
5891 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005892 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005893 "destination size must be positive (%dx%d)",
5894 dst_width, dst_height);
5895 return;
5896 }
5897
5898 surface->pending.buffer_viewport.surface.width = dst_width;
5899 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005900 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005901}
5902
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005903static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005904 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005905 viewport_set_source,
5906 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01005907};
5908
5909static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005910viewporter_destroy(struct wl_client *client,
5911 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005912{
5913 wl_resource_destroy(resource);
5914}
5915
5916static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005917viewporter_get_viewport(struct wl_client *client,
5918 struct wl_resource *viewporter,
5919 uint32_t id,
5920 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005921{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005922 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005923 struct weston_surface *surface =
5924 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005925 struct wl_resource *resource;
5926
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005927 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005928 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005929 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005930 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01005931 return;
5932 }
5933
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005934 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005935 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005936 if (resource == NULL) {
5937 wl_client_post_no_memory(client);
5938 return;
5939 }
5940
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005941 wl_resource_set_implementation(resource, &viewport_interface,
5942 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01005943
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005944 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005945}
5946
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005947static const struct wp_viewporter_interface viewporter_interface = {
5948 viewporter_destroy,
5949 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01005950};
5951
5952static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005953bind_viewporter(struct wl_client *client,
5954 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005955{
5956 struct wl_resource *resource;
5957
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005958 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005959 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005960 if (resource == NULL) {
5961 wl_client_post_no_memory(client);
5962 return;
5963 }
5964
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005965 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01005966 NULL, NULL);
5967}
5968
5969static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04005970destroy_presentation_feedback(struct wl_resource *feedback_resource)
5971{
5972 struct weston_presentation_feedback *feedback;
5973
5974 feedback = wl_resource_get_user_data(feedback_resource);
5975
5976 wl_list_remove(&feedback->link);
5977 free(feedback);
5978}
5979
5980static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005981presentation_destroy(struct wl_client *client, struct wl_resource *resource)
5982{
5983 wl_resource_destroy(resource);
5984}
5985
5986static void
5987presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005988 struct wl_resource *presentation_resource,
5989 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005990 uint32_t callback)
5991{
Pekka Paalanen133e4392014-09-23 22:08:46 -04005992 struct weston_surface *surface;
5993 struct weston_presentation_feedback *feedback;
5994
5995 surface = wl_resource_get_user_data(surface_resource);
5996
Bryce Harringtonde16d892014-11-20 22:21:57 -08005997 feedback = zalloc(sizeof *feedback);
5998 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04005999 goto err_calloc;
6000
6001 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006002 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006003 1, callback);
6004 if (!feedback->resource)
6005 goto err_create;
6006
6007 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6008 destroy_presentation_feedback);
6009
6010 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6011
6012 return;
6013
6014err_create:
6015 free(feedback);
6016
6017err_calloc:
6018 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006019}
6020
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006021static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006022 presentation_destroy,
6023 presentation_feedback
6024};
6025
6026static void
6027bind_presentation(struct wl_client *client,
6028 void *data, uint32_t version, uint32_t id)
6029{
6030 struct weston_compositor *compositor = data;
6031 struct wl_resource *resource;
6032
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006033 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006034 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006035 if (resource == NULL) {
6036 wl_client_post_no_memory(client);
6037 return;
6038 }
6039
6040 wl_resource_set_implementation(resource, &presentation_implementation,
6041 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006042 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006043}
6044
6045static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006046compositor_bind(struct wl_client *client,
6047 void *data, uint32_t version, uint32_t id)
6048{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006049 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006050 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006051
Jason Ekstranda85118c2013-06-27 20:17:02 -05006052 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006053 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006054 if (resource == NULL) {
6055 wl_client_post_no_memory(client);
6056 return;
6057 }
6058
6059 wl_resource_set_implementation(resource, &compositor_interface,
6060 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006061}
6062
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006063WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006064weston_environment_get_fd(const char *env)
6065{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006066 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006067 int fd, flags;
6068
6069 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006070 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006071 return -1;
6072
6073 flags = fcntl(fd, F_GETFD);
6074 if (flags == -1)
6075 return -1;
6076
6077 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6078 unsetenv(env);
6079
6080 return fd;
6081}
6082
Pekka Paalanenb5026542014-11-12 15:09:24 +02006083static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006084timeline_key_binding_handler(struct weston_keyboard *keyboard,
6085 const struct timespec *time, uint32_t key,
6086 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006087{
6088 struct weston_compositor *compositor = data;
6089
6090 if (weston_timeline_enabled_)
6091 weston_timeline_close();
6092 else
6093 weston_timeline_open(compositor);
6094}
6095
Giulio Camuffo459137b2014-10-11 23:56:24 +03006096/** Create the compositor.
6097 *
6098 * This functions creates and initializes a compositor instance.
6099 *
6100 * \param display The Wayland display to be used.
6101 * \param user_data A pointer to an object that can later be retrieved
6102 * using the \ref weston_compositor_get_user_data function.
6103 * \return The compositor instance on success or NULL on failure.
6104 */
6105WL_EXPORT struct weston_compositor *
6106weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006107{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006108 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006109 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006110
Giulio Camuffo459137b2014-10-11 23:56:24 +03006111 ec = zalloc(sizeof *ec);
6112 if (!ec)
6113 return NULL;
6114
6115 ec->wl_display = display;
6116 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006117 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006118 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006119 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006120 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006121 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006122 wl_signal_init(&ec->idle_signal);
6123 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006124 wl_signal_init(&ec->show_input_panel_signal);
6125 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006126 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006127 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006128 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006129 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006130 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006131 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006132 wl_signal_init(&ec->heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006133 wl_signal_init(&ec->session_signal);
6134 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006135
Casey Dahlin58ba1372012-04-19 22:50:08 -04006136 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006137 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006138
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006139 ec->activate_serial = 1;
6140
Derek Foreman152254b2015-11-26 14:17:48 -06006141 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006142 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006143 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006144
Giulio Camuffo954f1832014-10-11 18:27:30 +03006145 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006146 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006147 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006148
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006149 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006150 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006151 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006152
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006153 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006154 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006155 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006156
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006157 if (weston_input_init(ec) != 0)
6158 goto fail;
6159
Jason Ekstranda7af7042013-10-12 22:38:11 -05006160 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006161 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006162 wl_list_init(&ec->layer_list);
6163 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006164 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006165 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006166 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006167 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006168 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006169 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006170 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006171 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006172 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006173
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006174 wl_list_init(&ec->plugin_api_list);
6175
Xiong Zhang97116532013-10-23 13:58:31 +08006176 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006177 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006178
Giulio Camuffo459137b2014-10-11 23:56:24 +03006179 wl_data_device_manager_init(ec->wl_display);
6180
6181 wl_display_init_shm(ec->wl_display);
6182
6183 loop = wl_display_get_event_loop(ec->wl_display);
6184 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006185 ec->repaint_timer =
6186 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6187 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006188
Quentin Glidic82681572016-12-17 13:40:51 +01006189 weston_layer_init(&ec->fade_layer, ec);
6190 weston_layer_init(&ec->cursor_layer, ec);
6191
6192 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6193 weston_layer_set_position(&ec->cursor_layer,
6194 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006195
6196 weston_compositor_add_debug_binding(ec, KEY_T,
6197 timeline_key_binding_handler, ec);
6198
Giulio Camuffo459137b2014-10-11 23:56:24 +03006199 return ec;
6200
6201fail:
6202 free(ec);
6203 return NULL;
6204}
6205
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006206WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006207weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006208{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006209 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006210
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006211 wl_event_source_remove(ec->idle_source);
6212
Matt Roper361d2ad2011-08-29 13:52:23 -07006213 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006214 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006215 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006216
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006217 /* Destroy all pending outputs associated with this compositor */
6218 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6219 output->destroy(output);
6220
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006221 if (ec->renderer)
6222 ec->renderer->destroy(ec);
6223
Daniel Stone325fc2d2012-05-30 16:31:58 +01006224 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006225 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006226 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006227 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006228 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006229 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006230
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006231 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006232}
6233
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006234WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006235weston_compositor_exit_with_code(struct weston_compositor *compositor,
6236 int exit_code)
6237{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006238 if (compositor->exit_code == EXIT_SUCCESS)
6239 compositor->exit_code = exit_code;
6240
Giulio Camuffo459137b2014-10-11 23:56:24 +03006241 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006242}
6243
6244WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006245weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6246 const struct weston_pointer_grab_interface *interface)
6247{
6248 struct weston_seat *seat;
6249
6250 ec->default_pointer_grab = interface;
6251 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05006252 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6253
6254 if (pointer)
6255 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006256 }
6257}
6258
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04006259WL_EXPORT int
6260weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
6261 clockid_t clk_id)
6262{
6263 struct timespec ts;
6264
6265 if (clock_gettime(clk_id, &ts) < 0)
6266 return -1;
6267
6268 compositor->presentation_clock = clk_id;
6269
6270 return 0;
6271}
6272
6273/*
6274 * For choosing the software clock, when the display hardware or API
6275 * does not expose a compatible presentation timestamp.
6276 */
6277WL_EXPORT int
6278weston_compositor_set_presentation_clock_software(
6279 struct weston_compositor *compositor)
6280{
6281 /* In order of preference */
6282 static const clockid_t clocks[] = {
6283 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
6284 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
6285 CLOCK_MONOTONIC, /* no jumps, may crawl */
6286 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
6287 CLOCK_REALTIME /* may jump and crawl */
6288 };
6289 unsigned i;
6290
6291 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
6292 if (weston_compositor_set_presentation_clock(compositor,
6293 clocks[i]) == 0)
6294 return 0;
6295
6296 weston_log("Error: no suitable presentation clock available.\n");
6297
6298 return -1;
6299}
6300
Pekka Paalanen662f3842015-03-18 12:17:26 +02006301/** Read the current time from the Presentation clock
6302 *
6303 * \param compositor
6304 * \param ts[out] The current time.
6305 *
6306 * \note Reading the current time in user space is always imprecise to some
6307 * degree.
6308 *
6309 * This function is never meant to fail. If reading the clock does fail,
6310 * an error message is logged and a zero time is returned. Callers are not
6311 * supposed to detect or react to failures.
6312 */
6313WL_EXPORT void
6314weston_compositor_read_presentation_clock(
6315 const struct weston_compositor *compositor,
6316 struct timespec *ts)
6317{
6318 static bool warned;
6319 int ret;
6320
6321 ret = clock_gettime(compositor->presentation_clock, ts);
6322 if (ret < 0) {
6323 ts->tv_sec = 0;
6324 ts->tv_nsec = 0;
6325
6326 if (!warned)
6327 weston_log("Error: failure to read "
6328 "the presentation clock %#x: '%m' (%d)\n",
6329 compositor->presentation_clock, errno);
6330 warned = true;
6331 }
6332}
6333
Pekka Paalanen230f3b12014-09-29 14:18:40 -04006334/** Import dmabuf buffer into current renderer
6335 *
6336 * \param compositor
6337 * \param buffer the dmabuf buffer to import
6338 * \return true on usable buffers, false otherwise
6339 *
6340 * This function tests that the linux_dmabuf_buffer is usable
6341 * for the current renderer. Returns false on unusable buffers. Usually
6342 * usability is tested by importing the dmabufs for composition.
6343 *
6344 * This hook is also used for detecting if the renderer supports
6345 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
6346 * supported.
6347 * */
6348WL_EXPORT bool
6349weston_compositor_import_dmabuf(struct weston_compositor *compositor,
6350 struct linux_dmabuf_buffer *buffer)
6351{
6352 struct weston_renderer *renderer;
6353
6354 renderer = compositor->renderer;
6355
6356 if (renderer->import_dmabuf == NULL)
6357 return false;
6358
6359 return renderer->import_dmabuf(compositor, buffer);
6360}
6361
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006362WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006363weston_version(int *major, int *minor, int *micro)
6364{
6365 *major = WESTON_VERSION_MAJOR;
6366 *minor = WESTON_VERSION_MINOR;
6367 *micro = WESTON_VERSION_MICRO;
6368}
6369
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03006370WL_EXPORT void *
6371weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006372{
Derek Foreman3f86e502015-06-08 11:46:54 -05006373 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006374 char path[PATH_MAX];
6375 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00006376 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006377
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08006378 if (name == NULL)
6379 return NULL;
6380
Derek Foreman3f86e502015-06-08 11:46:54 -05006381 if (name[0] != '/') {
6382 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00006383 len = snprintf(path, sizeof path, "%s/.libs/%s",
6384 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006385 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00006386 len = snprintf(path, sizeof path, "%s/%s",
6387 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006388 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00006389 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006390 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006391
Daniel Stonebeb97e52016-11-28 12:13:54 +00006392 /* snprintf returns the length of the string it would've written,
6393 * _excluding_ the NUL byte. So even being equal to the size of
6394 * our buffer is an error here. */
6395 if (len >= sizeof path)
6396 return NULL;
6397
Kristian Høgsberga6813d22012-09-12 12:21:01 -04006398 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
6399 if (module) {
6400 weston_log("Module '%s' already loaded\n", path);
6401 dlclose(module);
6402 return NULL;
6403 }
6404
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006405 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04006406 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006407 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006408 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006409 return NULL;
6410 }
6411
6412 init = dlsym(module, entrypoint);
6413 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006414 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00006415 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006416 return NULL;
6417 }
6418
6419 return init;
6420}
6421
Giulio Camuffo459137b2014-10-11 23:56:24 +03006422
6423/** Destroys the compositor.
6424 *
6425 * This function cleans up the compositor state and destroys it.
6426 *
6427 * \param compositor The compositor to be destroyed.
6428 */
6429WL_EXPORT void
6430weston_compositor_destroy(struct weston_compositor *compositor)
6431{
6432 /* prevent further rendering while shutting down */
6433 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
6434
6435 wl_signal_emit(&compositor->destroy_signal, compositor);
6436
6437 weston_compositor_xkb_destroy(compositor);
6438
Giulio Camuffo2d24e642015-10-03 16:25:15 +03006439 if (compositor->backend)
6440 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006441
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006442 /* The backend is responsible for destroying the heads. */
6443 assert(wl_list_empty(&compositor->head_list));
6444
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006445 weston_plugin_api_destroy_list(compositor);
6446
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006447 if (compositor->heads_changed_source)
6448 wl_event_source_remove(compositor->heads_changed_source);
6449
Giulio Camuffo459137b2014-10-11 23:56:24 +03006450 free(compositor);
6451}
6452
6453/** Instruct the compositor to exit.
6454 *
6455 * This functions does not directly destroy the compositor object, it merely
6456 * command it to start the tear down process. It is not guaranteed that the
6457 * tear down will happen immediately.
6458 *
6459 * \param compositor The compositor to tear down.
6460 */
6461WL_EXPORT void
6462weston_compositor_exit(struct weston_compositor *compositor)
6463{
6464 compositor->exit(compositor);
6465}
6466
6467/** Return the user data stored in the compositor.
6468 *
6469 * This function returns the user data pointer set with user_data parameter
6470 * to the \ref weston_compositor_create function.
6471 */
6472WL_EXPORT void *
6473weston_compositor_get_user_data(struct weston_compositor *compositor)
6474{
6475 return compositor->user_data;
6476}
Pekka Paalanendd186732016-06-03 14:49:54 +03006477
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006478static const char * const backend_map[] = {
6479 [WESTON_BACKEND_DRM] = "drm-backend.so",
6480 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
6481 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
6482 [WESTON_BACKEND_RDP] = "rdp-backend.so",
6483 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
6484 [WESTON_BACKEND_X11] = "x11-backend.so",
6485};
6486
Pekka Paalanendd186732016-06-03 14:49:54 +03006487/** Load a backend into a weston_compositor
6488 *
6489 * A backend must be loaded to make a weston_compositor work. A backend
6490 * provides input and output capabilities, and determines the renderer to use.
6491 *
6492 * \param compositor A compositor that has not had a backend loaded yet.
6493 * \param backend Name of the backend file.
6494 * \param config_base A pointer to a backend-specific configuration
6495 * structure's 'base' member.
6496 *
6497 * \return 0 on success, or -1 on error.
6498 */
6499WL_EXPORT int
6500weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006501 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03006502 struct weston_backend_config *config_base)
6503{
6504 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03006505 struct weston_backend_config *config_base);
6506
Pekka Paalanend7e35112017-08-29 17:04:12 +03006507 if (compositor->backend) {
6508 weston_log("Error: attempt to load a backend when one is already loaded\n");
6509 return -1;
6510 }
6511
Quentin Glidic887c0182016-07-10 11:00:53 +02006512 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006513 return -1;
6514
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01006515 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03006516 if (!backend_init)
6517 return -1;
6518
Pekka Paalanend7e35112017-08-29 17:04:12 +03006519 if (backend_init(compositor, config_base) < 0) {
6520 compositor->backend = NULL;
6521 return -1;
6522 }
6523
6524 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03006525}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006526
6527WL_EXPORT int
6528weston_compositor_load_xwayland(struct weston_compositor *compositor)
6529{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006530 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006531
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006532 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006533 if (!module_init)
6534 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006535 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006536 return -1;
6537 return 0;
6538}