blob: f5039333223034a402f1e18a195f7926e2ddfe56 [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
Bryce Harrington689fece2016-08-10 17:25:23 -070054#include <idle-inhibit-unstable-v1-server-protocol.h>
55
Pekka Paalanenb5026542014-11-12 15:09:24 +020056#include "timeline.h"
57
Kristian Høgsberg82863022010-06-04 21:52:02 -040058#include "compositor.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030059#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020060#include "presentation-time-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070061#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070062#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070063#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030064#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040065#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050066#include "version.h"
Pekka Paalanen827b5d22016-06-29 11:54:26 +020067#include "plugin-registry.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050068
Pekka Paalanen0513a952014-05-21 16:17:27 +030069#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
70
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020071static void
Alexander Larsson0b135062013-05-28 16:23:36 +020072weston_output_transform_scale_init(struct weston_output *output,
73 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020074
Rob Bradford27b17932013-06-26 18:08:46 +010075static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050076weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010077
Derek Foreman6ae7bc92014-11-04 10:47:33 -060078static void weston_mode_switch_finish(struct weston_output *output,
79 int mode_changed,
80 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080081{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020082 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020083 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020084 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060085 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020086
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020087 pixman_region32_init(&old_output_region);
88 pixman_region32_copy(&old_output_region, &output->region);
89
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020090 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020091 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020092
93 pixman_region32_init(&output->previous_damage);
94 pixman_region32_init_rect(&output->region, output->x, output->y,
95 output->width, output->height);
96
97 weston_output_update_matrix(output);
98
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020099 /* If a pointer falls outside the outputs new geometry, move it to its
100 * lower-right corner */
101 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500102 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200103 int32_t x, y;
104
105 if (!pointer)
106 continue;
107
108 x = wl_fixed_to_int(pointer->x);
109 y = wl_fixed_to_int(pointer->y);
110
111 if (!pixman_region32_contains_point(&old_output_region,
112 x, y, NULL) ||
113 pixman_region32_contains_point(&output->region,
114 x, y, NULL))
115 continue;
116
117 if (x >= output->x + output->width)
118 x = output->x + output->width - 1;
119 if (y >= output->y + output->height)
120 y = output->y + output->height - 1;
121
122 pointer->x = wl_fixed_from_int(x);
123 pointer->y = wl_fixed_from_int(y);
124 }
125
126 pixman_region32_fini(&old_output_region);
127
Derek Foremandd4cd332014-11-10 10:29:59 -0600128 if (!mode_changed && !scale_changed)
129 return;
130
Hardening57388e42013-09-18 23:56:36 +0200131 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600132 wl_resource_for_each(resource, &output->resource_list) {
133 if (mode_changed) {
134 wl_output_send_mode(resource,
135 output->current_mode->flags,
136 output->current_mode->width,
137 output->current_mode->height,
138 output->current_mode->refresh);
139 }
Hardening57388e42013-09-18 23:56:36 +0200140
Derek Foreman41bdc272014-11-05 13:26:57 -0600141 version = wl_resource_get_version(resource);
142 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600143 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200144
Derek Foreman41bdc272014-11-05 13:26:57 -0600145 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
146 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600147 }
148}
149
David Fort0de859e2016-05-27 23:22:57 +0200150
151static void
152weston_compositor_reflow_outputs(struct weston_compositor *compositor,
153 struct weston_output *resized_output, int delta_width);
154
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600155WL_EXPORT int
156weston_output_mode_set_native(struct weston_output *output,
157 struct weston_mode *mode,
158 int32_t scale)
159{
160 int ret;
161 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200162 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600163
164 if (!output->switch_mode)
165 return -1;
166
167 if (!output->original_mode) {
168 mode_changed = 1;
169 ret = output->switch_mode(output, mode);
170 if (ret < 0)
171 return ret;
172 if (output->current_scale != scale) {
173 scale_changed = 1;
174 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200175 }
176 }
177
David Fort0de859e2016-05-27 23:22:57 +0200178 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600179 output->native_mode = mode;
180 output->native_scale = scale;
181
182 weston_mode_switch_finish(output, mode_changed, scale_changed);
183
David Fort0de859e2016-05-27 23:22:57 +0200184 if (mode_changed || scale_changed) {
185 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
186
187 wl_signal_emit(&output->compositor->output_resized_signal, output);
188 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600189 return 0;
190}
191
192WL_EXPORT int
193weston_output_mode_switch_to_native(struct weston_output *output)
194{
195 int ret;
196 int mode_changed = 0, scale_changed = 0;
197
198 if (!output->switch_mode)
199 return -1;
200
201 if (!output->original_mode) {
202 weston_log("already in the native mode\n");
203 return -1;
204 }
205 /* the non fullscreen clients haven't seen a mode set since we
206 * switched into a temporary, so we need to notify them if the
207 * mode at that time is different from the native mode now.
208 */
209 mode_changed = (output->original_mode != output->native_mode);
210 scale_changed = (output->original_scale != output->native_scale);
211
212 ret = output->switch_mode(output, output->native_mode);
213 if (ret < 0)
214 return ret;
215
216 output->current_scale = output->native_scale;
217
218 output->original_mode = NULL;
219 output->original_scale = 0;
220
221 weston_mode_switch_finish(output, mode_changed, scale_changed);
222
223 return 0;
224}
225
226WL_EXPORT int
227weston_output_mode_switch_to_temporary(struct weston_output *output,
228 struct weston_mode *mode,
229 int32_t scale)
230{
231 int ret;
232
233 if (!output->switch_mode)
234 return -1;
235
236 /* original_mode is the last mode non full screen clients have seen,
237 * so we shouldn't change it if we already have one set.
238 */
239 if (!output->original_mode) {
240 output->original_mode = output->native_mode;
241 output->original_scale = output->native_scale;
242 }
243 ret = output->switch_mode(output, mode);
244 if (ret < 0)
245 return ret;
246
247 output->current_scale = scale;
248
249 weston_mode_switch_finish(output, 0, 0);
250
251 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800252}
253
Benjamin Franzke06286262011-05-06 19:12:33 +0200254static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300255region_init_infinite(pixman_region32_t *region)
256{
257 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
258 UINT32_MAX, UINT32_MAX);
259}
260
Pekka Paalanene67858b2013-04-25 13:57:42 +0300261static struct weston_subsurface *
262weston_surface_to_subsurface(struct weston_surface *surface);
263
Jason Ekstranda7af7042013-10-12 22:38:11 -0500264WL_EXPORT struct weston_view *
265weston_view_create(struct weston_surface *surface)
266{
267 struct weston_view *view;
268
Bryce Harringtonde16d892014-11-20 22:21:57 -0800269 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500270 if (view == NULL)
271 return NULL;
272
273 view->surface = surface;
274
Jason Ekstranda7af7042013-10-12 22:38:11 -0500275 /* Assign to surface */
276 wl_list_insert(&surface->views, &view->surface_link);
277
278 wl_signal_init(&view->destroy_signal);
279 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300280 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500281
Jason Ekstranda7af7042013-10-12 22:38:11 -0500282 pixman_region32_init(&view->clip);
283
284 view->alpha = 1.0;
285 pixman_region32_init(&view->transform.opaque);
286
287 wl_list_init(&view->geometry.transformation_list);
288 wl_list_insert(&view->geometry.transformation_list,
289 &view->transform.position.link);
290 weston_matrix_init(&view->transform.position.matrix);
291 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200292 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500293 pixman_region32_init(&view->transform.boundingbox);
294 view->transform.dirty = 1;
295
Jason Ekstranda7af7042013-10-12 22:38:11 -0500296 return view;
297}
298
Jason Ekstrand108865d2014-06-26 10:04:49 -0700299struct weston_frame_callback {
300 struct wl_resource *resource;
301 struct wl_list link;
302};
303
Pekka Paalanen133e4392014-09-23 22:08:46 -0400304struct weston_presentation_feedback {
305 struct wl_resource *resource;
306
307 /* XXX: could use just wl_resource_get_link() instead */
308 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200309
310 /* The per-surface feedback flags */
311 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400312};
313
314static void
315weston_presentation_feedback_discard(
316 struct weston_presentation_feedback *feedback)
317{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200318 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400319 wl_resource_destroy(feedback->resource);
320}
321
322static void
323weston_presentation_feedback_discard_list(struct wl_list *list)
324{
325 struct weston_presentation_feedback *feedback, *tmp;
326
327 wl_list_for_each_safe(feedback, tmp, list, link)
328 weston_presentation_feedback_discard(feedback);
329}
330
331static void
332weston_presentation_feedback_present(
333 struct weston_presentation_feedback *feedback,
334 struct weston_output *output,
335 uint32_t refresh_nsec,
336 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200337 uint64_t seq,
338 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400339{
340 struct wl_client *client = wl_resource_get_client(feedback->resource);
341 struct wl_resource *o;
342 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400343
344 wl_resource_for_each(o, &output->resource_list) {
345 if (wl_resource_get_client(o) != client)
346 continue;
347
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200348 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400349 }
350
351 secs = ts->tv_sec;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200352 wp_presentation_feedback_send_presented(feedback->resource,
353 secs >> 32, secs & 0xffffffff,
354 ts->tv_nsec,
355 refresh_nsec,
356 seq >> 32, seq & 0xffffffff,
357 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400358 wl_resource_destroy(feedback->resource);
359}
360
361static void
362weston_presentation_feedback_present_list(struct wl_list *list,
363 struct weston_output *output,
364 uint32_t refresh_nsec,
365 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200366 uint64_t seq,
367 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400368{
369 struct weston_presentation_feedback *feedback, *tmp;
370
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200371 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200372 wl_list_empty(list));
373
Pekka Paalanen133e4392014-09-23 22:08:46 -0400374 wl_list_for_each_safe(feedback, tmp, list, link)
375 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200376 refresh_nsec, ts, seq,
377 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400378}
379
Jason Ekstrand7b982072014-05-20 14:33:03 -0500380static void
381surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
382{
383 struct weston_surface_state *state =
384 container_of(listener, struct weston_surface_state,
385 buffer_destroy_listener);
386
387 state->buffer = NULL;
388}
389
390static void
391weston_surface_state_init(struct weston_surface_state *state)
392{
393 state->newly_attached = 0;
394 state->buffer = NULL;
395 state->buffer_destroy_listener.notify =
396 surface_state_handle_buffer_destroy;
397 state->sx = 0;
398 state->sy = 0;
399
Derek Foreman152254b2015-11-26 14:17:48 -0600400 pixman_region32_init(&state->damage_surface);
401 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500402 pixman_region32_init(&state->opaque);
403 region_init_infinite(&state->input);
404
405 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400406 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500407
408 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
409 state->buffer_viewport.buffer.scale = 1;
410 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
411 state->buffer_viewport.surface.width = -1;
412 state->buffer_viewport.changed = 0;
413}
414
415static void
416weston_surface_state_fini(struct weston_surface_state *state)
417{
418 struct weston_frame_callback *cb, *next;
419
420 wl_list_for_each_safe(cb, next,
421 &state->frame_callback_list, link)
422 wl_resource_destroy(cb->resource);
423
Pekka Paalanen133e4392014-09-23 22:08:46 -0400424 weston_presentation_feedback_discard_list(&state->feedback_list);
425
Jason Ekstrand7b982072014-05-20 14:33:03 -0500426 pixman_region32_fini(&state->input);
427 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600428 pixman_region32_fini(&state->damage_surface);
429 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500430
431 if (state->buffer)
432 wl_list_remove(&state->buffer_destroy_listener.link);
433 state->buffer = NULL;
434}
435
436static void
437weston_surface_state_set_buffer(struct weston_surface_state *state,
438 struct weston_buffer *buffer)
439{
440 if (state->buffer == buffer)
441 return;
442
443 if (state->buffer)
444 wl_list_remove(&state->buffer_destroy_listener.link);
445 state->buffer = buffer;
446 if (state->buffer)
447 wl_signal_add(&state->buffer->destroy_signal,
448 &state->buffer_destroy_listener);
449}
450
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500451WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500452weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500453{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500454 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400455
Bryce Harringtonde16d892014-11-20 22:21:57 -0800456 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400457 if (surface == NULL)
458 return NULL;
459
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500460 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800461 wl_signal_init(&surface->commit_signal);
Bryce Harringtonca5b6242016-08-23 13:39:14 -0700462 wl_signal_init(&surface->drop_idle_inhibitor_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500463
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500464 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200465 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400466
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200467 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
468 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200469 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
470 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500471
472 weston_surface_state_init(&surface->pending);
473
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400474 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500475 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300476 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400477
Jason Ekstranda7af7042013-10-12 22:38:11 -0500478 wl_list_init(&surface->views);
479
480 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400481 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500482
Pekka Paalanene67858b2013-04-25 13:57:42 +0300483 wl_list_init(&surface->subsurface_list);
484 wl_list_init(&surface->subsurface_list_pending);
485
Jason Ekstrand1e059042014-10-16 10:55:19 -0500486 weston_matrix_init(&surface->buffer_to_surface_matrix);
487 weston_matrix_init(&surface->surface_to_buffer_matrix);
488
Jonas Ådahld3414f22016-07-22 17:56:31 +0800489 wl_list_init(&surface->pointer_constraints);
490
Bryce Harringtonf8300c82016-08-10 17:25:22 -0700491 surface->inhibit_idling = false;
492
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400493 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500494}
495
Alex Wu8811bf92012-02-28 18:07:54 +0800496WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500497weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200498 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500499{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100500 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500501}
502
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400503WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500504weston_view_to_global_float(struct weston_view *view,
505 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200506{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500507 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200508 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
509
Jason Ekstranda7af7042013-10-12 22:38:11 -0500510 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200511
512 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200513 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700514 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200515 v.f[3]);
516 *x = 0;
517 *y = 0;
518 return;
519 }
520
521 *x = v.f[0] / v.f[3];
522 *y = v.f[1] / v.f[3];
523 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500524 *x = sx + view->geometry.x;
525 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200526 }
527}
528
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500529WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200530weston_transformed_coord(int width, int height,
531 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200532 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200533 float sx, float sy, float *bx, float *by)
534{
535 switch (transform) {
536 case WL_OUTPUT_TRANSFORM_NORMAL:
537 default:
538 *bx = sx;
539 *by = sy;
540 break;
541 case WL_OUTPUT_TRANSFORM_FLIPPED:
542 *bx = width - sx;
543 *by = sy;
544 break;
545 case WL_OUTPUT_TRANSFORM_90:
546 *bx = height - sy;
547 *by = sx;
548 break;
549 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
550 *bx = height - sy;
551 *by = width - sx;
552 break;
553 case WL_OUTPUT_TRANSFORM_180:
554 *bx = width - sx;
555 *by = height - sy;
556 break;
557 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
558 *bx = sx;
559 *by = height - sy;
560 break;
561 case WL_OUTPUT_TRANSFORM_270:
562 *bx = sy;
563 *by = width - sx;
564 break;
565 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
566 *bx = sy;
567 *by = sx;
568 break;
569 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200570
571 *bx *= scale;
572 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200573}
574
575WL_EXPORT pixman_box32_t
576weston_transformed_rect(int width, int height,
577 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200578 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200579 pixman_box32_t rect)
580{
581 float x1, x2, y1, y2;
582
583 pixman_box32_t ret;
584
Alexander Larsson4ea95522013-05-22 14:41:37 +0200585 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200586 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200587 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200588 rect.x2, rect.y2, &x2, &y2);
589
590 if (x1 <= x2) {
591 ret.x1 = x1;
592 ret.x2 = x2;
593 } else {
594 ret.x1 = x2;
595 ret.x2 = x1;
596 }
597
598 if (y1 <= y2) {
599 ret.y1 = y1;
600 ret.y2 = y2;
601 } else {
602 ret.y1 = y2;
603 ret.y2 = y1;
604 }
605
606 return ret;
607}
608
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600609/** Transform a region by a matrix, restricted to axis-aligned transformations
610 *
611 * Warning: This function does not work for projective, affine, or matrices
612 * that encode arbitrary rotations. Only 90-degree step rotations are
613 * supported.
614 */
615WL_EXPORT void
616weston_matrix_transform_region(pixman_region32_t *dest,
617 struct weston_matrix *matrix,
618 pixman_region32_t *src)
619{
620 pixman_box32_t *src_rects, *dest_rects;
621 int nrects, i;
622
623 src_rects = pixman_region32_rectangles(src, &nrects);
624 dest_rects = malloc(nrects * sizeof(*dest_rects));
625 if (!dest_rects)
626 return;
627
628 for (i = 0; i < nrects; i++) {
629 struct weston_vector vec1 = {{
630 src_rects[i].x1, src_rects[i].y1, 0, 1
631 }};
632 weston_matrix_transform(matrix, &vec1);
633 vec1.f[0] /= vec1.f[3];
634 vec1.f[1] /= vec1.f[3];
635
636 struct weston_vector vec2 = {{
637 src_rects[i].x2, src_rects[i].y2, 0, 1
638 }};
639 weston_matrix_transform(matrix, &vec2);
640 vec2.f[0] /= vec2.f[3];
641 vec2.f[1] /= vec2.f[3];
642
643 if (vec1.f[0] < vec2.f[0]) {
644 dest_rects[i].x1 = floor(vec1.f[0]);
645 dest_rects[i].x2 = ceil(vec2.f[0]);
646 } else {
647 dest_rects[i].x1 = floor(vec2.f[0]);
648 dest_rects[i].x2 = ceil(vec1.f[0]);
649 }
650
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600651 if (vec1.f[1] < vec2.f[1]) {
652 dest_rects[i].y1 = floor(vec1.f[1]);
653 dest_rects[i].y2 = ceil(vec2.f[1]);
654 } else {
655 dest_rects[i].y1 = floor(vec2.f[1]);
656 dest_rects[i].y2 = ceil(vec1.f[1]);
657 }
658 }
659
660 pixman_region32_clear(dest);
661 pixman_region32_init_rects(dest, dest_rects, nrects);
662 free(dest_rects);
663}
664
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200665WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500666weston_transformed_region(int width, int height,
667 enum wl_output_transform transform,
668 int32_t scale,
669 pixman_region32_t *src, pixman_region32_t *dest)
670{
671 pixman_box32_t *src_rects, *dest_rects;
672 int nrects, i;
673
674 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
675 if (src != dest)
676 pixman_region32_copy(dest, src);
677 return;
678 }
679
680 src_rects = pixman_region32_rectangles(src, &nrects);
681 dest_rects = malloc(nrects * sizeof(*dest_rects));
682 if (!dest_rects)
683 return;
684
685 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
686 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
687 } else {
688 for (i = 0; i < nrects; i++) {
689 switch (transform) {
690 default:
691 case WL_OUTPUT_TRANSFORM_NORMAL:
692 dest_rects[i].x1 = src_rects[i].x1;
693 dest_rects[i].y1 = src_rects[i].y1;
694 dest_rects[i].x2 = src_rects[i].x2;
695 dest_rects[i].y2 = src_rects[i].y2;
696 break;
697 case WL_OUTPUT_TRANSFORM_90:
698 dest_rects[i].x1 = height - src_rects[i].y2;
699 dest_rects[i].y1 = src_rects[i].x1;
700 dest_rects[i].x2 = height - src_rects[i].y1;
701 dest_rects[i].y2 = src_rects[i].x2;
702 break;
703 case WL_OUTPUT_TRANSFORM_180:
704 dest_rects[i].x1 = width - src_rects[i].x2;
705 dest_rects[i].y1 = height - src_rects[i].y2;
706 dest_rects[i].x2 = width - src_rects[i].x1;
707 dest_rects[i].y2 = height - src_rects[i].y1;
708 break;
709 case WL_OUTPUT_TRANSFORM_270:
710 dest_rects[i].x1 = src_rects[i].y1;
711 dest_rects[i].y1 = width - src_rects[i].x2;
712 dest_rects[i].x2 = src_rects[i].y2;
713 dest_rects[i].y2 = width - src_rects[i].x1;
714 break;
715 case WL_OUTPUT_TRANSFORM_FLIPPED:
716 dest_rects[i].x1 = width - src_rects[i].x2;
717 dest_rects[i].y1 = src_rects[i].y1;
718 dest_rects[i].x2 = width - src_rects[i].x1;
719 dest_rects[i].y2 = src_rects[i].y2;
720 break;
721 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
722 dest_rects[i].x1 = height - src_rects[i].y2;
723 dest_rects[i].y1 = width - src_rects[i].x2;
724 dest_rects[i].x2 = height - src_rects[i].y1;
725 dest_rects[i].y2 = width - src_rects[i].x1;
726 break;
727 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
728 dest_rects[i].x1 = src_rects[i].x1;
729 dest_rects[i].y1 = height - src_rects[i].y2;
730 dest_rects[i].x2 = src_rects[i].x2;
731 dest_rects[i].y2 = height - src_rects[i].y1;
732 break;
733 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
734 dest_rects[i].x1 = src_rects[i].y1;
735 dest_rects[i].y1 = src_rects[i].x1;
736 dest_rects[i].x2 = src_rects[i].y2;
737 dest_rects[i].y2 = src_rects[i].x2;
738 break;
739 }
740 }
741 }
742
743 if (scale != 1) {
744 for (i = 0; i < nrects; i++) {
745 dest_rects[i].x1 *= scale;
746 dest_rects[i].x2 *= scale;
747 dest_rects[i].y1 *= scale;
748 dest_rects[i].y2 *= scale;
749 }
750 }
751
752 pixman_region32_clear(dest);
753 pixman_region32_init_rects(dest, dest_rects, nrects);
754 free(dest_rects);
755}
756
Jonny Lamb74130762013-11-26 18:19:46 +0100757static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300758viewport_surface_to_buffer(struct weston_surface *surface,
759 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100760{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200761 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200762 double src_width, src_height;
763 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200764
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200765 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
766 if (vp->surface.width == -1) {
767 *bx = sx;
768 *by = sy;
769 return;
770 }
Jonny Lamb74130762013-11-26 18:19:46 +0100771
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200772 src_x = 0.0;
773 src_y = 0.0;
774 src_width = surface->width_from_buffer;
775 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100776 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200777 src_x = wl_fixed_to_double(vp->buffer.src_x);
778 src_y = wl_fixed_to_double(vp->buffer.src_y);
779 src_width = wl_fixed_to_double(vp->buffer.src_width);
780 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100781 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200782
783 *bx = sx * src_width / surface->width + src_x;
784 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100785}
786
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500787WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200788weston_surface_to_buffer_float(struct weston_surface *surface,
789 float sx, float sy, float *bx, float *by)
790{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200791 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
792
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300793 /* first transform coordinates if the viewport is set */
794 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100795
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500796 weston_transformed_coord(surface->width_from_buffer,
797 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200798 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100799 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200800}
801
Derek Foreman99739672015-12-03 16:38:11 -0600802/** Transform a rectangle from surface coordinates to buffer coordinates
803 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300804 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600805 * from.
806 * \param rect The rectangle to transform.
807 * \return The transformed rectangle.
808 *
809 * Viewport and buffer transformations can only do translation, scaling,
810 * and rotations in 90-degree steps. Therefore the only loss in the
811 * conversion is coordinate rounding.
812 *
813 * However, some coordinate rounding takes place as an intermediate
814 * step before the buffer scale factor is applied, so the rectangle
815 * boundary may not be exactly as expected.
816 *
817 * This is OK for damage tracking since a little extra coverage is
818 * not a problem.
819 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200820WL_EXPORT pixman_box32_t
821weston_surface_to_buffer_rect(struct weston_surface *surface,
822 pixman_box32_t rect)
823{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200824 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100825 float xf, yf;
826
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300827 /* first transform box coordinates if the viewport is set */
828 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100829 rect.x1 = floorf(xf);
830 rect.y1 = floorf(yf);
831
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300832 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600833 rect.x2 = ceilf(xf);
834 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100835
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500836 return weston_transformed_rect(surface->width_from_buffer,
837 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200838 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200839 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200840}
841
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200842/** Transform a region from surface coordinates to buffer coordinates
843 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300844 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200845 * from.
846 * \param surface_region[in] The region in surface coordinates.
847 * \param buffer_region[out] The region converted to buffer coordinates.
848 *
849 * Buffer_region must be init'd, but will be completely overwritten.
850 *
851 * Viewport and buffer transformations can only do translation, scaling,
852 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600853 * conversion is from the coordinate rounding that takes place in
854 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200855 */
856WL_EXPORT void
857weston_surface_to_buffer_region(struct weston_surface *surface,
858 pixman_region32_t *surface_region,
859 pixman_region32_t *buffer_region)
860{
861 pixman_box32_t *src_rects, *dest_rects;
862 int nrects, i;
863
864 src_rects = pixman_region32_rectangles(surface_region, &nrects);
865 dest_rects = malloc(nrects * sizeof(*dest_rects));
866 if (!dest_rects)
867 return;
868
869 for (i = 0; i < nrects; i++) {
870 dest_rects[i] = weston_surface_to_buffer_rect(surface,
871 src_rects[i]);
872 }
873
874 pixman_region32_fini(buffer_region);
875 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
876 free(dest_rects);
877}
878
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200879WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500880weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400881 struct weston_plane *plane)
882{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500883 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400884 return;
885
Jason Ekstranda7af7042013-10-12 22:38:11 -0500886 weston_view_damage_below(view);
887 view->plane = plane;
888 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400889}
890
Pekka Paalanen51723d52015-02-17 13:10:01 +0200891/** Inflict damage on the plane where the view is visible.
892 *
893 * \param view The view that causes the damage.
894 *
895 * If the view is currently on a plane (including the primary plane),
896 * take the view's boundingbox, subtract all the opaque views that cover it,
897 * and add the remaining region as damage to the plane. This corresponds
898 * to the damage inflicted to the plane if this view disappeared.
899 *
900 * A repaint is scheduled for this view.
901 *
902 * The region of all opaque views covering this view is stored in
903 * weston_view::clip and updated by view_accumulate_damage() during
904 * weston_output_repaint(). Specifically, that region matches the
905 * scenegraph as it was last painted.
906 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400907WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500908weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200909{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500910 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200911
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500912 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200913 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500914 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800915 if (view->plane)
916 pixman_region32_union(&view->plane->damage,
917 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500918 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800919 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200920}
921
Bryce Harrington3f650b82015-12-23 11:01:58 -0800922/**
923 * \param es The surface
924 * \param mask The new set of outputs for the surface
925 *
926 * Sets the surface's set of outputs to the ones specified by
927 * the new output mask provided. Identifies the outputs that
928 * have changed, the posts enter and leave events for these
929 * outputs as appropriate.
930 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400931static void
932weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
933{
934 uint32_t different = es->output_mask ^ mask;
935 uint32_t entered = mask & different;
936 uint32_t left = es->output_mask & different;
937 struct weston_output *output;
938 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500939 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400940
941 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500942 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400943 return;
944 if (different == 0)
945 return;
946
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500947 client = wl_resource_get_client(es->resource);
948
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400949 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -0800950 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400951 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500952 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400953 client);
954 if (resource == NULL)
955 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -0800956 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500957 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -0800958 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500959 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400960 }
961}
962
Bryce Harrington3f650b82015-12-23 11:01:58 -0800963/** Recalculate which output(s) the surface has views displayed on
964 *
965 * \param es The surface to remap to outputs
966 *
967 * Finds the output that is showing the largest amount of one
968 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300969 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800970 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300971 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800972 * in the output_mask for the surface.
973 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400974static void
975weston_surface_assign_output(struct weston_surface *es)
976{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 struct weston_output *new_output;
978 struct weston_view *view;
979 pixman_region32_t region;
980 uint32_t max, area, mask;
981 pixman_box32_t *e;
982
983 new_output = NULL;
984 max = 0;
985 mask = 0;
986 pixman_region32_init(&region);
987 wl_list_for_each(view, &es->views, surface_link) {
988 if (!view->output)
989 continue;
990
991 pixman_region32_intersect(&region, &view->transform.boundingbox,
992 &view->output->region);
993
994 e = pixman_region32_extents(&region);
995 area = (e->x2 - e->x1) * (e->y2 - e->y1);
996
997 mask |= view->output_mask;
998
999 if (area >= max) {
1000 new_output = view->output;
1001 max = area;
1002 }
1003 }
1004 pixman_region32_fini(&region);
1005
1006 es->output = new_output;
1007 weston_surface_update_output_mask(es, mask);
1008}
1009
Bryce Harrington3f650b82015-12-23 11:01:58 -08001010/** Recalculate which output(s) the view is displayed on
1011 *
1012 * \param ev The view to remap to outputs
1013 *
1014 * Identifies the set of outputs that the view is visible on,
1015 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001016 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001017 *
1018 * Also does the same for the view's surface. See
1019 * weston_surface_assign_output().
1020 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001021static void
1022weston_view_assign_output(struct weston_view *ev)
1023{
1024 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001025 struct weston_output *output, *new_output;
1026 pixman_region32_t region;
1027 uint32_t max, area, mask;
1028 pixman_box32_t *e;
1029
1030 new_output = NULL;
1031 max = 0;
1032 mask = 0;
1033 pixman_region32_init(&region);
1034 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001035 if (output->destroying)
1036 continue;
1037
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001039 &output->region);
1040
1041 e = pixman_region32_extents(&region);
1042 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1043
1044 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001045 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001046
1047 if (area >= max) {
1048 new_output = output;
1049 max = area;
1050 }
1051 }
1052 pixman_region32_fini(&region);
1053
Jason Ekstranda7af7042013-10-12 22:38:11 -05001054 ev->output = new_output;
1055 ev->output_mask = mask;
1056
1057 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001058}
1059
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001060static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001061weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1062 int from_x, int from_y, int *to_x, int *to_y)
1063{
1064 float x, y;
1065
1066 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1067 weston_view_from_global_float(to, x, y, &x, &y);
1068
1069 *to_x = round(x);
1070 *to_y = round(y);
1071}
1072
1073static void
1074weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1075{
1076 pixman_box32_t *a;
1077 pixman_box32_t b;
1078
1079 a = pixman_region32_extents(&from->geometry.scissor);
1080
1081 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1082 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1083
1084 pixman_region32_fini(&to->geometry.scissor);
1085 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1086}
1087
1088static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001089view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001090 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001091{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001092 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1093 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001094 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001095 { inbox->x1, inbox->y1 },
1096 { inbox->x1, inbox->y2 },
1097 { inbox->x2, inbox->y1 },
1098 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001099 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001100 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001101 int i;
1102
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001103 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001104 /* avoid rounding empty bbox to 1x1 */
1105 pixman_region32_init(bbox);
1106 return;
1107 }
1108
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001109 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001110 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001111 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001112 if (x < min_x)
1113 min_x = x;
1114 if (x > max_x)
1115 max_x = x;
1116 if (y < min_y)
1117 min_y = y;
1118 if (y > max_y)
1119 max_y = y;
1120 }
1121
Pekka Paalanen219b9822012-02-08 15:38:37 +02001122 int_x = floorf(min_x);
1123 int_y = floorf(min_y);
1124 pixman_region32_init_rect(bbox, int_x, int_y,
1125 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001126}
1127
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001128static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001129weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001130{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001131 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001132
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001133 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001134 view->geometry.x = roundf(view->geometry.x);
1135 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001136
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001137 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1139 view->transform.position.matrix.d[12] = view->geometry.x;
1140 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001141
Jason Ekstranda7af7042013-10-12 22:38:11 -05001142 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001143
Jason Ekstranda7af7042013-10-12 22:38:11 -05001144 view->transform.inverse = view->transform.position.matrix;
1145 view->transform.inverse.d[12] = -view->geometry.x;
1146 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001147
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001149 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001150 view->surface->width,
1151 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001152 if (view->geometry.scissor_enabled)
1153 pixman_region32_intersect(&view->transform.boundingbox,
1154 &view->transform.boundingbox,
1155 &view->geometry.scissor);
1156
1157 pixman_region32_translate(&view->transform.boundingbox,
1158 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001159
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160 if (view->alpha == 1.0) {
1161 pixman_region32_copy(&view->transform.opaque,
1162 &view->surface->opaque);
1163 pixman_region32_translate(&view->transform.opaque,
1164 view->geometry.x,
1165 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001166 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001167}
1168
1169static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001171{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 struct weston_view *parent = view->geometry.parent;
1173 struct weston_matrix *matrix = &view->transform.matrix;
1174 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001175 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001176 pixman_region32_t surfregion;
1177 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001178
Jason Ekstranda7af7042013-10-12 22:38:11 -05001179 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001180
1181 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1183 view->transform.position.matrix.d[12] = view->geometry.x;
1184 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001185
1186 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001188 weston_matrix_multiply(matrix, &tform->matrix);
1189
Pekka Paalanen483243f2013-03-08 14:56:50 +02001190 if (parent)
1191 weston_matrix_multiply(matrix, &parent->transform.matrix);
1192
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001193 if (weston_matrix_invert(inverse, matrix) < 0) {
1194 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001195 weston_log("error: weston_view %p"
1196 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001197 return -1;
1198 }
1199
Pekka Paalanen380adf52015-02-16 14:39:11 +02001200 pixman_region32_init_rect(&surfregion, 0, 0,
1201 view->surface->width, view->surface->height);
1202 if (view->geometry.scissor_enabled)
1203 pixman_region32_intersect(&surfregion, &surfregion,
1204 &view->geometry.scissor);
1205 surfbox = pixman_region32_extents(&surfregion);
1206
1207 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1208 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001209
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001210 return 0;
1211}
1212
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001213static struct weston_layer *
1214get_view_layer(struct weston_view *view)
1215{
1216 if (view->parent_view)
1217 return get_view_layer(view->parent_view);
1218 return view->layer_link.layer;
1219}
1220
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001221WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001223{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001225 struct weston_layer *layer;
1226 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001227
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001229 return;
1230
Pekka Paalanen483243f2013-03-08 14:56:50 +02001231 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001233
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001235
Jason Ekstranda7af7042013-10-12 22:38:11 -05001236 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001237
Jason Ekstranda7af7042013-10-12 22:38:11 -05001238 pixman_region32_fini(&view->transform.boundingbox);
1239 pixman_region32_fini(&view->transform.opaque);
1240 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001241
Pekka Paalanencd403622012-01-25 13:37:39 +02001242 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 if (view->geometry.transformation_list.next ==
1244 &view->transform.position.link &&
1245 view->geometry.transformation_list.prev ==
1246 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001247 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001248 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001249 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250 if (weston_view_update_transform_enable(view) < 0)
1251 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001252 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001253
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001254 layer = get_view_layer(view);
1255 if (layer) {
1256 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001257 pixman_region32_intersect(&view->transform.boundingbox,
1258 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001259 pixman_region32_intersect(&view->transform.opaque,
1260 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001261 pixman_region32_fini(&mask);
1262 }
1263
Pekka Paalanen380adf52015-02-16 14:39:11 +02001264 if (parent) {
1265 if (parent->geometry.scissor_enabled) {
1266 view->geometry.scissor_enabled = true;
1267 weston_view_transfer_scissor(parent, view);
1268 } else {
1269 view->geometry.scissor_enabled = false;
1270 }
1271 }
1272
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001274
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001276
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 wl_signal_emit(&view->surface->compositor->transform_signal,
1278 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001279}
1280
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001281WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001283{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001285
1286 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 * The invariant: if view->geometry.dirty, then all views
1288 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001289 * Corollary: if not parent->geometry.dirty, then all ancestors
1290 * are not dirty.
1291 */
1292
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001294 return;
1295
Jason Ekstranda7af7042013-10-12 22:38:11 -05001296 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001297
Jason Ekstranda7af7042013-10-12 22:38:11 -05001298 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001299 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001300 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001301}
1302
1303WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304weston_view_to_global_fixed(struct weston_view *view,
1305 wl_fixed_t vx, wl_fixed_t vy,
1306 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001307{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001308 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001309
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 weston_view_to_global_float(view,
1311 wl_fixed_to_double(vx),
1312 wl_fixed_to_double(vy),
1313 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001314 *x = wl_fixed_from_double(xf);
1315 *y = wl_fixed_from_double(yf);
1316}
1317
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001318WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319weston_view_from_global_float(struct weston_view *view,
1320 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001321{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001323 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1324
Jason Ekstranda7af7042013-10-12 22:38:11 -05001325 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001326
1327 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001328 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001329 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001330 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001331 *vx = 0;
1332 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001333 return;
1334 }
1335
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336 *vx = v.f[0] / v.f[3];
1337 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001338 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339 *vx = x - view->geometry.x;
1340 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001341 }
1342}
1343
1344WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345weston_view_from_global_fixed(struct weston_view *view,
1346 wl_fixed_t x, wl_fixed_t y,
1347 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001348{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001350
Jason Ekstranda7af7042013-10-12 22:38:11 -05001351 weston_view_from_global_float(view,
1352 wl_fixed_to_double(x),
1353 wl_fixed_to_double(y),
1354 &vxf, &vyf);
1355 *vx = wl_fixed_from_double(vxf);
1356 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001357}
1358
1359WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360weston_view_from_global(struct weston_view *view,
1361 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001362{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001364
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1366 *vx = floorf(vxf);
1367 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001368}
1369
Bryce Harrington3f650b82015-12-23 11:01:58 -08001370/**
1371 * \param surface The surface to be repainted
1372 *
1373 * Marks the output(s) that the surface is shown on as needing to be
1374 * repainted. See weston_output_schedule_repaint().
1375 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001376WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001377weston_surface_schedule_repaint(struct weston_surface *surface)
1378{
1379 struct weston_output *output;
1380
1381 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001382 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001383 weston_output_schedule_repaint(output);
1384}
1385
Bryce Harrington3f650b82015-12-23 11:01:58 -08001386/**
1387 * \param view The view to be repainted
1388 *
1389 * Marks the output(s) that the view is shown on as needing to be
1390 * repainted. See weston_output_schedule_repaint().
1391 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001392WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001393weston_view_schedule_repaint(struct weston_view *view)
1394{
1395 struct weston_output *output;
1396
1397 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001398 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 weston_output_schedule_repaint(output);
1400}
1401
Pekka Paalanene508ce62015-02-19 13:59:55 +02001402/**
1403 * XXX: This function does it the wrong way.
1404 * surface->damage is the damage from the client, and causes
1405 * surface_flush_damage() to copy pixels. No window management action can
1406 * cause damage to the client-provided content, warranting re-upload!
1407 *
1408 * Instead of surface->damage, this function should record the damage
1409 * with all the views for this surface to avoid extraneous texture
1410 * uploads.
1411 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001412WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001413weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001414{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001415 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 0, 0, surface->width,
1417 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001418
Kristian Høgsberg98238702012-08-03 16:29:12 -04001419 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001420}
1421
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001422WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001424{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001425 if (view->geometry.x == x && view->geometry.y == y)
1426 return;
1427
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 view->geometry.x = x;
1429 view->geometry.y = y;
1430 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001431}
1432
Pekka Paalanen483243f2013-03-08 14:56:50 +02001433static void
1434transform_parent_handle_parent_destroy(struct wl_listener *listener,
1435 void *data)
1436{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437 struct weston_view *view =
1438 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001439 geometry.parent_destroy_listener);
1440
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001442}
1443
1444WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001446 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001447{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 if (view->geometry.parent) {
1449 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1450 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001451
1452 if (!parent)
1453 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001454 }
1455
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001457
Jason Ekstranda7af7042013-10-12 22:38:11 -05001458 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001459 transform_parent_handle_parent_destroy;
1460 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001461 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001463 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001465 }
1466
Jason Ekstranda7af7042013-10-12 22:38:11 -05001467 weston_view_geometry_dirty(view);
1468}
1469
Pekka Paalanen380adf52015-02-16 14:39:11 +02001470/** Set a clip mask rectangle on a view
1471 *
1472 * \param view The view to set the clip mask on.
1473 * \param x Top-left corner X coordinate of the clip rectangle.
1474 * \param y Top-left corner Y coordinate of the clip rectangle.
1475 * \param width Width of the clip rectangle, non-negative.
1476 * \param height Height of the clip rectangle, non-negative.
1477 *
1478 * A shell may set a clip mask rectangle on a view. Everything outside
1479 * the rectangle is cut away for input and output purposes: it is
1480 * not drawn and cannot be hit by hit-test based input like pointer
1481 * motion or touch-downs. Everything inside the rectangle will behave
1482 * normally. Clients are unaware of clipping.
1483 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001484 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001485 * mask rectangle does not affect the view position, the view is positioned
1486 * as it would be without a clip. The clip also does not change
1487 * weston_surface::width,height.
1488 *
1489 * The clip mask rectangle is part of transformation inheritance
1490 * (weston_view_set_transform_parent()). A clip set in the root of the
1491 * transformation inheritance tree will affect all views in the tree.
1492 * A clip can be set only on the root view. Attempting to set a clip
1493 * on view that has a transformation parent will fail. Assigning a parent
1494 * to a view that has a clip set will cause the clip to be forgotten.
1495 *
1496 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1497 * on the additional transformations in the child views. These transformations
1498 * may not rotate the coordinate axes, i.e., only translation and scaling
1499 * are allowed. Violating this restriction causes the clipping to malfunction.
1500 * Furthermore, using scaling may cause rounding errors in child clipping.
1501 *
1502 * The clip mask rectangle is not automatically adjusted based on
1503 * wl_surface.attach dx and dy arguments.
1504 *
1505 * A clip mask rectangle can be set only if the compositor capability
1506 * WESTON_CAP_VIEW_CLIP_MASK is present.
1507 *
1508 * This function sets the clip mask rectangle and schedules a repaint for
1509 * the view.
1510 */
1511WL_EXPORT void
1512weston_view_set_mask(struct weston_view *view,
1513 int x, int y, int width, int height)
1514{
1515 struct weston_compositor *compositor = view->surface->compositor;
1516
1517 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1518 weston_log("%s not allowed without capability!\n", __func__);
1519 return;
1520 }
1521
1522 if (view->geometry.parent) {
1523 weston_log("view %p has a parent, clip forbidden!\n", view);
1524 return;
1525 }
1526
1527 if (width < 0 || height < 0) {
1528 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1529 x, y, width, height);
1530 return;
1531 }
1532
1533 pixman_region32_fini(&view->geometry.scissor);
1534 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1535 view->geometry.scissor_enabled = true;
1536 weston_view_geometry_dirty(view);
1537 weston_view_schedule_repaint(view);
1538}
1539
1540/** Remove the clip mask from a view
1541 *
1542 * \param view The view to remove the clip mask from.
1543 *
1544 * Removed the clip mask rectangle and schedules a repaint.
1545 *
1546 * \sa weston_view_set_mask
1547 */
1548WL_EXPORT void
1549weston_view_set_mask_infinite(struct weston_view *view)
1550{
1551 view->geometry.scissor_enabled = false;
1552 weston_view_geometry_dirty(view);
1553 weston_view_schedule_repaint(view);
1554}
1555
Armin Krezović0da12b82016-06-30 06:04:33 +02001556/* Check if view should be displayed
1557 *
1558 * The indicator is set manually when assigning
1559 * a view to a surface.
1560 *
1561 * This needs reworking. See the thread starting at:
1562 *
1563 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1564 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001565WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566weston_view_is_mapped(struct weston_view *view)
1567{
Armin Krezović0da12b82016-06-30 06:04:33 +02001568 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001569}
1570
Armin Krezović0da12b82016-06-30 06:04:33 +02001571/* Check if a surface has a view assigned to it
1572 *
1573 * The indicator is set manually when mapping
1574 * a surface and creating a view for it.
1575 *
1576 * This needs to go. See the thread starting at:
1577 *
1578 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1579 *
1580 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001581WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001582weston_surface_is_mapped(struct weston_surface *surface)
1583{
Armin Krezović0da12b82016-06-30 06:04:33 +02001584 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001585}
1586
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001587static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001588surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001589{
1590 struct weston_view *view;
1591
1592 if (surface->width == width && surface->height == height)
1593 return;
1594
1595 surface->width = width;
1596 surface->height = height;
1597
1598 wl_list_for_each(view, &surface->views, surface_link)
1599 weston_view_geometry_dirty(view);
1600}
1601
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001602WL_EXPORT void
1603weston_surface_set_size(struct weston_surface *surface,
1604 int32_t width, int32_t height)
1605{
1606 assert(!surface->resource);
1607 surface_set_size(surface, width, height);
1608}
1609
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001610static int
1611fixed_round_up_to_int(wl_fixed_t f)
1612{
1613 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1614}
1615
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001616static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001617convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1618 int32_t width, int32_t height,
1619 uint32_t transform,
1620 int32_t scale)
1621{
1622 assert(scale > 0);
1623
1624 switch (transform) {
1625 case WL_OUTPUT_TRANSFORM_NORMAL:
1626 case WL_OUTPUT_TRANSFORM_180:
1627 case WL_OUTPUT_TRANSFORM_FLIPPED:
1628 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1629 *width_out = width / scale;
1630 *height_out = height / scale;
1631 break;
1632 case WL_OUTPUT_TRANSFORM_90:
1633 case WL_OUTPUT_TRANSFORM_270:
1634 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1635 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1636 *width_out = height / scale;
1637 *height_out = width / scale;
1638 break;
1639 default:
1640 assert(0 && "invalid transform");
1641 }
1642}
1643
1644static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001645weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001646{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001647 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001648
1649 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001650 surface->width_from_buffer = 0;
1651 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001652 return;
1653 }
1654
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001655 convert_size_by_transform_scale(&surface->width_from_buffer,
1656 &surface->height_from_buffer,
1657 surface->buffer_ref.buffer->width,
1658 surface->buffer_ref.buffer->height,
1659 vp->buffer.transform,
1660 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001661}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001662
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001663static void
1664weston_surface_update_size(struct weston_surface *surface)
1665{
1666 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1667 int32_t width, height;
1668
1669 width = surface->width_from_buffer;
1670 height = surface->height_from_buffer;
1671
1672 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001673 surface_set_size(surface,
1674 vp->surface.width, vp->surface.height);
1675 return;
1676 }
1677
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001678 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001679 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1680 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1681
1682 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001683 return;
1684 }
1685
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001686 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001687}
1688
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001689WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001690weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001691{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001692 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001693
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001694 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001695
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001696 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001697}
1698
Jason Ekstranda7af7042013-10-12 22:38:11 -05001699WL_EXPORT struct weston_view *
1700weston_compositor_pick_view(struct weston_compositor *compositor,
1701 wl_fixed_t x, wl_fixed_t y,
1702 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001703{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001704 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001705 wl_fixed_t view_x, view_y;
1706 int view_ix, view_iy;
1707 int ix = wl_fixed_to_int(x);
1708 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001709
Jason Ekstranda7af7042013-10-12 22:38:11 -05001710 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001711 if (!pixman_region32_contains_point(
1712 &view->transform.boundingbox, ix, iy, NULL))
1713 continue;
1714
1715 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1716 view_ix = wl_fixed_to_int(view_x);
1717 view_iy = wl_fixed_to_int(view_y);
1718
1719 if (!pixman_region32_contains_point(&view->surface->input,
1720 view_ix, view_iy, NULL))
1721 continue;
1722
Pekka Paalanen380adf52015-02-16 14:39:11 +02001723 if (view->geometry.scissor_enabled &&
1724 !pixman_region32_contains_point(&view->geometry.scissor,
1725 view_ix, view_iy, NULL))
1726 continue;
1727
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001728 *vx = view_x;
1729 *vy = view_y;
1730 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001731 }
1732
Derek Foremanf9318d12015-05-11 15:40:11 -05001733 *vx = wl_fixed_from_int(-1000000);
1734 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001735 return NULL;
1736}
1737
1738static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001739weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001740{
Daniel Stone37816df2012-05-16 18:45:18 +01001741 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001742
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001743 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001744 return;
1745
Daniel Stone37816df2012-05-16 18:45:18 +01001746 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001747 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001748}
1749
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001750WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001751weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001752{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001753 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001754
Jason Ekstranda7af7042013-10-12 22:38:11 -05001755 if (!weston_view_is_mapped(view))
1756 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001757
Jason Ekstranda7af7042013-10-12 22:38:11 -05001758 weston_view_damage_below(view);
1759 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001760 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001761 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001762 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763 wl_list_remove(&view->link);
1764 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001765 view->output_mask = 0;
1766 weston_surface_assign_output(view->surface);
1767
1768 if (weston_surface_is_mapped(view->surface))
1769 return;
1770
1771 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001772 struct weston_touch *touch = weston_seat_get_touch(seat);
1773 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1774 struct weston_keyboard *keyboard =
1775 weston_seat_get_keyboard(seat);
1776
1777 if (keyboard && keyboard->focus == view->surface)
1778 weston_keyboard_set_focus(keyboard, NULL);
1779 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001780 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001781 if (touch && touch->focus == view)
1782 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001783 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001784}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001785
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786WL_EXPORT void
1787weston_surface_unmap(struct weston_surface *surface)
1788{
1789 struct weston_view *view;
1790
Armin Krezovićf8486c32016-06-30 06:04:28 +02001791 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001792 wl_list_for_each(view, &surface->views, surface_link)
1793 weston_view_unmap(view);
1794 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001795}
1796
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001797static void
1798weston_surface_reset_pending_buffer(struct weston_surface *surface)
1799{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001800 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001801 surface->pending.sx = 0;
1802 surface->pending.sy = 0;
1803 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001804 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001805}
1806
Jason Ekstranda7af7042013-10-12 22:38:11 -05001807WL_EXPORT void
1808weston_view_destroy(struct weston_view *view)
1809{
1810 wl_signal_emit(&view->destroy_signal, view);
1811
1812 assert(wl_list_empty(&view->geometry.child_list));
1813
1814 if (weston_view_is_mapped(view)) {
1815 weston_view_unmap(view);
1816 weston_compositor_build_view_list(view->surface->compositor);
1817 }
1818
1819 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001820 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821
1822 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001823 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001824 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001825 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826
1827 weston_view_set_transform_parent(view, NULL);
1828
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 wl_list_remove(&view->surface_link);
1830
1831 free(view);
1832}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001833
1834WL_EXPORT void
1835weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001836{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001837 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001838 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001839 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001840
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001841 if (--surface->ref_count > 0)
1842 return;
1843
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001844 assert(surface->resource == NULL);
1845
Pekka Paalanenca790762015-04-17 14:23:38 +03001846 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001847
Pekka Paalanene67858b2013-04-25 13:57:42 +03001848 assert(wl_list_empty(&surface->subsurface_list_pending));
1849 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001850
Jason Ekstranda7af7042013-10-12 22:38:11 -05001851 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1852 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001853
Jason Ekstrand7b982072014-05-20 14:33:03 -05001854 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001855
Pekka Paalanende685b82012-12-04 15:58:12 +02001856 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001857
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001858 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001859 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001860 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001861
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001862 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001863 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001864
Pekka Paalanen133e4392014-09-23 22:08:46 -04001865 weston_presentation_feedback_discard_list(&surface->feedback_list);
1866
Jonas Ådahld3414f22016-07-22 17:56:31 +08001867 wl_list_for_each_safe(constraint, next_constraint,
1868 &surface->pointer_constraints,
1869 link)
1870 weston_pointer_constraint_destroy(constraint);
1871
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001872 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001873}
1874
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001875static void
1876destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001877{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001878 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001879
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001880 assert(surface);
1881
Giulio Camuffo0d379742013-11-15 22:06:15 +01001882 /* Set the resource to NULL, since we don't want to leave a
1883 * dangling pointer if the surface was refcounted and survives
1884 * the weston_surface_destroy() call. */
1885 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001886
1887 if (surface->viewport_resource)
1888 wl_resource_set_user_data(surface->viewport_resource, NULL);
1889
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001890 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001891}
1892
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001893static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001894weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1895{
1896 struct weston_buffer *buffer =
1897 container_of(listener, struct weston_buffer, destroy_listener);
1898
1899 wl_signal_emit(&buffer->destroy_signal, buffer);
1900 free(buffer);
1901}
1902
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001903WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001904weston_buffer_from_resource(struct wl_resource *resource)
1905{
1906 struct weston_buffer *buffer;
1907 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001908
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001909 listener = wl_resource_get_destroy_listener(resource,
1910 weston_buffer_destroy_handler);
1911
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001912 if (listener)
1913 return container_of(listener, struct weston_buffer,
1914 destroy_listener);
1915
1916 buffer = zalloc(sizeof *buffer);
1917 if (buffer == NULL)
1918 return NULL;
1919
1920 buffer->resource = resource;
1921 wl_signal_init(&buffer->destroy_signal);
1922 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001923 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001924 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001925
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001926 return buffer;
1927}
1928
1929static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001930weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1931 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001932{
Pekka Paalanende685b82012-12-04 15:58:12 +02001933 struct weston_buffer_reference *ref =
1934 container_of(listener, struct weston_buffer_reference,
1935 destroy_listener);
1936
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001937 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001938 ref->buffer = NULL;
1939}
1940
1941WL_EXPORT void
1942weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001943 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001944{
1945 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001946 ref->buffer->busy_count--;
1947 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001948 assert(wl_resource_get_client(ref->buffer->resource));
1949 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001950 WL_BUFFER_RELEASE);
1951 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001952 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001953 }
1954
Pekka Paalanende685b82012-12-04 15:58:12 +02001955 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001956 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001957 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001958 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001959 }
1960
Pekka Paalanende685b82012-12-04 15:58:12 +02001961 ref->buffer = buffer;
1962 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1963}
1964
1965static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001966weston_surface_attach(struct weston_surface *surface,
1967 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001968{
1969 weston_buffer_reference(&surface->buffer_ref, buffer);
1970
Pekka Paalanena6421c42012-12-04 15:58:10 +02001971 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001972 if (weston_surface_is_mapped(surface))
1973 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001974 }
1975
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001976 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001977
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001978 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001979 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001980}
1981
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001982WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001983weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001984{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001985 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001986
1987 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001988 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001989}
1990
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001991WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001992weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001993{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001994 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001995
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001996 pixman_region32_union(&compositor->primary_plane.damage,
1997 &compositor->primary_plane.damage,
1998 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001999 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002000}
2001
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002002static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002003surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002004{
Pekka Paalanende685b82012-12-04 15:58:12 +02002005 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002006 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002007 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002008
Pekka Paalanenb5026542014-11-12 15:09:24 +02002009 if (weston_timeline_enabled_ &&
2010 pixman_region32_not_empty(&surface->damage))
2011 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2012 TLP_OUTPUT(surface->output), TLP_END);
2013
Jason Ekstrandef540082014-06-26 10:37:36 -07002014 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002015}
2016
2017static void
2018view_accumulate_damage(struct weston_view *view,
2019 pixman_region32_t *opaque)
2020{
2021 pixman_region32_t damage;
2022
2023 pixman_region32_init(&damage);
2024 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002025 pixman_box32_t *extents;
2026
Jason Ekstranda7af7042013-10-12 22:38:11 -05002027 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002028 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002029 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002030 pixman_region32_copy(&damage, &view->surface->damage);
2031 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002032 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002033 }
2034
Pekka Paalanen380adf52015-02-16 14:39:11 +02002035 pixman_region32_intersect(&damage, &damage,
2036 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002037 pixman_region32_subtract(&damage, &damage, opaque);
2038 pixman_region32_union(&view->plane->damage,
2039 &view->plane->damage, &damage);
2040 pixman_region32_fini(&damage);
2041 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002042 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002043}
2044
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002045static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002046compositor_accumulate_damage(struct weston_compositor *ec)
2047{
2048 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002050 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002051
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002052 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002053
2054 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002055 pixman_region32_copy(&plane->clip, &clip);
2056
2057 pixman_region32_init(&opaque);
2058
Jason Ekstranda7af7042013-10-12 22:38:11 -05002059 wl_list_for_each(ev, &ec->view_list, link) {
2060 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002061 continue;
2062
Jason Ekstranda7af7042013-10-12 22:38:11 -05002063 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002064 }
2065
2066 pixman_region32_union(&clip, &clip, &opaque);
2067 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002068 }
2069
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002070 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002071
Jason Ekstranda7af7042013-10-12 22:38:11 -05002072 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002073 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002074
2075 wl_list_for_each(ev, &ec->view_list, link) {
2076 if (ev->surface->touched)
2077 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002078 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079
2080 surface_flush_damage(ev->surface);
2081
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002082 /* Both the renderer and the backend have seen the buffer
2083 * by now. If renderer needs the buffer, it has its own
2084 * reference set. If the backend wants to keep the buffer
2085 * around for migrating the surface into a non-primary plane
2086 * later, keep_buffer is true. Otherwise, drop the core
2087 * reference now, and allow early buffer release. This enables
2088 * clients to use single-buffering.
2089 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002090 if (!ev->surface->keep_buffer)
2091 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002092 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002093}
2094
2095static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002096surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002097{
2098 struct weston_subsurface *sub;
2099
Pekka Paalanene67858b2013-04-25 13:57:42 +03002100 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002101 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002102 continue;
2103
Jason Ekstranda7af7042013-10-12 22:38:11 -05002104 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2105 wl_list_init(&sub->surface->views);
2106
2107 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002108 }
2109}
2110
2111static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002113{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002114 struct weston_subsurface *sub;
2115 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002116
Jason Ekstranda7af7042013-10-12 22:38:11 -05002117 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2118 if (sub->surface == surface)
2119 continue;
2120
George Kiagiadakised04d382014-06-13 18:10:26 +02002121 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2122 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002123 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002124 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002125
2126 surface_free_unused_subsurface_views(sub->surface);
2127 }
2128}
2129
2130static void
2131view_list_add_subsurface_view(struct weston_compositor *compositor,
2132 struct weston_subsurface *sub,
2133 struct weston_view *parent)
2134{
2135 struct weston_subsurface *child;
2136 struct weston_view *view = NULL, *iv;
2137
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002138 if (!weston_surface_is_mapped(sub->surface))
2139 return;
2140
Jason Ekstranda7af7042013-10-12 22:38:11 -05002141 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2142 if (iv->geometry.parent == parent) {
2143 view = iv;
2144 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002145 }
2146 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002147
2148 if (view) {
2149 /* Put it back in the surface's list of views */
2150 wl_list_remove(&view->surface_link);
2151 wl_list_insert(&sub->surface->views, &view->surface_link);
2152 } else {
2153 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002154 weston_view_set_position(view,
2155 sub->position.x,
2156 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002157 weston_view_set_transform_parent(view, parent);
2158 }
2159
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002160 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002162 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002163
Pekka Paalanenb188e912013-11-19 14:03:35 +02002164 if (wl_list_empty(&sub->surface->subsurface_list)) {
2165 wl_list_insert(compositor->view_list.prev, &view->link);
2166 return;
2167 }
2168
2169 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2170 if (child->surface == sub->surface)
2171 wl_list_insert(compositor->view_list.prev, &view->link);
2172 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002173 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002174 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002175}
2176
2177static void
2178view_list_add(struct weston_compositor *compositor,
2179 struct weston_view *view)
2180{
2181 struct weston_subsurface *sub;
2182
2183 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002184
Pekka Paalanenb188e912013-11-19 14:03:35 +02002185 if (wl_list_empty(&view->surface->subsurface_list)) {
2186 wl_list_insert(compositor->view_list.prev, &view->link);
2187 return;
2188 }
2189
2190 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2191 if (sub->surface == view->surface)
2192 wl_list_insert(compositor->view_list.prev, &view->link);
2193 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002194 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002195 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002196}
2197
2198static void
2199weston_compositor_build_view_list(struct weston_compositor *compositor)
2200{
2201 struct weston_view *view;
2202 struct weston_layer *layer;
2203
2204 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002205 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002206 surface_stash_subsurface_views(view->surface);
2207
2208 wl_list_init(&compositor->view_list);
2209 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002210 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002211 view_list_add(compositor, view);
2212 }
2213 }
2214
2215 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002216 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002217 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002218}
2219
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002220static void
2221weston_output_take_feedback_list(struct weston_output *output,
2222 struct weston_surface *surface)
2223{
2224 struct weston_view *view;
2225 struct weston_presentation_feedback *feedback;
2226 uint32_t flags = 0xffffffff;
2227
2228 if (wl_list_empty(&surface->feedback_list))
2229 return;
2230
2231 /* All views must have the flag for the flag to survive. */
2232 wl_list_for_each(view, &surface->views, surface_link) {
2233 /* ignore views that are not on this output at all */
2234 if (view->output_mask & (1u << output->id))
2235 flags &= view->psf_flags;
2236 }
2237
2238 wl_list_for_each(feedback, &surface->feedback_list, link)
2239 feedback->psf_flags = flags;
2240
2241 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2242 wl_list_init(&surface->feedback_list);
2243}
2244
David Herrmann1edf44c2013-10-22 17:11:26 +02002245static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002246weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002247{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002248 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002249 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002250 struct weston_animation *animation, *next;
2251 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002252 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002253 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002254 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002255
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002256 if (output->destroying)
2257 return 0;
2258
Pekka Paalanenb5026542014-11-12 15:09:24 +02002259 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2260
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002261 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002262 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002263
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002264 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002265 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002266 } else {
2267 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002268 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002269 ev->psf_flags = 0;
2270 }
2271 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002272
Pekka Paalanene67858b2013-04-25 13:57:42 +03002273 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002274 wl_list_for_each(ev, &ec->view_list, link) {
2275 /* Note: This operation is safe to do multiple times on the
2276 * same surface.
2277 */
2278 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002279 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002280 &ev->surface->frame_callback_list);
2281 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002282
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002283 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002284 }
2285 }
2286
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002287 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002288
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002289 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002290 pixman_region32_intersect(&output_damage,
2291 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002292 pixman_region32_subtract(&output_damage,
2293 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002294
Scott Moreauccbf29d2012-02-22 14:21:41 -07002295 if (output->dirty)
2296 weston_output_update_matrix(output);
2297
David Herrmann1edf44c2013-10-22 17:11:26 +02002298 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002299
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002300 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002301
Kristian Høgsbergef044142011-06-21 15:02:12 -04002302 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002303
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002304 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002305
Jonas Ådahldb773762012-06-13 00:01:21 +02002306 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002307 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002308 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002309 }
2310
Scott Moreaud64cf212012-06-08 19:40:54 -06002311 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002312 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002313 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002314 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002315
Pekka Paalanenb5026542014-11-12 15:09:24 +02002316 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2317
David Herrmann1edf44c2013-10-22 17:11:26 +02002318 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002319}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002320
Pekka Paalanen82919792014-05-21 13:51:49 +03002321static void
2322weston_output_schedule_repaint_reset(struct weston_output *output)
2323{
Pekka Paalanen82919792014-05-21 13:51:49 +03002324 output->repaint_scheduled = 0;
2325 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002326}
2327
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002328/** Retrieves a mask of outputs that should inhibit screensaving
2329 *
2330 * \param compositor The compositor instance.
2331 * \return An output mask indicating the ids of all inhibiting outputs
2332 *
2333 * Checks for surfaces whose clients have requested that they
2334 * disable the screenserver and display powersaving. Note
2335 * the output ids for these surfaces.
2336 */
2337WL_EXPORT uint32_t
2338weston_compositor_inhibited_outputs(struct weston_compositor *compositor)
2339{
2340 struct weston_view *view;
2341 uint32_t inhibited_outputs_mask = 0;
2342
2343 wl_list_for_each(view, &compositor->view_list, link) {
2344 /* Does the view's surface inhibit this output? */
2345 if (!view->surface->inhibit_idling)
2346 continue;
2347
2348 inhibited_outputs_mask |= view->output_mask;
2349 }
2350 return inhibited_outputs_mask;
2351}
2352
Pekka Paalanen0513a952014-05-21 16:17:27 +03002353static int
2354output_repaint_timer_handler(void *data)
2355{
2356 struct weston_output *output = data;
2357 struct weston_compositor *compositor = output->compositor;
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002358 uint32_t inhibited_outputs_mask = weston_compositor_inhibited_outputs(compositor);
Pekka Paalanen0513a952014-05-21 16:17:27 +03002359
2360 if (output->repaint_needed &&
Pekka Paalanen0513a952014-05-21 16:17:27 +03002361 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002362 (compositor->state != WESTON_COMPOSITOR_SLEEPING
2363 || inhibited_outputs_mask & (1 << output->id)) &&
Pekka Paalanen0513a952014-05-21 16:17:27 +03002364 weston_output_repaint(output) == 0)
2365 return 0;
2366
2367 weston_output_schedule_repaint_reset(output);
2368
2369 return 0;
2370}
2371
Kristian Høgsbergef044142011-06-21 15:02:12 -04002372WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002373weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002374 const struct timespec *stamp,
2375 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002376{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002377 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002378 int32_t refresh_nsec;
2379 struct timespec now;
2380 struct timespec gone;
2381 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002382
Pekka Paalanenb5026542014-11-12 15:09:24 +02002383 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2384 TLP_VBLANK(stamp), TLP_END);
2385
Pekka Paalanend7894d02015-07-03 15:08:53 +03002386 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002387 weston_presentation_feedback_present_list(&output->feedback_list,
2388 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002389 output->msc,
2390 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002391
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002392 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002393
Pekka Paalanen0513a952014-05-21 16:17:27 +03002394 weston_compositor_read_presentation_clock(compositor, &now);
2395 timespec_sub(&gone, &now, stamp);
2396 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2397 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002398
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002399 if (msec < -1000 || msec > 1000) {
2400 static bool warned;
2401
2402 if (!warned)
2403 weston_log("Warning: computed repaint delay is "
2404 "insane: %d msec\n", msec);
2405 warned = true;
2406
2407 msec = 0;
2408 }
2409
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002410 /* Called from restart_repaint_loop and restart happens already after
2411 * the deadline given by repaint_msec? In that case we delay until
2412 * the deadline of the next frame, to give clients a more predictable
2413 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002414 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002415 msec += refresh_nsec / 1000000;
2416
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002417 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002418 output_repaint_timer_handler(output);
2419 else
2420 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002421}
2422
2423static void
2424idle_repaint(void *data)
2425{
2426 struct weston_output *output = data;
2427
Jonas Ådahle5a12252013-04-05 23:07:11 +02002428 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002429}
2430
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002431WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002432weston_layer_entry_insert(struct weston_layer_entry *list,
2433 struct weston_layer_entry *entry)
2434{
2435 wl_list_insert(&list->link, &entry->link);
2436 entry->layer = list->layer;
2437}
2438
2439WL_EXPORT void
2440weston_layer_entry_remove(struct weston_layer_entry *entry)
2441{
2442 wl_list_remove(&entry->link);
2443 wl_list_init(&entry->link);
2444 entry->layer = NULL;
2445}
2446
2447WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002448weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2449{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002450 wl_list_init(&layer->view_list.link);
2451 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002452 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002453 if (below != NULL)
2454 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002455}
2456
2457WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002458weston_layer_set_mask(struct weston_layer *layer,
2459 int x, int y, int width, int height)
2460{
2461 struct weston_view *view;
2462
2463 layer->mask.x1 = x;
2464 layer->mask.x2 = x + width;
2465 layer->mask.y1 = y;
2466 layer->mask.y2 = y + height;
2467
2468 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2469 weston_view_geometry_dirty(view);
2470 }
2471}
2472
2473WL_EXPORT void
2474weston_layer_set_mask_infinite(struct weston_layer *layer)
2475{
2476 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2477 UINT32_MAX, UINT32_MAX);
2478}
2479
2480WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002481weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002482{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002483 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002484 struct wl_event_loop *loop;
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002485 uint32_t inhibited_outputs_mask = weston_compositor_inhibited_outputs(compositor);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002486
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002487 /* If we're offscreen, or if we're sleeping and the monitor
2488 * isn't currently being inhibited by an active surface, then
2489 * skip repainting.
2490 */
2491 if (compositor->state == WESTON_COMPOSITOR_OFFSCREEN ||
2492 (compositor->state == WESTON_COMPOSITOR_SLEEPING &&
2493 !(inhibited_outputs_mask & (1 << output->id))))
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002494 return;
2495
Pekka Paalanenb5026542014-11-12 15:09:24 +02002496 if (!output->repaint_needed)
2497 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2498
Kristian Høgsbergef044142011-06-21 15:02:12 -04002499 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002500 output->repaint_needed = 1;
2501 if (output->repaint_scheduled)
2502 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002503
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002504 wl_event_loop_add_idle(loop, idle_repaint, output);
2505 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002506 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002507}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002508
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002509WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002510weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2511{
2512 struct weston_output *output;
2513
2514 wl_list_for_each(output, &compositor->output_list, link)
2515 weston_output_schedule_repaint(output);
2516}
2517
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002518static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002519surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002520{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002521 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002522}
2523
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002524static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002525surface_attach(struct wl_client *client,
2526 struct wl_resource *resource,
2527 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2528{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002529 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002530 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002531
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002532 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002533 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002534 if (buffer == NULL) {
2535 wl_client_post_no_memory(client);
2536 return;
2537 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002538 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002539
Pekka Paalanende685b82012-12-04 15:58:12 +02002540 /* Attach, attach, without commit in between does not send
2541 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002542 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002543
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002544 surface->pending.sx = sx;
2545 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002546 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002547}
2548
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002549static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002550surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002551 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002552 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002553{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002554 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002555
Derek Foreman57e92ed2015-11-17 14:11:35 -06002556 if (width <= 0 || height <= 0)
2557 return;
2558
Derek Foreman152254b2015-11-26 14:17:48 -06002559 pixman_region32_union_rect(&surface->pending.damage_surface,
2560 &surface->pending.damage_surface,
2561 x, y, width, height);
2562}
2563
2564static void
2565surface_damage_buffer(struct wl_client *client,
2566 struct wl_resource *resource,
2567 int32_t x, int32_t y, int32_t width, int32_t height)
2568{
2569 struct weston_surface *surface = wl_resource_get_user_data(resource);
2570
2571 if (width <= 0 || height <= 0)
2572 return;
2573
2574 pixman_region32_union_rect(&surface->pending.damage_buffer,
2575 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002576 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002577}
2578
Kristian Høgsberg33418202011-08-16 23:01:28 -04002579static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002580destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002581{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002582 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002583
2584 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002585 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002586}
2587
2588static void
2589surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002590 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002591{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002592 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002593 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002594
2595 cb = malloc(sizeof *cb);
2596 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002597 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002598 return;
2599 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002600
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002601 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2602 callback);
2603 if (cb->resource == NULL) {
2604 free(cb);
2605 wl_resource_post_no_memory(resource);
2606 return;
2607 }
2608
Jason Ekstranda85118c2013-06-27 20:17:02 -05002609 wl_resource_set_implementation(cb->resource, NULL, cb,
2610 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002611
Pekka Paalanenbc106382012-10-10 12:49:31 +03002612 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002613}
2614
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002615static void
2616surface_set_opaque_region(struct wl_client *client,
2617 struct wl_resource *resource,
2618 struct wl_resource *region_resource)
2619{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002620 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002621 struct weston_region *region;
2622
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002623 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002624 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002625 pixman_region32_copy(&surface->pending.opaque,
2626 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002627 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002628 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002629 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002630}
2631
2632static void
2633surface_set_input_region(struct wl_client *client,
2634 struct wl_resource *resource,
2635 struct wl_resource *region_resource)
2636{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002637 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002638 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002639
2640 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002641 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002642 pixman_region32_copy(&surface->pending.input,
2643 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002644 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002645 pixman_region32_fini(&surface->pending.input);
2646 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002647 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002648}
2649
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002650static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002651weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002652{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002653 struct weston_subsurface *sub;
2654
2655 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2656 parent_link_pending) {
2657 wl_list_remove(&sub->parent_link);
2658 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2659 }
2660}
2661
2662static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002663weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002664 struct weston_matrix *matrix)
2665{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002666 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002667 double src_width, src_height, dest_width, dest_height;
2668
2669 weston_matrix_init(matrix);
2670
2671 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2672 src_width = surface->width_from_buffer;
2673 src_height = surface->height_from_buffer;
2674 } else {
2675 src_width = wl_fixed_to_double(vp->buffer.src_width);
2676 src_height = wl_fixed_to_double(vp->buffer.src_height);
2677 }
2678
2679 if (vp->surface.width == -1) {
2680 dest_width = src_width;
2681 dest_height = src_height;
2682 } else {
2683 dest_width = vp->surface.width;
2684 dest_height = vp->surface.height;
2685 }
2686
2687 if (src_width != dest_width || src_height != dest_height)
2688 weston_matrix_scale(matrix,
2689 src_width / dest_width,
2690 src_height / dest_height, 1);
2691
2692 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2693 weston_matrix_translate(matrix,
2694 wl_fixed_to_double(vp->buffer.src_x),
2695 wl_fixed_to_double(vp->buffer.src_y),
2696 0);
2697
2698 switch (vp->buffer.transform) {
2699 case WL_OUTPUT_TRANSFORM_FLIPPED:
2700 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2701 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2702 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2703 weston_matrix_scale(matrix, -1, 1, 1);
2704 weston_matrix_translate(matrix,
2705 surface->width_from_buffer, 0, 0);
2706 break;
2707 }
2708
2709 switch (vp->buffer.transform) {
2710 default:
2711 case WL_OUTPUT_TRANSFORM_NORMAL:
2712 case WL_OUTPUT_TRANSFORM_FLIPPED:
2713 break;
2714 case WL_OUTPUT_TRANSFORM_90:
2715 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2716 weston_matrix_rotate_xy(matrix, 0, 1);
2717 weston_matrix_translate(matrix,
2718 surface->height_from_buffer, 0, 0);
2719 break;
2720 case WL_OUTPUT_TRANSFORM_180:
2721 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2722 weston_matrix_rotate_xy(matrix, -1, 0);
2723 weston_matrix_translate(matrix,
2724 surface->width_from_buffer,
2725 surface->height_from_buffer, 0);
2726 break;
2727 case WL_OUTPUT_TRANSFORM_270:
2728 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2729 weston_matrix_rotate_xy(matrix, 0, -1);
2730 weston_matrix_translate(matrix,
2731 0, surface->width_from_buffer, 0);
2732 break;
2733 }
2734
2735 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2736}
2737
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002738/**
2739 * Compute a + b > c while being safe to overflows.
2740 */
2741static bool
2742fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2743{
2744 return (int64_t)a + (int64_t)b > (int64_t)c;
2745}
2746
2747static bool
2748weston_surface_is_pending_viewport_source_valid(
2749 const struct weston_surface *surface)
2750{
2751 const struct weston_surface_state *pend = &surface->pending;
2752 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2753 int width_from_buffer = 0;
2754 int height_from_buffer = 0;
2755 wl_fixed_t w;
2756 wl_fixed_t h;
2757
2758 /* If viewport source rect is not set, it is always ok. */
2759 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2760 return true;
2761
2762 if (pend->newly_attached) {
2763 if (pend->buffer) {
2764 convert_size_by_transform_scale(&width_from_buffer,
2765 &height_from_buffer,
2766 pend->buffer->width,
2767 pend->buffer->height,
2768 vp->buffer.transform,
2769 vp->buffer.scale);
2770 } else {
2771 /* No buffer: viewport is irrelevant. */
2772 return true;
2773 }
2774 } else {
2775 width_from_buffer = surface->width_from_buffer;
2776 height_from_buffer = surface->height_from_buffer;
2777 }
2778
2779 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2780 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2781
2782 /* No buffer: viewport is irrelevant. */
2783 if (width_from_buffer == 0 || height_from_buffer == 0)
2784 return true;
2785
2786 /* overflow checks for wl_fixed_from_int() */
2787 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2788 return false;
2789 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2790 return false;
2791
2792 w = wl_fixed_from_int(width_from_buffer);
2793 h = wl_fixed_from_int(height_from_buffer);
2794
2795 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2796 return false;
2797 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2798 return false;
2799
2800 return true;
2801}
2802
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002803static bool
2804fixed_is_integer(wl_fixed_t v)
2805{
2806 return (v & 0xff) == 0;
2807}
2808
2809static bool
2810weston_surface_is_pending_viewport_dst_size_int(
2811 const struct weston_surface *surface)
2812{
2813 const struct weston_buffer_viewport *vp =
2814 &surface->pending.buffer_viewport;
2815
2816 if (vp->surface.width != -1) {
2817 assert(vp->surface.width > 0 && vp->surface.height > 0);
2818 return true;
2819 }
2820
2821 return fixed_is_integer(vp->buffer.src_width) &&
2822 fixed_is_integer(vp->buffer.src_height);
2823}
2824
Derek Foreman152254b2015-11-26 14:17:48 -06002825/* Translate pending damage in buffer co-ordinates to surface
2826 * co-ordinates and union it with a pixman_region32_t.
2827 * This should only be called after the buffer is attached.
2828 */
2829static void
2830apply_damage_buffer(pixman_region32_t *dest,
2831 struct weston_surface *surface,
2832 struct weston_surface_state *state)
2833{
2834 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2835
2836 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2837 * translated into surface co-ordinates and unioned with
2838 * any other surface damage.
2839 * None of this makes sense if there is no buffer though.
2840 */
2841 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2842 pixman_region32_t buffer_damage;
2843
2844 pixman_region32_intersect_rect(&state->damage_buffer,
2845 &state->damage_buffer,
2846 0, 0, buffer->width,
2847 buffer->height);
2848 pixman_region32_init(&buffer_damage);
2849 weston_matrix_transform_region(&buffer_damage,
2850 &surface->buffer_to_surface_matrix,
2851 &state->damage_buffer);
2852 pixman_region32_union(dest, dest, &buffer_damage);
2853 pixman_region32_fini(&buffer_damage);
2854 }
2855 /* We should clear this on commit even if there was no buffer */
2856 pixman_region32_clear(&state->damage_buffer);
2857}
2858
Jason Ekstrand1e059042014-10-16 10:55:19 -05002859static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002860weston_surface_commit_state(struct weston_surface *surface,
2861 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002862{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002863 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002864 pixman_region32_t opaque;
2865
Alexander Larsson4ea95522013-05-22 14:41:37 +02002866 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002867 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03002868 /* wp_viewport.set_source */
2869 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002870 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002871
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002872 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002873 if (state->newly_attached)
2874 weston_surface_attach(surface, state->buffer);
2875 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002876
Jason Ekstrand1e059042014-10-16 10:55:19 -05002877 weston_surface_build_buffer_matrix(surface,
2878 &surface->surface_to_buffer_matrix);
2879 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2880 &surface->surface_to_buffer_matrix);
2881
Jason Ekstrand7b982072014-05-20 14:33:03 -05002882 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002883 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002884 if (surface->committed)
2885 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002886 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002887
Jason Ekstrand7b982072014-05-20 14:33:03 -05002888 state->sx = 0;
2889 state->sy = 0;
2890 state->newly_attached = 0;
2891 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002892
Derek Foreman152254b2015-11-26 14:17:48 -06002893 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002894 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002895 (pixman_region32_not_empty(&state->damage_surface) ||
2896 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002897 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002898
Pekka Paalanen8e159182012-10-10 12:49:25 +03002899 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002900 &state->damage_surface);
2901
2902 apply_damage_buffer(&surface->damage, surface, state);
2903
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002904 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002905 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002906 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002907
2908 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002909 pixman_region32_init(&opaque);
2910 pixman_region32_intersect_rect(&opaque, &state->opaque,
2911 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002912
2913 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2914 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002915 wl_list_for_each(view, &surface->views, surface_link)
2916 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002917 }
2918
2919 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002920
2921 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002922 pixman_region32_intersect_rect(&surface->input, &state->input,
2923 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002924
Pekka Paalanenbc106382012-10-10 12:49:31 +03002925 /* wl_surface.frame */
2926 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002927 &state->frame_callback_list);
2928 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002929
2930 /* XXX:
2931 * What should happen with a feedback request, if there
2932 * is no wl_buffer attached for this commit?
2933 */
2934
2935 /* presentation.feedback */
2936 wl_list_insert_list(&surface->feedback_list,
2937 &state->feedback_list);
2938 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08002939
2940 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002941}
2942
2943static void
2944weston_surface_commit(struct weston_surface *surface)
2945{
2946 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002947
Pekka Paalanene67858b2013-04-25 13:57:42 +03002948 weston_surface_commit_subsurface_order(surface);
2949
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002950 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002951}
2952
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002953static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002954weston_subsurface_commit(struct weston_subsurface *sub);
2955
2956static void
2957weston_subsurface_parent_commit(struct weston_subsurface *sub,
2958 int parent_is_synchronized);
2959
2960static void
2961surface_commit(struct wl_client *client, struct wl_resource *resource)
2962{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002963 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002964 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2965
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002966 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
2967 assert(surface->viewport_resource);
2968
2969 wl_resource_post_error(surface->viewport_resource,
2970 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
2971 "wl_surface@%d has viewport source outside buffer",
2972 wl_resource_get_id(resource));
2973 return;
2974 }
2975
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002976 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
2977 assert(surface->viewport_resource);
2978
2979 wl_resource_post_error(surface->viewport_resource,
2980 WP_VIEWPORT_ERROR_BAD_SIZE,
2981 "wl_surface@%d viewport dst size not integer",
2982 wl_resource_get_id(resource));
2983 return;
2984 }
2985
Pekka Paalanene67858b2013-04-25 13:57:42 +03002986 if (sub) {
2987 weston_subsurface_commit(sub);
2988 return;
2989 }
2990
2991 weston_surface_commit(surface);
2992
2993 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2994 if (sub->surface != surface)
2995 weston_subsurface_parent_commit(sub, 0);
2996 }
2997}
2998
2999static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003000surface_set_buffer_transform(struct wl_client *client,
3001 struct wl_resource *resource, int transform)
3002{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003003 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003004
Jonny Lamba55f1392014-05-30 12:07:15 +02003005 /* if wl_output.transform grows more members this will need to be updated. */
3006 if (transform < 0 ||
3007 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3008 wl_resource_post_error(resource,
3009 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3010 "buffer transform must be a valid transform "
3011 "('%d' specified)", transform);
3012 return;
3013 }
3014
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003015 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003016 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003017}
3018
Alexander Larsson4ea95522013-05-22 14:41:37 +02003019static void
3020surface_set_buffer_scale(struct wl_client *client,
3021 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003022 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003023{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003024 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003025
Jonny Lamba55f1392014-05-30 12:07:15 +02003026 if (scale < 1) {
3027 wl_resource_post_error(resource,
3028 WL_SURFACE_ERROR_INVALID_SCALE,
3029 "buffer scale must be at least one "
3030 "('%d' specified)", scale);
3031 return;
3032 }
3033
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003034 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003035 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003036}
3037
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003038static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003039 surface_destroy,
3040 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003041 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003042 surface_frame,
3043 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003044 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003045 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003046 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003047 surface_set_buffer_scale,
3048 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003049};
3050
3051static void
3052compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003053 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003054{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003055 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003056 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003057
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003058 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003059 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003060 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003061 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003062 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003063
Jason Ekstranda85118c2013-06-27 20:17:02 -05003064 surface->resource =
3065 wl_resource_create(client, &wl_surface_interface,
3066 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003067 if (surface->resource == NULL) {
3068 weston_surface_destroy(surface);
3069 wl_resource_post_no_memory(resource);
3070 return;
3071 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003072 wl_resource_set_implementation(surface->resource, &surface_interface,
3073 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003074
3075 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003076}
3077
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003078static void
3079destroy_region(struct wl_resource *resource)
3080{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003081 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003082
3083 pixman_region32_fini(&region->region);
3084 free(region);
3085}
3086
3087static void
3088region_destroy(struct wl_client *client, struct wl_resource *resource)
3089{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003090 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003091}
3092
3093static void
3094region_add(struct wl_client *client, struct wl_resource *resource,
3095 int32_t x, int32_t y, int32_t width, int32_t height)
3096{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003097 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003098
3099 pixman_region32_union_rect(&region->region, &region->region,
3100 x, y, width, height);
3101}
3102
3103static void
3104region_subtract(struct wl_client *client, struct wl_resource *resource,
3105 int32_t x, int32_t y, int32_t width, int32_t height)
3106{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003107 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003108 pixman_region32_t rect;
3109
3110 pixman_region32_init_rect(&rect, x, y, width, height);
3111 pixman_region32_subtract(&region->region, &region->region, &rect);
3112 pixman_region32_fini(&rect);
3113}
3114
3115static const struct wl_region_interface region_interface = {
3116 region_destroy,
3117 region_add,
3118 region_subtract
3119};
3120
3121static void
3122compositor_create_region(struct wl_client *client,
3123 struct wl_resource *resource, uint32_t id)
3124{
3125 struct weston_region *region;
3126
3127 region = malloc(sizeof *region);
3128 if (region == NULL) {
3129 wl_resource_post_no_memory(resource);
3130 return;
3131 }
3132
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003133 pixman_region32_init(&region->region);
3134
Jason Ekstranda85118c2013-06-27 20:17:02 -05003135 region->resource =
3136 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003137 if (region->resource == NULL) {
3138 free(region);
3139 wl_resource_post_no_memory(resource);
3140 return;
3141 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003142 wl_resource_set_implementation(region->resource, &region_interface,
3143 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003144}
3145
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003146static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003147 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003148 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003149};
3150
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003151static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003152weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3153{
3154 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003155
Jason Ekstrand7b982072014-05-20 14:33:03 -05003156 weston_surface_commit_state(surface, &sub->cached);
3157 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003158
3159 weston_surface_commit_subsurface_order(surface);
3160
3161 weston_surface_schedule_repaint(surface);
3162
Jason Ekstrand7b982072014-05-20 14:33:03 -05003163 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003164}
3165
3166static void
3167weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3168{
3169 struct weston_surface *surface = sub->surface;
3170
3171 /*
3172 * If this commit would cause the surface to move by the
3173 * attach(dx, dy) parameters, the old damage region must be
3174 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003175 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003176 */
Derek Foreman152254b2015-11-26 14:17:48 -06003177 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003178 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003179 pixman_region32_union(&sub->cached.damage_surface,
3180 &sub->cached.damage_surface,
3181 &surface->pending.damage_surface);
3182 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003183
3184 if (surface->pending.newly_attached) {
3185 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003186 weston_surface_state_set_buffer(&sub->cached,
3187 surface->pending.buffer);
3188 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003190 weston_presentation_feedback_discard_list(
3191 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003192 }
3193 sub->cached.sx += surface->pending.sx;
3194 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003195
Derek Foreman152254b2015-11-26 14:17:48 -06003196 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3197
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003198 sub->cached.buffer_viewport.changed |=
3199 surface->pending.buffer_viewport.changed;
3200 sub->cached.buffer_viewport.buffer =
3201 surface->pending.buffer_viewport.buffer;
3202 sub->cached.buffer_viewport.surface =
3203 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003204
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003205 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003206
3207 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3208
3209 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3210
3211 wl_list_insert_list(&sub->cached.frame_callback_list,
3212 &surface->pending.frame_callback_list);
3213 wl_list_init(&surface->pending.frame_callback_list);
3214
Pekka Paalanen133e4392014-09-23 22:08:46 -04003215 wl_list_insert_list(&sub->cached.feedback_list,
3216 &surface->pending.feedback_list);
3217 wl_list_init(&surface->pending.feedback_list);
3218
Jason Ekstrand7b982072014-05-20 14:33:03 -05003219 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003220}
3221
Derek Foreman280e7dd2014-10-03 13:13:42 -05003222static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003223weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3224{
3225 while (sub) {
3226 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003227 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003228
3229 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003230 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003231
3232 sub = weston_surface_to_subsurface(sub->parent);
3233 }
3234
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003235 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003236}
3237
3238static void
3239weston_subsurface_commit(struct weston_subsurface *sub)
3240{
3241 struct weston_surface *surface = sub->surface;
3242 struct weston_subsurface *tmp;
3243
3244 /* Recursive check for effectively synchronized. */
3245 if (weston_subsurface_is_synchronized(sub)) {
3246 weston_subsurface_commit_to_cache(sub);
3247 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003248 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003249 /* flush accumulated state from cache */
3250 weston_subsurface_commit_to_cache(sub);
3251 weston_subsurface_commit_from_cache(sub);
3252 } else {
3253 weston_surface_commit(surface);
3254 }
3255
3256 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3257 if (tmp->surface != surface)
3258 weston_subsurface_parent_commit(tmp, 0);
3259 }
3260 }
3261}
3262
3263static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003264weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003265{
3266 struct weston_surface *surface = sub->surface;
3267 struct weston_subsurface *tmp;
3268
Pekka Paalanene67858b2013-04-25 13:57:42 +03003269 /* From now on, commit_from_cache the whole sub-tree, regardless of
3270 * the synchronized mode of each child. This sub-surface or some
3271 * of its ancestors were synchronized, so we are synchronized
3272 * all the way down.
3273 */
3274
Jason Ekstrand7b982072014-05-20 14:33:03 -05003275 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003276 weston_subsurface_commit_from_cache(sub);
3277
3278 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3279 if (tmp->surface != surface)
3280 weston_subsurface_parent_commit(tmp, 1);
3281 }
3282}
3283
3284static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003285weston_subsurface_parent_commit(struct weston_subsurface *sub,
3286 int parent_is_synchronized)
3287{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003288 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003289 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003290 wl_list_for_each(view, &sub->surface->views, surface_link)
3291 weston_view_set_position(view,
3292 sub->position.x,
3293 sub->position.y);
3294
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003295 sub->position.set = 0;
3296 }
3297
3298 if (parent_is_synchronized || sub->synchronized)
3299 weston_subsurface_synchronized_commit(sub);
3300}
3301
Pekka Paalanen8274d902014-08-06 19:36:51 +03003302static int
3303subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3304{
3305 return snprintf(buf, len, "sub-surface");
3306}
3307
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003308static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003309subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003310{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003311 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003312
Jason Ekstranda7af7042013-10-12 22:38:11 -05003313 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003314 weston_view_set_position(view,
3315 view->geometry.x + dx,
3316 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003317
3318 /* No need to check parent mappedness, because if parent is not
3319 * mapped, parent is not in a visible layer, so this sub-surface
3320 * will not be drawn either.
3321 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003322
Pekka Paalanene67858b2013-04-25 13:57:42 +03003323 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003324 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003325
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003326 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003327 * because that would call it also for the parent surface,
3328 * which might not be mapped yet. That would lead to
3329 * inconsistent state, where the window could never be
3330 * mapped.
3331 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003332 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003333 * weston_surface_is_mapped() return true, so that when the
3334 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003335 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003336 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003337 }
3338}
3339
3340static struct weston_subsurface *
3341weston_surface_to_subsurface(struct weston_surface *surface)
3342{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003343 if (surface->committed == subsurface_committed)
3344 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003345
3346 return NULL;
3347}
3348
Pekka Paalanen01388e22013-04-25 13:57:44 +03003349WL_EXPORT struct weston_surface *
3350weston_surface_get_main_surface(struct weston_surface *surface)
3351{
3352 struct weston_subsurface *sub;
3353
3354 while (surface && (sub = weston_surface_to_subsurface(surface)))
3355 surface = sub->parent;
3356
3357 return surface;
3358}
3359
Pekka Paalanen50b67472014-10-01 15:02:41 +03003360WL_EXPORT int
3361weston_surface_set_role(struct weston_surface *surface,
3362 const char *role_name,
3363 struct wl_resource *error_resource,
3364 uint32_t error_code)
3365{
3366 assert(role_name);
3367
3368 if (surface->role_name == NULL ||
3369 surface->role_name == role_name ||
3370 strcmp(surface->role_name, role_name) == 0) {
3371 surface->role_name = role_name;
3372
3373 return 0;
3374 }
3375
3376 wl_resource_post_error(error_resource, error_code,
3377 "Cannot assign role %s to wl_surface@%d,"
3378 " already has role %s\n",
3379 role_name,
3380 wl_resource_get_id(surface->resource),
3381 surface->role_name);
3382 return -1;
3383}
3384
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003385WL_EXPORT const char *
3386weston_surface_get_role(struct weston_surface *surface)
3387{
3388 return surface->role_name;
3389}
3390
Pekka Paalanen8274d902014-08-06 19:36:51 +03003391WL_EXPORT void
3392weston_surface_set_label_func(struct weston_surface *surface,
3393 int (*desc)(struct weston_surface *,
3394 char *, size_t))
3395{
3396 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003397 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003398}
3399
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003400/** Get the size of surface contents
3401 *
3402 * \param surface The surface to query.
3403 * \param width Returns the width of raw contents.
3404 * \param height Returns the height of raw contents.
3405 *
3406 * Retrieves the raw surface content size in pixels for the given surface.
3407 * This is the whole content size in buffer pixels. If the surface
3408 * has no content or the renderer does not implement this feature,
3409 * zeroes are returned.
3410 *
3411 * This function is used to determine the buffer size needed for
3412 * a weston_surface_copy_content() call.
3413 */
3414WL_EXPORT void
3415weston_surface_get_content_size(struct weston_surface *surface,
3416 int *width, int *height)
3417{
3418 struct weston_renderer *rer = surface->compositor->renderer;
3419
3420 if (!rer->surface_get_content_size) {
3421 *width = 0;
3422 *height = 0;
3423 return;
3424 }
3425
3426 rer->surface_get_content_size(surface, width, height);
3427}
3428
Quentin Glidic248dd102016-08-12 10:41:34 +02003429/** Get the bounding box of a surface and its subsurfaces
3430 *
3431 * \param surface The surface to query.
3432 * \return The bounding box relative to the surface origin.
3433 *
3434 */
3435WL_EXPORT struct weston_geometry
3436weston_surface_get_bounding_box(struct weston_surface *surface)
3437{
3438 pixman_region32_t region;
3439 pixman_box32_t *box;
3440 struct weston_subsurface *subsurface;
3441
3442 pixman_region32_init_rect(&region,
3443 0, 0,
3444 surface->width, surface->height);
3445
3446 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3447 pixman_region32_union_rect(&region, &region,
3448 subsurface->position.x,
3449 subsurface->position.y,
3450 subsurface->surface->width,
3451 subsurface->surface->height);
3452
3453 box = pixman_region32_extents(&region);
3454 struct weston_geometry geometry = {
3455 .x = box->x1,
3456 .y = box->y1,
3457 .width = box->x2 - box->x1,
3458 .height = box->y2 - box->y1,
3459 };
3460
3461 pixman_region32_fini(&region);
3462
3463 return geometry;
3464}
3465
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003466/** Copy surface contents to system memory.
3467 *
3468 * \param surface The surface to copy from.
3469 * \param target Pointer to the target memory buffer.
3470 * \param size Size of the target buffer in bytes.
3471 * \param src_x X location on contents to copy from.
3472 * \param src_y Y location on contents to copy from.
3473 * \param width Width in pixels of the area to copy.
3474 * \param height Height in pixels of the area to copy.
3475 * \return 0 for success, -1 for failure.
3476 *
3477 * Surface contents are maintained by the renderer. They can be in a
3478 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3479 * else.
3480 *
3481 * Surface contents are copied into memory pointed to by target,
3482 * which has size bytes of space available. The target memory
3483 * may be larger than needed, but being smaller returns an error.
3484 * The extra bytes in target may or may not be written; their content is
3485 * unspecified. Size must be large enough to hold the image.
3486 *
3487 * The image in the target memory will be arranged in rows from
3488 * top to bottom, and pixels on a row from left to right. The pixel
3489 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3490 * width * 4.
3491 *
3492 * Parameters src_x and src_y define the upper-left corner in buffer
3493 * coordinates (pixels) to copy from. Parameters width and height
3494 * define the size of the area to copy in pixels.
3495 *
3496 * The rectangle defined by src_x, src_y, width, height must fit in
3497 * the surface contents. Otherwise an error is returned.
3498 *
3499 * Use surface_get_data_size to determine the content size; the
3500 * needed target buffer size and rectangle limits.
3501 *
3502 * CURRENT IMPLEMENTATION RESTRICTIONS:
3503 * - the machine must be little-endian due to Pixman formats.
3504 *
3505 * NOTE: Pixman formats are premultiplied.
3506 */
3507WL_EXPORT int
3508weston_surface_copy_content(struct weston_surface *surface,
3509 void *target, size_t size,
3510 int src_x, int src_y,
3511 int width, int height)
3512{
3513 struct weston_renderer *rer = surface->compositor->renderer;
3514 int cw, ch;
3515 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3516
3517 if (!rer->surface_copy_content)
3518 return -1;
3519
3520 weston_surface_get_content_size(surface, &cw, &ch);
3521
3522 if (src_x < 0 || src_y < 0)
3523 return -1;
3524
3525 if (width <= 0 || height <= 0)
3526 return -1;
3527
3528 if (src_x + width > cw || src_y + height > ch)
3529 return -1;
3530
3531 if (width * bytespp * height > size)
3532 return -1;
3533
3534 return rer->surface_copy_content(surface, target, size,
3535 src_x, src_y, width, height);
3536}
3537
Pekka Paalanene67858b2013-04-25 13:57:42 +03003538static void
3539subsurface_set_position(struct wl_client *client,
3540 struct wl_resource *resource, int32_t x, int32_t y)
3541{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003542 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003543
3544 if (!sub)
3545 return;
3546
3547 sub->position.x = x;
3548 sub->position.y = y;
3549 sub->position.set = 1;
3550}
3551
3552static struct weston_subsurface *
3553subsurface_from_surface(struct weston_surface *surface)
3554{
3555 struct weston_subsurface *sub;
3556
3557 sub = weston_surface_to_subsurface(surface);
3558 if (sub)
3559 return sub;
3560
3561 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3562 if (sub->surface == surface)
3563 return sub;
3564
3565 return NULL;
3566}
3567
3568static struct weston_subsurface *
3569subsurface_sibling_check(struct weston_subsurface *sub,
3570 struct weston_surface *surface,
3571 const char *request)
3572{
3573 struct weston_subsurface *sibling;
3574
3575 sibling = subsurface_from_surface(surface);
3576
3577 if (!sibling) {
3578 wl_resource_post_error(sub->resource,
3579 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3580 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003581 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003582 return NULL;
3583 }
3584
3585 if (sibling->parent != sub->parent) {
3586 wl_resource_post_error(sub->resource,
3587 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3588 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003589 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003590 return NULL;
3591 }
3592
3593 return sibling;
3594}
3595
3596static void
3597subsurface_place_above(struct wl_client *client,
3598 struct wl_resource *resource,
3599 struct wl_resource *sibling_resource)
3600{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003601 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003602 struct weston_surface *surface =
3603 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003604 struct weston_subsurface *sibling;
3605
3606 if (!sub)
3607 return;
3608
3609 sibling = subsurface_sibling_check(sub, surface, "place_above");
3610 if (!sibling)
3611 return;
3612
3613 wl_list_remove(&sub->parent_link_pending);
3614 wl_list_insert(sibling->parent_link_pending.prev,
3615 &sub->parent_link_pending);
3616}
3617
3618static void
3619subsurface_place_below(struct wl_client *client,
3620 struct wl_resource *resource,
3621 struct wl_resource *sibling_resource)
3622{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003623 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003624 struct weston_surface *surface =
3625 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003626 struct weston_subsurface *sibling;
3627
3628 if (!sub)
3629 return;
3630
3631 sibling = subsurface_sibling_check(sub, surface, "place_below");
3632 if (!sibling)
3633 return;
3634
3635 wl_list_remove(&sub->parent_link_pending);
3636 wl_list_insert(&sibling->parent_link_pending,
3637 &sub->parent_link_pending);
3638}
3639
3640static void
3641subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3642{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003643 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003644
3645 if (sub)
3646 sub->synchronized = 1;
3647}
3648
3649static void
3650subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3651{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003652 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003653
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003654 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003655 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003656
3657 /* If sub became effectively desynchronized, flush. */
3658 if (!weston_subsurface_is_synchronized(sub))
3659 weston_subsurface_synchronized_commit(sub);
3660 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003661}
3662
3663static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003664weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3665{
3666 wl_list_remove(&sub->parent_link);
3667 wl_list_remove(&sub->parent_link_pending);
3668 wl_list_remove(&sub->parent_destroy_listener.link);
3669 sub->parent = NULL;
3670}
3671
3672static void
3673weston_subsurface_destroy(struct weston_subsurface *sub);
3674
3675static void
3676subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3677{
3678 struct weston_subsurface *sub =
3679 container_of(listener, struct weston_subsurface,
3680 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003681 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003682
3683 /* The protocol object (wl_resource) is left inert. */
3684 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003685 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003686
3687 weston_subsurface_destroy(sub);
3688}
3689
3690static void
3691subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3692{
3693 struct weston_subsurface *sub =
3694 container_of(listener, struct weston_subsurface,
3695 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003696 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003697 assert(sub->surface != sub->parent);
3698
3699 if (weston_surface_is_mapped(sub->surface))
3700 weston_surface_unmap(sub->surface);
3701
3702 weston_subsurface_unlink_parent(sub);
3703}
3704
3705static void
3706subsurface_resource_destroy(struct wl_resource *resource)
3707{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003708 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003709
3710 if (sub)
3711 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003712}
3713
3714static void
3715subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3716{
3717 wl_resource_destroy(resource);
3718}
3719
3720static void
3721weston_subsurface_link_parent(struct weston_subsurface *sub,
3722 struct weston_surface *parent)
3723{
3724 sub->parent = parent;
3725 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003726 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003727 &sub->parent_destroy_listener);
3728
3729 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3730 wl_list_insert(&parent->subsurface_list_pending,
3731 &sub->parent_link_pending);
3732}
3733
3734static void
3735weston_subsurface_link_surface(struct weston_subsurface *sub,
3736 struct weston_surface *surface)
3737{
3738 sub->surface = surface;
3739 sub->surface_destroy_listener.notify =
3740 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003741 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003742 &sub->surface_destroy_listener);
3743}
3744
3745static void
3746weston_subsurface_destroy(struct weston_subsurface *sub)
3747{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003748 struct weston_view *view, *next;
3749
Pekka Paalanene67858b2013-04-25 13:57:42 +03003750 assert(sub->surface);
3751
3752 if (sub->resource) {
3753 assert(weston_surface_to_subsurface(sub->surface) == sub);
3754 assert(sub->parent_destroy_listener.notify ==
3755 subsurface_handle_parent_destroy);
3756
George Kiagiadakised04d382014-06-13 18:10:26 +02003757 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3758 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003759 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003760 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003761
Pekka Paalanene67858b2013-04-25 13:57:42 +03003762 if (sub->parent)
3763 weston_subsurface_unlink_parent(sub);
3764
Jason Ekstrand7b982072014-05-20 14:33:03 -05003765 weston_surface_state_fini(&sub->cached);
3766 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003767
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003768 sub->surface->committed = NULL;
3769 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003770 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003771 } else {
3772 /* the dummy weston_subsurface for the parent itself */
3773 assert(sub->parent_destroy_listener.notify == NULL);
3774 wl_list_remove(&sub->parent_link);
3775 wl_list_remove(&sub->parent_link_pending);
3776 }
3777
3778 wl_list_remove(&sub->surface_destroy_listener.link);
3779 free(sub);
3780}
3781
3782static const struct wl_subsurface_interface subsurface_implementation = {
3783 subsurface_destroy,
3784 subsurface_set_position,
3785 subsurface_place_above,
3786 subsurface_place_below,
3787 subsurface_set_sync,
3788 subsurface_set_desync
3789};
3790
3791static struct weston_subsurface *
3792weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3793 struct weston_surface *parent)
3794{
3795 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003796 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003797
Bryce Harringtonde16d892014-11-20 22:21:57 -08003798 sub = zalloc(sizeof *sub);
3799 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003800 return NULL;
3801
Jason Ekstranda7af7042013-10-12 22:38:11 -05003802 wl_list_init(&sub->unused_views);
3803
Jason Ekstranda85118c2013-06-27 20:17:02 -05003804 sub->resource =
3805 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003806 if (!sub->resource) {
3807 free(sub);
3808 return NULL;
3809 }
3810
Jason Ekstranda85118c2013-06-27 20:17:02 -05003811 wl_resource_set_implementation(sub->resource,
3812 &subsurface_implementation,
3813 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003814 weston_subsurface_link_surface(sub, surface);
3815 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003816 weston_surface_state_init(&sub->cached);
3817 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003818 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003819
3820 return sub;
3821}
3822
3823/* Create a dummy subsurface for having the parent itself in its
3824 * sub-surface lists. Makes stacking order manipulation easy.
3825 */
3826static struct weston_subsurface *
3827weston_subsurface_create_for_parent(struct weston_surface *parent)
3828{
3829 struct weston_subsurface *sub;
3830
Bryce Harringtonde16d892014-11-20 22:21:57 -08003831 sub = zalloc(sizeof *sub);
3832 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003833 return NULL;
3834
3835 weston_subsurface_link_surface(sub, parent);
3836 sub->parent = parent;
3837 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3838 wl_list_insert(&parent->subsurface_list_pending,
3839 &sub->parent_link_pending);
3840
3841 return sub;
3842}
3843
3844static void
3845subcompositor_get_subsurface(struct wl_client *client,
3846 struct wl_resource *resource,
3847 uint32_t id,
3848 struct wl_resource *surface_resource,
3849 struct wl_resource *parent_resource)
3850{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003851 struct weston_surface *surface =
3852 wl_resource_get_user_data(surface_resource);
3853 struct weston_surface *parent =
3854 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003855 struct weston_subsurface *sub;
3856 static const char where[] = "get_subsurface: wl_subsurface@";
3857
3858 if (surface == parent) {
3859 wl_resource_post_error(resource,
3860 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3861 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003862 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003863 return;
3864 }
3865
3866 if (weston_surface_to_subsurface(surface)) {
3867 wl_resource_post_error(resource,
3868 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3869 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003870 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003871 return;
3872 }
3873
Pekka Paalanen50b67472014-10-01 15:02:41 +03003874 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3875 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003876 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003877
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003878 if (weston_surface_get_main_surface(parent) == surface) {
3879 wl_resource_post_error(resource,
3880 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3881 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003882 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003883 return;
3884 }
3885
Pekka Paalanene67858b2013-04-25 13:57:42 +03003886 /* make sure the parent is in its own list */
3887 if (wl_list_empty(&parent->subsurface_list)) {
3888 if (!weston_subsurface_create_for_parent(parent)) {
3889 wl_resource_post_no_memory(resource);
3890 return;
3891 }
3892 }
3893
3894 sub = weston_subsurface_create(id, surface, parent);
3895 if (!sub) {
3896 wl_resource_post_no_memory(resource);
3897 return;
3898 }
3899
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003900 surface->committed = subsurface_committed;
3901 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003902 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003903}
3904
3905static void
3906subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3907{
3908 wl_resource_destroy(resource);
3909}
3910
3911static const struct wl_subcompositor_interface subcompositor_interface = {
3912 subcompositor_destroy,
3913 subcompositor_get_subsurface
3914};
3915
3916static void
3917bind_subcompositor(struct wl_client *client,
3918 void *data, uint32_t version, uint32_t id)
3919{
3920 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003921 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003922
Jason Ekstranda85118c2013-06-27 20:17:02 -05003923 resource =
3924 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003925 if (resource == NULL) {
3926 wl_client_post_no_memory(client);
3927 return;
3928 }
3929 wl_resource_set_implementation(resource, &subcompositor_interface,
3930 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003931}
3932
Bryce Harrington0795ece2016-08-30 12:04:26 -07003933/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003934 *
3935 * \param compositor The compositor instance
3936 * \param state The DPMS state the outputs will be set to
3937 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003938static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003939weston_compositor_dpms(struct weston_compositor *compositor,
3940 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003941{
3942 struct weston_output *output;
Bryce Harringtonf8300c82016-08-10 17:25:22 -07003943 uint32_t inhibited_outputs_mask = weston_compositor_inhibited_outputs(compositor);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003944
Bryce Harringtonf8300c82016-08-10 17:25:22 -07003945 wl_list_for_each(output, &compositor->output_list, link) {
3946 if (!output->set_dpms)
3947 continue;
3948
3949 /* If output is idle-inhibited, don't toggle to any DPMS state except ON. */
3950 if (state != WESTON_DPMS_ON &&
3951 inhibited_outputs_mask & (1 << output->id))
3952 continue;
3953
3954 output->set_dpms(output, state);
3955 }
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003956}
3957
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003958/** Restores the compositor to active status
3959 *
3960 * \param compositor The compositor instance
3961 *
3962 * If the compositor was in a sleeping mode, all outputs are powered
3963 * back on via DPMS. Otherwise if the compositor was inactive
3964 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3965 * signal will fire.
3966 *
3967 * Restarts the idle timer.
3968 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003969WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003970weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003971{
Neil Roberts8b62e202013-09-30 13:14:47 +01003972 uint32_t old_state = compositor->state;
3973
3974 /* The state needs to be changed before emitting the wake
3975 * signal because that may try to schedule a repaint which
3976 * will not work if the compositor is still sleeping */
3977 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3978
3979 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003980 case WESTON_COMPOSITOR_SLEEPING:
3981 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3982 /* fall through */
3983 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003984 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003985 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003986 /* fall through */
3987 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003988 wl_event_source_timer_update(compositor->idle_source,
3989 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003990 }
3991}
3992
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003993/** Turns off rendering and frame events for the compositor.
3994 *
3995 * \param compositor The compositor instance
3996 *
3997 * This is used for example to prevent further rendering while the
3998 * compositor is shutting down.
3999 *
4000 * \note When offscreen state is entered, outputs will be powered
4001 * back on if they were sleeping (in DPMS off mode), even though
4002 * no rendering will be performed.
4003 *
4004 * Stops the idle timer.
4005 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004006WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004007weston_compositor_offscreen(struct weston_compositor *compositor)
4008{
4009 switch (compositor->state) {
4010 case WESTON_COMPOSITOR_OFFSCREEN:
4011 return;
4012 case WESTON_COMPOSITOR_SLEEPING:
4013 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
4014 /* fall through */
4015 default:
4016 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4017 wl_event_source_timer_update(compositor->idle_source, 0);
4018 }
4019}
4020
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004021/** Powers down all attached output devices
4022 *
4023 * \param compositor The compositor instance
4024 *
4025 * Causes rendering to the outputs to cease, and no frame events to be
4026 * sent. Only powers down the outputs if the compositor is not already
4027 * in sleep mode.
4028 *
4029 * Stops the idle timer.
4030 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004031WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004032weston_compositor_sleep(struct weston_compositor *compositor)
4033{
4034 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4035 return;
4036
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004037 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004038 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4039 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4040}
4041
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004042/** Sets compositor to idle mode
4043 *
4044 * \param data The compositor instance
4045 *
4046 * This is called when the idle timer fires. Once the compositor is in
4047 * idle mode it requires a wake action (e.g. via
4048 * weston_compositor_wake()) to restore it. The compositor's
4049 * idle_signal will be triggered when the idle event occurs.
4050 *
4051 * Idleness can be inhibited by setting the compositor's idle_inhibit
4052 * property.
4053 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004054static int
4055idle_handler(void *data)
4056{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004057 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004058
4059 if (compositor->idle_inhibit)
4060 return 1;
4061
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004062 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004063 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004064
4065 return 1;
4066}
4067
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004068WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004069weston_plane_init(struct weston_plane *plane,
4070 struct weston_compositor *ec,
4071 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004072{
4073 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004074 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004075 plane->x = x;
4076 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004077 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004078
4079 /* Init the link so that the call to wl_list_remove() when releasing
4080 * the plane without ever stacking doesn't lead to a crash */
4081 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004082}
4083
4084WL_EXPORT void
4085weston_plane_release(struct weston_plane *plane)
4086{
Xiong Zhang97116532013-10-23 13:58:31 +08004087 struct weston_view *view;
4088
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004089 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004090 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004091
Xiong Zhang97116532013-10-23 13:58:31 +08004092 wl_list_for_each(view, &plane->compositor->view_list, link) {
4093 if (view->plane == plane)
4094 view->plane = NULL;
4095 }
4096
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004097 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004098}
4099
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004100WL_EXPORT void
4101weston_compositor_stack_plane(struct weston_compositor *ec,
4102 struct weston_plane *plane,
4103 struct weston_plane *above)
4104{
4105 if (above)
4106 wl_list_insert(above->link.prev, &plane->link);
4107 else
4108 wl_list_insert(&ec->plane_list, &plane->link);
4109}
4110
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004111static void
4112output_release(struct wl_client *client, struct wl_resource *resource)
4113{
4114 wl_resource_destroy(resource);
4115}
4116
4117static const struct wl_output_interface output_interface = {
4118 output_release,
4119};
4120
4121
Casey Dahlin9074db52012-04-19 22:50:09 -04004122static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004123{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004124 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004125}
4126
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004127static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004128bind_output(struct wl_client *client,
4129 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004130{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004131 struct weston_output *output = data;
4132 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004133 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004134
Jason Ekstranda85118c2013-06-27 20:17:02 -05004135 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004136 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004137 if (resource == NULL) {
4138 wl_client_post_no_memory(client);
4139 return;
4140 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004141
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004142 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004143 wl_resource_set_implementation(resource, &output_interface, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004144
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004145 wl_output_send_geometry(resource,
4146 output->x,
4147 output->y,
4148 output->mm_width,
4149 output->mm_height,
4150 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004151 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004152 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004153 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004154 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004155 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004156
4157 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004158 wl_output_send_mode(resource,
4159 mode->flags,
4160 mode->width,
4161 mode->height,
4162 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004163 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004164
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004165 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004166 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004167}
4168
David Fort0de859e2016-05-27 23:22:57 +02004169/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004170static void
David Fort0de859e2016-05-27 23:22:57 +02004171weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4172 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004173{
4174 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004175 bool start_resizing = false;
4176
4177 if (!delta_width)
4178 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004179
4180 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004181 if (output == resized_output) {
4182 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004183 continue;
4184 }
4185
David Fort0de859e2016-05-27 23:22:57 +02004186 if (start_resizing) {
4187 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004188 output->dirty = 1;
4189 }
4190 }
4191}
4192
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004193WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004194weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004195{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004196 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004197 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004198
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02004199 output->destroying = 1;
4200
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004201 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08004202 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004203 weston_view_assign_output(view);
4204 }
4205
Pekka Paalanen0513a952014-05-21 16:17:27 +03004206 wl_event_source_remove(output->repaint_timer);
4207
Pekka Paalanen133e4392014-09-23 22:08:46 -04004208 weston_presentation_feedback_discard_list(&output->feedback_list);
4209
David Fort0de859e2016-05-27 23:22:57 +02004210 weston_compositor_reflow_outputs(output->compositor, output, output->width);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004211 wl_list_remove(&output->link);
4212
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004213 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004214 wl_signal_emit(&output->destroy_signal, output);
4215
Richard Hughesafe690c2013-05-02 10:10:04 +01004216 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004217 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004218 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08004219 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004220
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004221 wl_resource_for_each(resource, &output->resource_list) {
4222 wl_resource_set_destructor(resource, NULL);
4223 }
4224
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004225 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004226}
4227
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004228WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004229weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004230{
Scott Moreau850ca422012-05-21 15:21:25 -06004231 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004232
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004233 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004234 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004235
Scott Moreauccbf29d2012-02-22 14:21:41 -07004236 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004237 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004238 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004239 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004240 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004241 weston_matrix_scale(&output->matrix, magnification,
4242 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004243 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004244
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004245 switch (output->transform) {
4246 case WL_OUTPUT_TRANSFORM_FLIPPED:
4247 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4248 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4249 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4250 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4251 weston_matrix_scale(&output->matrix, -1, 1, 1);
4252 break;
4253 }
4254
4255 switch (output->transform) {
4256 default:
4257 case WL_OUTPUT_TRANSFORM_NORMAL:
4258 case WL_OUTPUT_TRANSFORM_FLIPPED:
4259 break;
4260 case WL_OUTPUT_TRANSFORM_90:
4261 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4262 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4263 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4264 break;
4265 case WL_OUTPUT_TRANSFORM_180:
4266 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4267 weston_matrix_translate(&output->matrix,
4268 -output->width, -output->height, 0);
4269 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4270 break;
4271 case WL_OUTPUT_TRANSFORM_270:
4272 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4273 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4274 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4275 break;
4276 }
4277
4278 if (output->current_scale != 1)
4279 weston_matrix_scale(&output->matrix,
4280 output->current_scale,
4281 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004282
Scott Moreauccbf29d2012-02-22 14:21:41 -07004283 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004284
4285 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004286}
4287
Scott Moreau1bad5db2012-08-18 01:04:05 -06004288static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004289weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004290{
4291 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004292 output->native_scale = scale;
4293 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004294
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004295 convert_size_by_transform_scale(&output->width, &output->height,
4296 output->current_mode->width,
4297 output->current_mode->height,
4298 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004299}
4300
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004301static void
4302weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004303{
4304 output->x = x;
4305 output->y = y;
4306
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004307 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004308 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004309 output->width,
4310 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004311}
4312
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004313WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004314weston_output_move(struct weston_output *output, int x, int y)
4315{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004316 struct wl_resource *resource;
4317
4318 output->move_x = x - output->x;
4319 output->move_y = y - output->y;
4320
4321 if (output->move_x == 0 && output->move_y == 0)
4322 return;
4323
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004324 weston_output_init_geometry(output, x, y);
4325
4326 output->dirty = 1;
4327
4328 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004329 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004330
4331 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004332 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004333 wl_output_send_geometry(resource,
4334 output->x,
4335 output->y,
4336 output->mm_width,
4337 output->mm_height,
4338 output->subpixel,
4339 output->make,
4340 output->model,
4341 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004342
FORT David8a120692016-04-26 23:34:06 +02004343 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004344 wl_output_send_done(resource);
4345 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004346}
4347
Bryce Harrington3f650b82015-12-23 11:01:58 -08004348/** Initialize a weston_output object's parameters
4349 *
4350 * \param output The weston_output object to initialize
4351 * \param c The output's compositor
4352 * \param x x coordinate for the output in global coordinate space
4353 * \param y y coordinate for the output in global coordinate space
4354 * \param mm_width Physical width of the output as reported by the backend
4355 * \param mm_height Physical height of the output as reported by the backend
4356 * \param transform Rotation of the output
4357 * \param scale Native scaling factor for the output
4358 *
4359 * Sets up the transformation, zoom, and geometry of the output using
4360 * the input properties.
4361 *
4362 * Establishes a repaint timer for the output with the relevant display
4363 * object's event loop. See output_repaint_timer_handler().
4364 *
4365 * The output is assigned an ID. Weston can support up to 32 distinct
4366 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4367 * is referred to and used to find the first available ID number, and
4368 * then this ID is marked as used in output_id_pool.
4369 *
4370 * The output is also assigned a Wayland global with the wl_output
4371 * external interface.
4372 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004373WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004374weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004375 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004376 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004377{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004378 struct wl_event_loop *loop;
4379
Bryce Harrington18e45732015-12-23 20:53:53 -08004380 /* Verify we haven't reached the limit of 32 available output IDs */
4381 assert(ffs(~c->output_id_pool) > 0);
4382
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004383 output->compositor = c;
4384 output->x = x;
4385 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004386 output->mm_width = mm_width;
4387 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004388 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004389 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004390
Alexander Larsson0b135062013-05-28 16:23:36 +02004391 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004392 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004393
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004394 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004395 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004396
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004397 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004398 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004399 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004400 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004401 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004402 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004403
Pekka Paalanen0513a952014-05-21 16:17:27 +03004404 loop = wl_display_get_event_loop(c->wl_display);
4405 output->repaint_timer = wl_event_loop_add_timer(loop,
4406 output_repaint_timer_handler, output);
4407
Bryce Harrington3f650b82015-12-23 11:01:58 -08004408 /* Invert the output id pool and look for the lowest numbered
4409 * switch (the least significant bit). Take that bit's position
4410 * as our ID, and mark it used in the compositor's output_id_pool.
4411 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004412 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004413 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004414
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004415 output->global =
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004416 wl_global_create(c->wl_display, &wl_output_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004417 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004418}
4419
4420/** Adds an output to the compositor's output list and
4421 * send the compositor's output_created signal.
4422 *
4423 * \param compositor The compositor instance.
4424 * \param output The output to be added.
4425 */
4426WL_EXPORT void
4427weston_compositor_add_output(struct weston_compositor *compositor,
4428 struct weston_output *output)
4429{
Armin Krezoviće5403842016-08-05 15:28:29 +02004430 struct weston_view *view, *next;
4431
Giulio Camuffob1147152015-05-06 21:41:57 +03004432 wl_list_insert(compositor->output_list.prev, &output->link);
4433 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02004434
4435 wl_list_for_each_safe(view, next, &compositor->view_list, link)
4436 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004437}
4438
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004439WL_EXPORT void
4440weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004441 double device_x, double device_y,
4442 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004443{
Derek Foreman0f679412014-10-02 13:41:17 -05004444 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004445 device_x,
4446 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004447 0.0,
4448 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004449
Derek Foreman67a18b92015-03-24 11:36:14 -05004450 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004451
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004452 *x = p.f[0] / p.f[3];
4453 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004454}
4455
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004456static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004457destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004458{
Jonny Lamb74130762013-11-26 18:19:46 +01004459 struct weston_surface *surface =
4460 wl_resource_get_user_data(resource);
4461
Pekka Paalanen4826f872016-04-22 14:14:38 +03004462 if (!surface)
4463 return;
4464
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004465 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004466 surface->pending.buffer_viewport.buffer.src_width =
4467 wl_fixed_from_int(-1);
4468 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004469 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004470}
4471
4472static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004473viewport_destroy(struct wl_client *client,
4474 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004475{
4476 wl_resource_destroy(resource);
4477}
4478
4479static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004480viewport_set_source(struct wl_client *client,
4481 struct wl_resource *resource,
4482 wl_fixed_t src_x,
4483 wl_fixed_t src_y,
4484 wl_fixed_t src_width,
4485 wl_fixed_t src_height)
4486{
4487 struct weston_surface *surface =
4488 wl_resource_get_user_data(resource);
4489
Pekka Paalanen4826f872016-04-22 14:14:38 +03004490 if (!surface) {
4491 wl_resource_post_error(resource,
4492 WP_VIEWPORT_ERROR_NO_SURFACE,
4493 "wl_surface for this viewport is no longer exists");
4494 return;
4495 }
4496
4497 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004498 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004499
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004500 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004501 src_height == wl_fixed_from_int(-1) &&
4502 src_x == wl_fixed_from_int(-1) &&
4503 src_y == wl_fixed_from_int(-1)) {
4504 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004505 surface->pending.buffer_viewport.buffer.src_width =
4506 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004507 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004508 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004509 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004510
Pekka Paalanen201769a2016-04-26 14:42:11 +03004511 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004512 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004513 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03004514 "wl_surface@%d viewport source "
4515 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4516 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004517 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03004518 wl_fixed_to_double(src_height),
4519 wl_fixed_to_double(src_x),
4520 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004521 return;
4522 }
4523
4524 surface->pending.buffer_viewport.buffer.src_x = src_x;
4525 surface->pending.buffer_viewport.buffer.src_y = src_y;
4526 surface->pending.buffer_viewport.buffer.src_width = src_width;
4527 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004528 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004529}
4530
4531static void
4532viewport_set_destination(struct wl_client *client,
4533 struct wl_resource *resource,
4534 int32_t dst_width,
4535 int32_t dst_height)
4536{
4537 struct weston_surface *surface =
4538 wl_resource_get_user_data(resource);
4539
Pekka Paalanen4826f872016-04-22 14:14:38 +03004540 if (!surface) {
4541 wl_resource_post_error(resource,
4542 WP_VIEWPORT_ERROR_NO_SURFACE,
4543 "wl_surface for this viewport no longer exists");
4544 return;
4545 }
4546
4547 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004548
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004549 if (dst_width == -1 && dst_height == -1) {
4550 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004551 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004552 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004553 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004554 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004555
4556 if (dst_width <= 0 || dst_height <= 0) {
4557 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004558 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004559 "destination size must be positive (%dx%d)",
4560 dst_width, dst_height);
4561 return;
4562 }
4563
4564 surface->pending.buffer_viewport.surface.width = dst_width;
4565 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004566 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004567}
4568
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004569static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004570 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004571 viewport_set_source,
4572 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004573};
4574
4575static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004576viewporter_destroy(struct wl_client *client,
4577 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004578{
4579 wl_resource_destroy(resource);
4580}
4581
4582static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004583viewporter_get_viewport(struct wl_client *client,
4584 struct wl_resource *viewporter,
4585 uint32_t id,
4586 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004587{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004588 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004589 struct weston_surface *surface =
4590 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004591 struct wl_resource *resource;
4592
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004593 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004594 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004595 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004596 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004597 return;
4598 }
4599
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004600 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004601 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004602 if (resource == NULL) {
4603 wl_client_post_no_memory(client);
4604 return;
4605 }
4606
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004607 wl_resource_set_implementation(resource, &viewport_interface,
4608 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004609
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004610 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004611}
4612
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004613static const struct wp_viewporter_interface viewporter_interface = {
4614 viewporter_destroy,
4615 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004616};
4617
4618static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004619bind_viewporter(struct wl_client *client,
4620 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004621{
4622 struct wl_resource *resource;
4623
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004624 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004625 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004626 if (resource == NULL) {
4627 wl_client_post_no_memory(client);
4628 return;
4629 }
4630
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004631 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004632 NULL, NULL);
4633}
4634
4635static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004636destroy_presentation_feedback(struct wl_resource *feedback_resource)
4637{
4638 struct weston_presentation_feedback *feedback;
4639
4640 feedback = wl_resource_get_user_data(feedback_resource);
4641
4642 wl_list_remove(&feedback->link);
4643 free(feedback);
4644}
4645
4646static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004647presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4648{
4649 wl_resource_destroy(resource);
4650}
4651
4652static void
4653presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004654 struct wl_resource *presentation_resource,
4655 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004656 uint32_t callback)
4657{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004658 struct weston_surface *surface;
4659 struct weston_presentation_feedback *feedback;
4660
4661 surface = wl_resource_get_user_data(surface_resource);
4662
Bryce Harringtonde16d892014-11-20 22:21:57 -08004663 feedback = zalloc(sizeof *feedback);
4664 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004665 goto err_calloc;
4666
4667 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004668 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004669 1, callback);
4670 if (!feedback->resource)
4671 goto err_create;
4672
4673 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4674 destroy_presentation_feedback);
4675
4676 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4677
4678 return;
4679
4680err_create:
4681 free(feedback);
4682
4683err_calloc:
4684 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004685}
4686
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004687static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004688 presentation_destroy,
4689 presentation_feedback
4690};
4691
4692static void
4693bind_presentation(struct wl_client *client,
4694 void *data, uint32_t version, uint32_t id)
4695{
4696 struct weston_compositor *compositor = data;
4697 struct wl_resource *resource;
4698
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004699 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004700 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004701 if (resource == NULL) {
4702 wl_client_post_no_memory(client);
4703 return;
4704 }
4705
4706 wl_resource_set_implementation(resource, &presentation_implementation,
4707 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004708 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004709}
4710
4711static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004712compositor_bind(struct wl_client *client,
4713 void *data, uint32_t version, uint32_t id)
4714{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004715 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004716 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004717
Jason Ekstranda85118c2013-06-27 20:17:02 -05004718 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004719 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004720 if (resource == NULL) {
4721 wl_client_post_no_memory(client);
4722 return;
4723 }
4724
4725 wl_resource_set_implementation(resource, &compositor_interface,
4726 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004727}
4728
Bryce Harrington689fece2016-08-10 17:25:23 -07004729struct weston_idle_inhibitor {
4730 struct weston_surface *surface;
4731};
4732
Bryce Harringtonca5b6242016-08-23 13:39:14 -07004733/* Called when the client requests destruction of the idle inhibitor */
Bryce Harrington689fece2016-08-10 17:25:23 -07004734static void
4735idle_inhibitor_destroy(struct wl_client *client, struct wl_resource *resource)
4736{
4737 struct weston_idle_inhibitor *inhibitor = wl_resource_get_user_data(resource);
4738
4739 assert(inhibitor);
4740
4741 inhibitor->surface->inhibit_idling = false;
Bryce Harringtonca5b6242016-08-23 13:39:14 -07004742
4743 // Notify to re-queue any idle behaviors
4744 wl_signal_emit(&inhibitor->surface->drop_idle_inhibitor_signal,
4745 inhibitor->surface);
4746
4747 weston_log("idle_inhibitor_destroy\n");
4748}
4749
4750/* Called when the idle inhibitor is destroyed on the server-side */
4751static void
4752destroy_idle_inhibitor(struct wl_resource *resource)
4753{
4754 struct weston_idle_inhibitor *inhibitor = wl_resource_get_user_data(resource);
4755
4756 weston_log("destroy_idle_inhibitor\n");
4757
4758 inhibitor->surface = NULL;
4759 free(inhibitor);
Bryce Harrington689fece2016-08-10 17:25:23 -07004760}
4761
4762static const struct zwp_idle_inhibitor_v1_interface idle_inhibitor_interface = {
4763 idle_inhibitor_destroy
4764};
4765
4766static void
4767idle_inhibit_manager_destroy(struct wl_client *client, struct wl_resource *resource)
4768{
4769}
4770
4771static void
4772idle_inhibit_manager_create_inhibitor(struct wl_client *client, struct wl_resource *resource,
4773 uint32_t id, struct wl_resource *surface_resource)
4774{
4775 struct weston_surface *surface = wl_resource_get_user_data(surface_resource);
4776 struct weston_idle_inhibitor *inhibitor;
4777 struct wl_resource *cr;
4778
4779 cr = wl_resource_create(client, &zwp_idle_inhibitor_v1_interface,
4780 wl_resource_get_version(resource), id);
4781 if (cr == NULL) {
4782 wl_client_post_no_memory(client);
4783 return;
4784 }
4785
4786 inhibitor = zalloc(sizeof *inhibitor);
4787 if (inhibitor == NULL) {
4788 wl_client_post_no_memory(client);
4789 return;
4790 }
4791
4792 inhibitor->surface = surface;
4793 inhibitor->surface->inhibit_idling = true;
4794
4795 wl_resource_set_implementation(cr, &idle_inhibitor_interface,
4796 inhibitor, destroy_idle_inhibitor);
4797}
4798
4799static const struct zwp_idle_inhibit_manager_v1_interface idle_inhibit_manager_interface = {
4800 idle_inhibit_manager_destroy,
4801 idle_inhibit_manager_create_inhibitor
4802};
4803
4804static void
4805bind_idle_inhibit_manager(struct wl_client *client,
4806 void *data, uint32_t version, uint32_t id)
4807{
4808 struct wl_resource *resource;
4809
4810 resource = wl_resource_create(client, &zwp_idle_inhibit_manager_v1_interface,
4811 version, id);
4812 if (resource == NULL) {
4813 wl_client_post_no_memory(client);
4814 return;
4815 }
4816
4817 wl_resource_set_implementation(resource, &idle_inhibit_manager_interface,
4818 NULL, NULL);
4819}
4820
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004821WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004822weston_environment_get_fd(const char *env)
4823{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07004824 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004825 int fd, flags;
4826
4827 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07004828 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004829 return -1;
4830
4831 flags = fcntl(fd, F_GETFD);
4832 if (flags == -1)
4833 return -1;
4834
4835 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4836 unsetenv(env);
4837
4838 return fd;
4839}
4840
Pekka Paalanenb5026542014-11-12 15:09:24 +02004841static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004842timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004843 uint32_t key, void *data)
4844{
4845 struct weston_compositor *compositor = data;
4846
4847 if (weston_timeline_enabled_)
4848 weston_timeline_close();
4849 else
4850 weston_timeline_open(compositor);
4851}
4852
Giulio Camuffo459137b2014-10-11 23:56:24 +03004853/** Create the compositor.
4854 *
4855 * This functions creates and initializes a compositor instance.
4856 *
4857 * \param display The Wayland display to be used.
4858 * \param user_data A pointer to an object that can later be retrieved
4859 * using the \ref weston_compositor_get_user_data function.
4860 * \return The compositor instance on success or NULL on failure.
4861 */
4862WL_EXPORT struct weston_compositor *
4863weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004864{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004865 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004866 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004867
Giulio Camuffo459137b2014-10-11 23:56:24 +03004868 ec = zalloc(sizeof *ec);
4869 if (!ec)
4870 return NULL;
4871
4872 ec->wl_display = display;
4873 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004874 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004875 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004876 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004877 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004878 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004879 wl_signal_init(&ec->idle_signal);
4880 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004881 wl_signal_init(&ec->show_input_panel_signal);
4882 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004883 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004884 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004885 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004886 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004887 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02004888 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004889 wl_signal_init(&ec->session_signal);
4890 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004891
Casey Dahlin58ba1372012-04-19 22:50:08 -04004892 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004893 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004894
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02004895 ec->activate_serial = 1;
4896
Derek Foreman152254b2015-11-26 14:17:48 -06004897 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004898 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004899 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004900
Giulio Camuffo954f1832014-10-11 18:27:30 +03004901 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004902 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004903 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004904
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004905 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004906 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004907 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004908
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004909 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004910 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004911 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004912
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004913 if (weston_input_init(ec) != 0)
4914 goto fail;
4915
Bryce Harrington689fece2016-08-10 17:25:23 -07004916 if (!wl_global_create(ec->wl_display, &zwp_idle_inhibit_manager_v1_interface, 1,
4917 ec, bind_idle_inhibit_manager))
4918 goto fail;
4919
Jason Ekstranda7af7042013-10-12 22:38:11 -05004920 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004921 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004922 wl_list_init(&ec->layer_list);
4923 wl_list_init(&ec->seat_list);
4924 wl_list_init(&ec->output_list);
4925 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004926 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004927 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004928 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004929 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004930 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004931
Pekka Paalanen827b5d22016-06-29 11:54:26 +02004932 wl_list_init(&ec->plugin_api_list);
4933
Xiong Zhang97116532013-10-23 13:58:31 +08004934 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004935 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004936
Giulio Camuffo459137b2014-10-11 23:56:24 +03004937 wl_data_device_manager_init(ec->wl_display);
4938
4939 wl_display_init_shm(ec->wl_display);
4940
4941 loop = wl_display_get_event_loop(ec->wl_display);
4942 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004943
Giulio Camuffo459137b2014-10-11 23:56:24 +03004944 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4945 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4946
4947 weston_compositor_add_debug_binding(ec, KEY_T,
4948 timeline_key_binding_handler, ec);
4949
Giulio Camuffo459137b2014-10-11 23:56:24 +03004950 return ec;
4951
4952fail:
4953 free(ec);
4954 return NULL;
4955}
4956
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004957WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004958weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004959{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004960 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004961
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004962 wl_event_source_remove(ec->idle_source);
4963
Matt Roper361d2ad2011-08-29 13:52:23 -07004964 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004965 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004966 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004967
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004968 if (ec->renderer)
4969 ec->renderer->destroy(ec);
4970
Daniel Stone325fc2d2012-05-30 16:31:58 +01004971 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004972 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004973 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004974 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004975 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004976 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004977
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004978 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004979}
4980
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004981WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004982weston_compositor_exit_with_code(struct weston_compositor *compositor,
4983 int exit_code)
4984{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004985 if (compositor->exit_code == EXIT_SUCCESS)
4986 compositor->exit_code = exit_code;
4987
Giulio Camuffo459137b2014-10-11 23:56:24 +03004988 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004989}
4990
4991WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004992weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4993 const struct weston_pointer_grab_interface *interface)
4994{
4995 struct weston_seat *seat;
4996
4997 ec->default_pointer_grab = interface;
4998 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004999 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5000
5001 if (pointer)
5002 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005003 }
5004}
5005
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04005006WL_EXPORT int
5007weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
5008 clockid_t clk_id)
5009{
5010 struct timespec ts;
5011
5012 if (clock_gettime(clk_id, &ts) < 0)
5013 return -1;
5014
5015 compositor->presentation_clock = clk_id;
5016
5017 return 0;
5018}
5019
5020/*
5021 * For choosing the software clock, when the display hardware or API
5022 * does not expose a compatible presentation timestamp.
5023 */
5024WL_EXPORT int
5025weston_compositor_set_presentation_clock_software(
5026 struct weston_compositor *compositor)
5027{
5028 /* In order of preference */
5029 static const clockid_t clocks[] = {
5030 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
5031 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
5032 CLOCK_MONOTONIC, /* no jumps, may crawl */
5033 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
5034 CLOCK_REALTIME /* may jump and crawl */
5035 };
5036 unsigned i;
5037
5038 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
5039 if (weston_compositor_set_presentation_clock(compositor,
5040 clocks[i]) == 0)
5041 return 0;
5042
5043 weston_log("Error: no suitable presentation clock available.\n");
5044
5045 return -1;
5046}
5047
Pekka Paalanen662f3842015-03-18 12:17:26 +02005048/** Read the current time from the Presentation clock
5049 *
5050 * \param compositor
5051 * \param ts[out] The current time.
5052 *
5053 * \note Reading the current time in user space is always imprecise to some
5054 * degree.
5055 *
5056 * This function is never meant to fail. If reading the clock does fail,
5057 * an error message is logged and a zero time is returned. Callers are not
5058 * supposed to detect or react to failures.
5059 */
5060WL_EXPORT void
5061weston_compositor_read_presentation_clock(
5062 const struct weston_compositor *compositor,
5063 struct timespec *ts)
5064{
5065 static bool warned;
5066 int ret;
5067
5068 ret = clock_gettime(compositor->presentation_clock, ts);
5069 if (ret < 0) {
5070 ts->tv_sec = 0;
5071 ts->tv_nsec = 0;
5072
5073 if (!warned)
5074 weston_log("Error: failure to read "
5075 "the presentation clock %#x: '%m' (%d)\n",
5076 compositor->presentation_clock, errno);
5077 warned = true;
5078 }
5079}
5080
Pekka Paalanen230f3b12014-09-29 14:18:40 -04005081/** Import dmabuf buffer into current renderer
5082 *
5083 * \param compositor
5084 * \param buffer the dmabuf buffer to import
5085 * \return true on usable buffers, false otherwise
5086 *
5087 * This function tests that the linux_dmabuf_buffer is usable
5088 * for the current renderer. Returns false on unusable buffers. Usually
5089 * usability is tested by importing the dmabufs for composition.
5090 *
5091 * This hook is also used for detecting if the renderer supports
5092 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
5093 * supported.
5094 * */
5095WL_EXPORT bool
5096weston_compositor_import_dmabuf(struct weston_compositor *compositor,
5097 struct linux_dmabuf_buffer *buffer)
5098{
5099 struct weston_renderer *renderer;
5100
5101 renderer = compositor->renderer;
5102
5103 if (renderer->import_dmabuf == NULL)
5104 return false;
5105
5106 return renderer->import_dmabuf(compositor, buffer);
5107}
5108
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005109WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005110weston_version(int *major, int *minor, int *micro)
5111{
5112 *major = WESTON_VERSION_MAJOR;
5113 *minor = WESTON_VERSION_MINOR;
5114 *micro = WESTON_VERSION_MICRO;
5115}
5116
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005117WL_EXPORT void *
5118weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005119{
Derek Foreman3f86e502015-06-08 11:46:54 -05005120 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005121 char path[PATH_MAX];
5122 void *module, *init;
5123
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005124 if (name == NULL)
5125 return NULL;
5126
Derek Foreman3f86e502015-06-08 11:46:54 -05005127 if (name[0] != '/') {
5128 if (builddir)
5129 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
5130 else
Giulio Camuffo179fcda2016-06-02 21:48:14 +03005131 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005132 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02005133 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005134 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005135
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005136 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
5137 if (module) {
5138 weston_log("Module '%s' already loaded\n", path);
5139 dlclose(module);
5140 return NULL;
5141 }
5142
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005143 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04005144 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005145 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005146 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005147 return NULL;
5148 }
5149
5150 init = dlsym(module, entrypoint);
5151 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005152 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00005153 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005154 return NULL;
5155 }
5156
5157 return init;
5158}
5159
Giulio Camuffo459137b2014-10-11 23:56:24 +03005160
5161/** Destroys the compositor.
5162 *
5163 * This function cleans up the compositor state and destroys it.
5164 *
5165 * \param compositor The compositor to be destroyed.
5166 */
5167WL_EXPORT void
5168weston_compositor_destroy(struct weston_compositor *compositor)
5169{
5170 /* prevent further rendering while shutting down */
5171 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5172
5173 wl_signal_emit(&compositor->destroy_signal, compositor);
5174
5175 weston_compositor_xkb_destroy(compositor);
5176
Giulio Camuffo2d24e642015-10-03 16:25:15 +03005177 if (compositor->backend)
5178 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005179
5180 weston_plugin_api_destroy_list(compositor);
5181
Giulio Camuffo459137b2014-10-11 23:56:24 +03005182 free(compositor);
5183}
5184
5185/** Instruct the compositor to exit.
5186 *
5187 * This functions does not directly destroy the compositor object, it merely
5188 * command it to start the tear down process. It is not guaranteed that the
5189 * tear down will happen immediately.
5190 *
5191 * \param compositor The compositor to tear down.
5192 */
5193WL_EXPORT void
5194weston_compositor_exit(struct weston_compositor *compositor)
5195{
5196 compositor->exit(compositor);
5197}
5198
5199/** Return the user data stored in the compositor.
5200 *
5201 * This function returns the user data pointer set with user_data parameter
5202 * to the \ref weston_compositor_create function.
5203 */
5204WL_EXPORT void *
5205weston_compositor_get_user_data(struct weston_compositor *compositor)
5206{
5207 return compositor->user_data;
5208}
Pekka Paalanendd186732016-06-03 14:49:54 +03005209
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005210static const char * const backend_map[] = {
5211 [WESTON_BACKEND_DRM] = "drm-backend.so",
5212 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5213 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5214 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5215 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5216 [WESTON_BACKEND_X11] = "x11-backend.so",
5217};
5218
Pekka Paalanendd186732016-06-03 14:49:54 +03005219/** Load a backend into a weston_compositor
5220 *
5221 * A backend must be loaded to make a weston_compositor work. A backend
5222 * provides input and output capabilities, and determines the renderer to use.
5223 *
5224 * \param compositor A compositor that has not had a backend loaded yet.
5225 * \param backend Name of the backend file.
5226 * \param config_base A pointer to a backend-specific configuration
5227 * structure's 'base' member.
5228 *
5229 * \return 0 on success, or -1 on error.
5230 */
5231WL_EXPORT int
5232weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005233 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005234 struct weston_backend_config *config_base)
5235{
5236 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005237 struct weston_backend_config *config_base);
5238
Quentin Glidic887c0182016-07-10 11:00:53 +02005239 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005240 return -1;
5241
5242 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005243 if (!backend_init)
5244 return -1;
5245
Pekka Paalanena256c5e2016-06-03 14:56:18 +03005246 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03005247}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005248
5249WL_EXPORT int
5250weston_compositor_load_xwayland(struct weston_compositor *compositor)
5251{
5252 int (*module_init)(struct weston_compositor *ec,
5253 int *argc, char *argv[]);
5254 int argc = 0;
5255
5256 module_init = weston_load_module("xwayland.so", "module_init");
5257 if (!module_init)
5258 return -1;
5259 if (module_init(compositor, &argc, NULL) < 0)
5260 return -1;
5261 return 0;
5262}